File: dbmatch.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 (553 lines) | stat: -rw-r--r-- 13,515 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
/*
                            XSW Object Matching

	Functions:

	int DBIsObjectGarbage(long object_num)
	int DBIsObjectNameIndex(char *name)
	double DBGetObjectVisibility(long object_num)
	double DBGetObjectVisibilityPtr(xsw_object_struct *obj_ptr)

	long MatchWeaponsLock(
		long ref_obj,
		long start_obj,
		double range
	)
	long MatchIntercept(long object_num, char *arg)
	long MatchInterceptByNumber(long ref_obj, long tar_obj)
	long MatchObjectByName(char *name, int type)
	int MatchIFF(long ref_obj, long tar_obj)
	int MatchIFFPtr(
	        xsw_object_struct *ref_obj_ptr,
	        xsw_object_struct *tar_obj_ptr
	)

	void DBSetupGarbageObject()

	---

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "../include/string.h"
#include "../include/reality.h"
#include "../include/mathutil.h"
#include "../include/isrefs.h"
#include "../include/dbmatch.h"


xsw_object_struct garbage_object;


/*
 *      Checks if object is allocated and garbage.
 *
 *      Returns true if not allocated or garbage.
 */
int DBIsObjectGarbage(long object_num)
{
        if((object_num < 0) ||
           (object_num >= total_objects) ||
           (xsw_object == NULL)
        )
            return(-1);
        else if(xsw_object[object_num] == NULL)
            return(-1);
        else if(xsw_object[object_num]->type <= XSW_OBJ_TYPE_GARBAGE)
            return(1);
        else
            return(0);
}

/*
 *	Checks if the object name is in index notation (returns true)
 *	or a regular name (returns false).
 */
int DBIsObjectNameIndex(char *name)
{
	if(name == NULL)
	    return(0);
	else if(*name == '#')
	    return(1);
	else
	    return(0);
}

/*           
 *      Returns the object's visibility.
 */         
double DBGetObjectVisibility(long object_num)
{
        if(DBIsObjectGarbage(object_num))
            return(0.00);
        else 
            return(DBGetObjectVisibilityPtr(xsw_object[object_num]));
}

double DBGetObjectVisibilityPtr(xsw_object_struct *obj_ptr)
{
        if(obj_ptr == NULL)
            return(0.00);

        if(obj_ptr->type == XSW_OBJ_TYPE_AREA)
            return(obj_ptr->cur_visibility);
	else if(obj_ptr->loc_type == XSW_LOC_TYPE_NEBULA)
	    return(obj_ptr->cur_visibility * VISIBILITY_NEBULA);
        else
            return(obj_ptr->cur_visibility);
}


/*
 *      returns the next XSW Object number that is within
 *      range range of object ref_object.   ref_object should be the object
 *      that is doing the weapons lock, and may never be -1.
 *      start_object maybe -1 or any object number less than total_objects.
 *      range should be the the maximum scanner range of ref_object.
 *      Returns the next XSW Object number of a valid (nongarbage or nonerror)
 *      object withing range range of ref_object starting from start_object,
 *      or returns -1 if no match can be made.
 */
long MatchWeaponsLock(long ref_obj, long start_obj, double range) 
{
	long i, f;
	xsw_object_struct *obj_ptr, *ref_obj_ptr;


	if(DBIsObjectGarbage(ref_obj))
	    return(-1);
	else
	    ref_obj_ptr = xsw_object[ref_obj];

	/* Sanitize start object (can be -1). */
        if(start_obj < -1)
            start_obj = -1;

	/* range must be positive. */
	if(range < 0)
	    range = 1;


	/* Begin search, note that start_obj could be -1. */
	for(i = start_obj + 1, f = -1;
            i < total_objects;
            i++
	)
	{
	    obj_ptr = xsw_object[i];

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

/*
	    if((obj_ptr->type == XSW_OBJ_TYPE_WEAPON) ||
               (obj_ptr->type == XSW_OBJ_TYPE_STREAMWEAPON) ||
               (obj_ptr->type == XSW_OBJ_TYPE_SPHEREWEAPON) ||
               (obj_ptr->type == XSW_OBJ_TYPE_AREA)
	    )
		continue;
 */

	    /* Don't match referance object. */
	    if(obj_ptr == ref_obj_ptr)
		continue;

	    /* See if object is in range. */
	    if(Mu3DInRangePtr(obj_ptr, ref_obj_ptr,
		    range * DBGetObjectVisibilityPtr(obj_ptr)
	       )
	    )
	    {
	        /* Checks passed, this is the object we want. */
	        f = i;
	        break;
	    }
	}


	return(f);
}



