[go: up one dir, main page]

Menu

[r205]: / code / unit.c  Maximize  Restore  History

Download this file

631 lines (457 with data), 16.2 kB

  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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*******************************************************************************
* UNIT.C *
* - Declarations and functions for handling game part of units *
* *
* FREE SPACE COLONISATION *
* Copyright (C) 2002 - 2007 Paul Mueller <pmtech@swissonline.ch> *
* *
* 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
*******************************************************************************/
/*******************************************************************************
* INCLUDES *
*******************************************************************************/
#include <memory.h> /* memset() */
#include <stdio.h> /* sprintf() */
#include "code/fscmap.h" /* Get destination position */
#include "code/fsctool.h"
#include "code/unitinfo.h" /* ORDER_* */
#include "code/unittype.h"
#include "unit.h"
/*******************************************************************************
* DEFINES *
*******************************************************************************/
#define UNIT_FREESLOT_TYPE (char)(-1)
#define UNIT_MAX 600
/*******************************************************************************
* DATA *
*******************************************************************************/
static char *UnitNames[] = { /* Names for shuttles and other transport units */
"",
"Columbus","Copernicus", "Cousteau",
"Einstein", "El-Baz", "Fermi", "Feynman",
"Galileo", "Goddard", "Hawking", "Justman",
"Magellan", "Onizuka", "Pickover", "Pike",
"Rees", "Sakharov", "Tichy", "Voltaire"
};
static UNIT Units[UNIT_MAX + 2]; /* 0 is invalid, shared with frontend */
/*******************************************************************************
* CODE *
*******************************************************************************/
/*
* Name:
* unitSetName
* Description:
* Creates a name for given unit, based on given unittype
* Input:
* punit *: Pointer on unit to create name for
* ut *: Pointer on unit type
*/
static void unitSetName(UNIT *punit, UNITTYPE *ut)
{
sprintf(punit -> name, "%s %d", ut -> name, fsctoolRand(600));
}
/* ========================================================================== */
/* ======================= PUBLIC FUNCTIONS ================================= */
/* ========================================================================== */
/*
* Name:
* unitInit
* Description:
* Initializes the buffer for the units
* Input:
* None
*/
void unitInit(void)
{
memset(&Units[0], 0, sizeof(UNIT) * (UNIT_MAX + 2));
}
/*
* Name:
* unitName
* Description:
* Returns the name of the unit with the given id
* Input:
* Unitid
*/
char *unitName(int unitno)
{
static int nameno = 0;
nameno += (3 + unitno);
nameno %= 19;
return(UnitNames[nameno]);
}
/*
* Name:
* unitGet
* Description:
* Returns a pointer on the unit with given id. If the id is invalid
* then a pointer on unit 0 is returned.
* Input:
* Unitid
*/
UNIT *unitGet(int unitno)
{
if ((unitno < 1) || (unitno >= UNIT_MAX)) {
Units[0].type = -1; /* Unit is invalid */
return &Units[0];
}
return &Units[unitno];
}
/*
* Name:
* unitGetList
* Description:
* Fills in the id' of the players units into given list
* Counts all units for given owner.
* Input:
* owner: Number of owner to counts unit for (-1: all players)
* plist *: Where to return the numbers of units
* withorder: Only units with this order (-1: all units)
* Output:
* Number of units in list.
*/
int unitGetList(char owner, int *plist, char withorder)
{
int i;
int count;
count = 0;
for(i = 1; i < UNIT_MAX; i++) {
/* First check if we have to bail out */
if (Units[i].type == 0) {
break;
}
if (owner < 0 || Units[i].owner == owner) {
if ((withorder < 0) || (Units[i].order.what == withorder)){
*plist = i; /* Save number of unit */
plist++;
count++;
}
}
}
*plist = 0; /* Sign end of list */
return count;
}
/* --------- Unit list functions ---------- */
/*
* Name:
* unitListRemove
* Description:
* Removes unit with given if from iven liked list
* Input:
* base *:
* unitno:
*/
void unitListRemove(int *base, int unitno)
{
UNIT *pactual;
int actualunit;
if ((*base) == 0) {
return; /* Error, should never happen */
}
actualunit = *base;
do {
pactual = &Units[actualunit];
if (actualunit == unitno) {
/* found it, remove it */
(*base) = pactual -> nextid; /* Attach next to prevoius unit */
pactual -> nextid = 0; /* Set next to zero */
return;
}
base = &pactual -> nextid; /* Where to attach next, if */
/* actual unit is removed */
actualunit = pactual -> nextid;
}
while (actualunit);
}
/*
* Name:
* unitListInsert
* Description:
* Adds a unit to the given list. Add it at the front of possible units
* already in existing list on tile
* Input:
* base *: Pointer on base of linked list
* unitno: Id of unit to add to list
* Replaces:
*
*/
void unitListInsert(int *base, int unitno)
{
int oldbase;
oldbase = (*base); /* Save posible actual unit for attachment */
/* If none available, is anyway 0! */
(*base) = unitno; /* Insert new unit at base */
Units[unitno].nextid = oldbase; /* Attach previous unit(s) to base */
}
/*
* Name:
* unitListFind
* Description:
* Looks for a unit in given list.
* Input:
* base *: Pointer on base of linked list
* unitno: Id of unit to look in list for
* Output:
* Number of given unit, if succeeded, else 0
*/
int unitListFind(int *base, int unitno)
{
UNIT *punit;
int next;
next = (*base);
while (next)
{
punit = &Units[next];
if (next == unitno)
{
return next;
}
next = punit -> nextid;
}
return 0;
}
/*
* Name:
* unitListCount
* Description:
* Counts the units in the given list.
* Input:
* base *: Pointer on base of linked list
* Output:
* Number of units in list.
*/
int unitListCount(int *base)
{
UNIT *punit;
int next;
int cnt;
cnt = 0;
next = (*base);
while (next) {
punit = &Units[next];
cnt++;
next = punit -> nextid;
}
return cnt;
}
/*
* Name:
* unitDelete
* Description:
* Removes a unit from thr games internal table
* Input:
* punit *: Pointer on unit to delete.
* Replaces:
* wipe_unit
*/
void unitDelete(UNIT *punit)
{
memset(punit, 0, sizeof(UNIT));
punit -> type = UNIT_FREESLOT_TYPE; /* Is a free slot */
}
/*
* Name:
* unitCreate
* Description:
* On success, returns the id of a new created unit, else 0.
* Input:
* unittype: Type of unit to create
* owner: Owner of unit
* pos: Position of unit
* cargo_load: Cargo to load on unit, if any
* Output:
* Pointer on unit slot, or pointer on Slot 0, if failed
*/
int unitCreate(char unittype, char owner, int pos, char cargo_load)
{
int i;
UNIT *punit;
UNITTYPE *ut;
for (i = 1; i < UNIT_MAX; i++) {
if (Units[i].type <= 0) {
punit = &Units[i];
/* We found a free slot - initialize it */
memset(punit, 0, sizeof(UNIT));
/* -------- Add the info part --------- */
punit -> type = unittype;
punit -> nation = owner;
punit -> owner = owner;
punit -> pos = pos;
punit -> cargo_load = cargo_load;
punit -> order.what = ORDER_NONE;
punit -> level = 0;
/* Add the additional info from unittype */
ut = unittypeGet(unittype);
punit -> hp[UI_VALUE_FULL] = ut -> hit;
punit -> hp[UI_VALUE_ACT] = ut -> hit;
punit -> moves[UI_VALUE_FULL] = ut -> speed;
punit -> moves[UI_VALUE_ACT] = ut -> speed;
punit -> attack = ut -> att;
punit -> defense = ut -> def;
punit -> sensorrange = ut -> sensorrange;
/* ---- Data usable while game is running --------- */
punit -> ability = ut -> ability; /* UNIT_ABILITY_* saves some func calls */
punit -> range_no = ut -> range; /* To save some call while playing */
/* The range of a ship: 0 / 1 / 2 */
/* RULES_RANGE_* */
unitSetName(punit, ut);
return i;
}
}
return 0; /* Invalid unit */
}
/*
* Name:
* unitCreateFromUnit
* Description:
* On success, returns the id of a new created unit, else 0.
* The unit is created using the given unit as template.
* Input:
* unitno: Unit to use as template
* Output:
* Id of new created unit
*/
int unitCreateFromUnit(int unitno)
{
int i;
for (i = 1; i < UNIT_MAX; i++) {
if (Units[i].pos < 0 || Units[i].type < 0) {
memcpy(&Units[i], &Units[unitno], sizeof(UNIT));
return i;
}
}
return 0;
}
/*
* Name:
* unitRestoreMoves
* Description:
* Restore the move points of all units in game
* Input:
* owner: Number of units owner
*/
void unitRestoreMoves(char owner)
{
UNIT *punit;
punit = &Units[1];
while(punit -> type != 0) {
if (punit -> owner == owner) {
punit -> moves[UI_VALUE_ACT] = punit -> moves[UI_VALUE_FULL];
}
punit++;
}
}
/*
* Name:
* unitGetMilitaryMightSum
* Description:
* Returns the sum of all attack and defense values of active units
* for every player in '*vallist'
* Input:
* attackval *: Sum of attack points
* defenseval *: Sum of defense values
* numvalues: Number of values in arrays
*/
void unitGetMilitaryMightSum(int *attackval, int *defenseval, int numvalues)
{
UNIT *punit;
memset(attackval, 0, numvalues * sizeof(int));
memset(defenseval, 0, numvalues * sizeof(int));
punit = &Units[1];
while(punit -> type != 0) {
if (punit -> owner > 0) {
attackval[punit -> owner] += punit -> attack;
defenseval[punit -> owner] += punit -> defense;
}
punit++;
}
}
/*
* Name:
* unitChangeToAbility
* Description:
* Change the given unit to the type given in argument
* Input:
* punit *: Pointer on unit to change given unit to
* ability: Target ability
*/
void unitChangeToAbility(UNIT *punit, int ability)
{
UNITTYPE *ut, *utold;
char type;
int hitpoints, attack, defense;
type = unittypeBestRoleUnit(0, ability);
ut = unittypeGet(type);
utold = unittypeGet(punit -> type); /* For calculation of bonus */
/* taken from unit changed from */
/* Add the additional info from unittype */
if (utold -> hit > 0) {
hitpoints = ut -> hit * punit -> hp[UI_VALUE_FULL] / utold -> hit;
}
if (utold -> att > 0) {
attack = ut -> att * punit -> attack / utold -> att;
}
if (utold -> def > 0) {
defense = ut -> def * punit -> defense / utold -> def;
}
punit -> type = (char)type; /* New type */
punit -> hp[UI_VALUE_FULL] = (char)hitpoints;
punit -> hp[UI_VALUE_ACT] = (char)hitpoints;
punit -> moves[UI_VALUE_FULL] = 0;
punit -> moves[UI_VALUE_ACT] = 0;
punit -> attack = (char)attack;
punit -> defense = (char)defense;
punit -> sensorrange = ut -> sensorrange;
unitSetName(punit, ut);
punit -> ability = (char)ability; /* UNIT_ABILITY_* saves some func calls */
punit -> range_no = ut -> range; /* To save some call while playing */
/* The range of th ship: 0 / 1 / 2 */
/* RULES_RANGE_* */
}
/* ========= Load and save data of units ========= */
/*
* Name:
* unitGetLoadSaveInfo
* Description:
* If 'save' is true then fill in struct 'info' with data needed for
* saving data given in 'data *'. 'numrec' must hold the maximum number
* of records that can be filled, 'recsize' the maximum size of record to
* be saved.
* Input:
* recsize *: Where to fill in the size of the record
* numrec *: Where to put the number of records to be saved
* save: Return info for save / load given info into game
* Output:
* Pointer on data to get data from / fill in with data
*/
char *unitGetLoadSaveInfo(int *recsize, int *numrec, int save)
{
UNIT *punit;
int numunit;
*recsize = sizeof(UNIT);
if (save) {
punit = &Units[1]; /* Start with first unit */
numunit = 0; /* Count units to be saved */
while(punit -> type != 0) {
numunit++;
punit++;
}
*numrec = numunit;
}
else {
*numrec = UNIT_MAX; /* Maximum number of units available */
}
return (char *)&Units[1];
}