File: unit_find.c

package info (click to toggle)
ale-clone 1.15pre16.3-1
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 2,432 kB
  • ctags: 3,217
  • sloc: ansic: 37,483; sh: 1,105; makefile: 343
file content (400 lines) | stat: -rw-r--r-- 8,426 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
/*
**	A clone of a famous game.
*/
/**@name unit_find.c	-	The find/select for units. */
/*
**	(c) Copyright 1998 by Lutz Sammer
**
**	$Id: unit_find.c,v 1.17 1999/11/21 21:59:58 root Exp $
*/

//@{

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

#include "clone.h"
#include "video.h"
#include "sound_id.h"
#include "unitsound.h"
#include "unittype.h"
#include "player.h"
#include "unit.h"
#include "interface.h"
#include "tileset.h"
#include "map.h"

/**
**	Select units in range.
**
**	@param x1	Left column of selection rectangle
**	@param y1	Top row of selection rectangle
**	@param x2	Right column of selection rectangle
**	@param y2	Bottom row of selection rectangle
**	@param table	All units in the selection rectangle
**
**	@return		Returns the number of units found
*/
global int SelectUnits(int x1,int y1,int x2,int y2,Unit** table)
{
    return UnitCacheSelect(x1,y1,x2,y2,table);
}

/**
**	Find all units of type.
**
**	@param type	type of unit requested
**	@param table	table in which we have to store the units
**
**	@return		Returns the number of units found.
*/
global int FindUnitsByType(int type,Unit** table)
{
    Unit* unit;
    int num,i;

    for( num=0, i=0; i<NumUnits; i++ ) {
	unit=Units[i];
	if( unit->Type->Type==type && !UnitUnusable(unit) ) {
	    table[num++]=unit;
	}
    }
    return num;
}

/**
**	Find all units of type.
**
**	@param player	we're looking for the units of this player
**	@param type	type of unit requested
**	@param table	table in which we have to store the units
**
**	@return		Returns the number of units found.
*/
global int FindPlayerUnitsByType(const Player* player,int type,Unit** table)
{
    Unit* unit;
    Unit** units;
    int num,nunits,i;

    nunits=player->TotalNumUnits;
    units=player->Units;
    // FIXME: Can't abort if all units are found: UnitTypeCount
    for( num=0,i=0; i<nunits; i++ ) {
	unit=units[i];
	if( unit->Type->Type==type && !UnitUnusable(unit) ) {
	    table[num++]=unit;
	}
    }
    return num;
}

/**
**	Unit on map tile, no special prefered.
**
**	@param tx	X position on map, tile-based.
**	@param ty	Y position on map, tile-based.
**
**	@return		Returns first found unit on tile.
*/
global Unit* UnitOnMapTile(unsigned tx,unsigned ty)
{
    Unit* table[MAX_UNITS];
    int n;
    int i;

    n=SelectUnits(tx,ty,tx+1,ty+1,table);
    for( i=0; i<n; ++i ) {
        // Note: this is less restrictive than UnitActionDie...
        // Is it normal?
	if( table[i]->Type->Vanishes ) {
	    continue;
	}
	return table[i];
    }

    return NoUnitP;
}

/**
**	Choose target on map tile.
**
**	@param source	Unit which want to attack.
**	@param tx	X position on map, tile-based.
**	@param ty	Y position on map, tile-based.
**
**	@return		Returns ideal target on map tile.
*/
global Unit* TargetOnMapTile(Unit* source,unsigned tx,unsigned ty)
{
    Unit* unit;
    UnitType* type;
    int i;

    // FIXME: This can be later rewritten.

    for( i=0; i<NumUnits; i++ ) {
	unit=Units[i];
	// unusable unit ?
	// if( UnitUnusable(unit) ) {
	if( unit->Removed || unit->Command.Action==UnitActionDie ) {
	    continue;
	}
	type=unit->Type;
	if( tx<unit->X || tx>=unit->X+type->TileWidth
		|| ty<unit->Y || ty>=unit->Y+type->TileHeight ) {
	    continue;
	}
	if( !CanTarget(source->Type,unit->Type) ) {
	    continue;
	}
	return unit;
    }
    return NoUnitP;
}

/*----------------------------------------------------------------------------
--	Finding special units
----------------------------------------------------------------------------*/

/**
**	Gold mine on map tile
**
**	@param tx	X position on map, tile-based.
**	@param ty	Y position on map, tile-based.
**
**	@return		Returns the gold mine if found, or NoUnitP.
*/
global Unit* GoldMineOnMap(int tx,int ty)
{
    Unit* table[MAX_UNITS];
    int i;
    int n;

    n=SelectUnits(tx,ty,tx+1,ty+1,table);
    for( i=0; i<n; ++i ) {
	if( table[i]->Type->GoldMine ) {
	    return table[i];
	}
    }
    return NoUnitP;
}

/**
**	Oil patch on map tile
**
**	@param tx	X position on map, tile-based.
**	@param ty	Y position on map, tile-based.
**
**	@return		Returns the oil patch if found, or NoUnitP.
*/
global Unit* OilPatchOnMap(int tx,int ty)
{
    Unit* table[MAX_UNITS];
    int i;
    int n;

    n=SelectUnits(tx,ty,tx+1,ty+1,table);
    for( i=0; i<n; ++i ) {
	if( table[i]->Type->OilPatch ) {
	    return table[i];
	}
    }
    return NoUnitP;
}

