Debian Codegraph @

engine/unit/script_unittype.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_unittype.cpp - The unit-type ccl functions. */
11 //
12 // (c) Copyright 1999-2008 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 #include <string.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "stratagus.h"
39 #include "video.h"
40 #include "map.h"
41 #include "sound.h"
42 #include "unitsound.h"
43 #include "unittype.h"
44 #include "animation.h"
45 #include "icons.h"
46 #include "missile.h"
47 #include "script.h"
48 #include "construct.h"
49 #include "spells.h"
50 #include "font.h"
51 #include "unit.h"
52 #include "unit_manager.h"
53 #include "player.h"
54 #include "luacallback.h"
55
56 /*----------------------------------------------------------------------------
57 -- Defines
58 ----------------------------------------------------------------------------*/
59
60 // FIXME: make configurable
61 #define CHARGE_FOR_NEW_UNITS 33 /// How many percent of max charge for new units
62
63 /*----------------------------------------------------------------------------
64 -- Variables
65 ----------------------------------------------------------------------------*/
66
67 CAnimation *AnimationsArray[ANIMATIONS_MAXANIM];
68 int NumAnimations;
69
70 std::map<std::string, CAnimations *> AnimationMap;/// Animation map
71
72 CUnitTypeVar UnitTypeVar; /// Variables for UnitType and unit.
73
74 struct LabelsStruct {
75 CAnimation *Anim;
76 std::string Name;
77 };
78 static std::vector<LabelsStruct> Labels;
79
80 struct LabelsLaterStruct {
81 CAnimation **Anim;
82 std::string Name;
83 };
84 static std::vector<LabelsLaterStruct> LabelsLater;
85
86 /*----------------------------------------------------------------------------
87 -- Functions
88 ----------------------------------------------------------------------------*/
89
90 extern int GetSpriteIndex(const std::string &spriteName);
91
92 /**
93 ** Parse a "terrain" build restriction.
94 **
95 ** @param l Lua state where the value at the top of the stack
96 ** is the table containing arguments for the "terrain"
97 ** build restriction. On exit, it will be the same.
98 **
99 ** @return The freshly constructed build restriction.
100 ** The caller is responsible of deleting it.
101 **
102 ** If the argument table is invalid, this function indirectly calls
103 ** lua_error and thus longjmp. This function doesn't itself leak
104 ** memory in that case, but its careless callers can do so.
105 */
106 static CBuildRestrictionTerrain *ParseBuildRestrictionTerrain(lua_State *l)
107 {
108 const char *value;
109 unsigned fieldFlags = 0;
110 int min = 0;
111 int max = INT_MAX;
112
113 for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
114 value = LuaToString(l, -2);
115 if (!strcmp(value, "CountLand")) {
116 if (LuaToBoolean(l, -1)) {
117 fieldFlags |= MapFieldLandAllowed;
118 } else {
119 fieldFlags &= ~MapFieldLandAllowed;
120 }
121 } else if (!strcmp(value, "CountCoast")) {
122 if (LuaToBoolean(l, -1)) {
123 fieldFlags |= MapFieldCoastAllowed;
124 } else {
125 fieldFlags &= ~MapFieldCoastAllowed;
126 }
127 } else if (!strcmp(value, "CountShallowWater")) {
128 if (LuaToBoolean(l, -1)) {
129 fieldFlags |= MapFieldShallowWater;
130 } else {
131 fieldFlags &= ~MapFieldShallowWater;
132 }
133 } else if (!strcmp(value, "CountDeepWater")) {
134 if (LuaToBoolean(l, -1)) {
135 fieldFlags |= MapFieldDeepWater;
136 } else {
137 fieldFlags &= ~MapFieldDeepWater;
138 }
139 } else if (!strcmp(value, "Min")) {
140 min = LuaToNumber(l, -1);
141 } else if (!strcmp(value, "Max")) {
142 max = LuaToNumber(l, -1);
143 } else {
144 LuaError(l, "Unsupported BuildingRules terrain tag: %s" _C_ value);
145 }
146 }
147
148 return new CBuildRestrictionTerrain(fieldFlags, min, max);
149 }
150
151 /**
152 ** Parse BuildingRules
153 **
154 ** @param l Lua state.
155 ** @param blist BuildingRestriction to fill in
156 */
157 static void ParseBuildingRules(lua_State *l, std::vector<CBuildRestriction *> &blist)
158 {
159 const char *value;
160 int args;
161 CBuildRestrictionAnd *andlist = new CBuildRestrictionAnd();
162
163 args = lua_objlen(l, -1);
164 Assert(!(args & 1)); // must be even
165
166 for (int i = 0; i < args; ++i) {
167 value = LuaToString(l, -1, i + 1);
168 ++i;
169 lua_rawgeti(l, -1, i + 1);
170 LuaCheckTable(l, -1);
171 if (!strcmp(value, "distance")) {
172 CBuildRestrictionDistance *b = new CBuildRestrictionDistance;
173
174 for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
175 value = LuaToString(l, -2);
176 if (!strcmp(value, "Distance")) {
177 b->Distance = LuaToNumber(l, -1);
178 } else if (!strcmp(value, "DistanceType")) {
179 value = LuaToString(l, -1);
180 if (value[0] == '=') {
181 if ((value[1] == '=' && value[2] == '\0') || (value[1] == '\0')) {
182 b->DistanceType = Equal;
183 }
184 } else if (value[0] == '>') {
185 if (value[1] == '=' && value[2] == '\0') {
186 b->DistanceType = GreaterThanEqual;
187 } else if (value[1] == '\0') {
188 b->DistanceType = GreaterThan;
189 }
190 } else if (value[0] == '<') {
191 if (value[1] == '=' && value[2] == '\0') {
192 b->DistanceType = LessThanEqual;
193 } else if (value[1] == '\0') {
194 b->DistanceType = LessThan;
195 }
196 } else if (value[0] == '!' && value[1] == '=' && value[2] == '\0') {
197 b->DistanceType = NotEqual;
198 }
199 } else if (!strcmp(value, "Type")) {
200 b->RestrictTypeName = LuaToString(l, -1);
201 } else {
202 LuaError(l, "Unsupported BuildingRules distance tag: %s" _C_ value);
203 }
204 }
205 andlist->push_back(b);
206 } else if (!strcmp(value, "addon")) {
207 CBuildRestrictionAddOn *b = new CBuildRestrictionAddOn;
208
209 for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
210 value = LuaToString(l, -2);
211 if (!strcmp(value, "OffsetX")) {
212 b->OffsetX = LuaToNumber(l, -1);
213 } else if (!strcmp(value, "OffsetY")) {
214 b->OffsetY = LuaToNumber(l, -1);
215 } else if (!strcmp(value, "Type")) {
216 b->ParentName = LuaToString(l, -1);
217 } else {
218 LuaError(l, "Unsupported BuildingRules addon tag: %s" _C_ value);
219 }
220 }
221 andlist->push_back(b);
222 } else if (!strcmp(value, "ontop")) {
223 CBuildRestrictionOnTop *b = new CBuildRestrictionOnTop;
224
225 for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
226 value = LuaToString(l, -2);
227 if (!strcmp(value, "Type")) {
228 b->ParentName = LuaToString(l, -1);
229 } else if (!strcmp(value, "ReplaceOnDie")) {
230 b->ReplaceOnDie = LuaToBoolean(l, -1);
231 } else if (!strcmp(value, "ReplaceOnBuild")) {
232 b->ReplaceOnBuild = LuaToBoolean(l, -1);
233 } else {
234 LuaError(l, "Unsupported BuildingRules ontop tag: %s" _C_ value);
235 }
236 }
237 andlist->push_back(b);
238 } else if (!strcmp(value, "terrain")) {
239 andlist->push_back(ParseBuildRestrictionTerrain(l));
240 } else {
241 LuaError(l, "Unsupported BuildingRules tag: %s" _C_ value);
242 }
243 lua_pop(l, 1);
244 }
245 blist.push_back(andlist);
246 }
247
248 /**
249 ** Explicitly allow or disallow the unit type in the specified terrain.
250 ** This overrides the automatic CUnitType::MovementMask computation.
251 ** For use when the author of the Lua script knows best.
252 */
253 static void SetExplicitTerrain(CUnitType *type, unsigned mask, bool allow)
254 {
255 if (allow) {
256 type->ExplicitAllowTerrainMask |= mask;
257 type->ExplicitForbidTerrainMask &= ~mask;
258 } else {
259 type->ExplicitAllowTerrainMask &= ~mask;
260 type->ExplicitForbidTerrainMask |= mask;
261 }
262 }
263
264 /**
265 ** Parse unit-type.
266 **
267 ** @param l Lua state.
268 */
269 static int CclDefineUnitType(lua_State *l)
270 {
271 const char *value;
272 CUnitType *type;
273 const char *str;
274 int i;
275 int subargs;
276 int k;
277
278 LuaCheckArgs(l, 2);
279 LuaCheckTable(l, 2);
280
281 // Slot identifier
282 str = LuaToString(l, 1);
283 type = UnitTypeByIdent(str);
284 if (!type) {
285 type = NewUnitTypeSlot(str);
286 }
287
288 type->NumDirections = 0;
289 type->ExplicitAllowTerrainMask = 0;
290 type->ExplicitForbidTerrainMask = 0;
291
292 //
293 // Parse the list: (still everything could be changed!)
294 //
295 for (lua_pushnil(l); lua_next(l, 2); lua_pop(l, 1)) {
296 value = LuaToString(l, -2);
297 if (!strcmp(value, "Name")) {
298 type->Name = LuaToString(l, -1);
299 } else if (!strcmp(value, "Image")) {
300 LuaCheckTable(l, -1);
301 subargs = lua_objlen(l, -1);
302 for (k = 0; k < subargs; ++k) {
303 value = LuaToString(l, -1, k + 1);
304 ++k;
305
306 if (!strcmp(value, "file")) {
307 type->File = LuaToString(l, -1, k + 1);
308 } else if (!strcmp(value, "size")) {
309 lua_rawgeti(l, -1, k + 1);
310 LuaCheckTable(l, -1);
311 type->Width = LuaToNumber(l, -1, 1);
312 type->Height = LuaToNumber(l, -1, 2);
313 lua_pop(l, 1);
314 } else {
315 LuaError(l, "Unsupported image tag: %s" _C_ value);
316 }
317 }
318 } else if (!strcmp(value, "Shadow")) {
319 LuaCheckTable(l, -1);
320 subargs = lua_objlen(l, -1);
321 for (k = 0; k < subargs; ++k) {
322 value = LuaToString(l, -1, k + 1);
323 ++k;
324
325 if (!strcmp(value, "file")) {
326 type->ShadowFile = LuaToString(l, -1, k + 1);
327 } else if (!strcmp(value, "size")) {
328 lua_rawgeti(l, -1, k + 1);
329 LuaCheckTable(l, -1);
330 type->ShadowWidth = LuaToNumber(l, -1, 1);
331 type->ShadowHeight = LuaToNumber(l, -1, 2);
332 lua_pop(l, 1);
333 } else if (!strcmp(value, "offset")) {
334 lua_rawgeti(l, -1, k + 1);
335 LuaCheckTable(l, -1);
336 type->ShadowOffsetX = LuaToNumber(l, -1, 1);
337 type->ShadowOffsetY = LuaToNumber(l, -1, 2);
338 lua_pop(l, 1);
339 } else {
340 LuaError(l, "Unsupported shadow tag: %s" _C_ value);
341 }
342 }
343 } else if (!strcmp(value, "Offset")) {
344 LuaCheckTableSize(l, -1, 2);
345 type->OffsetX = LuaToNumber(l, -1, 1);
346 type->OffsetY = LuaToNumber(l, -1, 2);
347 } else if (!strcmp(value, "Flip")) {
348 type->Flip = LuaToBoolean(l, -1);
349 } else if (!strcmp(value, "Animations")) {
350 type->Animations = AnimationsByIdent(LuaToString(l, -1));
351 if (!type->Animations) {
352 DebugPrint("Warning animation `%s' not found\n" _C_ LuaToString(l, -1));
353 }
354 } else if (!strcmp(value, "Icon")) {
355 type->Icon.Name = LuaToString(l, -1);
356 type->Icon.Icon = NULL;
357 } else if (!strcmp(value, "EnergyValue")) {
358 if (!lua_isnumber(l, -1)) {
359 LuaError(l, "incorrect argument");
360 }
361 type->SetEnergyValue(LuaToNumber(l, -1));
362 } else if (!strcmp(value, "MagmaValue")) {
363 if (!lua_isnumber(l, -1)) {
364 LuaError(l, "incorrect argument");
365 }
366 type->SetMagmaValue(LuaToNumber(l, -1));
367 } else if (!strcmp(value, "MaxEnergyUtilizationRate")) {
368 if (!lua_isnumber(l, -1)) {
369 LuaError(l, "incorrect argument");
370 }
371 type->SetMaxEnergyUtilizationRate(LuaToNumber(l, -1));
372 } else if (!strcmp(value, "MaxMagmaUtilizationRate")) {
373 if (!lua_isnumber(l, -1)) {
374 LuaError(l, "incorrect argument");
375 }
376 type->SetMaxMagmaUtilizationRate(LuaToNumber(l, -1));
377 } else if (!strcmp(value, "EnergyProductionRate")) {
378 if (!lua_isnumber(l, -1)) {
379 LuaError(l, "incorrect argument");
380 }
381 type->SetEnergyProductionRate(LuaToNumber(l, -1));
382 } else if (!strcmp(value, "MagmaProductionRate")) {
383 if (!lua_isnumber(l, -1)) {
384 LuaError(l, "incorrect argument");
385 }
386 type->SetMagmaProductionRate(LuaToNumber(l, -1));
387 } else if (!strcmp(value, "EnergyStorageCapacity")) {
388 if (!lua_isnumber(l, -1)) {
389 LuaError(l, "incorrect argument");
390 }
391 type->SetEnergyStorageCapacity(LuaToNumber(l, -1));
392 } else if (!strcmp(value, "MagmaStorageCapacity")) {
393 if (!lua_isnumber(l, -1)) {
394 LuaError(l, "incorrect argument");
395 }
396 type->SetMagmaStorageCapacity(LuaToNumber(l, -1));
397 } else if (!strcmp(value, "Construction")) {
398 // FIXME: What if constructions aren't yet loaded?
399 type->Construction = ConstructionByIdent(LuaToString(l, -1));
400 } else if (!strcmp(value, "DrawLevel")) {
401 type->DrawLevel = LuaToNumber(l, -1);
402 } else if (!strcmp(value, "MaxOnBoard")) {
403 type->MaxOnBoard = LuaToNumber(l, -1);
404 } else if (!strcmp(value, "RegenerationRate")) {
405 type->Variable[HP_INDEX].Increase = LuaToNumber(l, -1);
406 } else if (!strcmp(value, "BurnPercent")) {
407 type->BurnPercent = LuaToNumber(l, -1);
408 } else if (!strcmp(value, "BurnDamageRate")) {
409 type->BurnDamageRate = LuaToNumber(l, -1);
410 } else if (!strcmp(value, "MaxCharge")) {
411 type->Variable[CHARGE_INDEX].Max = LuaToNumber(l, -1);
412 type->Variable[CHARGE_INDEX].Value = (type->Variable[CHARGE_INDEX].Max * CHARGE_FOR_NEW_UNITS) / 100;
413 type->Variable[CHARGE_INDEX].Increase = 1;
414 type->Variable[CHARGE_INDEX].Enable = 1;
415 } else if (!strcmp(value, "TileSize")) {
416 LuaCheckTableSize(l, -1, 2);
417 type->TileWidth = LuaToNumber(l, -1, 1);
418 type->TileHeight = LuaToNumber(l, -1, 2);
419 } else if (!strcmp(value, "Decoration")) {
420 type->Decoration = LuaToBoolean(l, -1);
421 } else if (!strcmp(value, "NeutralMinimapColor")) {
422 LuaCheckTableSize(l, -1, 3);
423 type->NeutralMinimapColorRGB.r = LuaToNumber(l, -1, 1);
424 type->NeutralMinimapColorRGB.g = LuaToNumber(l, -1, 2);
425 type->NeutralMinimapColorRGB.b = LuaToNumber(l, -1, 3);
426 } else if (!strcmp(value, "BoxSize")) {
427 LuaCheckTableSize(l, -1, 2);
428 type->BoxWidth = LuaToNumber(l, -1, 1);
429 type->BoxHeight = LuaToNumber(l, -1, 2);
430 } else if (!strcmp(value, "NumDirections")) {
431 type->NumDirections = LuaToNumber(l, -1);
432 } else if (!strcmp(value, "Revealer")) {
433 type->Revealer = LuaToBoolean(l, -1);
434 } else if (!strcmp(value, "ComputerReactionRange")) {
435 type->ReactRangeComputer = LuaToNumber(l, -1);
436 } else if (!strcmp(value, "PersonReactionRange")) {
437 type->ReactRangePerson = LuaToNumber(l, -1);
438 } else if (!strcmp(value, "Missile")) {
439 type->Missile.Name = LuaToString(l, -1);
440 type->Missile.Missile = NULL;
441 } else if (!strcmp(value, "MinAttackRange")) {
442 type->MinAttackRange = LuaToNumber(l, -1);
443 } else if (!strcmp(value, "MaxAttackRange")) {
444 type->Variable[ATTACKRANGE_INDEX].Value = LuaToNumber(l, -1);
445 type->Variable[ATTACKRANGE_INDEX].Max = LuaToNumber(l, -1);
446 } else if (!strcmp(value, "Priority")) {
447 type->Priority = LuaToNumber(l, -1);
448 } else if (!strcmp(value, "AnnoyComputerFactor")) {
449 type->AnnoyComputerFactor = LuaToNumber(l, -1);
450 } else if (!strcmp(value, "DecayRate")) {
451 type->DecayRate = LuaToNumber(l, -1);
452 } else if (!strcmp(value, "Points")) {
453 type->Points = LuaToNumber(l, -1);
454 } else if (!strcmp(value, "Corpse")) {
455 type->CorpseName = LuaToString(l, -1);
456 type->CorpseType = NULL;
457 } else if (!strcmp(value, "ExplodeWhenKilled")) {
458 type->ExplodeWhenKilled = 1;
459 type->Explosion.Name = LuaToString(l, -1);
460 type->Explosion.Missile = NULL;
461 } else if (!strcmp(value, "DeathExplosion")) {
462 type->DeathExplosion = new LuaCallback(l, -1);
463 } else if (!strcmp(value, "Type")) {
464 value = LuaToString(l, -1);
465 if (!strcmp(value, "land")) {
466 type->UnitType = UnitTypeLand;
467 } else if (!strcmp(value, "fly")) {
468 type->UnitType = UnitTypeFly;
469 } else if (!strcmp(value, "naval")) {
470 type->UnitType = UnitTypeNaval;
471 } else {
472 LuaError(l, "Unsupported Type: %s" _C_ value);
473 }
474
475 } else if (!strcmp(value, "AllowTerrainLand")) {
476 SetExplicitTerrain(type, MapFieldLandAllowed,
477 LuaToBoolean(l, -1));
478 } else if (!strcmp(value, "AllowTerrainCoast")) {
479 SetExplicitTerrain(type, MapFieldCoastAllowed,
480 LuaToBoolean(l, -1));
481 } else if (!strcmp(value, "AllowTerrainShallowWater")) {
482 SetExplicitTerrain(type, MapFieldShallowWater,
483 LuaToBoolean(l, -1));
484 } else if (!strcmp(value, "AllowTerrainDeepWater")) {
485 SetExplicitTerrain(type, MapFieldDeepWater,
486 LuaToBoolean(l, -1));
487
488 } else if (!strcmp(value, "RightMouseAction")) {
489 value = LuaToString(l, -1);
490 if (!strcmp(value, "none")) {
491 type->MouseAction = MouseActionNone;
492 } else if (!strcmp(value, "attack")) {
493 type->MouseAction = MouseActionAttack;
494 } else if (!strcmp(value, "move")) {
495 type->MouseAction = MouseActionMove;
496 } else if (!strcmp(value, "harvest")) {
497 type->MouseAction = MouseActionHarvest;
498 } else if (!strcmp(value, "spell-cast")) {
499 type->MouseAction = MouseActionSpellCast;
500 } else {
501 LuaError(l, "Unsupported RightMouseAction: %s" _C_ value);
502 }
503
504 } else if (!strcmp(value, "CanGroundAttack")) {
505 type->GroundAttack = LuaToBoolean(l, -1);
506 } else if (!strcmp(value, "CanAttack")) {
507 type->CanAttack = LuaToBoolean(l, -1);
508 } else if (!strcmp(value, "RepairRange")) {
509 type->RepairRange = LuaToNumber(l, -1);
510 } else if (!strcmp(value, "RepairHp")) {
511 type->RepairHP = LuaToNumber(l, -1);
512 } else if (!strcmp(value, "CanTargetLand")) {
513 if (LuaToBoolean(l, -1)) {
514 type->CanTarget |= CanTargetLand;
515 } else {
516 type->CanTarget &= ~CanTargetLand;
517 }
518 } else if (!strcmp(value, "CanTargetSea")) {
519 if (LuaToBoolean(l, -1)) {
520 type->CanTarget |= CanTargetSea;
521 } else {
522 type->CanTarget &= ~CanTargetSea;
523 }
524 } else if (!strcmp(value, "CanTargetAir")) {
525 if (LuaToBoolean(l, -1)) {
526 type->CanTarget |= CanTargetAir;
527 } else {
528 type->CanTarget &= ~CanTargetAir;
529 }
530
531 } else if (!strcmp(value, "Building")) {
532 type->Building = LuaToBoolean(l, -1);
533 } else if (!strcmp(value, "VisibleUnderFog")) {
534 type->VisibleUnderFog = LuaToBoolean(l, -1);
535 } else if (!strcmp(value, "BuildingRules")) {
536 LuaCheckTable(l, -1);
537 subargs = lua_objlen(l, -1);
538 // Free any old restrictions if they are redefined
539 for (std::vector<CBuildRestriction *>::iterator b = type->BuildingRules.begin();
540 b != type->BuildingRules.end(); ++b) {
541 delete *b;
542 }
543 type->BuildingRules.clear();
544 for (k = 0; k < subargs; ++k) {
545 lua_rawgeti(l, -1, k + 1);
546 LuaCheckTable(l, -1);
547 ParseBuildingRules(l, type->BuildingRules);
548 lua_pop(l, 1);
549 }
550 } else if (!strcmp(value, "ShoreBuilding")) {
551 type->ShoreBuilding = LuaToBoolean(l, -1);
552 } else if (!strcmp(value, "Indestructible")) {
553 type->Indestructible = LuaToNumber(l, -1);
554 } else if (!strcmp(value, "CanTransport")) {
555 type->CanTransport = LuaToBoolean(l, -1);
556 } else if (!strcmp(value, "AttackFromTransporter")) {
557 type->AttackFromTransporter = LuaToBoolean(l, -1);
558 } else if (!strcmp(value, "Coward")) {
559 type->Coward = LuaToBoolean(l, -1);
560 } else if (!strcmp(value, "Harvester")) {
561 type->Harvester = LuaToBoolean(l, -1);
562 } else if (!strcmp(value, "Neutral")) {
563 type->Neutral = LuaToBoolean(l, -1);
564 } else if (!strcmp(value, "CanHarvestFrom")) {
565 type->CanHarvestFrom = LuaToBoolean(l, -1);
566 } else if (!strcmp(value, "Vanishes")) {
567 type->Vanishes = LuaToBoolean(l, -1);
568 } else if (!strcmp(value, "CanCastSpell")) {
569 LuaCheckTable(l, -1);
570 //
571 // Warning: can-cast-spell should only be used AFTER all spells
572 // have been defined. FIXME: MaxSpellType=500 or something?
573 //
574 if (!type->CanCastSpell) {
575 type->CanCastSpell = new char[SpellTypeTable.size()];
576 memset(type->CanCastSpell, 0, SpellTypeTable.size() * sizeof(char));
577 }
578 subargs = lua_objlen(l, -1);
579 if (subargs == 0) {
580 delete[] type->CanCastSpell;
581 type->CanCastSpell = NULL;
582
583 }
584 for (k = 0; k < subargs; ++k) {
585 const SpellType *spell;
586
587 value = LuaToString(l, -1, k + 1);
588 spell = SpellTypeByIdent(value);
589 if (spell == NULL) {
590 LuaError(l, "Unknown spell type: %s" _C_ value);
591 }
592 type->CanCastSpell[spell->Slot] = 1;
593 }
594 } else if (!strcmp(value, "AutoCastActive")) {
595 LuaCheckTable(l, -1);
596 //
597 // Warning: AutoCastActive should only be used AFTER all spells
598 // have been defined.
599 //
600 if (!type->AutoCastActive) {
601 type->AutoCastActive = new char[SpellTypeTable.size()];
602 memset(type->AutoCastActive, 0, SpellTypeTable.size() * sizeof(char));
603 }
604 subargs = lua_objlen(l, -1);
605 if (subargs == 0) {
606 delete[] type->AutoCastActive;
607 type->AutoCastActive = NULL;
608
609 }
610 for (k = 0; k < subargs; ++k) {
611 const SpellType *spell;
612
613 value = LuaToString(l, -1, k + 1);
614 spell = SpellTypeByIdent(value);
615 if (spell == NULL) {
616 LuaError(l, "AutoCastActive : Unknown spell type: %s" _C_ value);
617 }
618 if (!spell->AutoCast) {
619 LuaError(l, "AutoCastActive : Define autocast method for %s." _C_ value);
620 }
621 type->AutoCastActive[spell->Slot] = 1;
622 }
623 } else if (!strcmp(value, "ProductionEfficiency")) {
624 type->ProductionEfficiency = LuaToNumber(l, -1);
625 } else if (!strcmp(value, "IsNotSelectable")) {
626 type->IsNotSelectable = LuaToBoolean(l, -1);
627 } else if (!strcmp(value, "SelectableByRectangle")) {
628 type->SelectableByRectangle = LuaToBoolean(l, -1);
629 } else if (!strcmp(value, "Organic")) {
630 type->Organic = LuaToBoolean(l, -1);
631 } else if (!strcmp(value, "Sounds")) {
632 LuaCheckTable(l, -1);
633 subargs = lua_objlen(l, -1);
634 for (k = 0; k < subargs; ++k) {
635 value = LuaToString(l, -1, k + 1);
636 ++k;
637
638 if (!strcmp(value, "selected")) {
639 type->Sound.Selected.Name = LuaToString(l, -1, k + 1);
640 } else if (!strcmp(value, "acknowledge")) {
641 type->Sound.Acknowledgement.Name = LuaToString(l, -1, k + 1);
642 } else if (!strcmp(value, "ready")) {
643 type->Sound.Ready.Name = LuaToString(l, -1, k + 1);
644 } else if (!strcmp(value, "help")) {
645 type->Sound.Help.Name = LuaToString(l, -1, k + 1);
646 } else if (!strcmp(value, "dead")) {
647 type->Sound.Dead.Name = LuaToString(l, -1, k + 1);
648 } else {
649 LuaError(l, "Unsupported sound tag: %s" _C_ value);
650 }
651 }
652 } else {
653 i = GetVariableIndex(value);
654 if (i != -1) { // valid index
655 if (lua_isboolean(l, -1)) {
656 type->Variable[i].Enable = LuaToBoolean(l, -1);
657 } else if (lua_istable(l, -1)) {
658 DefineVariableField(l, type->Variable + i, -1);
659 } else if (lua_isnumber(l, -1)) {
660 type->Variable[i].Enable = 1;
661 type->Variable[i].Value = LuaToNumber(l, -1);
662 type->Variable[i].Max = LuaToNumber(l, -1);
663 } else { // Error
664 LuaError(l, "incorrect argument for the variable in unittype");
665 }
666 continue;
667 }
668 printf("\n%s\n", type->Name.c_str());
669 LuaError(l, "Unsupported tag: %s" _C_ value);
670 }
671 }
672
673 // If number of directions is not specified, make a guess
674 // Building have 1 direction and units 8
675 if (type->Building && type->NumDirections == 0) {
676 type->NumDirections = 1;
677 } else if (type->NumDirections == 0) {
678 type->NumDirections = 8;
679 }
680
681 // FIXME: try to simplify/combine the flags instead
682 if (type->MouseAction == MouseActionAttack && !type->CanAttack) {
683 LuaError(l, "Unit-type `%s': right-attack is set, but can-attack is not\n" _C_ type->Name.c_str());
684 }
685
686 return 0;
687 }
688
689
690 // ----------------------------------------------------------------------------
691
692 /**
693 ** Access unit-type object
694 **
695 ** @param l Lua state.
696 */
697 CUnitType *CclGetUnitType(lua_State *l)
698 {
699 CUnitType *unittype = NULL;
700 // Be kind allow also strings or symbols
701 if (lua_isstring(l, -1)) {
702 unittype = UnitTypeByIdent(LuaToString(l, -1));
703 } else if (lua_isuserdata(l, -1)) {
704 LuaUserData *data;
705 data = (LuaUserData *)lua_touserdata(l, -1);
706 if (data->Type == LuaUnitType) {
707 unittype = (CUnitType *)data->Data;
708 }
709 }
710 if (!unittype) {
711 LuaError(l, "CclGetUnitType: not a unit-type");
712 }
713 return unittype;
714 }
715
716 // ----------------------------------------------------------------------------
717
718 /**
719 ** Add a label
720 */
721 static void AddLabel(lua_State *l, CAnimation *anim, const std::string &name)
722 {
723 LabelsStruct label;
724 label.Anim = anim;
725 label.Name = name;
726 Labels.push_back(label);
727 }
728
729 /**
730 ** Find a label
731 */
732 static CAnimation *FindLabel(lua_State *l, const std::string &name)
733 {
734 for (int i = 0; i < (int)Labels.size(); ++i) {
735 if (Labels[i].Name == name) {
736 return Labels[i].Anim;
737 }
738 }
739 LuaError(l, "Label not found: %s" _C_ name.c_str());
740 return NULL;
741 }
742
743 /**
744 ** Find a label later
745 */
746 static void FindLabelLater(lua_State *l, CAnimation **anim, const std::string &name)
747 {
748 LabelsLaterStruct label;
749 label.Anim = anim;
750 label.Name = name;
751 LabelsLater.push_back(label);
752 }
753
754 /**
755 ** Fix labels
756 */
757 static void FixLabels(lua_State *l)
758 {
759 for (int i = 0; i < (int)LabelsLater.size(); ++i) {
760 *LabelsLater[i].Anim = FindLabel(l, LabelsLater[i].Name);
761 }
762 }
763
764 /**
765 ** Parse an animation frame
766 */
767 static void ParseAnimationFrame(lua_State *l, const char *str,
768 CAnimation *anim)
769 {
770 char *op1;
771 char *op2;
772
773 op1 = new_strdup(str);
774 op2 = strchr(op1, ' ');
775 if (op2) {
776 while (*op2 == ' ') {
777 *op2++ = '\0';
778 }
779 }
780
781 if (!strcmp(op1, "frame")) {
782 anim->Type = AnimationFrame;
783 anim->D.Frame.Frame = atoi(op2);
784 } else if (!strcmp(op1, "exact-frame")) {
785 anim->Type = AnimationExactFrame;
786 anim->D.Frame.Frame = atoi(op2);
787 } else if (!strcmp(op1, "random-frame")) {
788 anim->Type = AnimationRandomFrame;
789 anim->D.RandomFrame.MinFrame = atoi(op2);
790 op2 = strchr(op2, ' ');
791 while (*op2 == ' ') {
792 ++op2;
793 }
794 anim->D.RandomFrame.MaxFrame = atoi(op2);
795 } else if (!strcmp(op1, "wait")) {
796 anim->Type = AnimationWait;
797 anim->D.Wait.Wait = atoi(op2);
798 } else if (!strcmp(op1, "random-wait")) {
799 anim->Type = AnimationRandomWait;
800 anim->D.RandomWait.MinWait = atoi(op2);
801 op2 = strchr(op2, ' ');
802 while (*op2 == ' ') {
803 ++op2;
804 }
805 anim->D.RandomWait.MaxWait = atoi(op2);
806 } else if (!strcmp(op1, "sound")) {
807 anim->Type = AnimationSound;
808 anim->D.Sound.Name = new_strdup(op2);
809 } else if (!strcmp(op1, "random-sound")) {
810 int count;
811 char *next;
812
813 anim->Type = AnimationRandomSound;
814 count = 0;
815 while (op2 && *op2) {
816 next = strchr(op2, ' ');
817 if (next) {
818 while (*next == ' ') {
819 *next++ = '\0';
820 }
821 }
822 ++count;
823 char **newname = new char *[count];
824 if (anim->D.RandomSound.Name) {
825 memcpy(newname, anim->D.RandomSound.Name, (count - 1) * sizeof(char *));
826 delete[] anim->D.RandomSound.Name;
827 }
828 anim->D.RandomSound.Name = newname;
829 anim->D.RandomSound.Name[count - 1] = new_strdup(op2);
830 op2 = next;
831 }
832 anim->D.RandomSound.NumSounds = count;
833 anim->D.RandomSound.Sound = new CSound *[count];
834 } else if (!strcmp(op1, "attack")) {
835 anim->Type = AnimationAttack;
836 } else if (!strcmp(op1, "rotate")) {
837 anim->Type = AnimationRotate;
838 anim->D.Rotate.Rotate = atoi(op2);
839 } else if (!strcmp(op1, "random-rotate")) {
840 anim->Type = AnimationRandomRotate;
841 anim->D.Rotate.Rotate = atoi(op2);
842 } else if (!strcmp(op1, "move")) {
843 anim->Type = AnimationMove;
844 anim->D.Move.Move = atoi(op2);
845 } else if (!strcmp(op1, "unbreakable")) {
846 anim->Type = AnimationUnbreakable;
847 if (!strcmp(op2, "begin")) {
848 anim->D.Unbreakable.Begin = 1;
849 } else if (!strcmp(op2, "end")) {
850 anim->D.Unbreakable.Begin = 0;
851 } else {
852 LuaError(l, "Unbreakable must be 'begin' or 'end'. Found: %s" _C_ op2);
853 }
854 } else if (!strcmp(op1, "label")) {
855 anim->Type = AnimationLabel;
856 AddLabel(l, anim, op2);
857 } else if (!strcmp(op1, "goto")) {
858 anim->Type = AnimationGoto;
859 FindLabelLater(l, &anim->D.Goto.Goto, op2);
860 } else if (!strcmp(op1, "random-goto")) {
861 char *label;
862
863 anim->Type = AnimationRandomGoto;
864 label = strchr(op2, ' ');
865 if (!label) {
866 LuaError(l, "Missing random-goto label");
867 } else {
868 while (*label == ' ') {
869 *label++ = '\0';
870 }
871 }
872 anim->D.RandomGoto.Random = atoi(op2);
873 FindLabelLater(l, &anim->D.RandomGoto.Goto, label);
874 } else {
875 LuaError(l, "Unknown animation: %s" _C_ op1);
876 }
877
878 delete[] op1;
879 }
880
881 /**
882 ** Parse an animation
883 */
884 static CAnimation *ParseAnimation(lua_State *l, int idx)
885 {
886 CAnimation *anim;
887 CAnimation *tail;
888 int args;
889 int j;
890 const char *str;
891
892 LuaCheckTable(l, idx);
893 args = lua_objlen(l, idx);
894 anim = new CAnimation[args + 1];
895 tail = NULL;
896 Labels.clear();
897 LabelsLater.clear();
898
899 for (j = 0; j < args; ++j) {
900 str = LuaToString(l, idx, j + 1);
901 ParseAnimationFrame(l, str, &anim[j]);
902 if (!tail) {
903 tail = &anim[j];
904 } else {
905 tail->Next = &anim[j];
906 tail = &anim[j];
907 }
908 }
909 FixLabels(l);
910
911 return anim;
912 }
913
914 /**
915 ** Add animation to AnimationsArray
916 */
917 static void AddAnimationToArray(CAnimation *anim)
918 {
919 if (!anim) {
920 return;
921 }
922
923 AnimationsArray[NumAnimations++] = anim;
924 Assert(NumAnimations != ANIMATIONS_MAXANIM);
925 }
926
927 /**
928 ** Define a unit-type animation set.
929 **
930 ** @param l Lua state.
931 */
932 static int CclDefineAnimations(lua_State *l)
933 {
934 const char *name;
935 const char *value;
936 CAnimations *anims;
937
938 LuaCheckArgs(l, 2);
939 LuaCheckTable(l, 2);
940
941 name = LuaToString(l, 1);
942 anims = AnimationsByIdent(name);
943 if (!anims) {
944 anims = new CAnimations;
945 AnimationMap[name] = anims;
946 }
947
948 lua_pushnil(l);
949 while (lua_next(l, 2)) {
950 value = LuaToString(l, -2);
951
952 if (!strcmp(value, "Start")) {
953 anims->Start = ParseAnimation(l, -1);
954 } else if (!strcmp(value, "Still")) {
955 anims->Still = ParseAnimation(l, -1);
956 } else if (!strcmp(value, "Death")) {
957 anims->Death = ParseAnimation(l, -1);
958 } else if (!strcmp(value, "Attack")) {
959 anims->Attack = ParseAnimation(l, -1);
960 } else if (!strcmp(value, "Move")) {
961 anims->Move = ParseAnimation(l, -1);
962 } else if (!strcmp(value, "Repair")) {
963 anims->Repair = ParseAnimation(l, -1);
964 } else if (!strcmp(value, "Train")) {
965 anims->Train = ParseAnimation(l, -1);
966 } else if (!strcmp(value, "Harvest")) {
967 anims->Harvest = ParseAnimation(l, -1);
968 } else {
969 LuaError(l, "Unsupported animation: %s" _C_ value);
970 }
971 lua_pop(l, 1);
972 }
973
974 // Must add to array in a fixed order for save games
975 AddAnimationToArray(anims->Start);
976 AddAnimationToArray(anims->Still);
977 AddAnimationToArray(anims->Death);
978 AddAnimationToArray(anims->Attack);
979 AddAnimationToArray(anims->Move);
980 AddAnimationToArray(anims->Repair);
981 AddAnimationToArray(anims->Train);
982 AddAnimationToArray(anims->Harvest);
983
984 return 0;
985 }
986
987 /**
988 ** Define the field of the UserDefined variables.
989 **
990 ** @param l Lua state.
991 ** @param var Variable to set.
992 ** @param lua_index Index of the table where are the infos
993 **
994 ** @internal Use to not duplicate code.
995 */
996 void DefineVariableField(lua_State *l, CVariable *var, int lua_index)
997 {
998 if (lua_index < 0) { // relative index
999 --lua_index;
1000 }
1001 lua_pushnil(l);
1002 while (lua_next(l, lua_index)) {
1003 const char *key;
1004
1005 key = LuaToString(l, -2);
1006 if (!strcmp(key, "Value")) {
1007 var->Value = LuaToNumber(l, -1);
1008 } else if (!strcmp(key, "Max")) {
1009 var->Max = LuaToNumber(l, -1);
1010 } else if (!strcmp(key, "Increase")) {
1011 var->Increase = LuaToNumber(l, -1);
1012 } else if (!strcmp(key, "Enable")) {
1013 var->Enable = LuaToBoolean(l, -1);
1014 } else { // Error.
1015 LuaError(l, "incorrect field '%s' for variable\n" _C_ key);
1016 }
1017 lua_pop(l, 1); // pop the value;
1018 }
1019 }
1020
1021 /**
1022 ** Return the index of the variable.
1023 **
1024 ** @param varname Name of the variable.
1025 **
1026 ** @return Index of the variable, -1 if not found.
1027 */
1028 int GetVariableIndex(const char *varname)
1029 {
1030 for (int i = 0; i < UnitTypeVar.NumberVariable; ++i) {
1031 if (!strcmp(varname, UnitTypeVar.VariableName[i])) {
1032 return i;
1033 }
1034 }
1035 return -1;
1036 }
1037
1038 /**
1039 ** Define user variables.
1040 **
1041 ** @param l Lua state.
1042 */
1043 static int CclDefineVariables(lua_State *l)
1044 {
1045 const char *str;
1046 int i;
1047 int j;
1048 int args;
1049
1050 args = lua_gettop(l);
1051 for (j = 0; j < args; ++j) {
1052 str = LuaToString(l, j + 1);
1053 i = GetVariableIndex(str);
1054 if (i == -1) { // new variable.
1055 i = UnitTypeVar.NumberVariable;
1056 char **v = new char *[i + 1];
1057 memcpy(v, UnitTypeVar.VariableName, i * sizeof(char *));
1058 delete[] UnitTypeVar.VariableName;
1059 UnitTypeVar.VariableName = v;
1060 UnitTypeVar.VariableName[i] = new_strdup(str);
1061
1062 CVariable *t = new CVariable[i + 1];
1063 for (int x = 0; x < i; ++x) {
1064 t[x] = UnitTypeVar.Variable[x];
1065 }
1066 delete[] UnitTypeVar.Variable;
1067 UnitTypeVar.Variable = t;
1068 UnitTypeVar.NumberVariable++;
1069 } else {
1070 DebugPrint("Warning, User Variable \"%s\" redefined\n" _C_ str);
1071 }
1072 if (!lua_istable(l, j + 2)) { // No change => default value.
1073 continue;
1074 }
1075 ++j;
1076 DefineVariableField(l, UnitTypeVar.Variable + i, j + 1);
1077 }
1078 return 0;
1079 }
1080
1081 /**
1082 ** Define Decorations for user variables
1083 **
1084 ** @param l Lua state.
1085 **
1086 ** @todo modify Assert with luastate with User Error.
1087 ** @todo continue to add configuration.
1088 */
1089 static int CclDefineDecorations(lua_State *l)
1090 {
1091 int i; // iterator for arguments.
1092 int nargs; // number of arguments.
1093 const char *key; // key of lua table.
1094 CDecoVar *decovar; // variable for transit.
1095 struct {
1096 int Index;
1097 int OffsetX;
1098 int OffsetY;
1099 int OffsetXPercent;
1100 int OffsetYPercent;
1101 bool IsCenteredInX;
1102 bool IsCenteredInY;
1103 bool ShowIfNotEnable;
1104 bool ShowWhenMax;
1105 bool ShowOnlySelected;
1106 bool HideNeutral;
1107 bool HideAllied;
1108 bool ShowOpponent;
1109 } tmp;
1110
1111 nargs = lua_gettop(l);
1112 for (i = 0; i < nargs; i++) {
1113 Assert(lua_istable(l, i + 1));
1114 decovar = NULL;
1115 memset(&tmp, 0, sizeof(tmp));
1116 lua_pushnil(l);
1117 while (lua_next(l, i + 1)) {
1118 key = LuaToString(l, -2);
1119 if (!strcmp(key, "Index")) {
1120 tmp.Index = GetVariableIndex(LuaToString(l, -1));
1121 Assert(tmp.Index != -1);
1122 } else if (!strcmp(key, "Offset")) {
1123 LuaCheckTableSize(l, -1, 2);
1124 tmp.OffsetX = LuaToNumber(l, -1, 1);
1125 tmp.OffsetY = LuaToNumber(l, -1, 2);
1126 } else if (!strcmp(key, "OffsetPercent")) {
1127 LuaCheckTableSize(l, -1, 2);
1128 tmp.OffsetXPercent = LuaToNumber(l, -1, 1);
1129 tmp.OffsetYPercent = LuaToNumber(l, -1, 2);
1130 } else if (!strcmp(key, "CenterX")) {
1131 tmp.IsCenteredInX = LuaToBoolean(l, -1);
1132 } else if (!strcmp(key, "CenterY")) {
1133 tmp.IsCenteredInY = LuaToBoolean(l, -1);
1134 } else if (!strcmp(key, "ShowIfNotEnable")) {
1135 tmp.ShowIfNotEnable = LuaToBoolean(l, -1);
1136 } else if (!strcmp(key, "ShowWhenMax")) {
1137 tmp.ShowWhenMax = LuaToBoolean(l, -1);
1138 } else if (!strcmp(key, "ShowOnlySelected")) {
1139 tmp.ShowOnlySelected = LuaToBoolean(l, -1);
1140 } else if (!strcmp(key, "HideNeutral")) {
1141 tmp.HideNeutral = LuaToBoolean(l, -1);
1142 } else if (!strcmp(key, "HideAllied")) {
1143 tmp.HideAllied = LuaToBoolean(l, -1);
1144 } else if (!strcmp(key, "ShowOpponent")) {
1145 tmp.ShowOpponent = LuaToBoolean(l, -1);
1146 } else if (!strcmp(key, "Method")) {
1147 Assert(lua_istable(l, -1));
1148 lua_rawgeti(l, -1, 1); // MethodName
1149 lua_rawgeti(l, -2, 2); // Data
1150 LuaCheckTable(l, -1);
1151 key = LuaToString(l, -2);
1152 if (!strcmp(key, "sprite")) {
1153 CDecoVarSpriteBar *decovarspritebar = new CDecoVarSpriteBar;
1154 lua_rawgeti(l, -1, 1);
1155 decovarspritebar->SpriteIndex = GetSpriteIndex(LuaToString(l, -1));
1156 if (decovarspritebar->SpriteIndex == -1) {
1157 LuaError(l, "invalid sprite-name '%s' for Method in DefineDecorations" _C_
1158 LuaToString(l, -1));
1159 }
1160 lua_pop(l, 1);
1161 // FIXME : More arguments ?
1162 decovar = decovarspritebar;
1163 } else { // Error
1164 LuaError(l, "invalid method '%s' for Method in DefineDecorations" _C_ key);
1165 }
1166 lua_pop(l, 2); // MethodName and data
1167 } else { // Error
1168 LuaError(l, "invalid key '%s' for DefineDecorations" _C_ key);
1169 }
1170 lua_pop(l, 1); // Pop the value
1171 }
1172 decovar->Index = tmp.Index;
1173 decovar->OffsetX = tmp.OffsetX;
1174 decovar->OffsetY = tmp.OffsetY;
1175 decovar->OffsetXPercent = tmp.OffsetXPercent;
1176 decovar->OffsetYPercent = tmp.OffsetYPercent;
1177 decovar->IsCenteredInX = tmp.IsCenteredInX;
1178 decovar->IsCenteredInY = tmp.IsCenteredInY;
1179 decovar->ShowIfNotEnable = tmp.ShowIfNotEnable;
1180 decovar->ShowWhenMax = tmp.ShowWhenMax;
1181 decovar->ShowOnlySelected = tmp.ShowOnlySelected;
1182 decovar->HideNeutral = tmp.HideNeutral;
1183 decovar->HideAllied = tmp.HideAllied;
1184 decovar->ShowOpponent = tmp.ShowOpponent;
1185 UnitTypeVar.DecoVar.push_back(decovar);
1186 }
1187 Assert(lua_gettop(l));
1188 return 0;
1189 }
1190
1191
1192 // ----------------------------------------------------------------------------
1193
1194
1195 /**
1196 ** Update unit variables which are not user defined.
1197 */
1198 void UpdateUnitVariables(const CUnit *unit)
1199 {
1200 const CUnitType *type; // unit->Type.
1201
1202 type = unit->Type;
1203 for (int i = 0; i < NVARALREADYDEFINED; i++) { // default values
1204 if (i == ARMOR_INDEX || i == PIERCINGDAMAGE_INDEX ||
1205 i == BASICDAMAGE_INDEX || i == CHARGE_INDEX ||
1206 i == KILL_INDEX || i == HP_INDEX) {
1207 continue;
1208 }
1209 unit->Variable[i].Value = 0;
1210 unit->Variable[i].Max = 0;
1211 unit->Variable[i].Enable = 1;
1212 }
1213
1214 // Build
1215 if (unit->Orders[0]->Action == UnitActionBuilt) {
1216 const int *pcosts = type->ProductionCosts;
1217 int pcost = CYCLES_PER_SECOND * (pcosts[EnergyCost] ? pcosts[EnergyCost] : pcosts[MagmaCost]);
1218 unit->Variable[BUILD_INDEX].Value = unit->Data.Built.Progress;
1219 unit->Variable[BUILD_INDEX].Max = pcost;
1220
1221 // This should happen when building unit with several peons
1222 // Maybe also with only one.
1223 // FIXME : Should be better to fix it in action_{build,repair}.c ?
1224 if (unit->Variable[BUILD_INDEX].Value > unit->Variable[BUILD_INDEX].Max) {
1225 // assume value is wrong.
1226 unit->Variable[BUILD_INDEX].Value = unit->Variable[BUILD_INDEX].Max;
1227 }
1228 }
1229
1230 // Transport
1231 unit->Variable[TRANSPORT_INDEX].Value = unit->BoardCount;
1232 unit->Variable[TRANSPORT_INDEX].Max = unit->Type->MaxOnBoard;
1233
1234 // Training
1235 if (unit->Orders[0]->Action == UnitActionTrain) {
1236 unit->Variable[TRAINING_INDEX].Value = unit->Data.Train.Ticks;
1237 int *costs = unit->Orders[0]->Type->ProductionCosts;
1238 int cost = costs[EnergyCost] ? costs[EnergyCost] : costs[MagmaCost];
1239 unit->Variable[TRAINING_INDEX].Max = cost;
1240 }
1241
1242 // Resources.
1243 if (unit->Type->CanHarvestFrom) {
1244 int i;
1245 for (i = 0; i < MaxCosts; ++i) {
1246 if (unit->ResourcesHeld[i] != 0) {
1247 break;
1248 }
1249 }
1250 Assert(i != MaxCosts);
1251 unit->Variable[GIVERESOURCE_INDEX].Value = unit->ResourcesHeld[i] / CYCLES_PER_SECOND;
1252 unit->Variable[GIVERESOURCE_INDEX].Max = 0x7FFFFFFF;
1253 }
1254
1255 // SightRange
1256 unit->Variable[SIGHTRANGE_INDEX].Value = type->Variable[SIGHTRANGE_INDEX].Value;
1257 unit->Variable[SIGHTRANGE_INDEX].Max = unit->Stats->Variables[SIGHTRANGE_INDEX].Max;
1258
1259 // AttackRange
1260 unit->Variable[ATTACKRANGE_INDEX].Value = type->Variable[ATTACKRANGE_INDEX].Max;
1261 unit->Variable[ATTACKRANGE_INDEX].Max = unit->Stats->Variables[ATTACKRANGE_INDEX].Max;
1262
1263 // Position
1264 unit->Variable[POSX_INDEX].Value = unit->X;
1265 unit->Variable[POSX_INDEX].Max = Map.Info.MapWidth;
1266 unit->Variable[POSY_INDEX].Value = unit->Y;
1267 unit->Variable[POSY_INDEX].Max = Map.Info.MapHeight;
1268
1269 // RadarRange
1270 unit->Variable[RADAR_INDEX].Value = unit->Stats->Variables[RADAR_INDEX].Value;
1271 unit->Variable[RADAR_INDEX].Max = unit->Stats->Variables[RADAR_INDEX].Value;
1272
1273 // RadarJammerRange
1274 unit->Variable[RADARJAMMER_INDEX].Value = unit->Stats->Variables[RADARJAMMER_INDEX].Value;
1275 unit->Variable[RADARJAMMER_INDEX].Max = unit->Stats->Variables[RADARJAMMER_INDEX].Value;
1276
1277 // SlotNumber
1278 unit->Variable[SLOT_INDEX].Value = unit->Slot;
1279 unit->Variable[SLOT_INDEX].Max = UnitSlotFree - 1;
1280
1281 for (int i = 0; i < NVARALREADYDEFINED; i++) { // default values
1282 unit->Variable[i].Enable &= unit->Variable[i].Max > 0;
1283 #ifdef DEBUG
1284 if (unit->Variable[i].Value > unit->Variable[i].Max) {
1285 DebugPrint("Value out of range: '%s'(%d), for variable '%s',"
1286 " value = %d, max = %d\n"
1287 _C_ type->Ident.c_str() _C_ unit->Slot _C_ UnitTypeVar.VariableName[i]
1288 _C_ unit->Variable[i].Value _C_ unit->Variable[i].Max);
1289 }
1290 #endif
1291 Assert(unit->Variable[i].Value <= unit->Variable[i].Max);
1292 }
1293 }
1294
1295 /**
1296 ** Define already variables, useful for drawing now.
1297 */
1298 void InitDefinedVariables()
1299 {
1300 const char *var[NVARALREADYDEFINED] = {"HitPoints", "Build", "Charge", "Transport",
1301 "Training", "GiveResource", "Kill", "Armor", "SightRange",
1302 "AttackRange", "PiercingDamage", "BasicDamage", "PosX", "PosY", "RadarRange",
1303 "RadarJammerRange", "AutoRepairRange", "Slot"};
1304 int i;
1305
1306 // Variables.
1307 UnitTypeVar.VariableName = new char *[NVARALREADYDEFINED];
1308 for (i = 0; i < NVARALREADYDEFINED; ++i) {
1309 UnitTypeVar.VariableName[i] = new_strdup(var[i]);
1310 }
1311 UnitTypeVar.Variable = new CVariable[i];
1312 UnitTypeVar.NumberVariable = i;
1313 }
1314
1315 /**
1316 ** Register CCL features for unit-type.
1317 */
1318 void UnitTypeCclRegister(void)
1319 {
1320 lua_register(Lua, "DefineUnitType", CclDefineUnitType);
1321 lua_register(Lua, "DefineVariables", CclDefineVariables);
1322 lua_register(Lua, "DefineDecorations", CclDefineDecorations);
1323
1324 InitDefinedVariables();
1325
1326 lua_register(Lua, "DefineAnimations", CclDefineAnimations);
1327 }
1328
1329 //@}