File: base.js

package info (click to toggle)
warzone2100 4.6.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 660,332 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (366 lines) | stat: -rw-r--r-- 9,851 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

////////////////////////////////////////////////////////////////////////////////
// Enemy base management.
////////////////////////////////////////////////////////////////////////////////

//;; ## camSetEnemyBases([bases])
//;;
//;; Tell `libcampaign.js` to manage a certain set of enemy bases.
//;; Management assumes auto-cleanup of leftovers on destruction, and also
//;; counting how many bases have been successfully destroyed by the player.
//;; The argument is a JavaScript map from group labels to base descriptions.
//;; Each label points to a group of vital base structures. If no group label
//;; with this name is defined, a group is created automatically
//;; based on `cleanup` area and labeled. Base description
//;; is a JavaScript object with the following optional fields:
//;; * `cleanup` An area label to clean up features in once base is destroyed.
//;;   If base id is not a group label, this field is required in order to auto-create
//;;   the group of stuff in the area which doesn't qualify as a valid leftover.
//;; * `detectMsg` A `PROX_MSG` message id to play when the base is detected.
//;; * `detectSnd` A sound file to play when the base is detected.
//;; * `eliminateSnd` A sound file to play when the base is eliminated.
//;;   The sound is played in the center of the cleanup area, which needs to be defined.
//;; * `player` If base is detected by cleanup area, only objects matching
//;;   this player filter would be added to the base group or cleaned up.
//;;   Note that this most likely disables feature cleanup.
//;;   Additionally, this function would call special event callbacks if they are defined in your script,
//;;   which should be named as follows, where LABEL is the label of the base group:
//;; * `camEnemyBaseDetected_LABEL` Called when the player sees an object from the enemy base group for the first time.
//;; * `camEnemyBaseEliminated_LABEL` Called when the base is eliminated, right after leftovers were cleaned up.
//;;
//;; @param {Object} [bases]
//;; @returns {void}
//;;
function camSetEnemyBases(bases)
{
	const __RELOAD = !camDef(bases);
	if (!__RELOAD)
	{
		__camEnemyBases = bases;
		__camNumEnemyBases = 0;
	}
	// convert label strings to groups and store
	for (const baseLabel in __camEnemyBases)
	{
		const bi = __camEnemyBases[baseLabel];
		const obj = getObject(baseLabel);
		if (camDef(obj) && obj) // group already defined
		{
			if (!camDef(bi.group))
			{
				bi.group = obj.id;
			}
			else
			{
				const structures = enumGroup(bi.group);
				addLabel({ type: GROUP, id: bi.group }, baseLabel);
				for (let idx = 0, len = structures.length; idx < len; ++idx)
				{
					const s = structures[idx];
					if (s.type !== STRUCTURE || __camIsValidLeftover(s))
					{
						continue;
					}
					if (!camDef(bi.player) || camPlayerMatchesFilter(s.player, bi.player))
					{
						//camTrace("Auto-adding", s.id, "to base", baseLabel);
						groupAdd(bi.group, s);
					}
				}
			}
			if (!camDef(bi.cleanup)) // auto-detect cleanup area
			{
				const objs = enumGroup(bi.group);
				if (objs.length > 0)
				{
					const __OFFSET = 2; // increases size of the auto-detected base area a bit
					const a = {
						type: AREA,
						x: mapWidth, y: mapHeight,
						x2: 0, y2: 0
					};
					// smallest rectangle to contain all objects
					for (let idx = 0, len = objs.length; idx < len; ++idx)
					{
						const o = objs[idx];
						if (o.x < a.x)
						{
							a.x = o.x;
						}
						if (o.y < a.y)
						{
							a.y = o.y;
						}
						if (o.x > a.x2)
						{
							a.x2 = o.x;
						}
						if (o.y > a.y2)
						{
							a.y2 = o.y;
						}
					}
					a.x -= __OFFSET;
					a.y -= __OFFSET;
					a.x2 += __OFFSET;
					a.y2 += __OFFSET;
					camTrace("Auto-detected cleanup area for", baseLabel, ":", a.x, a.y, a.x2, a.y2);
					bi.cleanup = "__cam_enemy_base_cleanup__" + baseLabel;
					addLabel(a, bi.cleanup);
				}
			}
		}
		else // define a group automatically
		{
			if (!camDef(bi.cleanup))
			{
				camDebug("Neither group nor cleanup area found for", baseLabel);
				continue;
			}
			bi.group = camNewGroup();
			addLabel({ type: GROUP, id: bi.group }, baseLabel);
			const structs = enumArea(bi.cleanup, ENEMIES, false);
			for (let idx = 0, len = structs.length; idx < len; ++idx)
			{
				const s = structs[idx];
				if (s.type !== STRUCTURE || __camIsValidLeftover(s))
				{
					continue;
				}
				if (!camDef(bi.player) || camPlayerMatchesFilter(s.player, bi.player))
				{
					//camTrace("Auto-adding", s.id, "to base", baseLabel);
					groupAdd(bi.group, s);
				}
			}
		}
		if (groupSize(bi.group) === 0)
		{
			//camDebug("Base", baseLabel, "defined as empty group");
		}
		if (!__RELOAD)
		{
			bi.detected = false;
			bi.eliminated = false;
		}
		//camTrace("Resetting label", baseLabel);
		resetLabel(baseLabel, CAM_HUMAN_PLAYER); // subscribe for eventGroupSeen
	}
}

