-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
endconditions.js
455 lines (419 loc) · 10.6 KB
/
endconditions.js
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
// registrate events conditions_eventGameInit, conditions_eventDroidBuilt, conditions_eventStructureBuil, conditions_eventResearched, conditions_eventAttacked
namespace("conditions_");
const STATE_contender = "contender";
const STATE_winner = "winner";
const STATE_loser = "loser";
const STATE_spectator = "spectator";
const STRUCTS = [FACTORY, CYBORG_FACTORY, VTOL_FACTORY]; // structures in which you can continue to play
// Uses global var: ```idleTime```
// The time that the player's inactivity is allowed. Actions are considered
// - unit building
// - completion of the research
// - construction of base structures (factories, power plants, laboratories, modules and oil rigs)
// - dealing damage
const BASESTRUCTS = [FACTORY, CYBORG_FACTORY, VTOL_FACTORY, HQ, RESOURCE_EXTRACTOR, POWER_GEN, RESEARCH_LAB];
var ENABLE_activity;
var teams; // array class instance Team
var playersTeam; // array class instancePlayer
class Player
{
constructor(playNum)
{
this.playNum = playNum;
}
hasFactory()
{
for (let i = 0; i < STRUCTS.length; ++i)
{
const onMapStructss = enumStruct(this.playNum, STRUCTS[i]);
for (let j = 0; j < onMapStructss.length; ++j)
{
if (onMapStructss[j].status === BUILT)
{
return true;
}
}
}
return false;
}
hasDroid()
{
if (countDroid(DROID_ANY, this.playNum) > 0)
{
return true;
}
return false;
}
hasOnlyConstructor()
{
if (countDroid(DROID_ANY, this.playNum) - countDroid(DROID_CONSTRUCT, this.playNum) === 0)
{
return true;
}
return false;
}
canReachOil()
{
if (enumStruct(this.playNum, RESOURCE_EXTRACTOR).length > 0)
{
return true;
}
let oils = enumFeature(ALL_PLAYERS).filter((e) => (
e.stattype === OIL_RESOURCE
));
for (let playnum = 0; playnum < maxPlayers; ++playnum)
{
oils = oils.concat(enumStruct(playnum, "A0ResourceExtractor"));
}
const trucks = enumDroid(this.playNum, DROID_CONSTRUCT);
for (let i = 0, len = trucks.length; i < len; ++i)
{
const truck = trucks[i];
for (let j = 0, len2 = oils.length; j < len2; ++j)
{
const oil = oils[j];
if (droidCanReach(truck, oil.x, oil.y))
{
return true;
}
}
}
return false;
}
finalizeGame(state)
{
const transformToSpectator = (state === STATE_loser && !isSpectator(this.playNum) && playerData[this.playNum].isHuman);
if (state === STATE_loser && this.playNum === selectedPlayer)
{
gameOverMessage(false);
}
if (state === STATE_winner && this.playNum === selectedPlayer)
{
gameOverMessage(true);
}
if (transformToSpectator)
{
// should come *after* the gameOverMessage call to ensure the proper gameOverMessage is displayed
transformPlayerToSpectator(this.playNum);
}
}
}
class Team
{
constructor(playerPlayNums)
{
this.players = playerPlayNums.map((playNum) => (new Player(playNum))); // array class instance Player
this.lastActivity = gameTime;
this.onlyAIbots = playerPlayNums.some((playNum) => (
playerData[playNum].isAI === true
));
playerPlayNums.forEach((playerNum) => {
playersTeam[playerNum] = this;
});
}
activeGame()
{
if (this.lastActivity + idleTime >= gameTime)
{
return true;
}
if (this.onlyAIbots)
{
// don't boot teams that are made up of only AI bots
return true;
}
return false;
}
hasFactory()
{
return this.players.some(
(player) => {return player.hasFactory();}
);
}
hasDroid()
{
return this.players.some(
(player) => {return player.hasDroid();}
);
}
hasOnlyConstructor()
{
return this.players.some(
(player) => {return player.hasOnlyConstructor();}
);
}
canReachOil()
{
return this.players.some(
(player) => {return player.canReachOil();}
);
}
canPlay() // TODO skip check if no new events.
{
if (!this.activeGame() && ENABLE_activity)
{
return false;
}
if (!this.hasFactory() && !this.hasDroid())
{
return false;
}
if (!this.hasFactory() && this.hasOnlyConstructor() && !this.canReachOil())
{
return false;
}
return true;
}
containsPlayer(playNum)
{
return this.players.some(
(player) => {return player.playNum === playNum;}
);
}
setState(state)
{
this.state = state;
this.players.forEach(
(player) =>
{
setGameStoryLogPlayerDataValue(player.playNum, "usertype", this.state);
}
);
if (state === STATE_winner || state === STATE_loser || state === STATE_spectator)
{
this.players.forEach(
(player) =>
{
// debug("Setting player " + player.playNum + " to state: " + this.state);
player.finalizeGame(this.state);
}
);
}
}
isContender()
{
return this.state === STATE_contender;
}
}
function checkEndConditions()
{
const newlyLosingTeams = teams.filter((team) => (
team.isContender() && !team.canPlay()
));
newlyLosingTeams.forEach((team) => {
// inform other players if this team / player lost by virtue of being inactive
if (!team.activeGame() && ENABLE_activity)
{
team.players.forEach((player) => {
console(
_("Player")
+ player.playNum + ": " +
_("Lost due to being inactive / playing passively.")
);
});
}
// set to loser status
team.setState(STATE_loser);
});
const contenderTeams = teams.filter((team) => (team.isContender()));
if (contenderTeams.length <= 1 && newlyLosingTeams.length > 0) // game end
{
contenderTeams.forEach((team) => {
team.setState(STATE_winner);
});
// game over message for selectedPlayer who is spectator
// (can be spectator-only slots who have not yet received a message,
// or previous losers who were converted to spectators who should now receive
// a new message that the game has fully ended)
if (isSpectator(-1))
{
gameOverMessage(false);
}
}
}
// FIXME allianceExistsBetween() dont correct if leave player in ALLIANCES_UNSHARED, ALLIANCES_TEAMS mode
// and team is garbage in NO_ALLIANCES, ALLIANCES mode
function inOneTeam(playnum, splaynum)
{
if (
(alliancesType === ALLIANCES_UNSHARED || alliancesType === ALLIANCES_TEAMS) &&
playerData[playnum].team === playerData[splaynum].team
)
{
return true;
}
else if (alliancesType === NO_ALLIANCES && playnum === splaynum)
{
return true;
}
//Victory in alliance mode is also personal.
//Alliances do not affect victory.
//allianceExistsBetween() is not used.
else if (alliancesType === ALLIANCES && playnum === splaynum)
{
return true;
}
return false;
}
function createTeams()
{
teams = [];
playersTeam = Array(maxPlayers);
const inTeamPlayNums = Array(maxPlayers).fill(false);
for (let playNum = 0; playNum < maxPlayers; ++playNum)
{
if (isSpectator(playNum))
{
continue; // skip slots that start as spectators
}
if (!playerData[playNum].isHuman && !playerData[playNum].isAI)
{
// not an allocated slot (is closed or no player / AI)
continue;
}
if (inTeamPlayNums[playNum] === true)
{
continue;
}
inTeamPlayNums[playNum] = true;
const members =[playNum];
for (let splayNum = 0; splayNum < maxPlayers; ++splayNum)
{
if (isSpectator(splayNum))
{
continue; // skip slots that start as spectators
}
if (!playerData[splayNum].isHuman && !playerData[splayNum].isAI)
{
// not an allocated slot (is closed or no player / AI)
continue;
}
if (inTeamPlayNums[splayNum] === false && inOneTeam(playNum, splayNum) === true)
{
members.push(splayNum);
inTeamPlayNums[splayNum] = true;
}
}
const team = new Team(members);
if (team.canPlay())
{
teams.push(team);
team.setState(STATE_contender);
}
else
{
team.setState(STATE_spectator);
}
}
}
/////////////////////////////////////
//First start and loading the save.//
/////////////////////////////////////
function conditions_eventGameInit()
{
ENABLE_activity = (challenge != true && isMultiplayer === true && idleTime > 0); //The prohibition on passive play can interfere when playing against bots. There is no reason to end a fight earlier in PVE.
createTeams();
//find old type spectators
if (ENABLE_activity && !isSpectator(-1))
{
setTimer("activityAlert", 10*1000);
}
setTimer("checkEndConditions", 3000);
if (gameTimeLimit > 0)
{
queue("timeOutGameTenMinWarning", gameTimeLimit - (10 * 60 * 1000));
queue("timeOutGame", gameTimeLimit);
}
}
function conditions_eventGameLoaded()
{
createTeams();
}
function timeOutGameTenMinWarning()
{
console(
_("Host-configured game time limit is approaching. If there is no winner within 10 minutes, the game will end.")
);
}
function timeOutGame()
{
console(
_("Host-configured game time limit exceeded. Game is over.")
);
gameOverMessage(false);
}
///////////////////////////////////////////////////////////////////////////
//Logging for active actions and displaying warnings during passive play.//
///////////////////////////////////////////////////////////////////////////
function activityAlert()
{
// avoid using selectedPlayer to access playersTeam array if it's a spectator (as it may be beyond the bounds of playersTeam.length for spectator-only slots)
if (isSpectator(-1) || (playersTeam[selectedPlayer].state !== STATE_contender))
{
setMissionTime(-1);
removeTimer("activityAlert");
return;
}
if (playersTeam[selectedPlayer].lastActivity + idleTime / 2 < gameTime)
{
console(
_("Playing passively will lead to defeat. Actions that are considered:")
);
console(
_("- unit building - research completion - construction of base structures (factories, power plants, laboratories, modules and oil derricks) - dealing damage")
);
if (getMissionTime() > idleTime)
{
setMissionTime(
(playersTeam[selectedPlayer].lastActivity + idleTime - gameTime) / 1000);
}
}
if (playersTeam[selectedPlayer].lastActivity + idleTime / 2 > gameTime)
{
setMissionTime(-1); // remove timer widget
}
}
function conditions_eventDroidBuilt(droid)
{
if (droid.player === scavengerPlayer || !ENABLE_activity)
{
return;
}
if (playersTeam[droid.player])
{
playersTeam[droid.player].lastActivity = gameTime;
}
}
function conditions_eventStructureBuilt(structure)
{
if (structure.player === scavengerPlayer || !ENABLE_activity)
{
return;
}
if (BASESTRUCTS.includes(structure.stattype) === true && playersTeam[structure.player])
{
playersTeam[structure.player].lastActivity = gameTime;
}
}
function conditions_eventResearched(research, structure, player)
{
if (player === scavengerPlayer || !ENABLE_activity)
{
return;
}
if (playersTeam[player])
{
playersTeam[player].lastActivity = gameTime;
}
}
function conditions_eventAttacked(victim, attacker)
{
if (attacker.player === scavengerPlayer || !ENABLE_activity)
{
return;
}
if (playersTeam[attacker.player] && playersTeam[attacker.player] != playersTeam[victim.player])
{
playersTeam[attacker.player].lastActivity = gameTime;
}
}