File: script_unit.cpp

package info (click to toggle)
boswars 2.7%2Bsvn160110-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 96,880 kB
  • sloc: cpp: 57,441; python: 1,759; makefile: 34; sh: 26
file content (1163 lines) | stat: -rw-r--r-- 29,043 bytes parent folder | download | duplicates (4)
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
//     ____                _       __               
//    / __ )____  _____   | |     / /___ ___________
//   / __  / __ \/ ___/   | | /| / / __ `/ ___/ ___/
//  / /_/ / /_/ (__  )    | |/ |/ / /_/ / /  (__  ) 
// /_____/\____/____/     |__/|__/\__,_/_/  /____/  
//                                              
//       A futuristic real-time strategy game.
//          This file is part of Bos Wars.
//
/**@name script_unit.cpp - The unit ccl functions. */
//
//      (c) Copyright 2001-2014 by Lutz Sammer and Jimmy Salmon
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; only version 2 of the License.
//
//      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; if not, write to the Free Software
//      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//      02111-1307, USA.
//

//@{

/*----------------------------------------------------------------------------
--  Includes
----------------------------------------------------------------------------*/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "stratagus.h"
#include "unit.h"
#include "unit_manager.h"
#include "unit_cache.h"
#include "unittype.h"
#include "animation.h"
#include "upgrade.h"
#include "player.h"
#include "script.h"
#include "spells.h"
#include "pathfinder.h"
#include "map.h"
#include "trigger.h"
#include "actions.h"
#include "construct.h"

/*----------------------------------------------------------------------------
--  Variables
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
--  Functions
----------------------------------------------------------------------------*/

/**
**  Set capture buildings
**
**  @param l  Lua state.
**
**  @return   The old state of the flag
*/
static int CclSetBuildingCapture(lua_State *l)
{
	LuaCheckArgs(l, 1);
	EnableBuildingCapture = LuaToBoolean(l, 1);
	return 0;
}

/**
**  Get a unit pointer
**
**  @param l  Lua state.
**
**  @return   The unit pointer
*/
static CUnit *CclGetUnit(lua_State *l)
{
	return UnitSlots[(int)LuaToNumber(l, -1)];
}

/**
**  Parse order
**
**  @param l      Lua state.
**  @param order  OUT: resulting order.
*/
void CclParseOrder(lua_State *l, COrder *order)
{
	const char *value;
	int args;
	int j;

	//
	// Parse the list: (still everything could be changed!)
	//
	args = lua_objlen(l, -1);
	for (j = 0; j < args; ++j) {
		value = LuaToString(l, -1, j + 1);
		if (!strcmp(value, "action-none")) {
			order->Action = UnitActionNone;
		} else if (!strcmp(value, "action-still")) {
			order->Action = UnitActionStill;
		} else if (!strcmp(value, "action-stand-ground")) {
			order->Action = UnitActionStandGround;
		} else if (!strcmp(value, "action-follow")) {
			order->Action = UnitActionFollow;
		} else if (!strcmp(value, "action-move")) {
			order->Action = UnitActionMove;
		} else if (!strcmp(value, "action-attack")) {
			order->Action = UnitActionAttack;
		} else if (!strcmp(value, "action-attack-ground")) {
			order->Action = UnitActionAttackGround;
		} else if (!strcmp(value, "action-die")) {
			order->Action = UnitActionDie;
		} else if (!strcmp(value, "action-spell-cast")) {
			order->Action = UnitActionSpellCast;
		} else if (!strcmp(value, "action-train")) {
			order->Action = UnitActionTrain;
		} else if (!strcmp(value, "action-built")) {
			order->Action = UnitActionBuilt;
		} else if (!strcmp(value, "action-board")) {
			order->Action = UnitActionBoard;
		} else if (!strcmp(value, "action-unload")) {
			order->Action = UnitActionUnload;
		} else if (!strcmp(value, "action-patrol")) {
			order->Action = UnitActionPatrol;
		} else if (!strcmp(value, "action-build")) {
			order->Action = UnitActionBuild;
		} else if (!strcmp(value, "action-repair")) {
			order->Action = UnitActionRepair;
		} else if (!strcmp(value, "action-resource")) {
			order->Action = UnitActionResource;
		} else if (!strcmp(value, "range")) {
			++j;
			order->Range = LuaToNumber(l, -1, j + 1);
		} else if (!strcmp(value, "min-range")) {
			++j;
			order->MinRange = LuaToNumber(l, -1, j + 1);
		} else if (!strcmp(value, "width")) {
			++j;
			order->Width = LuaToNumber(l, -1, j + 1);
		} else if (!strcmp(value, "height")) {
			++j;
			order->Height = LuaToNumber(l, -1, j + 1);
		} else if (!strcmp(value, "goal")) {
			int slot;

			++j;
			value = LuaToString(l, -1, j + 1);

			slot = strtol(value + 1, NULL, 16);
			order->Goal = UnitSlots[slot];
			if (!UnitSlots[slot]) {
				DebugPrint("FIXME: Forward reference not supported\n");
			}
			//++UnitSlots[slot]->Refs;

		} else if (!strcmp(value, "tile")) {
			++j;
			lua_rawgeti(l, -1, j + 1);
			LuaCheckTableSize(l, -1, 2);
			order->X = LuaToNumber(l, -1, 1);
			order->Y = LuaToNumber(l, -1, 2);
			lua_pop(l, 1);

		} else if (!strcmp(value, "type")) {
			++j;
			order->Type = UnitTypeByIdent(LuaToString(l, -1, j + 1));

		} else if (!strcmp(value, "patrol")) {
			++j;
			lua_rawgeti(l, -1, j + 1);
			LuaCheckTableSize(l, -1, 2);
			order->Arg1.Patrol.X = LuaToNumber(l, -1, 1);
			order->Arg1.Patrol.Y = LuaToNumber(l, -1, 2);
			lua_pop(l, 1);

		} else if (!strcmp(value, "spell")) {
			++j;
			order->Arg1.Spell = SpellTypeByIdent(LuaToString(l, -1, j + 1));

		} else {
		   // This leaves a half initialized unit
		   LuaError(l, "Unsupported tag: %s" _C_ value);
		}
	}
}

