File: player.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 (569 lines) | stat: -rw-r--r-- 13,177 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
/*
**	A clone of a famous game.
*/
/**@name player.c	-	The players. */
/*
**	(c) Copyright 1998 by Lutz Sammer
**
**	$Id: player.c,v 1.24 1999/12/09 16:27:08 root Exp $
*/

//@{

/*----------------------------------------------------------------------------
--	Includes
----------------------------------------------------------------------------*/

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

#include "clone.h"
#include "video.h"
#include "sound_id.h"
#include "unitsound.h"
#include "unittype.h"
#include "player.h"
#include "unit.h"
#include "ai.h"

/*----------------------------------------------------------------------------
--	Variables
----------------------------------------------------------------------------*/

global int NumPlayers;			/// How many player used
global int NetPlayers;			/// How many network players
global Player Players[PlayerMax];	/// All players in play
global Player* ThisPlayer;		/// Player on this computer

/**
**	Colors used for minimap.
*/
local int PlayerColors[PlayerMax] = {
    208,	// red
    1,		// blue
    216,	// green
    220,	// violett
    224,	// orange
    228,	// black
    255,	// white
    2,		// yellow
    251,	// yellow
    251,	// yellow
    251,	// yellow
    251,	// yellow
    251,	// yellow
    251,	// yellow
    251,	// yellow
    251,	// yellow
};

/*----------------------------------------------------------------------------
--	Functions
----------------------------------------------------------------------------*/

/**
**	Create a new player.
**
**	@param name	Player name.
**	@param type	Player type (Computer,Human,...).
*/
global void CreatePlayer(char* name,int type)
{
    int team;
    Player* player;

    DebugLevel3("Player %d, type %d\n",NumPlayers,type);

    if( NumPlayers==PlayerMax ) {	// already done for bigmaps!
	return;
    }
    player=&Players[NumPlayers];
    player->Player=NumPlayers;

    //  Allocate memory for the "list" of this player's units.
    //  FIXME: brutal way, as we won't need MAX_UNITS for this player...
    if( !(player->Units=(Unit**)calloc(MAX_UNITS,sizeof(Unit*))) ) {
	DebugLevel0("Not enough memory to create player %d.\n",NumPlayers);
	return;
    }
    player->TotalNumUnits=0;

    //
    //	Take first slot for human on this computer,
    //	fill other with computer players.
    //
    if( type==PlayerHuman ) {
	if( !ThisPlayer ) {
	    ThisPlayer=player;
	} else if( !NetPlayers ) {
	    // FIXME: only for single players
	    type=PlayerComputer;
	}
    }

    //
    //	Make simple teams.
    //
    switch( type ) {
	case PlayerNeutral:
	case PlayerNobody:
	default:
	    team=0;
	    break;
	case PlayerComputer:
	    team=1;
	    break;
	case PlayerHuman:
	    team=2+NumPlayers;
	    break;
	case PlayerRescuePassive:
	case PlayerRescueActive:
	    // FIXME: correct for multiplayer games?
	    team=2+NumPlayers;
	    break;
    }

    if( NumPlayers==PlayerMax ) {
	static int already_warned;

	if( !already_warned ) {
	    DebugLevel0("Too many players\n");
	    already_warned=1;
	}
	return;
    }

    player->Name=name;
    player->Type=type;
    player->Race=PlayerRaceHuman;
    player->Team=team;
    player->Ai=PlayerAiUniversal;
    // FIXME: Should become Resource[]...
    player->Gold=3000;
    player->Wood=1000;
    player->Oil=1000;

    // FIXME: Should become Income[]...
    player->GoldPerMine=GOLD_PER_MINE;
    player->WoodPerChop=WOOD_PER_CHOP;
    player->OilPerHaul=OIL_PER_HAUL;

    /*
    for( i=0; i<UnitTypeMax/32; ++i ) {
	player->UnitFlags[i]=0;
    }
    */
    memset( &(player->UnitTypesCount), 0, sizeof(player->UnitTypesCount));

    player->Food=0;
    player->NumUnits=0;
    player->NumBuildings=0;
    player->Score=0;

    player->Color=PlayerColors[NumPlayers];

    if( Players[NumPlayers].Type==PlayerComputer ) {
	player->AiEnabled=1;
    } else {
	player->AiEnabled=0;
    }

    ++NumPlayers;
}

/**
**	Change player side.
**
**	@param player	Pointer to player.
**	@param side	New side (Race).
*/
global void PlayerSetSide(Player* player,int side)
{
    player->Race=side;
}

/**
**	Change player ai.
**
**	@param player	Pointer to player.
**	@param ai	AI type.
*/
global void PlayerSetAiNum(Player* player,int ai)
{
    player->Ai=ai;
}

/**
**	Change player gold.
**
**	@param player	Pointer to player.
**	@param gold	How many gold.
*/
global void PlayerSetGold(Player* player,int gold)
{
    player->Gold=gold;
}

/*
**	Change player wood.
**
**	@param player	Pointer to player.
**	@param wood	How many wood.
*/
global void PlayerSetWood(Player* player,int wood)
{
    player->Wood=wood;
}

