File: db.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 (618 lines) | stat: -rw-r--r-- 14,144 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
                     XSW Object Database Management

	Functions:

	int DBIsObjectAllocated(long object_num)

	xsw_object_struct **DBAllocObjectPointers(
		xsw_object_struct **cur_obj_ptrs,
		long num_objects
	);
	xsw_object_struct *DBAllocObject();

        long DBCreateObject(
            long imageset,
            long type,
            long owner,
            double x,
            double y,  
            double z,
            double heading,
            double pitch,
            double bank
        )
        int DBCreateExplicitObject(
            long object_num,
            long imageset,
            long type,
            long owner,
            double x,
            double y,
            double z,
            double heading,
            double pitch,
            double bank
        )

	void DBDeleteObject(xsw_object_struct *obj_ptr)
	void DBDeleteAllObjects()
        void DBRecycleObject(long object_num)

        void DBReclaim()

	---
	
 */

#include "xsw.h"
#include "../include/unvfile.h"


/*
 *      Checks if object allocated.
 *
 *      Does not check if it is garbage.
 */
int DBIsObjectAllocated(long object_num)
{
        if((object_num < 0) ||
           (object_num >= total_objects) ||
           (xsw_object == NULL)
        )
            return(0);
        else if(xsw_object[object_num] == NULL)
            return(0);
        else
            return(1);
}


/*
 *      Reallocates object pointers.   
 *      
 *      Does NOT change global total_objects.
 */
xsw_object_struct **DBAllocObjectPointers(
        xsw_object_struct **cur_obj_ptrs,
        long num_objects 
)
{
        xsw_object_struct **obj_ptrs_rtn;


        /* Error checks. */
        if(num_objects <= 0)
            return(NULL);


        /* Reallocate pointers. */
        obj_ptrs_rtn = (xsw_object_struct **)realloc(
            cur_obj_ptrs,
            num_objects * sizeof(xsw_object_struct *)
        );
        if(obj_ptrs_rtn == NULL)
        {   
            fprintf(stderr,
 "DBAllocObjectPointers(): Error allocating memory for %i object pointers.\n",
                (int)num_objects
            );
        }


        return(obj_ptrs_rtn);
}


/*
 *      Allocates a new object structure with its values reset
 *      to the values of the global garbage object.
 */
xsw_object_struct *DBAllocObject()
{
        xsw_object_struct *obj_ptr;


        /* Allocate a new object. */
        obj_ptr = (xsw_object_struct *)calloc(1,
            sizeof(xsw_object_struct)
        );
        if(obj_ptr == NULL)
        {
            fprintf(stderr,
                "DBAllocObject(): Memory allocation error.\n"
            );
            return(NULL);
        }
 
        /* Reset values on new object. */
        UNVResetObject(obj_ptr);


        return(obj_ptr);
}



/*      
 *      Creates an object, returns its number or -1 on error.
 */
long DBCreateObject(
        long isref_num,
        long type,
        long owner,
        double x,
        double y,
        double z,
        double heading,
        double pitch,
        double bank
)
{
        int i, n;
        int prev_total;
	xsw_object_struct **ptr;
        xsw_object_struct *obj_ptr;


        /* Type may not be garbage. */
        if(type <= XSW_OBJ_TYPE_GARBAGE)
        {
            fprintf(stderr,
                "DBCreateObject: Error: Useless request to create garbage.\n"
            );
            return(-1);
        }

        /* isref_num must be a valid number. */
        if((isref_num < 0) || (isref_num >= ISREF_MAX))
            return(-1);

        /* Load isref_num as needed. */
        if(!ISRefIsLoaded(isref_num))
            ISRefLoad(isref_num);


        /* *********************************************************** */
        
        /* Look for available object already allocated. */
        for(i = 0, ptr = xsw_object;
            i < total_objects;
            i++, ptr++
	)
        {
            if(*ptr == NULL)
                continue;  
                
            /* Look for garbage objects. */
            if((*ptr)->type == XSW_OBJ_TYPE_GARBAGE)
                break;
        }        
        if(i < total_objects)
        {
            /* Got available object. */
	    n = i;
	}
	else
	{
	    /* Need to allocate more pointers. */

            /* Can we allocate more? */
            if((total_objects + OBJECT_ALLOCATE_AHEAD) > MAX_OBJECTS)
                return(-1);

            prev_total = total_objects;   
            total_objects += OBJECT_ALLOCATE_AHEAD;

            /* Allocate pointer array. */
            xsw_object = DBAllocObjectPointers(
                xsw_object,
                total_objects
            );
	    if(xsw_object == NULL)
	    {
		total_objects = 0;
		return(-1);
	    }

            /* Allocate each object. */
            for(i = prev_total;
                i < total_objects;
                i++
            )
            {
                xsw_object[i] = DBAllocObject();
                if(xsw_object[i] == NULL)
                {
                    total_objects = i;
                    return(-1);
                }
            }

            /* New object will be the first object allocated. */
            n = prev_total;
	}


	/* Reset values. */
	obj_ptr = xsw_object[n];

        UNVResetObject(obj_ptr);
        obj_ptr->type = type;
        obj_ptr->owner = owner;
        obj_ptr->imageset = isref_num;
        obj_ptr->x = x;
        obj_ptr->y = y;
        obj_ptr->z = z;
        obj_ptr->heading = heading;
        obj_ptr->pitch = pitch;
        obj_ptr->bank = bank;

        obj_ptr->last_updated = cur_millitime;
        obj_ptr->birth_time = cur_millitime;


	/* Add object to in range list (as needed). */
	DBInRangeAdd(lplayer_object, n, 0);


        return(n);
}



