-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_battle.cpp
381 lines (269 loc) · 7.49 KB
/
map_battle.cpp
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
/*
MAP_BATTLE.CPP v0.30
Battle stuff.
Copyright (c) 2004-2007 Francesco Orsenigo <francesco.orsenigo@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
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; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <math.h>
#include "main.h"
#include "map.h"
#include "game.h"
#include "city.h"
#include "unit.h"
#include "particles.h"
/*=============================================================================
* CITY AFTERMATH
* What happens to a city after a battle?
*/
void battleCityAftermath(cellC* cell)
{
cityC* city = cell->City;
// who's left on the cell?
long players = 0;
for(unitC* u = cell->Unit; u; u = u->Next) if(u->IsAlive())
if(u->Owner) players |= u->Owner->Bit;
else players |= BRAIN_NEUTRAL;
// none survived, nothing happens
if(!players) return;
// raiders?
if(players & BRAIN_NEUTRAL)
{
city->IsConqueredBy(0);
return;
}
// successfully defended
if(city->Owner)
if(city->Owner->Bit & players) return;
// either no owner, or owner not present
// biggest army takes all
int army_power[GAME_MAX_BRAINS];
for(int i=0; i<GAME_MAX_BRAINS; i++) army_power[i] = 0;
for(unitC* u = cell->Unit; u; u = u->Next) if(u->IsAlive())
if(u->Owner) army_power[u->Owner->Id] += u->Figures();
//start from a random player to evenly resolve draws
int best_player = game->Rnd(GAME_MAX_BRAINS);
for(int i=0; i<GAME_MAX_BRAINS; i++)
if(army_power[i] > army_power[best_player]) best_player = i;
city->IsConqueredBy(game->Brains + best_player);
}
/*=============================================================================
* ESCAPE
* Tells if a cell can be used to escape and if so in which direction it leads.
*/
int battleEscape(int x, int y)
{
int up = BATTLE_H/4;
int dn = BATTLE_H - up;
if(x == 0) // left border, West
if(y < up) return 5;
else if(y < dn) return 6;
else return 7;
if(x == BATTLE_W-1) // right border, East
if(y < up) return 3;
else if(y < dn) return 2;
else return 1;
int lf = BATTLE_H/4;
int ri = BATTLE_H - lf;
if(y == 0) // lower border, South
if(x < lf) return 5;
else if(x < ri) return 4;
else return 3;
if(y == BATTLE_H-1) // upper border, North
if(x < lf) return 7;
else if(x < ri) return 0;
else return 1;
return -1;
}
/*=============================================================================
* BORDER
* Determine whether a coordinate couple refers to the border cells or is
* outside the battlefield.
*/
bool battleIsBorder(int x, int y)
{
if(x == BATTLE_W-1) return true;
if(y == BATTLE_H-1) return true;
if(x == 0) return true;
if(y == 0) return true;
return false;
}
bool battleIsOOBorder(signed x, signed y)
{
if(x > BATTLE_W-1) return true;
if(y > BATTLE_H-1) return true;
if(x < 0) return true;
if(y < 0) return true;
return false;
}
/*=============================================================================
* BattleCell
*/
bcC* battleC::BC(float x, float y)
{
return BC((signed)x, (signed)y);
}
bcC* battleC::BC(signed x, signed y)
{
while(x < 0) x += BATTLE_W;
while(y < 0) y += BATTLE_H;
return SubCellA + (x % BATTLE_W) + (y % BATTLE_H) * BATTLE_W;
}
/*=============================================================================
* TURN
*/
void battleC::Turn()
{
Time = 0;
TimeEnd = BATTLE_DEFAULT_DURATION;
}
/*=============================================================================
* IS POSTPONED
*/
bool battleC::IsPostponed()
{
return PrevCheck && Time >= TimeEnd;
}
/*=============================================================================
* THINK
*/
void battleC::Think()
{
if(IsPaused()) return;
if(IsPostponed()) return;
// advance time
Time++;
// particles
particleC* next;
for(particleC* p = ParticlesHook; p; p = next)
{
next = p->Next;
p->Think(); // may actually delete the particle!
}
// set pathing grid
umSetGrid(this);
// units
for(unitC* u = Cell->Unit; u; u = u->Next) if(u->IsAlive())
u->Think();
// check battle status
bool check = Cell->CheckBattle();
// check && !PrevCheck -> new units entered, restart battle
// !check && PrevCheck -> battle just finished, start counting for aftermath
if(!check && PrevCheck) TimeCheck = Time;
PrevCheck = check;
// is battle finished?
if(!check && Time - TimeCheck > BATTLE_AFTERMATH_TIME)
Cell->CloseBattle();
}
/*=============================================================================
* CHECK BATTLE
*
* Tells whether on a cell there are units engaging each other.
*/
bool cellC::CheckBattle(long players)
{
if(!Battle && City)
if(City->Owner) players |= City->Owner->Bit;
else players |= BRAIN_NEUTRAL;
for(unitC* u = Unit; u; u = u->Next) if(u->IsAlive())
if(u->Owner) players |= u->Owner->Bit;
else players |= BRAIN_NEUTRAL;
for(brainC* b = game->Brains; b < game->BrainsEnd; b++) if(b->Bit & players)
if(b->EngageMask & players) return true;
return false;
}
bool cellC::CheckBattle()
{
return CheckBattle(0);
}
/*=============================================================================
* PAUSES
*/
/*void battleC::PauseToggle(int id)
{
long mask = 1<<id;
if(PauseMask & mask) PauseMask &= ~mask;
else PauseMask |= mask;
}
*/
long battleC::IsPaused()
{
return Cell != game->ActiveBattle;
/* unsigned long mask = 0;
// you may have units fielded
for(unitC* u = Cell->Unit; u; u = u->Next)
if(u->Owner) mask |= u->Owner->Bit;
// or just a city
if(Cell->City && Cell->City->Owner)
mask |= Cell->City->Owner->Bit;
// discard ready players
for(brainC* b = game->Brains; b < game->BrainsEnd; b++)
if(b->Ready) mask &= ~b->Bit;
return Cell->Battle->PauseMask & mask;
*/
}
/*=============================================================================
* CONSTRUCTOR
*/
battleC::battleC(cellC* c)
{
if(c->Battle) throw "battleC::battleC: cell already has a battle";
Cell = c; Cell->Battle = this;
ParticlesHook = 0;
Time = 0;
TimeEnd = BATTLE_DEFAULT_DURATION;
// PauseMask = 0; //~0;
PrevCheck = true;
Rnd.Seed = c->X + c->Y + c->Land->Type;
GenerateMap();
}
/*=============================================================================
* DESTRUCTOR
*/
battleC::~battleC()
{
// conquered city?
if(Cell->City) battleCityAftermath(Cell);
// remove dead units and reset unit orders
unitC* u = Cell->Unit;
while(u)
if(u->IsAlive())
{
u->LandOrders.Path.Clear();
u->LandOrders.OrderDefault()->SetAction(u);
u = u->Next;
}
else
{
unitC* n = u->Next;
u->Remove();
u = n;
}
// destroy particles
while(ParticlesHook) delete ParticlesHook;
// detatch battle
Cell->Battle = 0;
//@@ if self has view, event with battle aftermath
//@@ if self has focus, open popup with battle aftermath
}
/*=============================================================================
* CELL
*/
void cellC::SpawnBattle()
{
if(!Battle) new battleC(this);
}
void cellC::CloseBattle()
{
if(Battle) delete Battle;
}
//EOF =========================================================================