/**
**	Change player oil.
**
**	@param player	Pointer to player.
**	@param oil	How many oil.
*/
global void PlayerSetOil(Player* player,int oil)
{
    player->Oil=oil;
}

/**
**	Check if enough food for new unit is available.
**
**	@param player	Pointer to player.
**	@param type	Type of unit.
**	@return		True if enought, false otherwise.
*/
global int PlayerCheckFood(const Player* player,const UnitType* type)
{
    // FIXME: currently all units costs 1 food

    if( player->Food<=player->NumUnits ) {
	if( player==ThisPlayer ) {
	    SetMessage("Not enough food...build more farms.");
	} else {
	    // FIXME: message to AI
	    DebugLevel3("Ai: Not enough food...build more farms.\n");
	}
	return 0;
    }
    return 1;
}

/**
**	Check if enough gold for new unit is available.
**
**	@param player	Pointer to player.
**	@param type	Type of unit.
**	@return		True if enought, false otherwise.
*/
global int PlayerCheckGoldCost(const Player* player,const UnitType* type)
{
    return player->Gold>type->Stats[player->Player].Costs[GoldCost];
}

/**
**	Check if enough wood for new unit is available.
**
**	@param player	Pointer to player.
**	@param type	Type of unit.
**	@return		True if enought, false otherwise.
*/
global int PlayerCheckWoodCost(const Player* player,const UnitType* type)
{
    return player->Wood>type->Stats[player->Player].Costs[WoodCost];
}

/**
**	Check if enough oil for new unit is available.
**
**	@param player	Pointer to player.
**	@param type	Type of unit.
**	@return		True if enought, false otherwise.
*/
global int PlayerCheckOilCost(const Player* player,const UnitType* type)
{
    return player->Oil>type->Stats[player->Player].Costs[OilCost];
}

/**
**	Check if enough resources for are available.
**
**	@param player	Pointer to player.
**	@param gold	How many gold.
**	@param wood	How many wood.
**	@param oil	How many oil.
**	@return		True if all enought, false otherwise.
*/
global int PlayerCheckCosts(const Player* player,int gold,int wood,int oil)
{
    if( player->Gold<gold ) {
	if( player==ThisPlayer ) {
	    SetMessage("Not enough gold...mine more gold.");
	} else {
	    DebugLevel3("Ai: Not enough gold...mine more gold.\n");
	}
	return 0;
    }
    if( player->Wood<wood ) {
	if( player==ThisPlayer ) {
	    SetMessage("Not enough wood...chop more trees.");
	} else {
	    DebugLevel3("Ai: Not enough wood...chop more trees.\n");
	}
	return 0;
    }
    if( player->Oil<oil ) {
	if( player==ThisPlayer ) {
	    SetMessage("Not enough oil...drill more oil.");
	} else {
	    DebugLevel3("Ai: Not enough oil...drill more oil.\n");
	}
	return 0;
    }
    return 1;
}

/**
**	Check if enough resources for new unit is available.
**
**	@param player	Pointer to player.
**	@param type	Type of unit.
**	@return		True if enought, false otherwise.
*/
global int PlayerCheckResources(const Player* player,const UnitType* type)
{
    return PlayerCheckCosts(player
	    ,type->Stats[player->Player].Costs[GoldCost]
	    ,type->Stats[player->Player].Costs[WoodCost]
	    ,type->Stats[player->Player].Costs[OilCost]);
}

/**
**	Subtract costs from resources.
**
**	@param player	Pointer to player.
**	@param gold	How many gold.
**	@param wood	How many wood.
**	@param oil	How many oil.
*/
global void PlayerSubCosts(Player* player,int gold,int wood,int oil)
{
    player->Gold-=gold;
    player->Wood-=wood;
    player->Oil-=oil;

    if( player==ThisPlayer ) {
	MustRedraw|=RedrawResources;
    }
}

/**
**	Add costs from resources.
**
**	@param player	Pointer to player.
**	@param gold	How many gold.
**	@param wood	How many wood.
**	@param oil	How many oil.
*/
global void PlayerAddCosts(Player* player,int gold,int wood,int oil)
{
    player->Gold+=gold;
    player->Wood+=wood;
    player->Oil+=oil;

    if( player==ThisPlayer ) {
	MustRedraw|=RedrawResources;
    }
}

/**
**	Subtract resources for new unit.
**
**	@param player	Pointer to player.
**	@param type	Pointer to unit type.
*/
global void PlayerSubResources(Player* player,const UnitType* type)
{
    player->Gold-=type->Stats[player->Player].Costs[GoldCost];
    player->Wood-=type->Stats[player->Player].Costs[WoodCost];
    player->Oil-=type->Stats[player->Player].Costs[OilCost];

    if( player==ThisPlayer ) {
	MustRedraw|=RedrawResources;
    }
}

/**
**	Add resources for canceled unit.
**
**	@param player	Pointer to player.
**	@param type	Pointer to unit type.
*/
global void PlayerAddResources(Player* player,const UnitType* type)
{
    player->Gold+=type->Stats[player->Player].Costs[GoldCost];
    player->Wood+=type->Stats[player->Player].Costs[WoodCost];
    player->Oil+=type->Stats[player->Player].Costs[OilCost];

    if( player==ThisPlayer ) {
	MustRedraw|=RedrawResources;
    }
}

