File: dbinrange.c

package info (click to toggle)
xshipwars 1.32-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 17,176 kB
  • ctags: 6,357
  • sloc: ansic: 157,152; makefile: 226; sh: 75
file content (332 lines) | stat: -rw-r--r-- 7,284 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
                  In Range XSW Objects Management

	Functions:

	void DBInRangeUpdate(long object_num)
	int DBInRangeAdd(long ref_obj, long tar_obj, char check_range)
	void DBInRangeDelete(xsw_object_struct *obj_ptr)
	void DBInRangeDeleteAll()

	---

	Manages the array pointer containing a list of
	objects in range of the player.

 */

#include "xsw.h"



/*
 *	Updates the in range objects list pointer array
 *	with respect to the given object object_num.
 *
 *	object_num must be valid or else the in range
 *	objects list is cleared.
 */
void DBInRangeUpdate(long object_num)
{
	int i;
	long l;
	xsw_object_struct *ref_obj_ptr, **obj_ptr;


	/* Get pointer to referance object. */
	if(DBIsObjectGarbage(object_num))
	    ref_obj_ptr = NULL;
	else
	    ref_obj_ptr = xsw_object[object_num];


	/* Clear in range objects list. */
	DBInRangeDeleteAll();


	/* Don't get new listing if referance object is NULL. */
	if(ref_obj_ptr == NULL)
	    return;


	/* ********************************************************* */
	/* Go through objects list. */

	/* Get home and area objects first. */
	for(l = 0, obj_ptr = xsw_object;
            l < total_objects;
            l++, obj_ptr++
	)
	{
	    if(*obj_ptr == NULL)
		continue;

	    /* Skip if not these type of objects. */
	    if(((*obj_ptr)->type != XSW_OBJ_TYPE_HOME) &&
               ((*obj_ptr)->type != XSW_OBJ_TYPE_AREA)
	    )
		continue;

	    /*   Check if object is in the same sector as referance
	     *   object and that it's recently updated.
	     */
	    if(!Mu3DInSameSectorPtr(*obj_ptr, ref_obj_ptr))
		continue;

	    if(((*obj_ptr)->last_updated + OBJECT_OUTDATED_TIMEOUT)
                < cur_millitime
	    )
	    {
		/* Referance object is always considered up to date. */
		if(*obj_ptr != ref_obj_ptr)
		    continue;
	    }


	    /* Object is in range and up to date. */

	    i = total_inrange_objects;

	    total_inrange_objects++;
	    inrange_xsw_object = (xsw_object_struct **)realloc(
		inrange_xsw_object,
		total_inrange_objects * sizeof(xsw_object_struct *)
	    );
	    if(inrange_xsw_object == NULL)
	    {
		total_inrange_objects = 0;
		return;
	    }

	    /* Set pointer to object. */
	    inrange_xsw_object[i] = *obj_ptr;
	}


        /* Next, get all other types of objects. */
        for(l = 0, obj_ptr = xsw_object;
            l < total_objects;
            l++, obj_ptr++
        )
        {
            if(*obj_ptr == NULL)
                continue;

	    /* Skip these types of objects. */
            if(((*obj_ptr)->type == XSW_OBJ_TYPE_HOME) ||
               ((*obj_ptr)->type == XSW_OBJ_TYPE_AREA)
            )
                continue;

            /*   Check if object is in the same sector as referance
             *   object and that it's recently updated.
             */
            if(!Mu3DInSameSectorPtr(*obj_ptr, ref_obj_ptr))
                continue;

            if(((*obj_ptr)->last_updated + OBJECT_OUTDATED_TIMEOUT)
                < cur_millitime
            )
            {
                /* Referance object is always considered up to date. */
                if(*obj_ptr != ref_obj_ptr)
                    continue;
            }
             
  
            /* Object is in range and up to date. */
            
            i = total_inrange_objects;
               
            total_inrange_objects++;
            inrange_xsw_object = (xsw_object_struct **)realloc(
                inrange_xsw_object,
                total_inrange_objects * sizeof(xsw_object_struct *)
            );
            if(inrange_xsw_object == NULL)
            {
                total_inrange_objects = 0;
                return;
            }
        
            /* Set pointer to object. */
            inrange_xsw_object[i] = *obj_ptr;
        }


	return;
}