/**
**  Parse orders.
**
**  @param l     Lua state.
**  @param unit  Unit pointer which should get the orders.
*/
static void CclParseOrders(lua_State *l, CUnit *unit)
{
	for (std::vector<COrder *>::iterator order = unit->Orders.begin();
			order != unit->Orders.end();
			++order) {
		delete *order;
	}
	unit->Orders.clear();
	for (int j = 0; j < unit->OrderCount; ++j) {
		lua_rawgeti(l, -1, j + 1);
		unit->Orders.push_back(new COrder);
		CclParseOrder(l, unit->Orders[j]);
		lua_pop(l, 1);
	}
}

/**
**  Parse built
**
**  @param l     Lua state.
**  @param unit  Unit pointer which should be filled with the data.
*/
static void CclParseBuilt(lua_State *l, CUnit *unit)
{
	const char *value;
	int args;
	int j;

	LuaCheckTable(l, -1);
	args = lua_objlen(l, -1);
	for (j = 0; j < args; ++j) {
		value = LuaToString(l, -1, j + 1);
		++j;
		if (!strcmp(value, "progress")) {
			unit->Data.Built.Progress = LuaToNumber(l, -1, j + 1);
		} else if (!strcmp(value, "cancel")) {
			unit->Data.Built.Cancel = 1;
			--j;
		} else if (!strcmp(value, "frame")) {
			int frame;
			CConstructionFrame *cframe;

			frame = LuaToNumber(l, -1, j + 1);
			cframe = unit->Type->Construction->Frames;
			while (frame--) {
				cframe = cframe->Next;
			}
			unit->Data.Built.Frame = cframe;
		} else {
			LuaError(l, "Unsupported tag: %s" _C_ value);
		}
	}
}

/**
**  Parse stored data for train order
**
**  @param l     Lua state.
**  @param unit  Unit pointer which should be filled with the data.
*/
static void CclParseTrain(lua_State *l, CUnit *unit)
{
	const char *value;
	int args;
	int j;

	LuaCheckTable(l, -1);
	args = lua_objlen(l, -1);
	for (j = 0; j < args; ++j) {
		value = LuaToString(l, -1, j + 1);
		++j;
		if (!strcmp(value, "ticks")) {
			unit->Data.Train.Ticks = LuaToNumber(l, -1, j + 1);
		} else {
			LuaError(l, "Unsupported tag: %s" _C_ value);
		}
	}
}

/**
**  Parse stored data for harvest order
**
**  @param l     Lua state.
**  @param unit  Unit pointer which should be filled with the data.
*/
static void CclParseHarvest(lua_State *l, CUnit *unit)
{
	const char *value;
	int args;
	int j;

	LuaCheckTable(l, -1);
	args = lua_objlen(l, -1);
	for (j = 0; j < args; ++j) {
		value = LuaToString(l, -1, j + 1);
		++j;
		if (!strcmp(value, "current-production")) {
			lua_rawgeti(l, -1, j + 1);
			LuaCheckTableSize(l, -1, MaxCosts);
			for (int i = 0; i < MaxCosts; ++i) {
				unit->Data.Harvest.CurrentProduction[i] = LuaToNumber(l, -1, i + 1);
			}
			lua_pop(l, 1);
		} else {
			LuaError(l, "Unsupported tag: %s" _C_ value);
		}
	}
}