/**
**	Have unit of type.
**
**	@param player	Pointer to player.
**	@param type	Type of unit.
**	@return		How many exists, false otherwise.
*/
global int HaveUnitTypeByType(const Player* player,const UnitType* type)
{
    return player->UnitTypesCount[type->Type];
}

/**
**	Have unit of type.
**
**	@param player	Pointer to player.
**	@param type	Type of unit.
**	@return		How many exists, false otherwise.
*/
global int HaveUnitTypeByIdent(const Player* player,const char* ident)
{
    return player->UnitTypesCount[UnitTypeByIdent(ident)->Type];
}

/**
**	Initialize the Ai for all players.
*/
global void PlayersInitAi(void)
{
    int player;

    for( player=0; player<NumPlayers; ++player ) {
	if( Players[player].AiEnabled ) {
	    AiInit(player);
	}
    }
}

/**
**	Handle AI of all players each frame.
*/
global void PlayersEachFrame(void)
{
    int player;

    for( player=0; player<NumPlayers; ++player ) {
	if( Players[player].AiEnabled ) {
	    AiEachFrame(player);
	}
    }
}

/**
**	Handle AI of all players each second.
*/
global void PlayersEachSecond(void)
{
    int player;

    for( player=0; player<NumPlayers; ++player ) {
	if( Players[player].AiEnabled ) {
	    AiEachSecond(player);
	}
    }
}

/**
**	Change current color set to new player.
**
**	@param player	Pointer to player.
*/
global void PlayerPixels(const Player* player)
{
    Pixels[208]=player->UnitColor1;
    Pixels[209]=player->UnitColor2;
    Pixels[210]=player->UnitColor3;
    Pixels[211]=player->UnitColor4;
}

/**
**	Setup the player colors for the current palette.
*/
global void SetPlayersPalette(void)
{
    int i;

    for( i=0; i<7; ++i ) {
	Players[i].UnitColor1=Pixels[i*4+208];
	Players[i].UnitColor2=Pixels[i*4+209];
	Players[i].UnitColor3=Pixels[i*4+210];
	Players[i].UnitColor4=Pixels[i*4+211];
    }
    Players[i].UnitColor1=Pixels[12];
    Players[i].UnitColor2=Pixels[13];
    Players[i].UnitColor3=Pixels[14];
    Players[i].UnitColor4=Pixels[15];

    // FIXME: nice colors for this players
    for( ++i; i<15; ++i ) {
	Players[i].UnitColor1=Pixels[i*4+208];
	Players[i].UnitColor2=Pixels[i*4+209];
	Players[i].UnitColor3=Pixels[i*4+210];
	Players[i].UnitColor4=Pixels[i*4+211];
    }
    Players[i].UnitColor1=Pixels[12];
    Players[i].UnitColor2=Pixels[13];
    Players[i].UnitColor3=Pixels[14];
    Players[i].UnitColor4=Pixels[15];
}

/**
**	Output debug informations for players.
*/
global void DebugPlayers(void)
{
    int i;

    DebugLevel0("Nr  I Name     Type         Race     Ai\n");
    DebugLevel0("--  - -------- ------------ -------- --\n");
    for( i=0; i<PlayerMax; ++i ) {
	if( Players[i].Type==PlayerNobody ) {
	    continue;
	}
	DebugLevel0("%2d: %c %-8.8s ",i
		,ThisPlayer==&Players[i] ? '*'
			: Players[i].AiEnabled ? '+' : ' '
		,Players[i].Name);
	switch( Players[i].Type ) {
	    case 0: DebugLevel0("Don't know 0 ");	break;
	    case 1: DebugLevel0("Don't know 1 ");	break;
	    case 2: DebugLevel0("neutral      ");	break;
	    case 3: DebugLevel0("nobody       ");	break;
	    case 4: DebugLevel0("computer     ");	break;
	    case 5: DebugLevel0("human        ");	break;
	    case 6: DebugLevel0("rescue pas.  ");	break;
	    case 7: DebugLevel0("rescue akt.  ");	break;
	}
	switch( Players[i].Race ) {
	    case PlayerRaceHuman:   DebugLevel0("human   ");	break;
	    case PlayerRaceOrc:     DebugLevel0("orc     ");	break;
	    case PlayerRaceNeutral: DebugLevel0("neutral ");	break;
	    default: DebugLevel0("what %d ",Players[i].Race);	break;
	}
	DebugLevel0("%2d ",Players[i].Ai);
	switch( Players[i].Ai ) {
	    case PlayerAiLand:	  DebugLevel0("(land)");	break;
	    case PlayerAiPassive: DebugLevel0("(passive)");	break;
	    case PlayerAiAir:	  DebugLevel0("(air)");		break;
	    case PlayerAiSea:	  DebugLevel0("(sea)");		break;
	    default:		  DebugLevel0("?unknown?");	break;
	}
	DebugLevel0("\n");
    }
}

//@}