File: dbrecycle.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 (414 lines) | stat: -rw-r--r-- 11,511 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
/*
                 Recycled Objects Buffer Management

	Functions:

        int DBRecycleBufferInit(long entries)
        void DBRecycleBufferDeleteAll()

        int DBSaveRecycledObject(long object_num)
        long DBRecoverRecycledObject(char *name, long owner)

	---

 */

#include "swserv.h"
#include "../include/unvfile.h"


/*      
 *      Procedure to allocate recycled objects buffers.
 */
int DBRecycleBufferInit(long entries)
{
        long recbuf_count;
        int recbufs_allocated;


        /* Entries must be valid. */
        if(entries < 1)
        {
            fprintf(stderr,
  "DBAllocateRecycleBuffer(): Warning: Ignoring request to allocate none.\n"
            );
            return(-1);
        }
  
        /* Free all currently allocated recycle buffers. */
        DBRecycleBufferDeleteAll();

        recbufs_allocated = 0;
        
        /* ************************************************************** */
        
        /* Adjust global variable total_recycled_objects. */
        total_recycled_objects = entries;
 
        /* Allocate recycle buffer pointer array. */
        recycled_xsw_object = (xsw_object_struct **)
            realloc(recycled_xsw_object, entries *
                sizeof(xsw_object_struct *)
	);
        if(recycled_xsw_object == NULL)
        {
	    total_recycled_objects = 0;
            return(-1);
        }

        /* Allocate each recycle buffer. */
        for(recbuf_count = 0; recbuf_count < entries; recbuf_count++)
        {
            /* Allocate recycle buffer. */
            recycled_xsw_object[recbuf_count] = (xsw_object_struct *)calloc(
		1,
		sizeof(xsw_object_struct)
	    );
            if(recycled_xsw_object[recbuf_count] == NULL)
            {
		total_recycled_objects = recbuf_count;
                return(-1);   
            }

            recbufs_allocated++;

            /* Set type to garbage. */
            recycled_xsw_object[recbuf_count]->type = XSW_OBJ_TYPE_GARBAGE;
        }


        return(recbufs_allocated);
}


/*
 *      Procedure to deallocate all objects in recycled
 *      buffers list.
 */
void DBRecycleBufferDeleteAll()
{
        long i;


#ifdef DEBUG_MEM_FREE
printf("Recycled objects: Deleting %i...\n", total_recycled_objects);
#endif
        
        for(i = 0; i < total_recycled_objects; i++)
        {
#ifdef DEBUG_MEM_FREE
if(recycled_xsw_object[i] != NULL)
    printf("Recycled object %i: Free'ed.\n", i);
#endif
            DBDeleteObject(recycled_xsw_object[i]);
            recycled_xsw_object[i] = NULL;
        }
 
        /* Free recycle objects pointer. */
#ifdef DEBUG_MEM_FREE
if(recycled_xsw_object != NULL)
    printf("Recycled objects pointers: Free'ed.\n");
#endif
        free(recycled_xsw_object);
        recycled_xsw_object = NULL;
        
        total_recycled_objects = 0;


        return;
}



/*
 *      Copy object_num to the recycled objects list.
 */
int DBSaveRecycledObject(long object_num)
{
        int i, n;


        /* Error checks. */
        if(recycled_xsw_object == NULL)
            return(-1);
        if(DBIsObjectGarbage(object_num))
            return(-1);
        
    
        /* ********************************************************** */

	/* Look for available recycled object structure. */
	for(i = 0; i < total_recycled_objects; i++)
	{
 	    if(recycled_xsw_object[i] == NULL)
                continue;
	    if(recycled_xsw_object[i]->type <= XSW_OBJ_TYPE_GARBAGE)
		break;
	}
	if(i < total_recycled_objects)
	{
	    n = i;
	}
	else
	{
	    /*   Could not find available recycled object structure, so
	     *   make room for more.
	     */

            /* Deallocate sub resources of highest recycled object. */
            UNVResetObject(recycled_xsw_object[total_recycled_objects - 1]);

            /* `Shift' the recycle buffers. */
            for(i = total_recycled_objects - 1; i > 0; i--)
            {
                if(recycled_xsw_object[i] == NULL)
		    continue;
 
                memcpy(
		    recycled_xsw_object[i],         /* Destination. */
                    recycled_xsw_object[i - 1],     /* Source. */
                    sizeof(xsw_object_struct)
                );
            }

	    n = 0;
   	}

        /* ********************************************************** */
        /* Copy the object to available recycled object structure n. */

        /* Structure. */
        memcpy(
	    recycled_xsw_object[n],     /* Destination. */
            xsw_object[object_num],     /* Source. */
            sizeof(xsw_object_struct)
        );

        /* Weapons. */ 
        if(xsw_object[object_num]->weapons != NULL)
        {
            /* Allocate weapon pointers on recycle object. */
            recycled_xsw_object[n]->weapons = (xsw_weapons_struct **)calloc(
                1,
                recycled_xsw_object[n]->total_weapons *
                    sizeof(xsw_weapons_struct *)
            );
            if(recycled_xsw_object[n]->weapons == NULL)
	    {
		recycled_xsw_object[n]->total_weapons = 0;
		return(-1);
	    }

            /* Allocate each weapon */
            for(i = 0; i < recycled_xsw_object[n]->total_weapons; i++)
            {
                recycled_xsw_object[n]->weapons[i] = (xsw_weapons_struct *)calloc(
		    1,
		    sizeof(xsw_weapons_struct)
                );
                if(recycled_xsw_object[n]->weapons[i] == NULL)
		{
		    return(-1);
		}
                memcpy(
		    recycled_xsw_object[n]->weapons[i], /* Destination. */
                    xsw_object[object_num]->weapons[i],	/* Source. */
                    sizeof(xsw_weapons_struct)
                );
            } 
        }
            
        /* Scores. */
        if(xsw_object[object_num]->score != NULL)
        {     
            /* Allocate score structure. */
            recycled_xsw_object[n]->score = (xsw_score_struct *)calloc(
		1,
                sizeof(xsw_score_struct)
            );
            if(recycled_xsw_object[n]->score == NULL)
		return(-1);

            memcpy(
		recycled_xsw_object[n]->score,  /* Destination. */
                xsw_object[object_num]->score,	/* Source. */
                sizeof(xsw_score_struct)
            );
        }
 
        /* Economy. */
        if(xsw_object[object_num]->eco != NULL)
        {
            recycled_xsw_object[n]->eco = (xsw_ecodata_struct *)calloc(
		1,
                sizeof(xsw_ecodata_struct)
            );
            if(recycled_xsw_object[n]->eco == NULL)
		return(-1);

            memcpy(
		recycled_xsw_object[n]->eco,	/* Destination. */
                xsw_object[object_num]->eco,	/* Source. */
                sizeof(xsw_ecodata_struct)
            );

	    /* Economy products. */
	    if(recycled_xsw_object[n]->eco->total_products > 0)
	    {
                recycled_xsw_object[n]->eco->product =
		    (xsw_ecoproduct_struct **)calloc(
                    1,
                    recycled_xsw_object[n]->eco->total_products *
                        sizeof(xsw_ecoproduct_struct *)
                );
                if(recycled_xsw_object[n]->eco->product == NULL)
                {
                    recycled_xsw_object[n]->eco->total_products = 0;
                    return(-1);
                }

                for(i = 0; i < recycled_xsw_object[n]->eco->total_products; i++)
                {
                    recycled_xsw_object[n]->eco->product[i] =
			(xsw_ecoproduct_struct *)calloc(
			    1, sizeof(xsw_ecoproduct_struct)
                    );
                    if(recycled_xsw_object[n]->eco->product[i] == NULL)
                    {
                        return(-1);
                    }
                    memcpy(
                        recycled_xsw_object[n]->eco->product[i], /* Destination. */
                        xsw_object[object_num]->eco->product[i], /* Source. */
                        sizeof(xsw_ecoproduct_struct)
                    );
                }
	    }
	    else
	    {
		recycled_xsw_object[n]->eco->product = NULL;		
	    }
        }


        return(0);
}