/**
**  Parse stored data for move order
**
**  @param l     Lua state.
**  @param unit  Unit pointer which should be filled with the data.
*/
static void CclParseMove(lua_State *l, CUnit *unit)
{
	const char *value;
	int args;
	int j;

	// Bos Wars 2.5 used to save unit->Data.Move as "data-move"
	// even for UnitActionDie, which doesn't actually use
	// unit->Data.Move at all and leaves it uninitialized.
	// This could cause out-of-range values in save files.
	// To work around the bug and allow loading such files,
	// ignore "data-move" when the unit is already dying.
	if (!unit->Orders.empty()
	    && unit->Orders[0]->Action == UnitActionDie) {
		return;
	}

	LuaCheckTable(l, -1);
	args = lua_objlen(l, -1);
	for (j = 0; j < args; ++j) {
		value = LuaToString(l, -1, j + 1);
		++j;
		if (!strcmp(value, "fast")) {
			unit->Data.Move.Fast = 1;
			--j;
		} else if (!strcmp(value, "path")) {
			int subargs;
			int k;

			lua_rawgeti(l, -1, j + 1);
			LuaCheckTable(l, -1);
			subargs = lua_objlen(l, -1);
			if (subargs > MAX_PATH_LENGTH) {
				// unit->Data.Move.Path[] would overflow.
				LuaError(l, "path data is too long");
			}
			for (k = 0; k < subargs; ++k) {
				unit->Data.Move.Path[k] = LuaToNumber(l, -1, k + 1);
			}
			unit->Data.Move.Length = subargs;
			lua_pop(l, 1);
		} else {
			LuaError(l, "Unsupported tag: %s" _C_ value);
		}
	}
}