/*
 *	Tries to match object specified in arg.  If arg
 *	is "" or "#off" then -1 is returned.   If the object specified in arg
 *	is found and is of type XSW_OBJ_TYPE_STATIC then that object's number
 *	is returned.  If the object is found but not of type static, then it 
 *	is checked to see if it is within scanner range of object_num and if 
 *	it is, then it's number is returned or of not in range then -1 is    
 *	returned.
 */
long MatchIntercept(long object_num, char *arg)
{
	char name[XSW_OBJ_NAME_MAX];
	long target_obj;
	xsw_object_struct *obj_ptr;
	xsw_object_struct *tar_obj_ptr;


	if(arg == NULL)
	    return(-1);

	if(DBIsObjectGarbage(object_num))
            return(-1);
	else
	    obj_ptr = xsw_object[object_num];


	/* Sanitize copy over name to be searched. */
	strncpy(name, arg, XSW_OBJ_NAME_MAX);
	name[XSW_OBJ_NAME_MAX - 1] = '\0';
	StringStripSpaces(name);


	/* Turn intercept off? */
	if(!strcasecmp(name, "#off") ||
	   (name[0] == '\0')
	)
	    return(-1);


	/* Match target_obj. */
	target_obj = MatchObjectByName(name, -1);
	if(DBIsObjectGarbage(target_obj))
	    return(-1);
	else
            tar_obj_ptr = xsw_object[target_obj];


	/* Cannot intercept itself. */
	if(target_obj == object_num)
	    return(-1);


	/* Following types of objects may not be intercepted. */
	if((tar_obj_ptr->type == XSW_OBJ_TYPE_STREAMWEAPON) ||
           (tar_obj_ptr->type == XSW_OBJ_TYPE_SPHEREWEAPON)
	)
	    return(-1);

        /* Following objects don't need range check. */
        if((tar_obj_ptr->type == XSW_OBJ_TYPE_STATIC) ||
	   (tar_obj_ptr->type == XSW_OBJ_TYPE_HOME) ||
           (tar_obj_ptr->type == XSW_OBJ_TYPE_AREA)
	)
            return(target_obj);


        /* Is target_obj within scanner range of object_num? */
        if(Mu3DInRangePtr(obj_ptr, tar_obj_ptr,
                obj_ptr->scanner_range *
                DBGetObjectVisibilityPtr(tar_obj_ptr)
            )
        )
	    return(target_obj);
	else
	    return(-1);
}


/*
 *	Checks if tar_obj is within intercept range of ref_obj,
 *	and that both are valid.  Returns tar_obj if everything
 *	checks out okay or -1 if not.
 */
long MatchInterceptByNumber(long ref_obj, long tar_obj)
{
        xsw_object_struct *obj_ptr;
        xsw_object_struct *tar_obj_ptr;


        /* Error checks. */
        if(DBIsObjectGarbage(ref_obj))
            return(-1);
        else
            obj_ptr = xsw_object[ref_obj];

        if(DBIsObjectGarbage(tar_obj))
            return(-1);
        else
            tar_obj_ptr = xsw_object[tar_obj];


        /* ref_obj and tar_obj cannot be the same. */
        if(ref_obj == tar_obj)
            return(-1);


        /* Skip these types of objects. */
        if((tar_obj_ptr->type == XSW_OBJ_TYPE_WEAPON) ||
           (tar_obj_ptr->type == XSW_OBJ_TYPE_STREAMWEAPON) ||
           (tar_obj_ptr->type == XSW_OBJ_TYPE_SPHEREWEAPON)
        )
            return(-1);


        /* Following objects always matchable regardless of distance. */
        if((tar_obj_ptr->type == XSW_OBJ_TYPE_STATIC) ||
	   (tar_obj_ptr->type == XSW_OBJ_TYPE_HOME) ||
	   (tar_obj_ptr->type == XSW_OBJ_TYPE_AREA)
	)
            return(tar_obj);


        /* If none of the above, then do scanner range check. */
        if(Mu3DInRangePtr(obj_ptr, tar_obj_ptr,
                obj_ptr->scanner_range *
                DBGetObjectVisibilityPtr(tar_obj_ptr)
            )
        )
	    return(tar_obj);
	else
            return(-1);
}