/*
 *      Creates an object explicitly by number.  If the object exists
 *      then it will be recycled first then recreated.
 *      
 *      Returns non-zero on error.
 */
int DBCreateExplicitObject(
        long object_num,
        long isref_num,
        long type,
        long owner,
        double x,
        double y,
        double z,
        double heading,   
        double pitch,
        double bank
)
{
        char name[XSW_OBJ_NAME_MAX];
        int i, n, prev_total;
	xsw_object_struct *obj_ptr;


        /* Type may not be garbage. */
        if(type <= XSW_OBJ_TYPE_GARBAGE)
        {       
            fprintf(stderr,
       "DBCreateExplicitObject(): Error: Useless request to create garbage.\n"
            );
            return(-1);
        }

        /* Make sure object_num is valid. */
        if(object_num < 0)
        {
            fprintf(stderr,
     "DBCreateExplicitObject(): Error: Request to create object_num #%i.\n",
                (int)object_num
            );   
            return(-1);
        }

        /* The isref_num must be valid. */
        if((isref_num < 0) || (isref_num >= ISREF_MAX))
        {
            fprintf(stderr,        
                "DBCreateExplicitObject(): Error: Invalid isref number %i.\n",
                (int)isref_num
            );
            return(-1);
        }

        /* Load isref as needed. */
        if(!ISRefIsLoaded(isref_num))
            ISRefLoad(isref_num);


        /* *********************************************************** */

        /* Record previous total. */
        prev_total = total_objects;


        /* If object_num already exists and is not garbage, recycle it. */
        if(!DBIsObjectGarbage(object_num))
            DBRecycleObject(object_num);


        /* Allocate more pointers as needed. */
        if(object_num >= total_objects)
        {
            /* Can we allocate more? */   
            if((object_num + OBJECT_ALLOCATE_AHEAD) >= MAX_OBJECTS)
            {
                fprintf(stderr, "Object maximum %i reached.\n", MAX_OBJECTS);
                return(-1);
            }

            /* Adjust global variable total_objects. */
            total_objects = object_num + OBJECT_ALLOCATE_AHEAD + 1;

            /* Allocate more pointers. */
            xsw_object = DBAllocObjectPointers(
                xsw_object,
                total_objects
            );
            if(xsw_object == NULL)
            {
                total_objects = 0;
                return(-1);
            }

            /* Allocate each object. */
            for(i = prev_total;
                i < total_objects;
                i++
            )
            {
                xsw_object[i] = DBAllocObject();
                if(xsw_object[i] == NULL)
                {
                    total_objects = i;
                    return(-1);
                }
            }
        }


        /* Create explicit object. */
        n = object_num;
        obj_ptr = xsw_object[n];


        /*   No need to reset object, if it wasn't garbage, it would be
         *   already reset at the beginning of the call to this function.
         */  
            
        sprintf(name, "Object %i", n);
        strncpy(obj_ptr->name, name, XSW_OBJ_NAME_MAX);
        obj_ptr->name[XSW_OBJ_NAME_MAX - 1] = '\0';

        obj_ptr->type = type;
        obj_ptr->owner = owner;
        obj_ptr->imageset = isref_num;
        obj_ptr->x = x;
        obj_ptr->y = y;
        obj_ptr->z = z;
        obj_ptr->heading = heading;
        obj_ptr->pitch = pitch;
        obj_ptr->bank = bank;

	obj_ptr->last_updated = cur_millitime;
        obj_ptr->birth_time = cur_millitime;  


        /* Add object to in range list (as needed). */
        DBInRangeAdd(lplayer_object, n, 0);


        return(0);
}

/*
 *      Deletes an object pointed to by obj_ptr and all of its
 *      substructures.
 */