//;; ## camDetectEnemyBase(baseLabel)
//;;
//;; Plays the "enemy base detected" message and places a beacon for the enemy base
//;; defined by the label, as if the base was actually found by the player.
//;;
//;; @param {string} baseLabel
//;; @returns {void}
//;;
function camDetectEnemyBase(baseLabel)
{
	const bi = __camEnemyBases[baseLabel];
	if (bi.detected || bi.eliminated)
	{
		return;
	}
	camTrace("Enemy base", baseLabel, "detected");
	bi.detected = true;
	if (camDef(bi.detectSnd))
	{
		let pos = camMakePos(bi.cleanup);
		if (!camDef(pos)) // auto-detect sound position by group object pos
		{
			const objs = enumGroup(bi.group);
			if (objs.length > 0)
			{
				const firstObject = objs[0];
				pos = camMakePos(firstObject);
			}
		}
		if (camDef(pos))
		{
			playSound(bi.detectSnd, pos.x, pos.y, 0);
		}
	}
	if (camDef(bi.detectMsg))
	{
		hackAddMessage(bi.detectMsg, PROX_MSG, CAM_HUMAN_PLAYER, false);
	}
	const callback = __camGlobalContext()["camEnemyBaseDetected_" + baseLabel];
	if (camDef(callback))
	{
		callback();
	}
}

//;; ## camAllEnemyBasesEliminated()
//;;
//;; Returns `true` if all enemy bases managed by `libcampaign.js` are destroyed.
//;;
//;; @returns {boolean}
//;;
function camAllEnemyBasesEliminated()
{
	// FIXME: O(n) lookup here
	return __camNumEnemyBases === Object.keys(__camEnemyBases).length;
}

//////////// privates

function __camCheckBaseSeen(seen)
{
	let group = seen; // group?
	if (camDef(seen.group)) // object?
	{
		group = seen.group;
	}
	if (!camDef(group) || !group)
	{
		return;
	}
	// FIXME: O(n) lookup here
	for (const baseLabel in __camEnemyBases)
	{
		const bi = __camEnemyBases[baseLabel];
		if (bi.group !== group)
		{
			continue;
		}
		camDetectEnemyBase(baseLabel);
	}
}

