Debian Codegraph @

engine/unit/script_unit.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_unit.cpp - The unit ccl functions. */
11 //
12 // (c) Copyright 2001-2014 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 <string.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #include "stratagus.h"
40 #include "unit.h"
41 #include "unit_manager.h"
42 #include "unit_cache.h"
43 #include "unittype.h"
44 #include "animation.h"
45 #include "upgrade.h"
46 #include "player.h"
47 #include "script.h"
48 #include "spells.h"
49 #include "pathfinder.h"
50 #include "map.h"
51 #include "trigger.h"
52 #include "actions.h"
53 #include "construct.h"
54
55 /*----------------------------------------------------------------------------
56 -- Variables
57 ----------------------------------------------------------------------------*/
58
59 /*----------------------------------------------------------------------------
60 -- Functions
61 ----------------------------------------------------------------------------*/
62
63 /**
64 ** Set capture buildings
65 **
66 ** @param l Lua state.
67 **
68 ** @return The old state of the flag
69 */
70 static int CclSetBuildingCapture(lua_State *l)
71 {
72 LuaCheckArgs(l, 1);
73 EnableBuildingCapture = LuaToBoolean(l, 1);
74 return 0;
75 }
76
77 /**
78 ** Get a unit pointer
79 **
80 ** @param l Lua state.
81 **
82 ** @return The unit pointer
83 */
84 static CUnit *CclGetUnit(lua_State *l)
85 {
86 return UnitSlots[(int)LuaToNumber(l, -1)];
87 }
88
89 /**
90 ** Parse order
91 **
92 ** @param l Lua state.
93 ** @param order OUT: resulting order.
94 */
95 void CclParseOrder(lua_State *l, COrder *order)
96 {
97 const char *value;
98 int args;
99 int j;
100
101 //
102 // Parse the list: (still everything could be changed!)
103 //
104 args = lua_objlen(l, -1);
105 for (j = 0; j < args; ++j) {
106 value = LuaToString(l, -1, j + 1);
107 if (!strcmp(value, "action-none")) {
108 order->Action = UnitActionNone;
109 } else if (!strcmp(value, "action-still")) {
110 order->Action = UnitActionStill;
111 } else if (!strcmp(value, "action-stand-ground")) {
112 order->Action = UnitActionStandGround;
113 } else if (!strcmp(value, "action-follow")) {
114 order->Action = UnitActionFollow;
115 } else if (!strcmp(value, "action-move")) {
116 order->Action = UnitActionMove;
117 } else if (!strcmp(value, "action-attack")) {
118 order->Action = UnitActionAttack;
119 } else if (!strcmp(value, "action-attack-ground")) {
120 order->Action = UnitActionAttackGround;
121 } else if (!strcmp(value, "action-die")) {
122 order->Action = UnitActionDie;
123 } else if (!strcmp(value, "action-spell-cast")) {
124 order->Action = UnitActionSpellCast;
125 } else if (!strcmp(value, "action-train")) {
126 order->Action = UnitActionTrain;
127 } else if (!strcmp(value, "action-built")) {
128 order->Action = UnitActionBuilt;
129 } else if (!strcmp(value, "action-board")) {
130 order->Action = UnitActionBoard;
131 } else if (!strcmp(value, "action-unload")) {
132 order->Action = UnitActionUnload;
133 } else if (!strcmp(value, "action-patrol")) {
134 order->Action = UnitActionPatrol;
135 } else if (!strcmp(value, "action-build")) {
136 order->Action = UnitActionBuild;
137 } else if (!strcmp(value, "action-repair")) {
138 order->Action = UnitActionRepair;
139 } else if (!strcmp(value, "action-resource")) {
140 order->Action = UnitActionResource;
141 } else if (!strcmp(value, "range")) {
142 ++j;
143 order->Range = LuaToNumber(l, -1, j + 1);
144 } else if (!strcmp(value, "min-range")) {
145 ++j;
146 order->MinRange = LuaToNumber(l, -1, j + 1);
147 } else if (!strcmp(value, "width")) {
148 ++j;
149 order->Width = LuaToNumber(l, -1, j + 1);
150 } else if (!strcmp(value, "height")) {
151 ++j;
152 order->Height = LuaToNumber(l, -1, j + 1);
153 } else if (!strcmp(value, "goal")) {
154 int slot;
155
156 ++j;
157 value = LuaToString(l, -1, j + 1);
158
159 slot = strtol(value + 1, NULL, 16);
160 order->Goal = UnitSlots[slot];
161 if (!UnitSlots[slot]) {
162 DebugPrint("FIXME: Forward reference not supported\n");
163 }
164 //++UnitSlots[slot]->Refs;
165
166 } else if (!strcmp(value, "tile")) {
167 ++j;
168 lua_rawgeti(l, -1, j + 1);
169 LuaCheckTableSize(l, -1, 2);
170 order->X = LuaToNumber(l, -1, 1);
171 order->Y = LuaToNumber(l, -1, 2);
172 lua_pop(l, 1);
173
174 } else if (!strcmp(value, "type")) {
175 ++j;
176 order->Type = UnitTypeByIdent(LuaToString(l, -1, j + 1));
177
178 } else if (!strcmp(value, "patrol")) {
179 ++j;
180 lua_rawgeti(l, -1, j + 1);
181 LuaCheckTableSize(l, -1, 2);
182 order->Arg1.Patrol.X = LuaToNumber(l, -1, 1);
183 order->Arg1.Patrol.Y = LuaToNumber(l, -1, 2);
184 lua_pop(l, 1);
185
186 } else if (!strcmp(value, "spell")) {
187 ++j;
188 order->Arg1.Spell = SpellTypeByIdent(LuaToString(l, -1, j + 1));
189
190 } else {
191 // This leaves a half initialized unit
192 LuaError(l, "Unsupported tag: %s" _C_ value);
193 }
194 }
195 }
196
197 /**
198 ** Parse orders.
199 **
200 ** @param l Lua state.
201 ** @param unit Unit pointer which should get the orders.
202 */
203 static void CclParseOrders(lua_State *l, CUnit *unit)
204 {
205 for (std::vector<COrder *>::iterator order = unit->Orders.begin();
206 order != unit->Orders.end();
207 ++order) {
208 delete *order;
209 }
210 unit->Orders.clear();
211 for (int j = 0; j < unit->OrderCount; ++j) {
212 lua_rawgeti(l, -1, j + 1);
213 unit->Orders.push_back(new COrder);
214 CclParseOrder(l, unit->Orders[j]);
215 lua_pop(l, 1);
216 }
217 }
218
219 /**
220 ** Parse built
221 **
222 ** @param l Lua state.
223 ** @param unit Unit pointer which should be filled with the data.
224 */
225 static void CclParseBuilt(lua_State *l, CUnit *unit)
226 {
227 const char *value;
228 int args;
229 int j;
230
231 LuaCheckTable(l, -1);
232 args = lua_objlen(l, -1);
233 for (j = 0; j < args; ++j) {
234 value = LuaToString(l, -1, j + 1);
235 ++j;
236 if (!strcmp(value, "progress")) {
237 unit->Data.Built.Progress = LuaToNumber(l, -1, j + 1);
238 } else if (!strcmp(value, "cancel")) {
239 unit->Data.Built.Cancel = 1;
240 --j;
241 } else if (!strcmp(value, "frame")) {
242 int frame;
243 CConstructionFrame *cframe;
244
245 frame = LuaToNumber(l, -1, j + 1);
246 cframe = unit->Type->Construction->Frames;
247 while (frame--) {
248 cframe = cframe->Next;
249 }
250 unit->Data.Built.Frame = cframe;
251 } else {
252 LuaError(l, "Unsupported tag: %s" _C_ value);
253 }
254 }
255 }
256
257 /**
258 ** Parse stored data for train order
259 **
260 ** @param l Lua state.
261 ** @param unit Unit pointer which should be filled with the data.
262 */
263 static void CclParseTrain(lua_State *l, CUnit *unit)
264 {
265 const char *value;
266 int args;
267 int j;
268
269 LuaCheckTable(l, -1);
270 args = lua_objlen(l, -1);
271 for (j = 0; j < args; ++j) {
272 value = LuaToString(l, -1, j + 1);
273 ++j;
274 if (!strcmp(value, "ticks")) {
275 unit->Data.Train.Ticks = LuaToNumber(l, -1, j + 1);
276 } else {
277 LuaError(l, "Unsupported tag: %s" _C_ value);
278 }
279 }
280 }
281
282 /**
283 ** Parse stored data for harvest order
284 **
285 ** @param l Lua state.
286 ** @param unit Unit pointer which should be filled with the data.
287 */
288 static void CclParseHarvest(lua_State *l, CUnit *unit)
289 {
290 const char *value;
291 int args;
292 int j;
293
294 LuaCheckTable(l, -1);
295 args = lua_objlen(l, -1);
296 for (j = 0; j < args; ++j) {
297 value = LuaToString(l, -1, j + 1);
298 ++j;
299 if (!strcmp(value, "current-production")) {
300 lua_rawgeti(l, -1, j + 1);
301 LuaCheckTableSize(l, -1, MaxCosts);
302 for (int i = 0; i < MaxCosts; ++i) {
303 unit->Data.Harvest.CurrentProduction[i] = LuaToNumber(l, -1, i + 1);
304 }
305 lua_pop(l, 1);
306 } else {
307 LuaError(l, "Unsupported tag: %s" _C_ value);
308 }
309 }
310 }
311
312 /**
313 ** Parse stored data for move order
314 **
315 ** @param l Lua state.
316 ** @param unit Unit pointer which should be filled with the data.
317 */
318 static void CclParseMove(lua_State *l, CUnit *unit)
319 {
320 const char *value;
321 int args;
322 int j;
323
324 // Bos Wars 2.5 used to save unit->Data.Move as "data-move"
325 // even for UnitActionDie, which doesn't actually use
326 // unit->Data.Move at all and leaves it uninitialized.
327 // This could cause out-of-range values in save files.
328 // To work around the bug and allow loading such files,
329 // ignore "data-move" when the unit is already dying.
330 if (!unit->Orders.empty()
331 && unit->Orders[0]->Action == UnitActionDie) {
332 return;
333 }
334
335 LuaCheckTable(l, -1);
336 args = lua_objlen(l, -1);
337 for (j = 0; j < args; ++j) {
338 value = LuaToString(l, -1, j + 1);
339 ++j;
340 if (!strcmp(value, "fast")) {
341 unit->Data.Move.Fast = 1;
342 --j;
343 } else if (!strcmp(value, "path")) {
344 int subargs;
345 int k;
346
347 lua_rawgeti(l, -1, j + 1);
348 LuaCheckTable(l, -1);
349 subargs = lua_objlen(l, -1);
350 if (subargs > MAX_PATH_LENGTH) {
351 // unit->Data.Move.Path[] would overflow.
352 LuaError(l, "path data is too long");
353 }
354 for (k = 0; k < subargs; ++k) {
355 unit->Data.Move.Path[k] = LuaToNumber(l, -1, k + 1);
356 }
357 unit->Data.Move.Length = subargs;
358 lua_pop(l, 1);
359 } else {
360 LuaError(l, "Unsupported tag: %s" _C_ value);
361 }
362 }
363 }
364
365 /**
366 ** Parse unit
367 **
368 ** @param l Lua state.
369 **
370 ** @todo Verify that vision table is always correct (transporter)
371 ** @todo (PlaceUnit() and host-info).
372 */
373 static int CclUnit(lua_State *l)
374 {
375 const char *value;
376 CUnit *unit;
377 CUnitType *type;
378 CUnitType *seentype;
379 CPlayer *player;
380 int slot;
381 int i;
382 const char *s;
383 int args;
384 int j;
385
386 args = lua_gettop(l);
387 j = 0;
388
389 slot = LuaToNumber(l, j + 1);
390 ++j;
391
392 unit = NULL;
393 type = NULL;
394 seentype = NULL;
395 player = NULL;
396 i = 0;
397
398 //
399 // Parse the list: (still everything could be changed!)
400 //
401 for (; j < args; ++j) {
402 value = LuaToString(l, j + 1);
403 ++j;
404
405 if (!strcmp(value, "type")) {
406 type = UnitTypeByIdent(LuaToString(l, j + 1));
407 } else if (!strcmp(value, "seen-type")) {
408 seentype = UnitTypeByIdent(LuaToString(l, j + 1));
409 } else if (!strcmp(value, "player")) {
410 player = &Players[(int)LuaToNumber(l, j + 1)];
411
412 // During a unit's death animation (when action is "die" but the
413 // unit still has its original type, i.e. it's still not a corpse)
414 // the unit is already removed from map and from player's
415 // unit list (=the unit went through LetUnitDie() which
416 // calls RemoveUnit() and UnitLost()). Such a unit should not
417 // be put on player's unit list! However, this state is not
418 // easily detected from this place. It seems that it is
419 // characterized by
420 // unit->Orders[0]->Action==UnitActionDie so we have to wait
421 // until we parsed at least Unit::Orders[].
422 Assert(type);
423 unit = UnitSlots[slot];
424 unit->Init(type);
425 unit->Seen.Type = seentype;
426 unit->Removed = 0;
427 Assert(unit->Slot == slot);
428 } else if (!strcmp(value, "next")) {
429 unit->Next = UnitSlots[(int)LuaToNumber(l, j + 1)];
430 } else if (!strcmp(value, "current-sight-range")) {
431 unit->CurrentSightRange = LuaToNumber(l, j + 1);
432 } else if (!strcmp(value, "refs")) {
433 unit->Refs = LuaToNumber(l, j + 1);
434 } else if (!strcmp(value, "host-info")) {
435 int x;
436 int y;
437 int w;
438 int h;
439
440 LuaCheckTableSize(l, j + 1, 4);
441 x = LuaToNumber(l, j + 1, 1);
442 y = LuaToNumber(l, j + 1, 2);
443 w = LuaToNumber(l, j + 1, 3);
444 h = LuaToNumber(l, j + 1, 4);
445 MapSight(player, x, y, w, h, unit->CurrentSightRange, MapMarkTileSight);
446
447 } else if (!strcmp(value, "tile")) {
448 LuaCheckTableSize(l, j + 1, 2);
449 unit->X = LuaToNumber(l, j + 1, 1);
450 unit->Y = LuaToNumber(l, j + 1, 2);
451 } else if (!strcmp(value, "seen-tile")) {
452 LuaCheckTableSize(l, j + 1, 2);
453 unit->Seen.X = LuaToNumber(l, j + 1, 1);
454 unit->Seen.Y = LuaToNumber(l, j + 1, 2);
455 } else if (!strcmp(value, "stats")) {
456 unit->Stats = &type->Stats[(int)LuaToNumber(l, j + 1)];
457 } else if (!strcmp(value, "pixel")) {
458 LuaCheckTableSize(l, j + 1, 2);
459 unit->IX = LuaToNumber(l, j + 1, 1);
460 unit->IY = LuaToNumber(l, j + 1, 2);
461 } else if (!strcmp(value, "seen-pixel")) {
462 LuaCheckTableSize(l, j + 1, 2);
463 unit->Seen.IX = LuaToNumber(l, j + 1, 1);
464 unit->Seen.IY = LuaToNumber(l, j + 1, 2);
465 } else if (!strcmp(value, "frame")) {
466 unit->Frame = LuaToNumber(l, j + 1);
467 } else if (!strcmp(value, "seen")) {
468 unit->Seen.Frame = LuaToNumber(l, j + 1);
469 } else if (!strcmp(value, "not-seen")) {
470 unit->Seen.Frame = UnitNotSeen;
471 --j;
472 } else if (!strcmp(value, "direction")) {
473 unit->Direction = LuaToNumber(l, j + 1);
474 } else if (!strcmp(value, "attacked")) {
475 // FIXME : unsigned long should be better handled
476 unit->Attacked = LuaToNumber(l, j + 1);
477 } else if (!strcmp(value, "auto-repair")) {
478 unit->AutoRepair = 1;
479 --j;
480 } else if (!strcmp(value, "burning")) {
481 unit->Burning = 1;
482 --j;
483 } else if (!strcmp(value, "destroyed")) {
484 unit->Destroyed = 1;
485 --j;
486 } else if (!strcmp(value, "removed")) {
487 unit->Removed = 1;
488 --j;
489 } else if (!strcmp(value, "selected")) {
490 unit->Selected = 1;
491 --j;
492 } else if (!strcmp(value, "rescued-from")) {
493 unit->RescuedFrom = &Players[(int)LuaToNumber(l, j + 1)];
494 } else if (!strcmp(value, "seen-by-player")) {
495 s = LuaToString(l, j + 1);
496 unit->Seen.ByPlayer = 0;
497 for (i = 0; i < PlayerMax && *s; ++i, ++s) {
498 if (*s == '-' || *s == '_' || *s == ' ') {
499 unit->Seen.ByPlayer &= ~(1 << i);
500 } else {
501 unit->Seen.ByPlayer |= (1 << i);
502 }
503 }
504 } else if (!strcmp(value, "seen-destroyed")) {
505 s = LuaToString(l, j + 1);
506 unit->Seen.Destroyed = 0;
507 for (i = 0; i < PlayerMax && *s; ++i, ++s) {
508 if (*s == '-' || *s == '_' || *s == ' ') {
509 unit->Seen.Destroyed &= ~(1 << i);
510 } else {
511 unit->Seen.Destroyed |= (1 << i);
512 }
513 }
514 } else if (!strcmp(value, "constructed")) {
515 unit->Constructed = 1;
516 --j;
517 } else if (!strcmp(value, "seen-constructed")) {
518 unit->Seen.Constructed = 1;
519 --j;
520 } else if (!strcmp(value, "seen-cframe")) {
521 int frame;
522 CConstructionFrame *cframe;
523
524 frame = LuaToNumber(l, j + 1);
525 cframe = unit->Seen.Type->Construction->Frames;
526 while (frame--) {
527 cframe = cframe->Next;
528 }
529 unit->Seen.CFrame = cframe;
530 } else if (!strcmp(value, "seen-state")) {
531 unit->Seen.State = LuaToNumber(l, j + 1);
532 } else if (!strcmp(value, "ttl")) {
533 // FIXME : unsigned long should be better handled
534 unit->TTL = LuaToNumber(l, j + 1);
535 } else if (!strcmp(value, "group-id")) {
536 unit->GroupId = LuaToNumber(l, j + 1);
537 } else if (!strcmp(value, "last-group")) {
538 unit->LastGroup = LuaToNumber(l, j + 1);
539 } else if (!strcmp(value, "resources-held")) {
540 LuaCheckTableSize(l, j + 1, MaxCosts);
541 for (i = 0; i < MaxCosts; ++i) {
542 unit->ResourcesHeld[i] = LuaToNumber(l, j + 1, i + 1);
543 }
544 } else if (!strcmp(value, "production-efficiency")) {
545 unit->ProductionEfficiency = LuaToNumber(l, j + 1);
546 } else if (!strcmp(value, "sub-action")) {
547 unit->SubAction = LuaToNumber(l, j + 1);
548 } else if (!strcmp(value, "wait")) {
549 unit->Wait = LuaToNumber(l, j + 1);
550 } else if (!strcmp(value, "state")) {
551 unit->State = LuaToNumber(l, j + 1);
552 } else if (!strcmp(value, "anim-wait")) {
553 unit->Anim.Wait = LuaToNumber(l, j + 1);
554 } else if (!strcmp(value, "curr-anim")) {
555 unit->Anim.CurrAnim = AnimationsArray[(int)LuaToNumber(l, j + 1)];
556 } else if (!strcmp(value, "anim")) {
557 unit->Anim.Anim = unit->Anim.CurrAnim + (int)LuaToNumber(l, j + 1);
558 } else if (!strcmp(value, "unbreakable")) {
559 unit->Anim.Unbreakable = 1;
560 --j;
561 } else if (!strcmp(value, "blink")) {
562 unit->Blink = LuaToNumber(l, j + 1);
563 } else if (!strcmp(value, "moving")) {
564 unit->Moving = 1;
565 --j;
566 } else if (!strcmp(value, "re-cast")) {
567 unit->ReCast = 1;
568 --j;
569 } else if (!strcmp(value, "boarded")) {
570 unit->Boarded = 1;
571 --j;
572 } else if (!strcmp(value, "units-boarded-count")) {
573 unit->BoardCount = LuaToNumber(l, j + 1);
574 } else if (!strcmp(value, "units-contained")) {
575 int subargs;
576 int k;
577
578 LuaCheckTable(l, j + 1);
579 subargs = lua_objlen(l, j + 1);
580 for (k = 0; k < subargs; ++k) {
581 value = LuaToString(l, j + 1, k + 1);
582 slot = strtol(value + 1, NULL, 16);
583 UnitSlots[slot]->AddInContainer(unit);
584 Assert(UnitSlots[slot]);
585 //++UnitSlots[slot]->Refs;
586 }
587 } else if (!strcmp(value, "order-count")) {
588 unit->OrderCount = LuaToNumber(l, j + 1);
589 } else if (!strcmp(value, "order-flush")) {
590 unit->OrderFlush = LuaToNumber(l, j + 1);
591 } else if (!strcmp(value, "orders")) {
592 lua_pushvalue(l, j + 1);
593 CclParseOrders(l, unit);
594 lua_pop(l, 1);
595 // now we know unit's action so we can assign it to a player
596 unit->AssignToPlayer (player);
597 if (unit->Orders[0]->Action == UnitActionBuilt) {
598 DebugPrint("HACK: the building is not ready yet\n");
599 // HACK: the building is not ready yet
600 unit->Player->UnitTypesCount[type->Slot]--;
601 }
602 } else if (!strcmp(value, "saved-order")) {
603 lua_pushvalue(l, j + 1);
604 CclParseOrder(l, &unit->SavedOrder);
605 lua_pop(l, 1);
606 } else if (!strcmp(value, "new-order")) {
607 lua_pushvalue(l, j + 1);
608 CclParseOrder(l, &unit->NewOrder);
609 lua_pop(l, 1);
610 } else if (!strcmp(value, "data-built")) {
611 lua_pushvalue(l, j + 1);
612 CclParseBuilt(l, unit);
613 lua_pop(l, 1);
614 } else if (!strcmp(value, "data-train")) {
615 lua_pushvalue(l, j + 1);
616 CclParseTrain(l, unit);
617 lua_pop(l, 1);
618 } else if (!strcmp(value, "data-harvest")) {
619 lua_pushvalue(l, j + 1);
620 CclParseHarvest(l, unit);
621 lua_pop(l, 1);
622 } else if (!strcmp(value, "data-move")) {
623 lua_pushvalue(l, j + 1);
624 CclParseMove(l, unit);
625 lua_pop(l, 1);
626 } else if (!strcmp(value, "goal")) {
627 unit->Goal = UnitSlots[(int)LuaToNumber(l, j + 1)];
628 } else if (!strcmp(value, "auto-cast")) {
629 s = LuaToString(l, j + 1);
630 Assert(SpellTypeByIdent(s));
631 if (!unit->AutoCastSpell) {
632 unit->AutoCastSpell = new char[SpellTypeTable.size()];
633 memset(unit->AutoCastSpell, 0, SpellTypeTable.size());
634 }
635 unit->AutoCastSpell[SpellTypeByIdent(s)->Slot] = 1;
636 } else {
637 i = GetVariableIndex(value); // User variables
638 if (i != -1) { // Valid index
639 DefineVariableField(l, unit->Variable + i, j + 1);
640 continue;
641 }
642 LuaError(l, "Unsupported tag: %s" _C_ value);
643 }
644 }
645
646 // Unit may not have been assigned to a player before now. If not,
647 // do so now. It is only assigned earlier if we have orders.
648 // for loading of units from a MAP, and not a savegame, we won't
649 // have orders for those units. They should appear here as if
650 // they were just created.
651 if (!unit->Player) {
652 unit->AssignToPlayer(player);
653 UpdateForNewUnit(unit, 0);
654 }
655
656 // Revealers are units that can see while removed
657 if (unit->Removed && unit->Type->Revealer) {
658 MapMarkUnitSight(unit);
659 }
660
661 // Fix Colors for rescued units.
662 if (unit->RescuedFrom) {
663 unit->Colors = &unit->RescuedFrom->UnitColors;
664 }
665
666 return 0;
667 }
668
669 /**
670 ** Move a unit on map.
671 **
672 ** @param l Lua state.
673 **
674 ** @return Returns the slot number of the made placed.
675 */
676 static int CclMoveUnit(lua_State *l)
677 {
678 CUnit *unit;
679 int heading;
680 int ix;
681 int iy;
682
683 LuaCheckArgs(l, 2);
684
685 lua_pushvalue(l, 1);
686 unit = CclGetUnit(l);
687 lua_pop(l, 1);
688
689 LuaCheckTableSize(l, 2, 2);
690 ix = LuaToNumber(l, 2, 1);
691 iy = LuaToNumber(l, 2, 2);
692
693 heading = SyncRand() % 256;
694 if (UnitCanBeAt(unit, ix, iy)) {
695 unit->Place(ix, iy);
696 } else {
697 unit->X = ix;
698 unit->Y = iy;
699 DropOutOnSide(unit, heading, 1, 1);
700 }
701
702 // PlaceUnit(unit, ix, iy);
703 lua_pushvalue(l, 1);
704 return 1;
705 }
706
707 /**
708 ** Create a unit and place it on the map
709 **
710 ** @param l Lua state.
711 **
712 ** @return Returns the slot number of the made unit.
713 */
714 static int CclCreateUnit(lua_State *l)
715 {
716 CUnitType *unittype;
717 CUnit *unit;
718 int heading;
719 int playerno;
720 int ix;
721 int iy;
722
723 if (SaveGameLoading) {
724 return 1;
725 }
726
727 LuaCheckArgs(l, 3);
728
729 lua_pushvalue(l, 1);
730 unittype = CclGetUnitType(l);
731 lua_pop(l, 1);
732 LuaCheckTableSize(l, 3, 2);
733 ix = LuaToNumber(l, 3, 1);
734 iy = LuaToNumber(l, 3, 2);
735
736 heading = SyncRand() % 256;
737 lua_pushvalue(l, 2);
738 playerno = TriggerGetPlayer(l);
739 lua_pop(l, 1);
740 if (playerno == -1) {
741 printf("CreateUnit: You cannot use \"any\" in create-unit, specify a player\n");
742 LuaError(l, "bad player");
743 return 0;
744 }
745 if (Players[playerno].Type == PlayerNobody) {
746 printf("CreateUnit: player %d does not exist\n", playerno);
747 LuaError(l, "bad player");
748 return 0;
749 }
750
751 unit = MakeUnit(unittype, &Players[playerno]);
752 if (unit == NoUnitP) {
753 DebugPrint("Unable to allocate unit");
754 return 0;
755 } else {
756 if (UnitCanBeAt(unit, ix, iy) ||
757 (unit->Type->Building && CanBuildUnitType(NULL, unit->Type, ix, iy, 0))) {
758 unit->Place(ix, iy);
759 } else {
760 unit->X = ix;
761 unit->Y = iy;
762 DropOutOnSide(unit, heading, 1, 1);
763 }
764 UpdateForNewUnit(unit, 0);
765
766 lua_pushnumber(l, unit->Slot);
767 return 1;
768 }
769 }
770
771 /**
772 ** Set resources held by a unit
773 **
774 ** @param l Lua state.
775 */
776 static int CclSetResourcesHeld(lua_State *l)
777 {
778 CUnit *unit;
779
780 LuaCheckArgs(l, 2);
781
782 if (lua_isnil(l, 1)) {
783 return 0;
784 }
785 LuaCheckTableSize(l, 2, MaxCosts);
786
787 lua_pushvalue(l, 1);
788 unit = CclGetUnit(l);
789 lua_pop(l, 1);
790
791 for (int i = 0; i < MaxCosts; ++i) {
792 unit->ResourcesHeld[i] = LuaToNumber(l, 2, i + 1) * CYCLES_PER_SECOND;
793 }
794
795 return 0;
796 }
797
798 /**
799 ** Order a unit
800 **
801 ** @param l Lua state.
802 **
803 ** OrderUnit(player, unit-type, sloc, dloc, order)
804 */
805 static int CclOrderUnit(lua_State *l)
806 {
807 int plynr;
808 int x1;
809 int y1;
810 int x2;
811 int y2;
812 int dx1;
813 int dy1;
814 int dx2;
815 int dy2;
816 const CUnitType *unittype;
817 CUnit *table[UnitMax];
818 CUnit *unit;
819 int an;
820 int j;
821 const char *order;
822
823 LuaCheckArgs(l, 5);
824
825 lua_pushvalue(l, 1);
826 plynr = TriggerGetPlayer(l);
827 lua_pop(l, 1);
828
829 lua_pushvalue(l, 2);
830 unittype = TriggerGetUnitType(l);
831 lua_pop(l, 1);
832
833 LuaCheckTable(l, 3);
834 x1 = LuaToNumber(l, 3, 1);
835 y1 = LuaToNumber(l, 3, 2);
836 if (lua_objlen(l, 3) == 4) {
837 x2 = LuaToNumber(l, 3, 3);
838 y2 = LuaToNumber(l, 3, 4);
839 } else {
840 x2 = x1;
841 y2 = y1;
842 }
843
844 LuaCheckTable(l, 4);
845 dx1 = LuaToNumber(l, 4, 1);
846 dy1 = LuaToNumber(l, 4, 2);
847 if (lua_objlen(l, 4) == 4) {
848 dx2 = LuaToNumber(l, 4, 3);
849 dy2 = LuaToNumber(l, 4, 4);
850 } else {
851 dx2 = dx1;
852 dy2 = dy1;
853 }
854
855 order = LuaToString(l, 5);
856
857 an = UnitCache.Select(x1, y1, x2 + 1, y2 + 1, table, UnitMax);
858 for (j = 0; j < an; ++j) {
859 unit = table[j];
860 if (unit->Destroyed || unit->Orders[0]->Action == UnitActionDie) {
861 continue;
862 }
863 if (TriggerMatchUnitType(unit, unittype)) {
864 if (plynr == -1 || plynr == unit->Player->Index) {
865 if (!strcmp(order,"move")) {
866 CommandMove(unit, (dx1 + dx2) / 2, (dy1 + dy2) / 2, 1);
867 } else if (!strcmp(order, "attack")) {
868 CUnit *attack;
869
870 attack = TargetOnMap(unit, dx1, dy1, dx2 + 1, dy2 + 1);
871 CommandAttack(unit, (dx1 + dx2) / 2, (dy1 + dy2) / 2, attack, 1);
872 } else if (!strcmp(order, "patrol")) {
873 CommandPatrolUnit(unit, (dx1 + dx2) / 2, (dy1 + dy2) / 2, 1);
874 } else {
875 LuaError(l, "Unsupported order: %s" _C_ order);
876 }
877 }
878 }
879 }
880
881 return 0;
882 }
883
884 /**
885 ** Kill a unit
886 **
887 ** @param l Lua state.
888 **
889 ** @return Returns true if a unit was killed.
890 */
891 static int CclKillUnit(lua_State *l)
892 {
893 int j;
894 int plynr;
895 const CUnitType *unittype;
896 CUnit *unit;
897 CUnit **table;
898
899 LuaCheckArgs(l, 2);
900
901 lua_pushvalue(l, 1);
902 unittype = TriggerGetUnitType(l);
903 lua_pop(l, 1);
904 plynr = TriggerGetPlayer(l);
905 if (plynr == -1) {
906 table = Units;
907 j = NumUnits - 1;
908 } else {
909 table = Players[plynr].Units;
910 j = Players[plynr].TotalNumUnits - 1;
911 }
912
913 for (; j >= 0; --j) {
914 unit = table[j];
915 if (TriggerMatchUnitType(unit, unittype)) {
916 LetUnitDie(unit);
917 lua_pushboolean(l, 1);
918 return 1;
919 }
920 }
921
922 lua_pushboolean(l, 0);
923 return 1;
924 }
925
926 /**
927 ** Kill a unit at a location
928 **
929 ** @param l Lua state.
930 **
931 ** @return Returns the number of units killed.
932 */
933 static int CclKillUnitAt(lua_State *l)
934 {
935 int plynr;
936 int q;
937 int x1;
938 int y1;
939 int x2;
940 int y2;
941 const CUnitType *unittype;
942 CUnit *table[UnitMax];
943 CUnit *unit;
944 int an;
945 int j;
946 int s;
947
948 LuaCheckArgs(l, 5);
949
950 lua_pushvalue(l, 1);
951 unittype = TriggerGetUnitType(l);
952 lua_pop(l, 1);
953
954 lua_pushvalue(l, 2);
955 plynr = TriggerGetPlayer(l);
956 lua_pop(l, 1);
957
958 q = LuaToNumber(l, 3);
959
960 LuaCheckTableSize(l, 4, 2);
961 x1 = LuaToNumber(l, 4, 1);
962 y1 = LuaToNumber(l, 4, 2);
963
964 LuaCheckTableSize(l, 5, 2);
965 x2 = LuaToNumber(l, 5, 1);
966 y2 = LuaToNumber(l, 5, 2);
967
968 an = UnitCache.Select(x1, y1, x2 + 1, y2 + 1, table, UnitMax);
969 for (j = s = 0; j < an && s < q; ++j) {
970 unit = table[j];
971
972 // can't kill dead or indestructible units
973 if (unit->Destroyed ||
974 unit->Orders[0]->Action == UnitActionDie ||
975 unit->Type->Indestructible)
976 {
977 continue;
978 }
979
980 if (TriggerMatchUnitType(unit, unittype))
981 {
982 if (plynr == -1 || plynr == unit->Player->Index) {
983 LetUnitDie(unit);
984 ++s;
985 }
986 }
987 }
988
989 lua_pushnumber(l, s);
990 return 1;
991 }
992
993 /**
994 ** Get a player's units
995 **
996 ** @param l Lua state.
997 **
998 ** @return Array of units.
999 */
1000 static int CclGetUnits(lua_State *l)
1001 {
1002 int plynr;
1003 int i;
1004
1005 LuaCheckArgs(l, 1);
1006
1007 plynr = TriggerGetPlayer(l);
1008
1009 lua_newtable(l);
1010 if (plynr == -1) {
1011 for (i = 0; i < NumUnits; ++i) {
1012 lua_pushnumber(l, Units[i]->Slot);
1013 lua_rawseti(l, -2, i + 1);
1014 }
1015 } else {
1016 for (i = 0; i < Players[plynr].TotalNumUnits; ++i) {
1017 lua_pushnumber(l, Players[plynr].Units[i]->Slot);
1018 lua_rawseti(l, -2, i + 1);
1019 }
1020 }
1021 return 1;
1022 }
1023
1024 /**
1025 ** Get the value of the unit variable.
1026 **
1027 ** @param l Lua state.
1028 **
1029 ** @return The value of the variable of the unit.
1030 */
1031 static int CclGetUnitVariable(lua_State *l)
1032 {
1033 const CUnit *unit;
1034 int index;
1035
1036 LuaCheckArgs(l, 2);
1037
1038 lua_pushvalue(l, 1);
1039 unit = CclGetUnit(l);
1040 lua_pop(l, 1);
1041
1042 index = GetVariableIndex(LuaToString(l, 2));
1043 if (index == -1) {
1044 LuaError(l, "Bad variable name '%s'\n" _C_ LuaToString(l, 2));
1045 }
1046 lua_pushnumber(l, unit->Variable[index].Value);
1047 return 1;
1048 }
1049
1050 /**
1051 ** Set the value of the unit variable.
1052 **
1053 ** @param l Lua state.
1054 **
1055 ** @return The new value of the unit.
1056 */
1057 static int CclSetUnitVariable(lua_State *l)
1058 {
1059 CUnit *unit;
1060 int index;
1061 int value;
1062
1063 LuaCheckArgs(l, 3);
1064
1065 lua_pushvalue(l, 1);
1066 unit = CclGetUnit(l);
1067 lua_pop(l, 1);
1068 index = GetVariableIndex(LuaToString(l, 2));
1069 if (index == -1) {
1070 LuaError(l, "Bad variable name '%s'\n" _C_ LuaToString(l, 2));
1071 }
1072 value = LuaToNumber(l, 3);
1073 if (value > unit->Variable[index].Max) {
1074 unit->Variable[index].Value = unit->Variable[index].Max;
1075 } else {
1076 unit->Variable[index].Value = value;
1077 }
1078 lua_pushnumber(l, value);
1079 return 1;
1080 }
1081
1082 /**
1083 ** Get the usage of unit slots during load to allocate memory
1084 **
1085 ** @param l Lua state.
1086 */
1087 static int CclSlotUsage(lua_State *l)
1088 {
1089 unsigned int args;
1090 unsigned int i;
1091 int unit_index;
1092 unsigned long cycle;
1093
1094 args = lua_gettop(l);
1095 if (args != 2) {
1096 LuaError(l, "SlotUsage called with %d params, expected 2." _C_ args);
1097 }
1098 UnitSlotFree = LuaToNumber(l, 1);
1099 for (i = 0; i < UnitSlotFree; i++) {
1100 UnitSlots[i] = new CUnit;
1101 UnitSlots[i]->Slot = i;
1102 }
1103
1104 // table of 'unit-slot' tables is at index 2
1105
1106 if (!lua_istable(l, 2)) {
1107 LuaError(l, "SlotUsage called with invalid second arg, expected table.");
1108 }
1109 size_t n = lua_objlen(l, 2);
1110
1111 if (n == 0) {
1112 return 0;
1113 }
1114
1115 lua_pushnil(l); // push nil key to start table traversal
1116 for (unsigned int i = 0; i < n; ++i) {
1117 lua_next(l, 2); // get next slot table
1118 if (!lua_istable(l, 4)) {
1119 LuaError(l, "Corrupt SlotUsage table.");
1120 }
1121 lua_getfield(l, 4, "Slot");
1122 unit_index = LuaToNumber(l, -1);
1123 lua_pop(l, 1);
1124
1125 lua_getfield(l, 4, "FreeCycle");
1126 cycle = LuaToNumber(l, -1);
1127 lua_pop(l, 1);
1128
1129 lua_pop(l, 1); // pop slot table
1130
1131 Assert(unit_index != -1 && cycle != (unsigned long)-1);
1132 UnitManager.ReleaseUnit(UnitSlots[unit_index]);
1133 UnitSlots[unit_index]->Refs = cycle;
1134 }
1135 return lua_next(l, 2); // pops key, pushes nothing, and returns 0
1136 }
1137
1138 /**
1139 ** Register CCL features for unit.
1140 */
1141 void UnitCclRegister(void)
1142 {
1143 lua_register(Lua, "SetBuildingCapture", CclSetBuildingCapture);
1144
1145 lua_register(Lua, "Unit", CclUnit);
1146
1147 lua_register(Lua, "MoveUnit", CclMoveUnit);
1148 lua_register(Lua, "CreateUnit", CclCreateUnit);
1149 lua_register(Lua, "SetResourcesHeld", CclSetResourcesHeld);
1150 lua_register(Lua, "OrderUnit", CclOrderUnit);
1151 lua_register(Lua, "KillUnit", CclKillUnit);
1152 lua_register(Lua, "KillUnitAt", CclKillUnitAt);
1153
1154 lua_register(Lua, "GetUnits", CclGetUnits);
1155
1156 // unit member access functions
1157 lua_register(Lua, "GetUnitVariable", CclGetUnitVariable);
1158 lua_register(Lua, "SetUnitVariable", CclSetUnitVariable);
1159
1160 lua_register(Lua, "SlotUsage", CclSlotUsage);
1161 }
1162
1163 //@}