/*
 *	Matches object by name, return its number or -1 on error or
 *	no match.
 *
 *	The search can be narrowed down to a certain type of object
 *	or -1 for all types.
 */
long MatchObjectByName(char *name, int type)
{
	long object_num;
	xsw_object_struct **obj_ptr;


	/* Name cannot be NULL or contain no characters. */
	if(name == NULL)
	    return(-1);
	if(name[0] == '\0')
	    return(-1);


	/* Skip leading spaces in name. */
	while(isblank(*name))
	    name++;

	/* Number matching? */
	if((*name == '#') ||
           (isdigit(*name))
	)
	{
	    /* Skip leading '#' characters. */
	    while(*name == '#')
		name++;

	    /* Get object number. */
	    object_num = atol(name);

	    /* Check if object_num is valid. */
	    if((object_num < 0) || (object_num >= total_objects))
		return(-1);
	    if(xsw_object[object_num] == NULL)
		return(-1);
	    if(xsw_object[object_num]->type <= XSW_OBJ_TYPE_GARBAGE)
		return(-1);

            /* Match all types of objects? */
	    if(type == -1)
		return(object_num);

	    /* Match specific type. */
	    if(xsw_object[object_num]->type == type)
		return(object_num);
	    else
		return(-1);
	}


	/* Go through XSW objects list. */
	for(object_num = 0, obj_ptr = xsw_object;
            object_num < total_objects;
            object_num++, obj_ptr++
	)
	{
	    if(*obj_ptr == NULL) continue;
	    if((*obj_ptr)->type <= XSW_OBJ_TYPE_GARBAGE) continue;

	    /* Object must characters in its name. */
	    if((*obj_ptr)->name[0] == '\0') continue;

	    /* Match all types? */
	    if(type == -1)
	    {
		/* Partial name match. */
		if(strstr((*obj_ptr)->name, name) == NULL)
		    continue;

		/* Got match. */
		return(object_num);
	    }
	    /* Match specific type. */
	    else
	    {
		/* Match type first. */
		if((*obj_ptr)->type != type)
		    continue;

                /* Partial name match. */
                if(strstr((*obj_ptr)->name, name) == NULL)
                    continue;

                /* Got match. */
                return(object_num);
	    }
	}


	/* No match. */
	return(-1);
}

/*
 *	Returns IFF of ref_obj to tar_obj.
 */
int MatchIFF(long ref_obj, long tar_obj)
{
	if(DBIsObjectGarbage(ref_obj))
	    return(IFF_UNKNOWN);
	else if(DBIsObjectGarbage(tar_obj))
	    return(IFF_UNKNOWN);
	else
	    return(MatchIFFPtr(
		xsw_object[ref_obj],
		xsw_object[tar_obj]
	    ));
}

/*
 *	Returns IFF of ref_obj_ptr to tar_obj_ptr
 */
int MatchIFFPtr(
	xsw_object_struct *ref_obj_ptr,
	xsw_object_struct *tar_obj_ptr
)
{
	char *strptr_ref, *strptr_tar;


	if((ref_obj_ptr == NULL) ||
           (tar_obj_ptr == NULL)
	)
	    return(IFF_UNKNOWN);

	strptr_ref = ref_obj_ptr->empire;
	strptr_tar = tar_obj_ptr->empire;

	if((strptr_ref == NULL) ||
           (strptr_tar == NULL) ||
           (*strptr_ref == '\0') ||
	   (*strptr_tar == '\0')
	)
	    return(IFF_UNKNOWN);

	if(strcasecmp(strptr_ref, strptr_tar))
	    return(IFF_UNKNOWN);
	else
	    return(IFF_FRIENDLY);
}