/**
**  Parse unit
**
**  @param l  Lua state.
**
**  @todo  Verify that vision table is always correct (transporter)
**  @todo (PlaceUnit() and host-info).
*/
static int CclUnit(lua_State *l)
{
	const char *value;
	CUnit *unit;
	CUnitType *type;
	CUnitType *seentype;
	CPlayer *player;
	int slot;
	int i;
	const char *s;
	int args;
	int j;

	args = lua_gettop(l);
	j = 0;

	slot = LuaToNumber(l, j + 1);
	++j;

	unit = NULL;
	type = NULL;
	seentype = NULL;
	player = NULL;
	i = 0;

	//
	// Parse the list: (still everything could be changed!)
	//
	for (; j < args; ++j) {
		value = LuaToString(l, j + 1);
		++j;

		if (!strcmp(value, "type")) {
			type = UnitTypeByIdent(LuaToString(l, j + 1));
		} else if (!strcmp(value, "seen-type")) {
			seentype = UnitTypeByIdent(LuaToString(l, j + 1));
		} else if (!strcmp(value, "player")) {
			player = &Players[(int)LuaToNumber(l, j + 1)];

			// During a unit's death animation (when action is "die" but the
			// unit still has its original type, i.e. it's still not a corpse)
			// the unit is already removed from map and from player's
			// unit list (=the unit went through LetUnitDie() which
			// calls RemoveUnit() and UnitLost()).  Such a unit should not
			// be put on player's unit list!  However, this state is not
			// easily detected from this place.  It seems that it is
			// characterized by
			// unit->Orders[0]->Action==UnitActionDie so we have to wait
			// until we parsed at least Unit::Orders[].
			Assert(type);
			unit = UnitSlots[slot];
			unit->Init(type);
			unit->Seen.Type = seentype;
			unit->Removed = 0;
			Assert(unit->Slot == slot);
		} else if (!strcmp(value, "next")) {
			unit->Next = UnitSlots[(int)LuaToNumber(l, j + 1)];
		} else if (!strcmp(value, "current-sight-range")) {
			unit->CurrentSightRange = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "refs")) {
			unit->Refs = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "host-info")) {
			int x;
			int y;
			int w;
			int h;

			LuaCheckTableSize(l, j + 1, 4);
			x = LuaToNumber(l, j + 1, 1);
			y = LuaToNumber(l, j + 1, 2);
			w = LuaToNumber(l, j + 1, 3);
			h = LuaToNumber(l, j + 1, 4);
			MapSight(player, x, y, w, h, unit->CurrentSightRange, MapMarkTileSight);

		} else if (!strcmp(value, "tile")) {
			LuaCheckTableSize(l, j + 1, 2);
			unit->X = LuaToNumber(l, j + 1, 1);
			unit->Y = LuaToNumber(l, j + 1, 2);
		} else if (!strcmp(value, "seen-tile")) {
			LuaCheckTableSize(l, j + 1, 2);
			unit->Seen.X = LuaToNumber(l, j + 1, 1);
			unit->Seen.Y = LuaToNumber(l, j + 1, 2);
		} else if (!strcmp(value, "stats")) {
			unit->Stats = &type->Stats[(int)LuaToNumber(l, j + 1)];
		} else if (!strcmp(value, "pixel")) {
			LuaCheckTableSize(l, j + 1, 2);
			unit->IX = LuaToNumber(l, j + 1, 1);
			unit->IY = LuaToNumber(l, j + 1, 2);
		} else if (!strcmp(value, "seen-pixel")) {
			LuaCheckTableSize(l, j + 1, 2);
			unit->Seen.IX = LuaToNumber(l, j + 1, 1);
			unit->Seen.IY = LuaToNumber(l, j + 1, 2);
		} else if (!strcmp(value, "frame")) {
			unit->Frame = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "seen")) {
			unit->Seen.Frame = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "not-seen")) {
			unit->Seen.Frame = UnitNotSeen;
			--j;
		} else if (!strcmp(value, "direction")) {
			unit->Direction = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "attacked")) {
			// FIXME : unsigned long should be better handled
			unit->Attacked = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "auto-repair")) {
			unit->AutoRepair = 1;
			--j;
		} else if (!strcmp(value, "burning")) {
			unit->Burning = 1;
			--j;
		} else if (!strcmp(value, "destroyed")) {
			unit->Destroyed = 1;
			--j;
		} else if (!strcmp(value, "removed")) {
			unit->Removed = 1;
			--j;
		} else if (!strcmp(value, "selected")) {
			unit->Selected = 1;
			--j;
		} else if (!strcmp(value, "rescued-from")) {
			unit->RescuedFrom = &Players[(int)LuaToNumber(l, j + 1)];
		} else if (!strcmp(value, "seen-by-player")) {
			s = LuaToString(l, j + 1);
			unit->Seen.ByPlayer = 0;
			for (i = 0; i < PlayerMax && *s; ++i, ++s) {
				if (*s == '-' || *s == '_' || *s == ' ') {
					unit->Seen.ByPlayer &= ~(1 << i);
				} else {
					unit->Seen.ByPlayer |= (1 << i);
				}
			}
		} else if (!strcmp(value, "seen-destroyed")) {
			s = LuaToString(l, j + 1);
			unit->Seen.Destroyed = 0;
			for (i = 0; i < PlayerMax && *s; ++i, ++s) {
				if (*s == '-' || *s == '_' || *s == ' ') {
					unit->Seen.Destroyed &= ~(1 << i);
				} else {
					unit->Seen.Destroyed |= (1 << i);
				}
			}
		} else if (!strcmp(value, "constructed")) {
			unit->Constructed = 1;
			--j;
		} else if (!strcmp(value, "seen-constructed")) {
			unit->Seen.Constructed = 1;
			--j;
		} else if (!strcmp(value, "seen-cframe")) {
			int frame;
			CConstructionFrame *cframe;

			frame = LuaToNumber(l, j + 1);
			cframe = unit->Seen.Type->Construction->Frames;
			while (frame--) {
				cframe = cframe->Next;
			}
			unit->Seen.CFrame = cframe;
		} else if (!strcmp(value, "seen-state")) {
			unit->Seen.State = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "ttl")) {
			// FIXME : unsigned long should be better handled
			unit->TTL = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "group-id")) {
			unit->GroupId = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "last-group")) {
			unit->LastGroup = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "resources-held")) {
			LuaCheckTableSize(l, j + 1, MaxCosts);
			for (i = 0; i < MaxCosts; ++i) {
				unit->ResourcesHeld[i] = LuaToNumber(l, j + 1, i + 1);
			}
		} else if (!strcmp(value, "production-efficiency")) {
			unit->ProductionEfficiency = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "sub-action")) {
			unit->SubAction = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "wait")) {
			unit->Wait = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "state")) {
			unit->State = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "anim-wait")) {
			unit->Anim.Wait = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "curr-anim")) {
			unit->Anim.CurrAnim = AnimationsArray[(int)LuaToNumber(l, j + 1)];
		} else if (!strcmp(value, "anim")) {
			unit->Anim.Anim = unit->Anim.CurrAnim + (int)LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "unbreakable")) {
			unit->Anim.Unbreakable = 1;
			--j;
		} else if (!strcmp(value, "blink")) {
			unit->Blink = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "moving")) {
			unit->Moving = 1;
			--j;
		} else if (!strcmp(value, "re-cast")) {
			unit->ReCast = 1;
			--j;
		} else if (!strcmp(value, "boarded")) {
			unit->Boarded = 1;
			--j;
		} else if (!strcmp(value, "units-boarded-count")) {
			unit->BoardCount = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "units-contained")) {
			int subargs;
			int k;

			LuaCheckTable(l, j + 1);
			subargs = lua_objlen(l, j + 1);
			for (k = 0; k < subargs; ++k) {
				value = LuaToString(l, j + 1, k + 1);
				slot = strtol(value + 1, NULL, 16);
				UnitSlots[slot]->AddInContainer(unit);
				Assert(UnitSlots[slot]);
				//++UnitSlots[slot]->Refs;
			}
		} else if (!strcmp(value, "order-count")) {
			unit->OrderCount = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "order-flush")) {
			unit->OrderFlush = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "orders")) {
			lua_pushvalue(l, j + 1);
			CclParseOrders(l, unit);
			lua_pop(l, 1);
			// now we know unit's action so we can assign it to a player
			unit->AssignToPlayer (player);
			if (unit->Orders[0]->Action == UnitActionBuilt) {
				DebugPrint("HACK: the building is not ready yet\n");
				// HACK: the building is not ready yet
				unit->Player->UnitTypesCount[type->Slot]--;
			}
		} else if (!strcmp(value, "saved-order")) {
			lua_pushvalue(l, j + 1);
			CclParseOrder(l, &unit->SavedOrder);
			lua_pop(l, 1);
		} else if (!strcmp(value, "new-order")) {
			lua_pushvalue(l, j + 1);
			CclParseOrder(l, &unit->NewOrder);
			lua_pop(l, 1);
		} else if (!strcmp(value, "data-built")) {
			lua_pushvalue(l, j + 1);
			CclParseBuilt(l, unit);
			lua_pop(l, 1);
		} else if (!strcmp(value, "data-train")) {
			lua_pushvalue(l, j + 1);
			CclParseTrain(l, unit);
			lua_pop(l, 1);
		} else if (!strcmp(value, "data-harvest")) {
			lua_pushvalue(l, j + 1);
			CclParseHarvest(l, unit);
			lua_pop(l, 1);
		} else if (!strcmp(value, "data-move")) {
			lua_pushvalue(l, j + 1);
			CclParseMove(l, unit);
			lua_pop(l, 1);
		} else if (!strcmp(value, "goal")) {
			unit->Goal = UnitSlots[(int)LuaToNumber(l, j + 1)];
		} else if (!strcmp(value, "auto-cast")) {
			s = LuaToString(l, j + 1);
			Assert(SpellTypeByIdent(s));
			if (!unit->AutoCastSpell) {
				unit->AutoCastSpell = new char[SpellTypeTable.size()];
				memset(unit->AutoCastSpell, 0, SpellTypeTable.size());
			}
			unit->AutoCastSpell[SpellTypeByIdent(s)->Slot] = 1;
		} else {
			i = GetVariableIndex(value); // User variables
			if (i != -1) { // Valid index
				DefineVariableField(l, unit->Variable + i, j + 1);
				continue;
			}
			LuaError(l, "Unsupported tag: %s" _C_ value);
		}
	}

	// Unit may not have been assigned to a player before now. If not,
	// do so now. It is only assigned earlier if we have orders.
	// for loading of units from a MAP, and not a savegame, we won't
	// have orders for those units.  They should appear here as if
	// they were just created.
	if (!unit->Player) {
		unit->AssignToPlayer(player);
		UpdateForNewUnit(unit, 0);
	}

	//  Revealers are units that can see while removed
	if (unit->Removed && unit->Type->Revealer) {
		MapMarkUnitSight(unit);
	}

	// Fix Colors for rescued units.
	if (unit->RescuedFrom) {
		unit->Colors = &unit->RescuedFrom->UnitColors;
	}

	return 0;
}

