Debian Codegraph @

engine/game/script_player.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name script_player.cpp - The player ccl functions. */
11 //
12 // (c) Copyright 2001-2007 by Lutz Sammer and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "stratagus.h"
40 #include "unittype.h"
41 #include "unit_cache.h"
42 #include "player.h"
43 #include "script.h"
44 #include "ai.h"
45 #include "actions.h"
46 #include "commands.h"
47 #include "trigger.h"
48
49 /*----------------------------------------------------------------------------
50 -- Variables
51 ----------------------------------------------------------------------------*/
52
53 /*----------------------------------------------------------------------------
54 -- Functions
55 ----------------------------------------------------------------------------*/
56
57 /**
58 ** Parse the player configuration.
59 **
60 ** @param l Lua state.
61 */
62 static int CclPlayer(lua_State *l)
63 {
64 const char *value;
65 CPlayer *player;
66 int i;
67 int args;
68 int j;
69 int subargs;
70 int k;
71
72 args = lua_gettop(l);
73 j = 0;
74
75 i = LuaToNumber(l, j + 1);
76 ++j;
77 player = &Players[i];
78 if (NumPlayers <= i) {
79 NumPlayers = i + 1;
80 }
81 player->Index = i;
82 memset(player->Units, 0, sizeof(player->Units));
83
84 //
85 // Parse the list: (still everything could be changed!)
86 //
87 for (; j < args; ++j) {
88 value = LuaToString(l, j + 1);
89 ++j;
90
91 if (!strcmp(value, "name")) {
92 player->SetName(LuaToString(l, j + 1));
93 } else if (!strcmp(value, "type")) {
94 value = LuaToString(l, j + 1);
95 if (!strcmp(value, "neutral")) {
96 player->Type = PlayerNeutral;
97 } else if (!strcmp(value, "nobody")) {
98 player->Type = PlayerNobody;
99 } else if (!strcmp(value, "computer")) {
100 player->Type = PlayerComputer;
101 } else if (!strcmp(value, "person")) {
102 player->Type = PlayerPerson;
103 } else if (!strcmp(value, "rescue-passive")) {
104 player->Type = PlayerRescuePassive;
105 } else if (!strcmp(value, "rescue-active")) {
106 player->Type = PlayerRescueActive;
107 } else {
108 LuaError(l, "Unsupported tag: %s" _C_ value);
109 }
110 } else if (!strcmp(value, "ai-name")) {
111 player->AiName = LuaToString(l, j + 1);
112 } else if (!strcmp(value, "team")) {
113 player->Team = LuaToNumber(l, j + 1);
114 } else if (!strcmp(value, "enemy")) {
115 value = LuaToString(l, j + 1);
116 for (i = 0; i < PlayerMax && *value; ++i, ++value) {
117 if (*value == '-' || *value == '_' || *value == ' ') {
118 player->Enemy &= ~(1 << i);
119 } else {
120 player->Enemy |= (1 << i);
121 }
122 }
123 } else if (!strcmp(value, "allied")) {
124 value = LuaToString(l, j + 1);
125 for (i = 0; i < PlayerMax && *value; ++i, ++value) {
126 if (*value == '-' || *value == '_' || *value == ' ') {
127 player->Allied &= ~(1 << i);
128 } else {
129 player->Allied |= (1 << i);
130 }
131 }
132 } else if (!strcmp(value, "shared-vision")) {
133 value = LuaToString(l, j + 1);
134 for (i = 0; i < PlayerMax && *value; ++i, ++value) {
135 if (*value == '-' || *value == '_' || *value == ' ') {
136 player->SharedVision &= ~(1 << i);
137 } else {
138 player->SharedVision |= (1 << i);
139 }
140 }
141 } else if (!strcmp(value, "start")) {
142 LuaCheckTableSize(l, j + 1, 2);
143 player->StartX = LuaToNumber(l, j + 1, 1);
144 player->StartY = LuaToNumber(l, j + 1, 2);
145 } else if (!strcmp(value, "production-rate")) {
146 LuaCheckTable(l, j + 1);
147 subargs = lua_objlen(l, j + 1);
148 for (k = 0; k < subargs; ++k) {
149 value = LuaToString(l, j + 1, k + 1);
150 ++k;
151
152 for (i = 0; i < MaxCosts; ++i) {
153 if (value == DefaultResourceNames[i]) {
154 player->ProductionRate[i] = LuaToNumber(l, j + 1, k + 1);
155 break;
156 }
157 }
158 if (i == MaxCosts) {
159 LuaError(l, "Unsupported tag: %s" _C_ value);
160 }
161 }
162 } else if (!strcmp(value, "stored-resources")) {
163 LuaCheckTable(l, j + 1);
164 subargs = lua_objlen(l, j + 1);
165 for (k = 0; k < subargs; ++k) {
166 value = LuaToString(l, j + 1, k + 1);
167 ++k;
168
169 for (i = 0; i < MaxCosts; ++i) {
170 if (value == DefaultResourceNames[i]) {
171 player->StoredResources[i] = LuaToNumber(l, j + 1, k + 1);
172 break;
173 }
174 }
175 if (i == MaxCosts) {
176 LuaError(l, "Unsupported tag: %s" _C_ value);
177 }
178 }
179 } else if (!strcmp(value, "storage-capacity")) {
180 LuaCheckTable(l, j + 1);
181 subargs = lua_objlen(l, j + 1);
182 for (k = 0; k < subargs; ++k) {
183 value = LuaToString(l, j + 1, k + 1);
184 ++k;
185
186 for (i = 0; i < MaxCosts; ++i) {
187 if (value == DefaultResourceNames[i]) {
188 player->StorageCapacity[i] = LuaToNumber(l, j + 1, k + 1);
189 break;
190 }
191 }
192 if (i == MaxCosts) {
193 LuaError(l, "Unsupported tag: %s" _C_ value);
194 }
195 }
196 } else if (!strcmp(value, "ai-enabled")) {
197 player->AiEnabled = 1;
198 --j;
199 } else if (!strcmp(value, "ai-disabled")) {
200 player->AiEnabled = 0;
201 --j;
202 } else if (!strcmp(value, "unit-limit")) {
203 player->UnitLimit = LuaToNumber(l, j + 1);
204 } else if (!strcmp(value, "building-limit")) {
205 player->BuildingLimit = LuaToNumber(l, j + 1);
206 } else if (!strcmp(value, "total-unit-limit")) {
207 player->TotalUnitLimit = LuaToNumber(l, j + 1);
208 } else if (!strcmp(value, "score")) {
209 player->Score = LuaToNumber(l, j + 1);
210 } else if (!strcmp(value, "total-units")) {
211 player->TotalUnits = LuaToNumber(l, j + 1);
212 } else if (!strcmp(value, "total-buildings")) {
213 player->TotalBuildings = LuaToNumber(l, j + 1);
214 } else if (!strcmp(value, "total-razings")) {
215 player->TotalRazings = LuaToNumber(l, j + 1);
216 } else if (!strcmp(value, "total-kills")) {
217 player->TotalKills = LuaToNumber(l, j + 1);
218 } else if (!strcmp(value, "total-resources")) {
219 LuaCheckTable(l, j + 1);
220 subargs = lua_objlen(l, j + 1);
221 if (subargs != MaxCosts) {
222 LuaError(l, "Wrong number of total-resources: %d" _C_ i);
223 }
224 for (k = 0; k < subargs; ++k) {
225 player->TotalResources[k] = LuaToNumber(l, j + 1, k + 1);
226 }
227 } else if (!strcmp(value, "color")) {
228 int r;
229 int g;
230 int b;
231
232 LuaCheckTableSize(l, j + 1, 3);
233 r = LuaToNumber(l, j + 1, 1);
234 g = LuaToNumber(l, j + 1, 2);
235 b = LuaToNumber(l, j + 1, 3);
236 player->Color = Video.MapRGB(TheScreen->format, r, g, b);
237 } else {
238 LuaError(l, "Unsupported tag: %s" _C_ value);
239 }
240 }
241
242 return 0;
243 }
244
245 /**
246 ** Change unit owner
247 **
248 ** @param l Lua state.
249 */
250 void CclChangeUnitsOwner(
251 const int topLeft[2],
252 const int bottomRight[2],
253 int oldPlayer,
254 int newPlayer,
255 lua_Object unitTypeLua,
256 lua_State *l)
257 {
258 const CUnitType *triggerUnitType;
259
260 if (oldPlayer < 0 || oldPlayer >= PlayerMax) {
261 LuaError(l, "Player number is out of range: %d" _C_ oldPlayer);
262 }
263
264 if (newPlayer < 0 || newPlayer >= PlayerMax) {
265 LuaError(l, "Player number is out of range: %d" _C_ newPlayer);
266 }
267
268 // tolua++ 1.0.93 claims to define TOLUA_NIL, but doesn't,
269 // so we use 0 instead.
270 //
271 // If the argument is nil, behave as if it had been omitted.
272 // This feature is not documented in game.html because
273 // script authors should use "any" rather than nil.
274 if (unitTypeLua == 0 /* TOLUA_NIL */ || lua_isnil(l, unitTypeLua)) {
275 triggerUnitType = ANY_UNIT;
276 } else {
277 lua_pushvalue(l, unitTypeLua);
278 triggerUnitType = TriggerGetUnitType(l);
279 lua_pop(l, 1);
280 }
281
282 // Okay, now Lua won't longjmp any more.
283
284 CUnit *units[UnitMax];
285 int n;
286 const int x1 = topLeft[0];
287 const int y1 = topLeft[1];
288 const int x2 = bottomRight[0];
289 const int y2 = bottomRight[1];
290
291 n = UnitCache.Select(x1, y1, x2 + 1, y2 + 1, units, UnitMax);
292 while (n) {
293 CUnit *const unit = units[n - 1];
294 if (unit->Player->Index == oldPlayer
295 && TriggerMatchUnitType(unit, triggerUnitType)) {
296 unit->ChangeOwner(&Players[newPlayer]);
297 }
298 --n;
299 }
300 }
301
302 /**
303 ** Set MaxSelectable
304 **
305 ** @param l Lua state.
306 */
307 static int CclSetMaxSelectable(lua_State *l)
308 {
309 LuaCheckArgs(l, 1);
310 MaxSelectable = LuaToNumber(l, 1);
311
312 lua_pushnumber(l, MaxSelectable);
313 return 1;
314 }
315
316 /**
317 ** Set player unit limit.
318 **
319 ** @param l Lua state.
320 */
321 static int CclSetAllPlayersUnitLimit(lua_State *l)
322 {
323 int i;
324
325 LuaCheckArgs(l, 1);
326 for (i = 0; i < PlayerMax; ++i) {
327 Players[i].UnitLimit = LuaToNumber(l, 1);
328 }
329
330 lua_pushnumber(l, lua_tonumber(l, 1));
331 return 1;
332 }
333
334 /**
335 ** Set player unit limit.
336 **
337 ** @param l Lua state.
338 */
339 static int CclSetAllPlayersBuildingLimit(lua_State *l)
340 {
341 int i;
342
343 LuaCheckArgs(l, 1);
344 for (i = 0; i < PlayerMax; ++i) {
345 Players[i].BuildingLimit = LuaToNumber(l, 1);
346 }
347
348 lua_pushnumber(l, lua_tonumber(l, 1));
349 return 1;
350 }
351
352 /**
353 ** Set player unit limit.
354 **
355 ** @param l Lua state.
356 */
357 static int CclSetAllPlayersTotalUnitLimit(lua_State *l)
358 {
359 int i;
360
361 LuaCheckArgs(l, 1);
362 for (i = 0; i < PlayerMax; ++i) {
363 Players[i].TotalUnitLimit = LuaToNumber(l, 1);
364 }
365
366 lua_pushnumber(l, lua_tonumber(l, 1));
367 return 1;
368 }
369
370 /**
371 ** Change the diplomacy from player to another player.
372 **
373 ** @param l Lua state.
374 */
375 static int CclSetDiplomacy(lua_State *l)
376 {
377 int plynr;
378 int base;
379 const char *state;
380
381 LuaCheckArgs(l, 3);
382 base = LuaToNumber(l, 1);
383 plynr = LuaToNumber(l, 3);
384 state = LuaToString(l, 2);
385
386 if (!strcmp(state, "allied")) {
387 SendCommandDiplomacy(base, DiplomacyAllied, plynr);
388 } else if (!strcmp(state, "neutral")) {
389 SendCommandDiplomacy(base, DiplomacyNeutral, plynr);
390 } else if (!strcmp(state, "crazy")) {
391 SendCommandDiplomacy(base, DiplomacyCrazy, plynr);
392 } else if (!strcmp(state, "enemy")) {
393 SendCommandDiplomacy(base, DiplomacyEnemy, plynr);
394 }
395
396 return 0;
397 }
398
399 /**
400 ** Change the diplomacy from ThisPlayer to another player.
401 **
402 ** @param l Lua state.
403 */
404 static int CclDiplomacy(lua_State *l)
405 {
406 lua_pushnumber(l, ThisPlayer->Index);
407 lua_insert(l, 1);
408 return CclSetDiplomacy(l);
409 }
410
411 /**
412 ** Change the shared vision from player to another player.
413 **
414 ** @param l Lua state.
415 */
416 static int CclSetSharedVision(lua_State *l)
417 {
418 int plynr;
419 int base;
420 bool shared;
421
422 LuaCheckArgs(l, 3);
423
424 base = LuaToNumber(l, 1);
425 shared = LuaToBoolean(l, 2);
426 plynr = LuaToNumber(l, 3);
427
428 SendCommandSharedVision(base, shared, plynr);
429
430 return 0;
431 }
432
433 /**
434 ** Change the shared vision from ThisPlayer to another player.
435 **
436 ** @param l Lua state.
437 */
438 static int CclSharedVision(lua_State *l)
439 {
440 lua_pushnumber(l, ThisPlayer->Index);
441 lua_insert(l, 1);
442 return CclSetSharedVision(l);
443 }
444
445 /**
446 ** Define player colors
447 **
448 ** @param l Lua state.
449 */
450 static int CclDefinePlayerColors(lua_State *l)
451 {
452 int i;
453 int args;
454 int j;
455 int numcolors;
456
457 LuaCheckArgs(l, 1);
458 LuaCheckTable(l, 1);
459
460 args = lua_objlen(l, 1);
461 for (i = 0; i < args; ++i) {
462 PlayerColorNames[i / 2] = LuaToString(l, 1, i + 1);
463 ++i;
464 lua_rawgeti(l, 1, i + 1);
465 LuaCheckTable(l, -1);
466 numcolors = lua_objlen(l, -1);
467 if (numcolors != PlayerColorIndexCount) {
468 LuaError(l, "You should use %d colors (See DefinePlayerColorIndex())" _C_ PlayerColorIndexCount);
469 }
470 for (j = 0; j < numcolors; ++j) {
471 lua_rawgeti(l, -1, j + 1);
472 LuaCheckTableSize(l, -1, 3);
473 PlayerColorsRGB[i / 2][j].r = LuaToNumber(l, -1, 1);
474 PlayerColorsRGB[i / 2][j].g = LuaToNumber(l, -1, 2);
475 PlayerColorsRGB[i / 2][j].b = LuaToNumber(l, -1, 3);
476 lua_pop(l, 1);
477 }
478 }
479
480 return 0;
481 }
482
483 /**
484 ** Make new player colors
485 **
486 ** @param l Lua state.
487 */
488 static int CclNewPlayerColors(lua_State *l)
489 {
490 LuaCheckArgs(l, 0);
491 SetPlayersPalette();
492
493 return 0;
494 }
495
496 /**
497 ** Define player color indexes
498 **
499 ** @param l Lua state.
500 */
501 static int CclDefinePlayerColorIndex(lua_State *l)
502 {
503 int i;
504
505 LuaCheckArgs(l, 2);
506 PlayerColorIndexStart = LuaToNumber(l, 1);
507 PlayerColorIndexCount = LuaToNumber(l, 2);
508
509 for (i = 0; i < PlayerMax; ++i) {
510 delete[] PlayerColorsRGB[i];
511 PlayerColorsRGB[i] = new SDL_Color[PlayerColorIndexCount];
512 memset(PlayerColorsRGB[i], 0, PlayerColorIndexCount * sizeof(SDL_Color));
513 delete[] PlayerColors[i];
514 PlayerColors[i] = new Uint32[PlayerColorIndexCount];
515 memset(PlayerColorsRGB[i], 0, PlayerColorIndexCount * sizeof(Uint32));
516 }
517 return 0;
518 }
519
520 // ----------------------------------------------------------------------------
521
522 /**
523 ** Register CCL features for players.
524 */
525 void PlayerCclRegister(void)
526 {
527 lua_register(Lua, "Player", CclPlayer);
528
529 lua_register(Lua, "SetMaxSelectable", CclSetMaxSelectable);
530
531 lua_register(Lua, "SetAllPlayersUnitLimit",
532 CclSetAllPlayersUnitLimit);
533 lua_register(Lua, "SetAllPlayersBuildingLimit",
534 CclSetAllPlayersBuildingLimit);
535 lua_register(Lua, "SetAllPlayersTotalUnitLimit",
536 CclSetAllPlayersTotalUnitLimit);
537
538 lua_register(Lua, "SetDiplomacy", CclSetDiplomacy);
539 lua_register(Lua, "Diplomacy", CclDiplomacy);
540 lua_register(Lua, "SetSharedVision", CclSetSharedVision);
541 lua_register(Lua, "SharedVision", CclSharedVision);
542
543 lua_register(Lua, "DefinePlayerColors", CclDefinePlayerColors);
544 lua_register(Lua, "DefinePlayerColorIndex", CclDefinePlayerColorIndex);
545
546 lua_register(Lua, "NewColors", CclNewPlayerColors);
547 }
548
549 //@}