1 /*
2  * XPilot NG, a multiplayer space war game.
3  *
4  * Copyright (C) 1991-2001 by
5  *
6  *      Bj�rn Stabell        <bjoern@xpilot.org>
7  *      Ken Ronny Schouten   <ken@xpilot.org>
8  *      Bert Gijsbers        <bert@xpilot.org>
9  *      Dick Balaska         <dick@xpilot.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25 
26 /*
27  * This file deals with low-level object structure manipulations.
28  */
29 
30 #include "xpserver.h"
31 
32 /*
33  * Global variables
34  */
35 int			ObjCount = 0;
36 object_t		*Obj[MAX_TOTAL_SHOTS];
37 
38 
Object_incr_count(void)39 static void Object_incr_count(void)
40 {
41     ObjCount++;
42 }
43 
Object_decr_count(void)44 static void Object_decr_count(void)
45 {
46     ObjCount--;
47 }
48 
Object_allocate(void)49 object_t *Object_allocate(void)
50 {
51     object_t	*obj = OBJ_PTR(NULL);
52 
53     if (ObjCount < MAX_TOTAL_SHOTS) {
54 	obj = Obj[ObjCount];
55 	Object_incr_count();
56 
57 	obj->type = OBJ_DEBRIS;
58 	obj->life = 0;
59     }
60 
61     return obj;
62 }
63 
Object_free_ind(int ind)64 void Object_free_ind(int ind)
65 {
66     if ((0 <= ind) && (ind < ObjCount) && (ObjCount <= MAX_TOTAL_SHOTS)) {
67 	object_t *obj = Obj[ind];
68 	Object_decr_count();
69 	Obj[ind] = Obj[ObjCount];
70 	Obj[ObjCount] = obj;
71     } else
72 	warn("Cannot free object %d, when count = %d, and total = %d !",
73 	     ind, ObjCount, MAX_TOTAL_SHOTS);
74 }
75 
Object_free_ptr(object_t * obj)76 void Object_free_ptr(object_t *obj)
77 {
78     int		i;
79 
80     for (i = ObjCount - 1; i >= 0; i--) {
81 	if (Obj[i] == obj) {
82 	    Object_free_ind(i);
83 	    break;
84 	}
85     }
86     if (i < 0)
87 	warn("Could NOT free object!");
88 }
89 
90 static anyobject_t *objArray;
91 
92 #define SHOWTYPESIZE(T) warn("sizeof(" #T ") = %d", sizeof(T))
93 
Alloc_shots(int number)94 void Alloc_shots(int number)
95 {
96     anyobject_t		*x;
97     int			i;
98 
99 #if 0
100     SHOWTYPESIZE(object_t);
101     SHOWTYPESIZE(ballobject_t);
102     SHOWTYPESIZE(mineobject_t);
103     SHOWTYPESIZE(missileobject_t);
104     SHOWTYPESIZE(smartobject_t);
105     SHOWTYPESIZE(torpobject_t);
106     SHOWTYPESIZE(heatobject_t);
107     SHOWTYPESIZE(wireobject_t);
108     SHOWTYPESIZE(pulseobject_t);
109     SHOWTYPESIZE(itemobject_t);
110     SHOWTYPESIZE(anyobject_t);
111     SHOWTYPESIZE(player_t);
112 #endif
113 
114     x = (anyobject_t *) calloc((size_t)number, sizeof(anyobject_t));
115     if (!x) {
116 	error("Not enough memory for shots.");
117 	exit(1);
118     }
119 
120     objArray = x;
121     for (i = 0; i < number; i++) {
122 	Obj[i] = &(x->obj);
123 	/* kps - shouldn't be necessary */
124 	/*MINE_PTR(Obj[i])->mine_owner = NO_ID;*/
125 	Cell_init_object(Obj[i]);
126 	x++;
127     }
128 }
129 
Free_shots(void)130 void Free_shots(void)
131 {
132     XFREE(objArray);
133 }
134 
135 
136 /* kps debug hack */
Object_typename(object_t * obj)137 const char *Object_typename(object_t *obj)
138 {
139     int type;
140 
141     if (!obj)
142 	return "none";
143 
144     type = obj->type;
145 
146     if (type == OBJ_PLAYER)
147 	return "OBJ_PLAYER";
148     if (type == OBJ_DEBRIS)
149 	return "OBJ_DEBRIS";
150     if (type == OBJ_SPARK)
151 	return "OBJ_SPARK";
152     if (type == OBJ_BALL)
153 	return "OBJ_BALL";
154     if (type == OBJ_SHOT)
155 	return "OBJ_SHOT";
156     if (type == OBJ_SMART_SHOT)
157 	return "OBJ_SMART_SHOT";
158     if (type == OBJ_MINE)
159 	return "OBJ_MINE";
160     if (type == OBJ_TORPEDO)
161 	return "OBJ_TORPEDO";
162     if (type == OBJ_HEAT_SHOT)
163 	return "OBJ_HEAT_SHOT";
164     if (type == OBJ_PULSE)
165 	return "OBJ_PULSE";
166     if (type == OBJ_ITEM)
167 	return "OBJ_ITEM";
168     if (type == OBJ_WRECKAGE)
169 	return "OBJ_WRECKAGE";
170     if (type == OBJ_ASTEROID)
171 	return "OBJ_ASTEROID";
172     if (type == OBJ_CANNON_SHOT)
173 	return "OBJ_CANNON_SHOT";
174     return "unknown type";
175 }
176