/**
**  Move a unit on map.
**
**  @param l  Lua state.
**
**  @return   Returns the slot number of the made placed.
*/
static int CclMoveUnit(lua_State *l)
{
	CUnit *unit;
	int heading;
	int ix;
	int iy;

	LuaCheckArgs(l, 2);

	lua_pushvalue(l, 1);
	unit = CclGetUnit(l);
	lua_pop(l, 1);

	LuaCheckTableSize(l, 2, 2);
	ix = LuaToNumber(l, 2, 1);
	iy = LuaToNumber(l, 2, 2);

	heading = SyncRand() % 256;
	if (UnitCanBeAt(unit, ix, iy)) {
		unit->Place(ix, iy);
	} else {
		unit->X = ix;
		unit->Y = iy;
		DropOutOnSide(unit, heading, 1, 1);
	}

//	PlaceUnit(unit, ix, iy);
	lua_pushvalue(l, 1);
	return 1;
}

/**
**  Create a unit and place it on the map
**
**  @param l  Lua state.
**
**  @return   Returns the slot number of the made unit.
*/
static int CclCreateUnit(lua_State *l)
{
	CUnitType *unittype;
	CUnit *unit;
	int heading;
	int playerno;
	int ix;
	int iy;

	if (SaveGameLoading) {
        	return 1;
        }    

	LuaCheckArgs(l, 3);

	lua_pushvalue(l, 1);
	unittype = CclGetUnitType(l);
	lua_pop(l, 1);
	LuaCheckTableSize(l, 3, 2);
	ix = LuaToNumber(l, 3, 1);
	iy = LuaToNumber(l, 3, 2);

	heading = SyncRand() % 256;
	lua_pushvalue(l, 2);
	playerno = TriggerGetPlayer(l);
	lua_pop(l, 1);
	if (playerno == -1) {
		printf("CreateUnit: You cannot use \"any\" in create-unit, specify a player\n");
		LuaError(l, "bad player");
		return 0;
	}
	if (Players[playerno].Type == PlayerNobody) {
		printf("CreateUnit: player %d does not exist\n", playerno);
		LuaError(l, "bad player");
		return 0;
	}

	unit = MakeUnit(unittype, &Players[playerno]);
	if (unit == NoUnitP) {
		DebugPrint("Unable to allocate unit");
		return 0;
	} else {
		if (UnitCanBeAt(unit, ix, iy) ||
				(unit->Type->Building && CanBuildUnitType(NULL, unit->Type, ix, iy, 0))) {
			unit->Place(ix, iy);
		} else {
			unit->X = ix;
			unit->Y = iy;
			DropOutOnSide(unit, heading, 1, 1);
		}
		UpdateForNewUnit(unit, 0);

		lua_pushnumber(l, unit->Slot);
		return 1;
	}
}