/**
**	Oil platform on map tile
**
**	@param tx	X position on map, tile-based.
**	@param ty	Y position on map, tile-based.
**
**	@return		Returns the oil platform if found, or NoUnitP.
*/
global Unit* PlatformOnMap(int tx,int ty)
{
    Unit* table[MAX_UNITS];
    int i;
    int n;

    n=SelectUnits(tx,ty,tx+1,ty+1,table);
    for( i=0; i<n; ++i ) {
	if( table[i]->Type->GivesOil ) {
	    return table[i];
	}
    }
    return NoUnitP;
}

/**
**	Oil deposit on map tile
**
**	@param tx	X position on map, tile-based.
**	@param ty	Y position on map, tile-based.
**
**	@return		Returns the oil deposit if found, or NoUnitP.
*/
global Unit* OilDepositOnMap(int tx,int ty)
{
    Unit* table[MAX_UNITS];
    int i;
    int n;

    n=SelectUnits(tx,ty,tx+1,ty+1,table);
    for( i=0; i<n; ++i ) {
	if( table[i]->Type->StoresOil ) {
	    return table[i];
	}
    }
    return NoUnitP;
}

/*----------------------------------------------------------------------------
--	Finding units for attack
----------------------------------------------------------------------------*/

/**
**	Attack units in distance.
**
**		If the unit can attack must be handled by caller.
**
**	@param unit	Find in distance for this unit.
**	@param range	Distance range to look.
**
**	@return		Unit to be attacked.
*/
global Unit* AttackUnitsInDistance(Unit* unit,int range)
{
    Unit* dest;
    UnitType* type;
    UnitType* dtype;
    Unit* table[MAX_UNITS];
    int n;
    int i;
    const Player* player;
    Unit* best_unit;
    int best_priority;
    int best_hp;

    n=SelectUnits(unit->X-range,unit->Y-range
	    ,unit->X+range+1,unit->Y+range+1,table);

    best_unit=NoUnitP;
    best_priority=0;
    best_hp=99999;

    player=unit->Player;
    type=unit->Type;
    for( i=0; i<n; ++i ) {
	dest=table[i];
	// unusable unit
	/*
	if( UnitUnusable(dest) ) {
	    continue;
	}
	*/
	if( dest->Removed || dest->Command.Action==UnitActionDie ) {
	    continue;
	}

	dtype=dest->Type;
	if( !IsEnemy(player,dest) ) {		// a friend
	    continue;
	}
	if( !dest->HP ) {
	    DebugLevel0(__FUNCTION__": HP==0\n");
	    continue;
	}

	if( MapDistanceToUnit(unit->X,unit->Y,dest)>range ) {
	    DebugLevel0("Internal error: %d - %d `%s'\n"
		    ,MapDistanceToUnit(unit->X,unit->Y,dest)
		    ,range
		    ,dest->Type->Name);
	    continue;
	}

	// Check if I can attack this unit.
	DebugLevel3("Can attack unit %p <- %p\n",dest,unit);
	if( CanTarget(type,dtype) ) {
	    DebugLevel3("Can target unit %p <- %p\n",dest,unit);
	    if( best_priority<dtype->Priority ) {
		best_priority=dtype->Priority;
		best_hp=dest->HP;
		best_unit=dest;
		DebugLevel3("Higher priority\n");
	    } else if( best_priority==dtype->Priority ) {
		if( best_hp>dest->HP ) {
		    best_hp=dest->HP;
		    best_unit=dest;
		    DebugLevel3("Less hit-points\n");
		}
	    }
	}
    }

    if( best_unit ) {
	return best_unit;
    }

    return NoUnitP;
}

/**
**	Attack units in attack range.
**
**	@param unit	Find unit in attack range for this unit.
**
**	@return		Pointer to unit which should be attacked.
*/
global Unit* AttackUnitsInRange(Unit* unit)
{
    //
    //	Only units which can attack.
    //
    IfDebug(
	
	if( !unit->Type->CanAttack && !unit->Type->Tower ) {
	    DebugLevel0("Should be handled by caller?\n");
	    abort();
	    return NoUnitP;
	}
    );

    return AttackUnitsInDistance(unit,unit->Stats->AttackRange);
}

/**
**	Attack units in reaction range.
**
**	@param unit	Find unit in reaction range for this unit.
**
**	@return		Pointer to unit which should be attacked.
*/
global Unit* AttackUnitsInReactRange(Unit* unit)
{
    int range;
    UnitType* type;

    //
    //	Only units which can attack.
    //
    type=unit->Type;
    IfDebug(
	if( !type->CanAttack && !type->Tower ) {
	    DebugLevel0("Should be handled by caller?\n");
	    abort();
	    return NoUnitP;
	}
    );

    if( unit->Player->Type==PlayerHuman ) {
	range=type->ReactRangeHuman;
    } else {
	range=type->ReactRangeComputer;
    }

    return AttackUnitsInDistance(unit,range);
}

//@}