/*
 *	Adds tar_obj to the in range objects list.
 *	If check_range is 0, then tar_obj is added to
 *	the list unconditionally.
 *	If check_range is 1 then the tar_obj is checked
 *	to see if it is in range with ref_obj. if any only if
 *	tar_obj is in range with ref_obj will tar_obj be added
 *	to the list.
 */
int DBInRangeAdd(long ref_obj, long tar_obj, char check_range)
{
	int i;
	xsw_object_struct *ref_obj_ptr, *tar_obj_ptr;


	/* Check if target object is valid and get pointer. */
        if(DBIsObjectGarbage(tar_obj))
            return(-1);
        else
            tar_obj_ptr = xsw_object[tar_obj];


        /* Check if target object is already in in range list. */
        for(i = 0; i < total_inrange_objects; i++)
        { 
            if(inrange_xsw_object[i] == tar_obj_ptr)
                return(0);
        }


	/* Check if target object is in range with referance object? */
	if(check_range)
	{
	    if(DBIsObjectGarbage(ref_obj))
		return(-1);
	    else
		ref_obj_ptr = xsw_object[ref_obj];

	    /* Check if in range. */
	    if(!Mu3DInSameSectorPtr(tar_obj_ptr, ref_obj_ptr))
		return(0);
	}


	/*   Add target object to in range list by type.
	 *   If the object is of type HOME or AREA, then add it to
	 *   the beginning of the list.  If it is not then append
	 *   it to the end.
	 */

	/* Sanitize total. */
	if(total_inrange_objects < 0)
	    total_inrange_objects = 0;

	/* Increment total and allocate more pointers. */
	total_inrange_objects++;

	inrange_xsw_object = (xsw_object_struct **)realloc(
            inrange_xsw_object,
            total_inrange_objects * sizeof(xsw_object_struct *)
        );
	if(inrange_xsw_object == NULL)
	{
	    total_inrange_objects = 0;
	    return(-1);
	}


	/* Add by type. */
	if((tar_obj_ptr->type == XSW_OBJ_TYPE_HOME) ||
           (tar_obj_ptr->type == XSW_OBJ_TYPE_AREA)
	)
	{
	    /* Home or area, add to beginning of list. */

	    /* Shift pointers. */
	    for(i = total_inrange_objects - 1; i > 0; i--)
		inrange_xsw_object[i] = inrange_xsw_object[i - 1];

	    inrange_xsw_object[0] = tar_obj_ptr;
	}
	else
	{
	    /* All other types, append to end. */

	    i = total_inrange_objects - 1;
	    inrange_xsw_object[i] = tar_obj_ptr;
	}


	return(0);
}


/*
 *	Removes obj_ptr from the in range objects list.
 *
 *	Warning, the global inrange_xsw_object pointers will
 *	be reallocated!
 */
void DBInRangeDelete(xsw_object_struct *obj_ptr)
{
	int i, h;
	xsw_object_struct **ptr;


	if(obj_ptr == NULL)
	    return;


	if(total_inrange_objects < 0)
	    total_inrange_objects = 0;


	for(i = 0, h = -1, ptr = inrange_xsw_object;
	    i < total_inrange_objects;
	    i++, ptr++
	)
	{
            if(*ptr == NULL)
		continue;

            if(*ptr == obj_ptr)
	    {
                *ptr = NULL;
		continue;
	    }

	    h = i;
        }

	/* Update inrange list size. */
	total_inrange_objects = h + 1;
	if(total_inrange_objects > 0)
	{
	    inrange_xsw_object = (xsw_object_struct **)realloc(
		inrange_xsw_object,
		total_inrange_objects * sizeof(xsw_object_struct *)
	    );
	    if(inrange_xsw_object == NULL)
	    {
		total_inrange_objects = 0;
		return;
	    }
	}
	else
	{
	    free(inrange_xsw_object);
            inrange_xsw_object = NULL;

            total_inrange_objects = 0;
	}


	return;
}


/*
 *	Deletes the in range objects list (but not the objects
 *	themselves).
 */
void DBInRangeDeleteAll()
{
	free(inrange_xsw_object);
	inrange_xsw_object = NULL;

	total_inrange_objects = 0;


	return;
}