/**
**  Set resources held by a unit
**
**  @param l  Lua state.
*/
static int CclSetResourcesHeld(lua_State *l)
{
	CUnit *unit;

	LuaCheckArgs(l, 2);

	if (lua_isnil(l, 1)) {
		return 0;
	}
	LuaCheckTableSize(l, 2, MaxCosts);

	lua_pushvalue(l, 1);
	unit = CclGetUnit(l);
	lua_pop(l, 1);

	for (int i = 0; i < MaxCosts; ++i) {
		unit->ResourcesHeld[i] = LuaToNumber(l, 2, i + 1) * CYCLES_PER_SECOND;
	}

	return 0;
}

/**
**  Order a unit
**
**  @param l  Lua state.
**
**  OrderUnit(player, unit-type, sloc, dloc, order)
*/
static int CclOrderUnit(lua_State *l)
{
	int plynr;
	int x1;
	int y1;
	int x2;
	int y2;
	int dx1;
	int dy1;
	int dx2;
	int dy2;
	const CUnitType *unittype;
	CUnit *table[UnitMax];
	CUnit *unit;
	int an;
	int j;
	const char *order;

	LuaCheckArgs(l, 5);

	lua_pushvalue(l, 1);
	plynr = TriggerGetPlayer(l);
	lua_pop(l, 1);

	lua_pushvalue(l, 2);
	unittype = TriggerGetUnitType(l);
	lua_pop(l, 1);

	LuaCheckTable(l, 3);
	x1 = LuaToNumber(l, 3, 1);
	y1 = LuaToNumber(l, 3, 2);
	if (lua_objlen(l, 3) == 4) {
		x2 = LuaToNumber(l, 3, 3);
		y2 = LuaToNumber(l, 3, 4);
	} else {
		x2 = x1;
		y2 = y1;
	}

	LuaCheckTable(l, 4);
	dx1 = LuaToNumber(l, 4, 1);
	dy1 = LuaToNumber(l, 4, 2);
	if (lua_objlen(l, 4) == 4) {
		dx2 = LuaToNumber(l, 4, 3);
		dy2 = LuaToNumber(l, 4, 4);
	} else {
		dx2 = dx1;
		dy2 = dy1;
	}

	order = LuaToString(l, 5);

	an = UnitCache.Select(x1, y1, x2 + 1, y2 + 1, table, UnitMax);
	for (j = 0; j < an; ++j) {
		unit = table[j];
		if (unit->Destroyed || unit->Orders[0]->Action == UnitActionDie) {
			continue;
		}
		if (TriggerMatchUnitType(unit, unittype)) {
			if (plynr == -1 || plynr == unit->Player->Index) {
				if (!strcmp(order,"move")) {
					CommandMove(unit, (dx1 + dx2) / 2, (dy1 + dy2) / 2, 1);
				} else if (!strcmp(order, "attack")) {
					CUnit *attack;

					attack = TargetOnMap(unit, dx1, dy1, dx2 + 1, dy2 + 1);
					CommandAttack(unit, (dx1 + dx2) / 2, (dy1 + dy2) / 2, attack, 1);
				} else if (!strcmp(order, "patrol")) {
					CommandPatrolUnit(unit, (dx1 + dx2) / 2, (dy1 + dy2) / 2, 1);
				} else {
					LuaError(l, "Unsupported order: %s" _C_ order);
				}
			}
		}
	}

	return 0;
}

