File: OBULLETA.cpp

package info (click to toggle)
7kaa 2.15.7%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,980 kB
  • sloc: cpp: 137,722; ansic: 3,599; asm: 3,523; perl: 1,665; makefile: 1,185; sh: 185; pascal: 27
file content (478 lines) | stat: -rw-r--r-- 15,110 bytes parent folder | download | duplicates (8)
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
/*
 * Seven Kingdoms: Ancient Adversaries
 *
 * Copyright 1997,1998 Enlight Software Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

//Filename    : OBULLETA.CPP
//Description : Object Bullet Array
//Owner		  : Alex

#include <OVGA.h>
#include <OUNIT.h>
#include <OBULLET.h>
#include <OWORLD.h>
#include <OTOWN.h>
#include <OB_PROJ.h>
#include <OB_HOMIN.h>
#include <OB_FLAME.h>


//--------- Begin of function BulletArray::BulletArray ---------//
//
// <int> initArraySize - the initial size of this array.
//
BulletArray::BulletArray(int initArraySize) : SpriteArray(initArraySize)
{
}
//----------- End of function BulletArray::BulletArray -----------//


//--------- Begin of function BulletArray::create_bullet ---------//
//
// return: <int> the recno of the bullet created.
//
int BulletArray::create_bullet(short spriteId, Bullet** bpp)
{
	Bullet* bulletPtr;
	SpriteInfo *spriteInfo = sprite_res[spriteId];
	err_when(!spriteInfo);
	err_when(spriteInfo->sprite_type != 'B');
	switch(spriteInfo->sprite_sub_type)
	{
	case 0:
	case ' ':
		bulletPtr = new Bullet;
		break;
	case 'P':
		bulletPtr = new Projectile;
		break;
	case 'H':
		bulletPtr = new BulletHoming;
		break;
	case 'F':
		bulletPtr = new BulletFlame;
		break;
	default:
		err_here();			// undefined bullet type
		if( bpp )
			*bpp = NULL;
		return 0;
	}

	add(bulletPtr);
	if( bpp )
		*bpp = bulletPtr;
	return recno();
}
//----------- End of function BulletArray::create_bullet -----------//


//--------- Begin of function BulletArray::bullet_class_size ---------//
int BulletArray::bullet_class_size(int spriteId)
{
	SpriteInfo *spriteInfo = sprite_res[spriteId];
	err_when(spriteInfo->sprite_type != 'B');
	switch(spriteInfo->sprite_sub_type)
	{
	case 0:
	case ' ':
		return sizeof(Bullet);
	case 'P':
		return sizeof(Projectile);
	case 'H':
		return sizeof(BulletHoming);
	case 'F':
		return sizeof(BulletFlame);
	default:
		err_here();			// undefined bullet type
		return 0;
	}
}
//----------- End of function BulletArray::bullet_class_size -----------//


//--------- Begin of function BulletArray::bullet_path_possible ---------//
// For default bullet:
//		if the bullet is not blocked by building in its path, this
//		function return 1 for success, otherwise return 0.
// For projectile bullet:
//		always return 1
//
int BulletArray::bullet_path_possible(short startXLoc, short startYLoc, char attackerMobileType,
												  short destXLoc, short destYLoc, char targetMobileType,
												  char bulletSpeed, short bulletSpriteId)
{
	if(attackerMobileType==UNIT_AIR || targetMobileType==UNIT_AIR)
		return 1;

	//-------- skip the checking for projectile -----------//
	SpriteInfo *spriteInfo = sprite_res[bulletSpriteId];
	if(spriteInfo->sprite_sub_type == 'P')
		return 1;

	err_when(spriteInfo->sprite_sub_type == 'P');

	//----------------------- define variables ---------------//

	int originX	= startXLoc*ZOOM_LOC_WIDTH;
	int originY	= startYLoc*ZOOM_LOC_HEIGHT;
	int goX		= destXLoc*ZOOM_LOC_WIDTH;
	int goY		= destYLoc*ZOOM_LOC_HEIGHT;

	int xStep	= (goX - originX)/bulletSpeed;
	int yStep	= (goY - originY)/bulletSpeed;

	int totalStep = MAX(1, MAX(abs(xStep), abs(yStep)));
	int curStep = 0;

	//------------------------------------------------------//
	// if the path of the bullet is blocked, return 0
	//------------------------------------------------------//
	int curX = originX + ZOOM_LOC_WIDTH/2;
	int curY = originY + ZOOM_LOC_HEIGHT/2;
	int curXLoc, curYLoc;
	Location *locPtr;

	while(curStep++<totalStep)
	{
		err_when(curStep > 200);

		curX += xStep;
		curY += yStep;

		curXLoc = curX/ZOOM_LOC_WIDTH;
		curYLoc = curY/ZOOM_LOC_HEIGHT;

		if(curXLoc==startXLoc && curYLoc==startYLoc)
			continue;

		if(curXLoc==destXLoc && curYLoc==destYLoc)
			break;	// is destination

		locPtr = world.get_loc(curXLoc, curYLoc);

		//### begin alex 2/6 ###//
		//if(!locPtr->walkable(3) || locPtr->has_unit(UNIT_LAND) || locPtr->has_unit(UNIT_SEA))
		if(!locPtr->walkable(3))
		//#### end alex 2/6 ####//
			return 0;
	}

	return 1;
}
//----------- End of function BulletArray::bullet_path_possible -----------//


//--------- Begin of function BulletArray::add_bullet_possible ---------//
// call bullet_possible to check whether bullets emitted from this position
// can reach the target location with blocking
//
// return 1 if bullets are able to reach the target location
// return 0 if not
//
int BulletArray::add_bullet_possible(short startXLoc, short startYLoc, char attackerMobileType,
												 short targetXLoc, short targetYLoc, char targetMobileType,
												 short targetWidth, short targetHeight, short& resultXLoc, short& resultYLoc,
												 char bulletSpeed, short bulletSpriteId)
{
	resultXLoc = resultYLoc = -1;

	//----------------------------------------------------------------------//
	// for target with size 1x1
	//----------------------------------------------------------------------//
	if(targetWidth==1 && targetHeight==1)
	{
		if(bullet_path_possible(startXLoc, startYLoc, attackerMobileType, targetXLoc, targetYLoc, targetMobileType, bulletSpeed, bulletSpriteId))
		{
			resultXLoc = targetXLoc;
			resultYLoc = targetYLoc;
			return 1;
		}
		else 
			return 0;
	}

	err_when(targetWidth==1 && targetHeight==1);
	//----------------------------------------------------------------------//
	// choose the closest corner to be the default attacking point of range attack
	//
	// generalized case for range-attack is coded below. Work for target with
	// size > 1x1 
	//----------------------------------------------------------------------//

	//-------------- define parameters --------------------//
	short	adjWidth	= targetWidth-1;	// adjusted width;
	short	adjHeight = targetHeight-1; // adjusted height
	short xOffset=0, yOffset=0;	// the 1st(default) location for range attack
	short atEdge = 0;	// i.e. the attacking point is at the corner or edge of the target
							// 1 for at the edge, 0 for at corner
		
	//----------------------------------------------------------------------//
	// determine initial xOffset
	//----------------------------------------------------------------------//
	if(startXLoc <= targetXLoc)
		xOffset = 0; // the left hand side of the target
	else if(startXLoc >= targetXLoc+adjWidth)
		xOffset = adjWidth; // the right hand side of the target
	else
	{
		xOffset = startXLoc - targetXLoc; // in the middle(vertical) of the target
		atEdge++;
	}

	//----------------------------------------------------------------------//
	// determine initial yOffset
	//----------------------------------------------------------------------//
	if(startYLoc <= targetYLoc)
		yOffset = 0;	// the upper of the target
	else if(startYLoc >= targetYLoc+adjHeight)
		yOffset = adjHeight;
	else
	{
		yOffset = startYLoc - targetYLoc; // in the middle(horizontal) of the target
		atEdge++;
	}

	//----------------------------------------------------------------------//
	// checking whether it is possible to add bullet
	//----------------------------------------------------------------------//
	if(bullet_path_possible(startXLoc, startYLoc, attackerMobileType, targetXLoc+xOffset, targetYLoc+yOffset, targetMobileType, bulletSpeed, bulletSpriteId))
	{
		resultXLoc = targetXLoc+xOffset;
		resultYLoc = targetYLoc+yOffset;
		return 1;
	}

	short	leftXOffset, leftYOffset;
	short	rightXOffset, rightYOffset;
	short leftX, leftY, rightX, rightY;
	short found=0;	// found a location to attack the target and the path is not blocked
	short end=0;
	
	if(atEdge)	// only check one edge of the target
	{
		if(xOffset==0 || xOffset==adjWidth) // horizontal edge
		{
			leftYOffset = rightYOffset = 0;
			leftXOffset = 1;
			rightXOffset = -1;
		}
		else if(yOffset==0 || yOffset==adjHeight) // vertical edge
		{
			leftXOffset = rightXOffset = 0;
			leftYOffset = 1;
			rightYOffset = -1;
		}
		else
			err_here();	// the sprite is within the target ???
	}
	else	// at the corner,	need to check two edges of the target
	{
		leftYOffset = rightXOffset = 0;
		leftXOffset = (xOffset==0) ? 1 : -1;
		rightYOffset = (yOffset==0) ? 1 : -1;
	}

	leftX = rightX = xOffset;
	leftY = rightY = yOffset;

	while(!found)
	{
		end = 0;
		
		//-------------------------------------------//
		// for the leftX, leftY
		//-------------------------------------------//
		leftX += leftXOffset;
		leftY += leftYOffset;
		if(leftX>=0 && leftX<targetWidth && leftY>=0 && leftY<targetHeight)
		{
			if(bullet_path_possible(startXLoc, startYLoc, attackerMobileType, targetXLoc+leftX, targetYLoc+leftY, targetMobileType, bulletSpeed, bulletSpriteId))
			{
				resultXLoc = targetXLoc+leftX;
				resultYLoc = targetYLoc+leftY;
				return 1;
			}
		}
		else
			end++;

		//-------------------------------------------//
		// for the rightX, rightY
		//-------------------------------------------//
		rightX += rightXOffset;
		rightY += rightYOffset;
		if(rightX>=0 && rightX<targetWidth && rightY>=0 && rightY<targetHeight)
		{
			if(bullet_path_possible(startXLoc, startYLoc, attackerMobileType, targetXLoc+rightX, targetYLoc+rightY, targetMobileType, bulletSpeed, bulletSpriteId))
			{
				resultXLoc = targetXLoc+rightX;
				resultYLoc = targetYLoc+rightY;
				return 1;
			}
		}
		else
		{
			if(end)	// all locations have been checked, all are blocked
				return 0;
		}
	}

	return 0;
}
//----------- End of function BulletArray::add_bullet_possible -----------//


//--------- Begin of function BulletArray::add_bullet ---------//
// unit attacks unit by bullet
//
// <Unit*> parentUnit - pointer to the attacking target unit
// <Unit*> targetUnit - pointer to the target unit
//
//	return 1 if bullet is added successfully otherwise return 0.
//
short BulletArray::add_bullet(Unit* parentUnit, Unit* targetUnit)
{
	//------------------------------------------------------//
	// define parameters
	//------------------------------------------------------//
	SpriteInfo *targetSpriteInfo = targetUnit->sprite_info;
	AttackInfo *attackInfo = parentUnit->attack_info_array+parentUnit->cur_attack;
	short attackXLoc	= parentUnit->range_attack_x_loc;
	short attackYLoc	= parentUnit->range_attack_y_loc;
	short targetXLoc = targetUnit->next_x_loc();
	short targetYLoc = targetUnit->next_y_loc();

	if(attackXLoc >= targetXLoc && attackXLoc < targetXLoc+targetSpriteInfo->loc_width &&
		attackYLoc >= targetYLoc && attackYLoc < targetYLoc+targetSpriteInfo->loc_height)
	{
		//-------------------------------------------------------//
		// the previous used range attack destination can be reused,
		// time is saved 'cos no need to check for bullet_path_possible()
		//-------------------------------------------------------//
		short bulletId = (parentUnit->attack_info_array+parentUnit->cur_attack)->bullet_sprite_id;
		Bullet* bulletPtr;
		create_bullet( bulletId, &bulletPtr);
		bulletPtr->init(BULLET_BY_UNIT, parentUnit->sprite_recno, attackXLoc, attackYLoc, targetUnit->mobile_type);
		err_when(parentUnit->cur_dir!=parentUnit->final_dir);
		//parentUnit->get_dir(parentUnit->next_x_loc(), parentUnit->next_y_loc(), attackXLoc, attackYLoc);
		return 1;
	}

	err_here();
	return 0;
}
//----------- End of function BulletArray::add_bullet -----------//


//--------- Begin of function Bullet::add_bullet ---------//
// Overload of add_bullet(), unit attack firm/town/wall
//
// <Unit*> parentUnit - pointer to the attacking target unit
// <short> xLoc - the target upper left corner x loc
//					   note: taregt can be firm, town or wall
// <short> yLoc - the target upper left corner y loc
//
//	return 1 if bullet is added successfully otherwise return 0.
//
//	note: unit attacks firm or town by bullet will call this
//			function. The location of the firm or town is
//			specified by xLoc and yLoc.
//
short BulletArray::add_bullet(Unit* parentUnit, short xLoc, short yLoc)
{
	//------------------------------------------------------//
	// define parameters
	//------------------------------------------------------//
	AttackInfo *attackInfo = parentUnit->attack_info_array+parentUnit->cur_attack;
	short	attackXLoc = parentUnit->range_attack_x_loc;
	short	attackYLoc = parentUnit->range_attack_y_loc;
	short	targetXLoc = xLoc;
	short	targetYLoc = yLoc;
	short	width, height;
	Firm*			targetFirm = NULL;
	Town*    	targetTown = NULL;
	FirmInfo*	firmInfo = NULL;
	Location*	locPtr = world.get_loc(xLoc, yLoc);
	
	if(locPtr->is_firm())
	{
		targetFirm = firm_array[locPtr->firm_recno()];
		firmInfo = firm_res[targetFirm->firm_id];
		width = firmInfo->loc_width;
		height = firmInfo->loc_height;
	}
	else if(locPtr->is_town())
	{
		targetTown = town_array[locPtr->town_recno()];
		width = targetTown->loc_width();
		height = targetTown->loc_height();
	}
	else if(locPtr->is_wall())
		width = height = 1;
	else
		err_here();

	if(attackXLoc >= targetXLoc && attackXLoc < targetXLoc+width &&
		attackYLoc >= targetYLoc && attackYLoc < targetYLoc+height)
	{
		//-------------------------------------------------------//
		// the previous used range attack destination can be reused,
		// time is saved 'cos no need to check for bullet_path_possible()
		//-------------------------------------------------------//
		short bulletId = (parentUnit->attack_info_array+parentUnit->cur_attack)->bullet_sprite_id;
		Bullet* bulletPtr;
		create_bullet( bulletId, &bulletPtr);
		bulletPtr->init(BULLET_BY_UNIT, parentUnit->sprite_recno, attackXLoc, attackYLoc, UNIT_LAND);
		err_when(parentUnit->cur_dir!=parentUnit->final_dir);
		//parentUnit->get_dir(parentUnit->next_x_loc(), parentUnit->next_y_loc(), attackXLoc, attackYLoc);
		return 1;
	}

	err_here();
	return 0;
}
//----------- End of function BulletArray::add_bullet -----------//


//--------- Begin of function Bullet::add_bullet ---------//
// Overload of add_bullet()
//
// <Unit*> parentFirm - pointer to the attacking target Firm
// <Unit*> targetUnit - pointer to the target Unit
//
//	return 1 if bullet is added successfully otherwise return 0.
short BulletArray::add_bullet(Firm* parentFirm, Unit* targetUnit)
{
	return 0;
}
//----------- End of function BulletArray::add_bullet -----------//


//--------- Begin of function Bullet::add_bullet ---------//
// Overload of add_bullet()
//
// <Unit*> parentFirm - pointer to the attacking target Firm
// <Unit*> targetFirm - pointer to the target Firm
//
//	return 1 if bullet is added successfully otherwise return 0.
short BulletArray::add_bullet(Firm* parentFirm, Firm* targetFirm)
{
	return 0;
}
//----------- End of function BulletArray::add_bullet -----------//