/*
 *	Sets up the global garbage object.
 */
void DBSetupGarbageObject()
{
	xsw_object_struct *obj_ptr;


	obj_ptr = &garbage_object;

	obj_ptr->client_options = 0;
	obj_ptr->server_options = 0;

        memset(obj_ptr->name, '\0', XSW_OBJ_NAME_MAX);
        memset(obj_ptr->empire, '\0', XSW_OBJ_PASSWORD_MAX); 
	memset(obj_ptr->password, '\0', XSW_OBJ_EMPIRE_MAX);

        obj_ptr->last_updated = 0;

        obj_ptr->type = XSW_OBJ_TYPE_GARBAGE;
	obj_ptr->loc_type = XSW_LOC_TYPE_SPACE;
        obj_ptr->imageset = ISREF_DEFAULT;
        obj_ptr->owner = -1;
        obj_ptr->size = 1;
        obj_ptr->locked_on = -1;  
        obj_ptr->intercepting_object = -1;

        obj_ptr->scanner_range = 8.0;
        obj_ptr->sect_x = 0;
        obj_ptr->sect_y = 0;
        obj_ptr->sect_z = 0;
        obj_ptr->x = 0;
        obj_ptr->y = 0;
        obj_ptr->z = 0;
        obj_ptr->heading = 0;
        obj_ptr->pitch = 0;
        obj_ptr->bank = 0;
	memset(&obj_ptr->attitude_vector_compoent, 0x00, sizeof(xsw_vector_compoent_struct));
        obj_ptr->velocity = 0;
        obj_ptr->velocity_max = 0;
        obj_ptr->velocity_heading = 0;
        obj_ptr->velocity_pitch = 0;
        obj_ptr->velocity_bank = 0;
        memset(&obj_ptr->momentum_vector_compoent, 0x00, sizeof(xsw_vector_compoent_struct));
        obj_ptr->thrust_dir = 3.1415927;
        obj_ptr->thrust = 0;   
        obj_ptr->thrust_power = 0;   
        obj_ptr->thrust_power = 0;
        obj_ptr->throttle = 0;
	obj_ptr->engine_state = ENGINE_STATE_ON;
        obj_ptr->turnrate = 0.0003;
        obj_ptr->hp = 1;
        obj_ptr->hp_max = 1;
        obj_ptr->power = 0;
        obj_ptr->power_max = 0;
        obj_ptr->power_purity = 1.0;
        obj_ptr->core_efficency = 0;
        obj_ptr->antimatter = 0;
        obj_ptr->antimatter_max = 0;
        obj_ptr->shield_state = SHIELD_STATE_NONE;
        obj_ptr->shield_frequency = XSW_DEF_SHIELD_FREQ;
        obj_ptr->selected_weapon = -1;
        obj_ptr->total_weapons = 0;
        obj_ptr->birth_time = 0;
        obj_ptr->lifespan = -1;
        obj_ptr->cloak_state = CLOAK_STATE_NONE;   
        obj_ptr->cloak_strength = 0.0;
        obj_ptr->shield_visibility = 0.0;
        obj_ptr->visibility = 1.0;
	obj_ptr->cur_visibility = 1.0;
        obj_ptr->damage_control = DMGCTL_STATE_OFF;
	obj_ptr->com_channel = XSW_DEF_COM_CHANNEL;
	obj_ptr->ai_flags = 0;

	obj_ptr->tractored_object = NULL;
	obj_ptr->total_tractored_objects = 0;

        obj_ptr->permission.uid = DEFAULT_UID;
        obj_ptr->permission.gid = DEFAULT_GID;

        obj_ptr->animation.interval = 500;
        obj_ptr->animation.last_interval = 0;
        obj_ptr->animation.current_frame = 0;
        obj_ptr->animation.total_frames = 1;
        obj_ptr->animation.cycle_count = 0;
        obj_ptr->animation.cycle_times = -1;	/* Loop infinatly. */

        obj_ptr->weapons = NULL;
	obj_ptr->score = NULL;
	obj_ptr->eco = NULL;


	return;
}