/**
**  Kill a unit
**
**  @param l  Lua state.
**
**  @return   Returns true if a unit was killed.
*/
static int CclKillUnit(lua_State *l)
{
	int j;
	int plynr;
	const CUnitType *unittype;
	CUnit *unit;
	CUnit **table;

	LuaCheckArgs(l, 2);

	lua_pushvalue(l, 1);
	unittype = TriggerGetUnitType(l);
	lua_pop(l, 1);
	plynr = TriggerGetPlayer(l);
	if (plynr == -1) {
		table = Units;
		j = NumUnits - 1;
	} else {
		table = Players[plynr].Units;
		j = Players[plynr].TotalNumUnits - 1;
	}

	for (; j >= 0; --j) {
		unit = table[j];
		if (TriggerMatchUnitType(unit, unittype)) {
			LetUnitDie(unit);
			lua_pushboolean(l, 1);
			return 1;
		}
	}

	lua_pushboolean(l, 0);
	return 1;
}

/**
**  Kill a unit at a location
**
**  @param l  Lua state.
**
**  @return   Returns the number of units killed.
*/
static int CclKillUnitAt(lua_State *l)
{
	int plynr;
	int q;
	int x1;
	int y1;
	int x2;
	int y2;
	const CUnitType *unittype;
	CUnit *table[UnitMax];
	CUnit *unit;
	int an;
	int j;
	int s;

	LuaCheckArgs(l, 5);

	lua_pushvalue(l, 1);
	unittype = TriggerGetUnitType(l);
	lua_pop(l, 1);

	lua_pushvalue(l, 2);
	plynr = TriggerGetPlayer(l);
	lua_pop(l, 1);

	q = LuaToNumber(l, 3);

	LuaCheckTableSize(l, 4, 2);
	x1 = LuaToNumber(l, 4, 1);
	y1 = LuaToNumber(l, 4, 2);

	LuaCheckTableSize(l, 5, 2);
	x2 = LuaToNumber(l, 5, 1);
	y2 = LuaToNumber(l, 5, 2);

	an = UnitCache.Select(x1, y1, x2 + 1, y2 + 1, table, UnitMax);
	for (j = s = 0; j < an && s < q; ++j) {
		unit = table[j];

		// can't kill dead or indestructible units
		if (unit->Destroyed ||
			unit->Orders[0]->Action == UnitActionDie ||
			unit->Type->Indestructible)
		{
			continue;
		}

		if (TriggerMatchUnitType(unit, unittype))
		{
			if (plynr == -1 || plynr == unit->Player->Index) {
				LetUnitDie(unit);
				++s;
			}
		}
	}

	lua_pushnumber(l, s);
	return 1;
}

/**
**  Get a player's units
**
**  @param l  Lua state.
**
**  @return   Array of units.
*/
static int CclGetUnits(lua_State *l)
{
	int plynr;
	int i;

	LuaCheckArgs(l, 1);

	plynr = TriggerGetPlayer(l);

	lua_newtable(l);
	if (plynr == -1) {
		for (i = 0; i < NumUnits; ++i) {
			lua_pushnumber(l, Units[i]->Slot);
			lua_rawseti(l, -2, i + 1);
		}
	} else {
		for (i = 0; i < Players[plynr].TotalNumUnits; ++i) {
			lua_pushnumber(l, Players[plynr].Units[i]->Slot);
			lua_rawseti(l, -2, i + 1);
		}
	}
	return 1;
}