void DBDeleteObject(xsw_object_struct *obj_ptr)
{
        if(obj_ptr == NULL)
            return;

	/* Make sure player object is not referancing garbage. */
	if(obj_ptr == net_parms.player_obj_ptr)
	    DBSetPlayerObject(-1);


        /* Delete object from in range list. */
        DBInRangeDelete(obj_ptr);

        /* Delete object and all its resources. */
	UNVDeleteObject(obj_ptr);


	return;
}

/*
 *      Deletes all objects and any dependant resources including;
 *	global lplayer_object referance, viewscreen labels,
 *	and scanner contacts.
 */
void DBDeleteAllObjects()
{
        long i;
	xsw_object_struct **obj_ptr;


        for(i = 0, obj_ptr = xsw_object;
            i < total_objects;
            i++, obj_ptr++
        )
        {
	    if(*obj_ptr == NULL)
		continue;

            DBDeleteObject(*obj_ptr);
        }
        free(xsw_object);
        xsw_object = NULL;

        total_objects = 0;


	/* Delete all entries in the in range objects list. */
	DBInRangeDeleteAll();


        /* Make sure player object is not referancing garbage. */
	DBSetPlayerObject(-1);

        /* Delete all viewscreen labels. */
        VSLabelDeleteAll();

        /* Delete all scanner contacts. */
        ScDeleteAll();


        return; 
}


/*
 *      Recycles object_num but does not free its structure.
 */
void DBRecycleObject(long object_num)   
{
        xsw_object_struct *obj_ptr;


        /* Is object_num valid or already garbage? */
        if(DBIsObjectGarbage(object_num))
            return;
        else 
            obj_ptr = xsw_object[object_num];


        /* If this is the player object, then unset player object. */
        if(object_num == net_parms.player_obj_num)
	    DBSetPlayerObject(-1);


	/* Delete viewscreen object label. */
	VSLabelDeleteByObjectPtr(obj_ptr);

	/* Remove object from in range objects list as needed. */
	DBInRangeDelete(obj_ptr);


        /* Reset values and deallocate all sub structures. */
        UNVResetObject(obj_ptr);

        /* Setting object_num garbage means it is recycled. */
        obj_ptr->type = XSW_OBJ_TYPE_GARBAGE;


        return;
}


/*
 *      Frees outdated objects and reallocates object pointers.
 */
void DBReclaim()
{
	long i, h;
	xsw_object_struct **obj_ptr, *lplayer_obj_ptr;


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


        /* No objects allocated? */
        if((total_objects == 0) ||
	   (xsw_object == NULL)
	)
            return;


        /* Get highest index numbered object that is valid. */
	for(i = 0, h = -1, obj_ptr = xsw_object;
            i < total_objects;
	    i++, obj_ptr++
	)
	{
	    if(*obj_ptr == NULL)
		continue;

	    if((*obj_ptr)->type <= XSW_OBJ_TYPE_GARBAGE)
		continue;

	    h = i;
	}


        /* Delete all allocated objects from total_objects - 1 to h + 1,
	 * the global player object referance will also be reset to -1 if
	 * it is deleted in the call to DBDeleteObject().
	 */
        for(i = total_objects - 1; i > h; i--)
            DBDeleteObject(xsw_object[i]);

        /* Adjust global total_objects. */
        total_objects = h + 1;
	if(total_objects > 0)
	{
	    /* Reallocate pointers. */
            xsw_object = DBAllocObjectPointers(
                xsw_object,
                total_objects
            );
            if(xsw_object == NULL)
            {
                total_objects = 0;
            }
 	}
	else
	{
	    /* Delete all pointers. */
	    free(xsw_object);
	    xsw_object = NULL;

	    total_objects = 0;
	}
  
        /*   Recycle outdated objects (after reallocating pointers).
         *   By doing this we reduce the need to reallocate incase
         *   objects are quickly recreated due to moderate lag.
         */

	lplayer_obj_ptr = net_parms.player_obj_ptr;

        for(i = 0, obj_ptr = xsw_object;
            i < total_objects;
            i++, obj_ptr++
	)
        {
	    if(*obj_ptr == NULL)
		continue;

	    /* Is object out of date? */
            if(((*obj_ptr)->last_updated + OBJECT_OUTDATED_TIMEOUT)
               < cur_millitime
            )
            {
                /* Skip player object. */
                if(i == lplayer_object)
		    continue;

                /* Recycle object. */
                DBRecycleObject(i);
            }

	    /* Object not in same sector with player object? */
	    if(!Mu3DInSameSectorPtr(*obj_ptr, lplayer_obj_ptr))
	    {
		DBRecycleObject(i);
	    }
        }


	return;
}