function __camIsValidLeftover(obj)
{
	if (camPlayerMatchesFilter(obj.player, ENEMIES))
	{
		if (obj.type === STRUCTURE && obj.stattype === WALL)
		{
			return true;
		}
	}
	if (obj.type === FEATURE)
	{
		if (obj.stattype === BUILDING)
		{
			return true;
		}
	}
	return false;
}

function __camShouldDestroyLeftover(objInfo, basePlayer)
{
	const object = getObject(objInfo.type, objInfo.player, objInfo.id);
	if (object === null)
	{
		return false;
	}
	return (objInfo.type === STRUCTURE &&
		object.status === BUILT &&
		__camIsValidLeftover(object) &&
		(!camDef(basePlayer) ||
		(camDef(basePlayer) && camPlayerMatchesFilter(objInfo.player, basePlayer))));
}

function __camCheckBaseEliminated(group)
{
	// FIXME: O(n) lookup here
	for (const baseLabel in __camEnemyBases)
	{
		const bi = __camEnemyBases[baseLabel];
		const leftovers = [];
		if (bi.eliminated || (bi.group !== group))
		{
			continue;
		}
		if (enumGroup(bi.group).length > 0)
		{
			return; //still has something in the base group
		}
		if (camDef(bi.cleanup))
		{
			const objects = enumArea(bi.cleanup, ENEMIES, false);
			for (let i = 0, len = objects.length; i < len; ++i)
			{
				const object = objects[i];
				const objInfo = {
					type: object.type,
					player: object.player,
					id: object.id
				};
				if (__camShouldDestroyLeftover(objInfo, bi.player))
				{
					leftovers.push(object);
				}
			}
			for (let i = 0, len = leftovers.length; i < len; ++i)
			{
				// remove with special effect
				const leftover = leftovers[i];
				camSafeRemoveObject(leftover, true);
			}
			if (camDef(bi.eliminateSnd))
			{
				// play sound
				const pos = camMakePos(bi.cleanup);
				playSound(bi.eliminateSnd, pos.x, pos.y, 0);
			}
		}
		else
		{
			camDebug("All bases must have a cleanup area : " + baseLabel);
			continue;
		}
		if (camDef(bi.detectMsg) && bi.detected) // remove the beacon
		{
			hackRemoveMessage(bi.detectMsg, PROX_MSG, CAM_HUMAN_PLAYER);
		}
		camTrace("Enemy base", baseLabel, "eliminated");
		bi.eliminated = true;
		// bump counter before the callback, so that it was
		// actual during the callback
		++__camNumEnemyBases;
		const callback = __camGlobalContext()["camEnemyBaseEliminated_" + baseLabel];
		if (camDef(callback))
		{
			callback();
		}
		__camSetupConsoleForVictoryConditions();
	}
}

function __camBasesTick()
{
	for (const baseLabel in __camEnemyBases)
	{
		const bi = __camEnemyBases[baseLabel];
		if (bi.eliminated || !camDef(bi.reinforce_kind))
		{
			continue;
		}
		if (bi.reinforce_kind === CAM_REINFORCE_NONE)
		{
			continue;
		}
		if (gameTime - bi.reinforce_last < bi.reinforce_interval)
		{
			continue;
		}
		if (!camDef(bi.player))
		{
			camDebug("Enemy base player needs to be set for", baseLabel);
			return;
		}
		if (!camDef(bi.cleanup))
		{
			camDebug("Enemy base cleanup area needs to be set for", baseLabel);
			return;
		}
		bi.reinforce_last = gameTime;
		const list = profile(bi.reinforce_callback);
		const __DEFINED_LZ = (camDef(bi.reinforce_data) && camDef(bi.reinforce_data.posLZ));
		const pos = (__DEFINED_LZ) ? camMakePos(bi.reinforce_data.posLZ) : camMakePos(bi.cleanup);
		camSendReinforcement(bi.player, pos, list, bi.reinforce_kind, bi.reinforce_data);
	}
}