/**
**  Get the value of the unit variable.
**
**  @param l  Lua state.
**
**  @return   The value of the variable of the unit.
*/
static int CclGetUnitVariable(lua_State *l)
{
	const CUnit *unit;
	int index;

	LuaCheckArgs(l, 2);

	lua_pushvalue(l, 1);
	unit = CclGetUnit(l);
	lua_pop(l, 1);

	index = GetVariableIndex(LuaToString(l, 2));
	if (index == -1) {
		LuaError(l, "Bad variable name '%s'\n" _C_ LuaToString(l, 2));
	}
	lua_pushnumber(l, unit->Variable[index].Value);
	return 1;
}

/**
**  Set the value of the unit variable.
**
**  @param l  Lua state.
**
**  @return The new value of the unit.
*/
static int CclSetUnitVariable(lua_State *l)
{
	CUnit *unit;
	int index;
	int value;

	LuaCheckArgs(l, 3);

	lua_pushvalue(l, 1);
	unit = CclGetUnit(l);
	lua_pop(l, 1);
	index = GetVariableIndex(LuaToString(l, 2));
	if (index == -1) {
		LuaError(l, "Bad variable name '%s'\n" _C_ LuaToString(l, 2));
	}
	value = LuaToNumber(l, 3);
	if (value > unit->Variable[index].Max) {
		unit->Variable[index].Value = unit->Variable[index].Max;
	} else {
		unit->Variable[index].Value = value;
	}
	lua_pushnumber(l, value);
	return 1;
}

/**
**  Get the usage of unit slots during load to allocate memory
**
**  @param l  Lua state.
*/
static int CclSlotUsage(lua_State *l)
{
	unsigned int args;
	unsigned int i;
	int unit_index;
	unsigned long cycle;

	args = lua_gettop(l);
	if (args != 2) {
		LuaError(l, "SlotUsage called with %d params, expected 2." _C_ args);
	}
	UnitSlotFree = LuaToNumber(l, 1);
	for (i = 0; i < UnitSlotFree; i++) {
		UnitSlots[i] = new CUnit;
		UnitSlots[i]->Slot = i;
	}

	// table of 'unit-slot' tables is at index 2

	if (!lua_istable(l, 2)) {
		LuaError(l, "SlotUsage called with invalid second arg, expected table.");
	}
	size_t n = lua_objlen(l, 2);

	if (n == 0) {
		return 0;
	}
	
	lua_pushnil(l); // push nil key to start table traversal
	for (unsigned int i = 0; i < n; ++i) {
		lua_next(l, 2); // get next slot table
		if (!lua_istable(l, 4)) {
			LuaError(l, "Corrupt SlotUsage table.");
		}
		lua_getfield(l, 4, "Slot");
		unit_index = LuaToNumber(l, -1);
		lua_pop(l, 1);

		lua_getfield(l, 4, "FreeCycle");
		cycle = LuaToNumber(l, -1);
		lua_pop(l, 1);

		lua_pop(l, 1); // pop slot table

		Assert(unit_index != -1 && cycle != (unsigned long)-1);
		UnitManager.ReleaseUnit(UnitSlots[unit_index]);
		UnitSlots[unit_index]->Refs = cycle;
	}
	return lua_next(l, 2); // pops key, pushes nothing, and returns 0
}

/**
**  Register CCL features for unit.
*/
void UnitCclRegister(void)
{
	lua_register(Lua, "SetBuildingCapture", CclSetBuildingCapture);

	lua_register(Lua, "Unit", CclUnit);

	lua_register(Lua, "MoveUnit", CclMoveUnit);
	lua_register(Lua, "CreateUnit", CclCreateUnit);
	lua_register(Lua, "SetResourcesHeld", CclSetResourcesHeld);
	lua_register(Lua, "OrderUnit", CclOrderUnit);
	lua_register(Lua, "KillUnit", CclKillUnit);
	lua_register(Lua, "KillUnitAt", CclKillUnitAt);

	lua_register(Lua, "GetUnits", CclGetUnits);

	// unit member access functions
	lua_register(Lua, "GetUnitVariable", CclGetUnitVariable);
	lua_register(Lua, "SetUnitVariable", CclSetUnitVariable);

	lua_register(Lua, "SlotUsage", CclSlotUsage);
}

//@}