/*
 *      Recover an object in the recycled objects list.
 */                   
long DBRecoverRecycledObject(char *name, long owner)
{
        long i, rec_object_num, object_num;   


        /* Error checks. */
        if(recycled_xsw_object == NULL)
            return(-1);

        /* Owner must be valid. */
        if(DBIsObjectGarbage(owner))
            return(-2);


        /*   Search for object in recycled objects buffer,
	 *   starting from oldest.
	 */
        for(i = total_recycled_objects - 1; i >= 0; i--)
        {
            /* Skip garbage objects. */
            if(recycled_xsw_object[i] == NULL)
                continue;
            if(recycled_xsw_object[i]->type <= XSW_OBJ_TYPE_GARBAGE)
                continue;

            /* Check if this recycled object is a player. */
            if(recycled_xsw_object[i]->type != XSW_OBJ_TYPE_PLAYER)
            {
		/* Recycled object is not a player. */

		/* Check if owner owns this recycled object. */
                if(recycled_xsw_object[i]->owner != owner)
                    continue;
            }
            else
            {
		/*   Recycled object is a player, so check if owner has
		 *   create player permission.
		 */
                if(xsw_object[owner]->permission.uid > ACCESS_UID_CREATEPLAYER)
                    continue;
            }

            /* Match name. */
            if(strstr(recycled_xsw_object[i]->name, name) == NULL)
                continue;

            /* Found recycled object that matches name and owner. */
            break;
        }
        rec_object_num = i;


        /* Did we find anything? */
        if((rec_object_num < 0) || (rec_object_num >= total_recycled_objects))
        {
            return(-1);
        }


        /* ******************************************************* */
        /* Recover object. */

	/* Create a new object. */
        object_num = DBCreateObject(
            recycled_xsw_object[rec_object_num]->imageset,
            recycled_xsw_object[rec_object_num]->type,
            recycled_xsw_object[rec_object_num]->owner,
            recycled_xsw_object[rec_object_num]->x,
            recycled_xsw_object[rec_object_num]->y,
            recycled_xsw_object[rec_object_num]->z,
            recycled_xsw_object[rec_object_num]->heading,
            recycled_xsw_object[rec_object_num]->pitch,
            recycled_xsw_object[rec_object_num]->bank
        );
        if(DBIsObjectGarbage(object_num))
        {
            /* Could not create object for recovery. */
            return(-3);
        }


        /* Copy the recycled object data to the recovered object. */
        memcpy(
	    xsw_object[object_num],			/* Destination. */
            recycled_xsw_object[rec_object_num],	/* Source. */
            sizeof(xsw_object_struct)
        );

        /*   NOTE: We copy the pointers to allocated substructures
         *   of the object in the recycled buffers list to the newly created
         *   recovered object.
         */


        /*   Reset the object in the recycle list so its substructure
         *   pointers are not free'ed.
         */
	memset(
	    recycled_xsw_object[rec_object_num],
	    0x00,
	    sizeof(xsw_object_struct)
	);
	recycled_xsw_object[rec_object_num]->type = XSW_OBJ_TYPE_GARBAGE;


	/* Player objects need to own themselves. */
	if(xsw_object[object_num]->type == XSW_OBJ_TYPE_PLAYER)
	    xsw_object[object_num]->owner = object_num;


        return(object_num);
}