Debian Codegraph @

engine/game/spells.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 spells.cpp - The spell cast action. */
11 //
12 // (c) Copyright 1998-2016 by Vladi Belperchinov-Shabanski, Lutz Sammer,
13 // Jimmy Salmon, and Joris DAUPHIN
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29
30 /*
31 ** And when we cast our final spell
32 ** And we meet in our dreams
33 ** A place that no one else can go
34 ** Don't ever let your love die
35 ** Don't ever go breaking this spell
36 */
37
38 //@{
39
40 /*----------------------------------------------------------------------------
41 -- Notes
42 ----------------------------------------------------------------------------*/
43
44 /*----------------------------------------------------------------------------
45 -- Includes
46 ----------------------------------------------------------------------------*/
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include "stratagus.h"
53
54 #include "unittype.h"
55 #include "unit_cache.h"
56 #include "spells.h"
57 #include "sound.h"
58 #include "missile.h"
59 #include "map.h"
60 #include "ui.h"
61 #include "actions.h"
62
63 /*----------------------------------------------------------------------------
64 -- Variables
65 ----------------------------------------------------------------------------*/
66
67 /**
68 ** Define the names and effects of all im play available spells.
69 */
70 std::vector<SpellType*> SpellTypeTable;
71
72
73 /*----------------------------------------------------------------------------
74 -- Functions
75 ----------------------------------------------------------------------------*/
76
77 // ****************************************************************************
78 // Cast the Spell
79 // ****************************************************************************
80
81 /**
82 ** Cast demolish
83 ** @param caster Unit that casts the spell
84 ** @param spell Spell-type pointer
85 ** @param target Target unit that spell is addressed to
86 ** @param x X coord of target spot when/if target does not exist
87 ** @param y Y coord of target spot when/if target does not exist
88 **
89 ** @return =!0 if spell should be repeated, 0 if not
90 */
91 int Demolish::Cast(CUnit *caster, const SpellType *spell,
92 CUnit *target, int x, int y)
93 {
94 int xmin;
95 int ymin;
96 int xmax;
97 int ymax;
98 int i;
99 int n;
100 CUnit *table[UnitMax];
101
102 //
103 // Allow error margins. (Lame, I know)
104 //
105 xmin = x - this->Range - 2;
106 ymin = y - this->Range - 2;
107 xmax = x + this->Range + 2;
108 ymax = y + this->Range + 2;
109 if (xmin < 0) {
110 xmin = 0;
111 }
112 if (xmax > Map.Info.MapWidth - 1) {
113 xmax = Map.Info.MapWidth - 1;
114 }
115 if (ymin < 0) {
116 ymin = 0;
117 }
118 if (ymax > Map.Info.MapHeight - 1) {
119 ymax = Map.Info.MapHeight - 1;
120 }
121
122 //
123 // Effect of the explosion on units. Don't bother if damage is 0
124 //
125 if (this->Damage) {
126 n = UnitCache.Select(xmin, ymin, xmax + 1, ymax + 1, table, UnitMax);
127 for (i = 0; i < n; ++i) {
128 if (table[i]->Type->UnitType != UnitTypeFly && table[i]->Orders[0]->Action != UnitActionDie &&
129 MapDistanceToUnit(x, y, table[i]) <= this->Range) {
130 // Don't hit flying units!
131 HitUnit(caster, table[i], this->Damage);
132 }
133 }
134 }
135
136 return 1;
137 }
138
139 /**
140 ** Cast circle of power.
141 **
142 ** @param caster Unit that casts the spell
143 ** @param spell Spell-type pointer
144 ** @param target Target unit that spell is addressed to
145 ** @param x X coord of target spot when/if target does not exist
146 ** @param y Y coord of target spot when/if target does not exist
147 **
148 ** @return =!0 if spell should be repeated, 0 if not
149 */
150 int SpawnPortal::Cast(CUnit *caster, const SpellType *spell,
151 CUnit *target, int x, int y)
152 {
153 // FIXME: vladi: cop should be placed only on explored land
154 CUnit *portal;
155 CUnitType *ptype;
156
157 ptype = this->PortalType;
158
159 DebugPrint("Spawning a portal exit.\n");
160 portal = caster->Goal;
161 if (portal) {
162 portal->MoveToXY(x, y);
163 } else {
164 portal = MakeUnitAndPlace(x, y, ptype, &Players[PlayerNumNeutral]);
165 }
166 // Goal is used to link to destination circle of power
167 caster->Goal = portal;
168 portal->RefsIncrease();
169 //FIXME: setting destination circle of power should use charge
170 return 0;
171 }
172
173 /**
174 ** Cast Area Adjust Vitals on all valid units in range.
175 **
176 ** @param caster Unit that casts the spell
177 ** @param spell Spell-type pointer
178 ** @param target Target unit that spell is addressed to
179 ** @param x X coord of target spot when/if target does not exist
180 ** @param y Y coord of target spot when/if target does not exist
181 **
182 ** @return =!0 if spell should be repeated, 0 if not
183 */
184 int AreaAdjustVitals::Cast(CUnit *caster, const SpellType *spell,
185 CUnit *target, int x, int y)
186 {
187 CUnit *units[UnitMax];
188 int nunits;
189 int j;
190 int hp;
191
192 // Get all the units around the unit
193 nunits = UnitCache.Select(x - spell->Range,
194 y - spell->Range,
195 x + spell->Range + caster->Type->Width,
196 y + spell->Range + caster->Type->Height,
197 units, UnitMax);
198 hp = this->HP;
199 caster->Variable[CHARGE_INDEX].Value -= spell->ChargeCost;
200 for (j = 0; j < nunits; ++j) {
201 target = units[j];
202 // if (!PassCondition(caster, spell, target, x, y) {
203 if (!CanCastSpell(caster, spell, target, x, y)) {
204 continue;
205 }
206 if (hp < 0) {
207 HitUnit(caster, target, -hp);
208 } else {
209 target->Variable[HP_INDEX].Value += hp;
210 if (target->Variable[HP_INDEX].Value > target->Variable[HP_INDEX].Max) {
211 target->Variable[HP_INDEX].Value = target->Variable[HP_INDEX].Max;
212 }
213 }
214 target->Variable[CHARGE_INDEX].Value += Charge;
215 if (target->Variable[CHARGE_INDEX].Value < 0) {
216 target->Variable[CHARGE_INDEX].Value = 0;
217 }
218 if (target->Variable[CHARGE_INDEX].Value > target->Variable[CHARGE_INDEX].Max) {
219 target->Variable[CHARGE_INDEX].Value = target->Variable[CHARGE_INDEX].Max;
220 }
221 }
222 return 0;
223 }
224
225 /**
226 ** Cast area bombardment.
227 **
228 ** @param caster Unit that casts the spell
229 ** @param spell Spell-type pointer
230 ** @param target Target unit that spell is addressed to
231 ** @param x X coord of target spot when/if target does not exist
232 ** @param y Y coord of target spot when/if target does not exist
233 **
234 ** @return =!0 if spell should be repeated, 0 if not
235 ** @internal: vladi: blizzard differs than original in this way:
236 ** original: launches 50 shards at 5 random spots x 10 for 25 charge.
237 */
238 int AreaBombardment::Cast(CUnit *caster, const SpellType *spell,
239 CUnit *target, int x, int y)
240 {
241 int fields;
242 int shards;
243 int damage;
244 ::Missile *mis;
245 int offsetx;
246 int offsety;
247 int dx;
248 int dy;
249 int i;
250 MissileType *missile;
251
252 mis = NULL;
253
254 fields = this->Fields;
255 shards = this->Shards;
256 damage = this->Damage;
257 offsetx = this->StartOffsetX;
258 offsety = this->StartOffsetY;
259 missile = this->Missile;
260 while (fields--) {
261 // FIXME: radius configurable...
262 do {
263 // find new destination in the map
264 dx = x + SyncRand() % 5 - 2;
265 dy = y + SyncRand() % 5 - 2;
266 } while (dx < 0 && dy < 0 && dx >= Map.Info.MapWidth && dy >= Map.Info.MapHeight);
267 for (i = 0; i < shards; ++i) {
268 mis = MakeMissile(missile,
269 dx * TileSizeX + TileSizeX / 2 + offsetx,
270 dy * TileSizeY + TileSizeY / 2 + offsety,
271 dx * TileSizeX + TileSizeX / 2,
272 dy * TileSizeY + TileSizeY / 2);
273 // FIXME: This is just patched up, it works, but I have no idea why.
274 // FIXME: What is the reasoning behind all this?
275 if (mis->Type->Speed) {
276 mis->Delay = i * mis->Type->Sleep * 2 * TileSizeX / mis->Type->Speed;
277 } else {
278 mis->Delay = i * mis->Type->Sleep * mis->Type->G->NumFrames;
279 }
280 mis->Damage = damage;
281 // FIXME: not correct -- blizzard should continue even if mage is
282 // destroyed (though it will be quite short time...)
283 mis->SourceUnit = caster;
284 caster->RefsIncrease();
285 }
286 }
287 return 1;
288 }
289
290 /**
291 ** Evaluate missile location description.
292 **
293 ** @param location Parameters for location.
294 ** @param caster Unit that casts the spell
295 ** @param target Target unit that spell is addressed to
296 ** @param x X coord of target spot when/if target does not exist
297 ** @param y Y coord of target spot when/if target does not exist
298 ** @param resx pointer to X coord of the result
299 ** @param resy pointer to Y coord of the result
300 */
301 static void EvaluateMissileLocation(const SpellActionMissileLocation *location,
302 CUnit *caster, CUnit *target, int x, int y, int *resx, int *resy)
303 {
304 if (location->Base == LocBaseCaster) {
305 *resx = caster->X * TileSizeX + TileSizeX / 2;
306 *resy = caster->Y * TileSizeY + TileSizeY / 2;
307 } else {
308 if (target) {
309 *resx = target->X * TileSizeX + TileSizeX / 2;
310 *resy = target->Y * TileSizeY + TileSizeY / 2;
311 } else {
312 *resx = x * TileSizeX + TileSizeX / 2;
313 *resy = y * TileSizeY + TileSizeY / 2;
314 }
315 }
316 *resx += location->AddX;
317 if (location->AddRandX) {
318 *resx += SyncRand() % location->AddRandX;
319 }
320 *resy += location->AddY;
321 if (location->AddRandY) {
322 *resy += SyncRand() % location->AddRandY;
323 }
324 }
325
326 /**
327 ** Cast spawn missile.
328 **
329 ** @param caster Unit that casts the spell
330 ** @param spell Spell-type pointer
331 ** @param target Target unit that spell is addressed to
332 ** @param x X coord of target spot when/if target does not exist
333 ** @param y Y coord of target spot when/if target does not exist
334 **
335 ** @return =!0 if spell should be repeated, 0 if not
336 */
337 int SpawnMissile::Cast(CUnit *caster, const SpellType *spell,
338 CUnit *target, int x, int y)
339 {
340 ::Missile *missile;
341 int sx;
342 int sy;
343 int dx;
344 int dy;
345
346 EvaluateMissileLocation(&this->StartPoint,
347 caster, target, x, y, &sx, &sy);
348 EvaluateMissileLocation(&this->EndPoint,
349 caster, target, x, y, &dx, &dy);
350
351 missile = MakeMissile(this->Missile, sx, sy, dx, dy);
352 missile->TTL = this->TTL;
353 missile->Delay = this->Delay;
354 missile->Damage = this->Damage;
355 if (missile->Damage != 0) {
356 missile->SourceUnit = caster;
357 caster->RefsIncrease();
358 }
359 if ((missile->TargetUnit = target)) {
360 target->RefsIncrease();
361 }
362 return 1;
363 }
364
365 /**
366 ** Adjust User Variables.
367 **
368 ** @param caster Unit that casts the spell
369 ** @param spell Spell-type pointer
370 ** @param target Target
371 ** @param x X coord of target spot when/if target does not exist
372 ** @param y Y coord of target spot when/if target does not exist
373 **
374 ** @return =!0 if spell should be repeated, 0 if not
375 */
376 int AdjustVariable::Cast(CUnit *caster, const SpellType *spell,
377 CUnit *target, int x, int y)
378 {
379 int i;
380 CUnit *unit; // unit to modify.
381
382 for (i = 0; i < UnitTypeVar.NumberVariable; ++i) {
383 unit = (this->Var[i].TargetIsCaster) ? caster : target;
384 if (!unit) {
385 continue;
386 }
387 // Enable flag.
388 if (this->Var[i].ModifEnable) {
389 unit->Variable[i].Enable = this->Var[i].Enable;
390 }
391 unit->Variable[i].Enable ^= this->Var[i].InvertEnable;
392
393 // Max field
394 if (this->Var[i].ModifMax) {
395 unit->Variable[i].Max = this->Var[i].Max;
396 }
397 unit->Variable[i].Max += this->Var[i].AddMax;
398
399 // Increase field
400 if (this->Var[i].ModifIncrease) {
401 unit->Variable[i].Increase = this->Var[i].Increase;
402 }
403 unit->Variable[i].Increase += this->Var[i].AddIncrease;
404
405 // Value field
406 if (this->Var[i].ModifValue) {
407 unit->Variable[i].Value = this->Var[i].Value;
408 }
409 unit->Variable[i].Value += this->Var[i].AddValue;
410 unit->Variable[i].Value += this->Var[i].IncreaseTime
411 * unit->Variable[i].Increase;
412
413 if (unit->Variable[i].Value <= 0) {
414 unit->Variable[i].Value = 0;
415 } else if (unit->Variable[i].Value > unit->Variable[i].Max) {
416 unit->Variable[i].Value = unit->Variable[i].Max;
417 }
418 }
419 return 0;
420 }
421
422
423 /**
424 ** Cast healing. (or exorcism)
425 **
426 ** @param caster Unit that casts the spell
427 ** @param spell Spell-type pointer
428 ** @param target Target unit that spell is addressed to
429 ** @param x X coord of target spot when/if target does not exist
430 ** @param y Y coord of target spot when/if target does not exist
431 **
432 ** @return =!0 if spell should be repeated, 0 if not
433 */
434 int AdjustVitals::Cast(CUnit *caster, const SpellType *spell,
435 CUnit *target, int x, int y)
436 {
437 int castcount;
438 int diffHP;
439 int diffCharge;
440 int hp;
441
442 Assert(caster);
443 Assert(spell);
444 if (!target) {
445 return 0;
446 }
447
448 hp = this->HP;
449
450 // Healing and harming
451 if (hp > 0) {
452 diffHP = target->Variable[HP_INDEX].Max - target->Variable[HP_INDEX].Value;
453 } else {
454 diffHP = target->Variable[HP_INDEX].Value;
455 }
456 if (Charge > 0) {
457 diffCharge = target->Stats->Variables[CHARGE_INDEX].Max - target->Variable[CHARGE_INDEX].Value;
458 } else {
459 diffCharge = target->Variable[CHARGE_INDEX].Value;
460 }
461
462 // When harming cast again to send the hp to negative values.
463 // Carefull, a perfect 0 target hp kills too.
464 // Avoid div by 0 errors too!
465 castcount = 0;
466 if (hp) {
467 castcount = std::max(castcount, diffHP / abs(hp) + (((hp < 0) &&
468 (diffHP % (-hp) > 0)) ? 1 : 0));
469 }
470 if (Charge) {
471 castcount = std::max(castcount, diffCharge / abs(Charge) + (((Charge < 0) &&
472 (diffCharge % (-Charge) > 0)) ? 1 : 0));
473 }
474 if (spell->ChargeCost) {
475 castcount = std::min(castcount, caster->Variable[CHARGE_INDEX].Value / spell->ChargeCost);
476 }
477 if (this->MaxMultiCast) {
478 castcount = std::min(castcount, this->MaxMultiCast);
479 }
480
481 caster->Variable[CHARGE_INDEX].Value -= castcount * spell->ChargeCost;
482 if (hp < 0) {
483 HitUnit(caster, target, -(castcount * hp));
484 } else {
485 target->Variable[HP_INDEX].Value += castcount * hp;
486 if (target->Variable[HP_INDEX].Value > target->Variable[HP_INDEX].Max) {
487 target->Variable[HP_INDEX].Value = target->Variable[HP_INDEX].Max;
488 }
489 }
490 target->Variable[CHARGE_INDEX].Value += castcount * Charge;
491 if (target->Variable[CHARGE_INDEX].Value < 0) {
492 target->Variable[CHARGE_INDEX].Value = 0;
493 }
494 if (target->Variable[CHARGE_INDEX].Value > target->Variable[CHARGE_INDEX].Max) {
495 target->Variable[CHARGE_INDEX].Value = target->Variable[CHARGE_INDEX].Max;
496 }
497
498 return 0;
499 }
500
501 /**
502 ** Cast polymorph.
503 **
504 ** @param caster Unit that casts the spell
505 ** @param spell Spell-type pointer
506 ** @param target Target unit that spell is addressed to
507 ** @param x X coord of target spot when/if target does not exist
508 ** @param y Y coord of target spot when/if target does not exist
509 **
510 ** @return =!0 if spell should be repeated, 0 if not
511 */
512 int Polymorph::Cast(CUnit *caster, const SpellType *spell,
513 CUnit *target, int x, int y)
514 {
515 int i;
516 int j;
517 CUnitType *type;
518
519 if (!target) {
520 return 0;
521 }
522
523 type = this->NewForm;
524
525 x = x - type->TileWidth / 2;
526 y = y - type->TileHeight / 2;
527
528 caster->Player->Score += target->Type->Points;
529 if (caster->IsEnemy(target)) {
530 if (target->Type->Building) {
531 caster->Player->TotalRazings++;
532 } else {
533 caster->Player->TotalKills++;
534 }
535 caster->Variable[KILL_INDEX].Value++;
536 caster->Variable[KILL_INDEX].Max++;
537 caster->Variable[KILL_INDEX].Enable = 1;
538 }
539
540 // as said somewhere else -- no corpses :)
541 target->Remove(NULL);
542 for (i = 0; i < type->TileWidth; ++i) {
543 for (j = 0; j < type->TileHeight; ++j) {
544 if (!UnitTypeCanBeAt(type, x + i, y + j)) {
545 target->Place(target->X, target->Y);
546 return 0;
547 }
548 }
549 }
550 caster->Variable[CHARGE_INDEX].Value -= spell->ChargeCost;
551 if (this->PlayerNeutral) {
552 MakeUnitAndPlace(x, y, type, Players + PlayerNumNeutral);
553 } else {
554 MakeUnitAndPlace(x, y, type, target->Player);
555 }
556 UnitLost(target);
557 UnitClearOrders(target);
558 target->Release();
559 return 1;
560 }
561
562 /**
563 ** Cast capture.
564 **
565 ** @param caster Unit that casts the spell
566 ** @param spell Spell-type pointer
567 ** @param target Target unit that spell is addressed to
568 ** @param x X coord of target spot when/if target does not exist
569 ** @param y Y coord of target spot when/if target does not exist
570 **
571 ** @return =!0 if spell should be repeated, 0 if not
572 */
573 int Capture::Cast(CUnit *caster, const SpellType *spell,
574 CUnit *target, int x, int y)
575 {
576 if (!target || caster->Player == target->Player) {
577 return 0;
578 }
579
580 if (this->DamagePercent) {
581 if ((100 * target->Variable[HP_INDEX].Value) /
582 target->Variable[HP_INDEX].Max > this->DamagePercent &&
583 target->Variable[HP_INDEX].Value > this->Damage) {
584 HitUnit(caster, target, this->Damage);
585 if (this->SacrificeEnable) {
586 // No corpse.
587 caster->Remove(NULL);
588 UnitLost(caster);
589 UnitClearOrders(caster);
590 }
591 return 1;
592 }
593 }
594 caster->Player->Score += target->Type->Points;
595 if (caster->IsEnemy(target)) {
596 if (target->Type->Building) {
597 caster->Player->TotalRazings++;
598 } else {
599 caster->Player->TotalKills++;
600 }
601 caster->Variable[KILL_INDEX].Value++;
602 caster->Variable[KILL_INDEX].Max++;
603 caster->Variable[KILL_INDEX].Enable = 1;
604 }
605 target->ChangeOwner(caster->Player);
606 if (this->SacrificeEnable) {
607 // No corpse.
608 caster->Remove(NULL);
609 UnitLost(caster);
610 UnitClearOrders(caster);
611 } else {
612 caster->Variable[CHARGE_INDEX].Value -= spell->ChargeCost;
613 }
614 UnitClearOrders(target);
615 return 0;
616 }
617
618 /**
619 ** Cast summon spell.
620 **
621 ** @param caster Unit that casts the spell
622 ** @param spell Spell-type pointer
623 ** @param target Target unit that spell is addressed to
624 ** @param x X coord of target spot when/if target does not exist
625 ** @param y Y coord of target spot when/if target does not exist
626 **
627 ** @return =!0 if spell should be repeated, 0 if not
628 */
629 int Summon::Cast(CUnit *caster, const SpellType *spell,
630 CUnit *target, int x, int y)
631 {
632 int ttl;
633 int cansummon;
634 int n;
635 CUnit *table[UnitMax];
636 CUnit *unit;
637 CUnitType *unittype;
638
639 unittype = this->UnitType;
640 ttl = this->TTL;
641
642 if (this->RequireCorpse) {
643 n = UnitCache.Select(x - 1, y - 1, x + 2, y + 2, table, UnitMax);
644 cansummon = 0;
645 while (n) {
646 n--;
647 unit = table[n];
648 if (unit->Orders[0]->Action == UnitActionDie && !unit->Type->Building) {
649 //
650 // Found a corpse. eliminate it and proceed to summoning.
651 //
652 x = unit->X;
653 y = unit->Y;
654 unit->Remove(NULL);
655 unit->Release();
656 cansummon = 1;
657 break;
658 }
659 }
660 } else {
661 cansummon = 1;
662 }
663
664 if (cansummon) {
665 DebugPrint("Summoning a %s\n" _C_ unittype->Name.c_str());
666
667 //
668 // Create units.
669 // FIXME: do summoned units count on food?
670 //
671 target = MakeUnit(unittype, caster->Player);
672 if (target != NoUnitP) {
673 // This is a hack to walk around behaviour of DropOutOnSide
674 target->X = x + 1;
675 target->Y = y;
676 DropOutOnSide(target, LookingW, 0, 0); // FIXME : 0,0) : good parameter ?
677 //
678 // set life span. ttl=0 results in a permanent unit.
679 //
680 if (ttl) {
681 target->TTL = GameCycle + ttl;
682 }
683
684 caster->Variable[CHARGE_INDEX].Value -= spell->ChargeCost;
685 } else {
686 DebugPrint("Unable to allocate Unit");
687 }
688 return 1;
689 }
690 return 0;
691 }
692
693 // ****************************************************************************
694 // Target constructor
695 // ****************************************************************************
696
697 /**
698 ** Target constructor for unit.
699 **
700 ** @param unit Target unit.
701 **
702 ** @return the new target.
703 */
704 static Target *NewTargetUnit(CUnit *unit)
705 {
706 return new Target(TargetUnit, unit, 0, 0);
707 }
708
709 #if 0
710 /**
711 ** Target constructor for position.
712 **
713 ** @param x X position.
714 ** @param y Y position.
715 **
716 ** @return the new target.
717 */
718 static Target *NewTargetPosition(int x, int y)
719 {
720 return new Target(TargetPosition, NULL, x, y);
721 }
722 #endif
723
724 // ****************************************************************************
725 // Main local functions
726 // ****************************************************************************
727
728 /**
729 ** Check the condition.
730 **
731 ** @param caster Pointer to caster unit.
732 ** @param spell Pointer to the spell to cast.
733 ** @param target Pointer to target unit, or 0 if it is a position spell.
734 ** @param x X position, or -1 if it is a unit spell.
735 ** @param y Y position, or -1 if it is a unit spell.
736 ** @param condition Pointer to condition info.
737 **
738 ** @return true if passed, false otherwise.
739 */
740 static bool PassCondition(const CUnit *caster, const SpellType *spell, const CUnit *target,
741 int x, int y, const ConditionInfo *condition)
742 {
743 if (caster->Variable[CHARGE_INDEX].Value < spell->ChargeCost) { // Check caster Charge.
744 return false;
745 }
746 if (spell->Target == TargetUnit) { // Casting a unit spell without a target.
747 if ((!target) || target->Destroyed || target->Orders[0]->Action == UnitActionDie) {
748 return false;
749 }
750 }
751 if (!condition) { // no condition, pass.
752 return true;
753 }
754
755 for (int i = 0; i < UnitTypeVar.NumberVariable; i++) { // for custom variables
756 const CUnit *unit;
757
758 unit = (condition->Variable[i].ConditionApplyOnCaster) ? caster : target;
759 // Spell should target location and have unit condition.
760 if (unit == NULL) {
761 continue;
762 }
763 if (condition->Variable[i].Enable != CONDITION_TRUE) {
764 if ((condition->Variable[i].Enable == CONDITION_ONLY) ^ (unit->Variable[i].Enable)) {
765 return false;
766 }
767 }
768 // Value and Max
769 if (condition->Variable[i].MinValue >= unit->Variable[i].Value) {
770 return false;
771 }
772 if (condition->Variable[i].MaxValue != -1 &&
773 condition->Variable[i].MaxValue <= unit->Variable[i].Value) {
774 return false;
775 }
776
777 if (condition->Variable[i].MinMax >= unit->Variable[i].Max) {
778 return false;
779 }
780
781 if (!unit->Variable[i].Max) {
782 continue;
783 }
784 // Percent
785 if (condition->Variable[i].MinValuePercent * unit->Variable[i].Max
786 >= 100 * unit->Variable[i].Value) {
787 return false;
788 }
789 if (condition->Variable[i].MaxValuePercent * unit->Variable[i].Max
790 <= 100 * unit->Variable[i].Value) {
791 return false;
792 }
793 }
794
795 if (!target) {
796 return true;
797 }
798 if (condition->Alliance != CONDITION_TRUE) {
799 if ((condition->Alliance == CONDITION_ONLY) ^
800 // own units could be not allied ?
801 (caster->IsAllied(target) || target->Player == caster->Player)) {
802 return false;
803 }
804 }
805 if (condition->Opponent != CONDITION_TRUE) {
806 if ((condition->Opponent == CONDITION_ONLY) ^
807 (caster->IsEnemy(target) && 1)) {
808 return false;
809 }
810 }
811 if (condition->TargetSelf != CONDITION_TRUE) {
812 if ((condition->TargetSelf == CONDITION_ONLY) ^ (caster == target)) {
813 return false;
814 }
815 }
816 if (condition->Building != CONDITION_TRUE) {
817 if ((condition->Building == CONDITION_ONLY) ^ (target->Type->Building)) {
818 return false;
819 }
820 }
821 if (condition->Organic != CONDITION_TRUE) {
822 if ((condition->Organic == CONDITION_ONLY) ^ (target->Type->Organic)) {
823 return false;
824 }
825 }
826
827 return true;
828 }
829
830 /**
831 ** Select the target for the autocast.
832 **
833 ** @param caster Unit who would cast the spell.
834 ** @param spell Spell-type pointer.
835 **
836 ** @return Target* chosen target or Null if spell can't be cast.
837 ** @todo FIXME: should be global (for AI) ???
838 ** @todo FIXME: write for position target.
839 */
840 static Target *SelectTargetUnitsOfAutoCast(CUnit *caster, const SpellType *spell)
841 {
842 CUnit *table[UnitMax];
843 int x;
844 int y;
845 int range;
846 int nunits;
847 int i;
848 int j;
849 int combat;
850 AutoCastInfo *autocast;
851
852 // Ai cast should be a lot better. Use autocast if not found.
853 if (caster->Player->AiEnabled && spell->AICast) {
854 autocast = spell->AICast;
855 } else {
856 autocast = spell->AutoCast;
857 }
858 Assert(autocast);
859 x = caster->X;
860 y = caster->Y;
861 range = spell->AutoCast->Range;
862
863 //
864 // Select all units aroung the caster
865 //
866 nunits = UnitCache.Select(caster->X - range, caster->Y - range,
867 caster->X + range + caster->Type->TileWidth,
868 caster->Y + range + caster->Type->TileHeight, table, UnitMax);
869 //
870 // Check every unit if it is hostile
871 //
872 combat = 0;
873 for (i = 0; i < nunits; ++i) {
874 if (caster->IsEnemy(table[i]) && !table[i]->Type->Coward) {
875 combat = 1;
876 }
877 }
878
879 //
880 // Check generic conditions. FIXME: a better way to do this?
881 //
882 if (autocast->Combat != CONDITION_TRUE) {
883 if ((autocast->Combat == CONDITION_ONLY) ^ (combat)) {
884 return NULL;
885 }
886 }
887
888 switch (spell->Target) {
889 case TargetSelf :
890 if (PassCondition(caster, spell, caster, x, y, spell->Condition) &&
891 PassCondition(caster, spell, caster, x, y, autocast->Condition)) {
892 return NewTargetUnit(caster);
893 }
894 return NULL;
895 case TargetPosition:
896 return 0;
897 // Autocast with a position? That's hard
898 // Possibilities: cast reveal-map on a dark region
899 // Cast raise dead on a bunch of corpses. That would rule.
900 // Cast summon until out of charge in the heat of battle. Trivial?
901 // Find a tight group of units and cast area-damage spells. HARD,
902 // but it is a must-have for AI. What about area-heal?
903 case TargetUnit:
904 //
905 // The units are already selected.
906 // Check every unit if it is a possible target
907 //
908 for (i = 0, j = 0; i < nunits; ++i) {
909 // Can't cast spell on ourself
910 if (caster == table[i]) {
911 continue;
912 }
913 // FIXME: autocast conditions should include normal conditions.
914 // FIXME: no, really, they should.
915 if (PassCondition(caster, spell, table[i], x, y, spell->Condition) &&
916 PassCondition(caster, spell, table[i], x, y, autocast->Condition)) {
917 table[j++] = table[i];
918 }
919 }
920 nunits = j;
921 //
922 // Now select the best unit to target.
923 // FIXME: Some really smart way to do this.
924 // FIXME: Heal the unit with the lowest hit-points
925 // FIXME: Bloodlust the unit with the highest hit-point
926 // FIMXE: it will survive more
927 //
928 if (nunits != 0) {
929 #if 0
930 // For the best target???
931 sort(table, nb_units, spell->autocast->f_order);
932 return NewTargetUnit(table[0]);
933 #else
934 // Best unit, random unit, oh well, same stuff.
935 i = SyncRand() % nunits;
936 return NewTargetUnit(table[i]);
937 #endif
938 }
939 break;
940 default:
941 // Something is wrong
942 DebugPrint("Spell is screwed up, unknown target type\n");
943 Assert(0);
944 return NULL;
945 break;
946 }
947 return NULL; // Can't spell the auto-cast.
948 }
949
950 // ****************************************************************************
951 // Public spell functions
952 // ****************************************************************************
953
954 // ****************************************************************************
955 // Constructor and destructor
956 // ****************************************************************************
957
958 /**
959 ** Spells constructor, inits spell id's and sounds
960 */
961 void InitSpells(void)
962 {
963 }
964
965 /**
966 ** Get spell-type struct pointer by string identifier.
967 **
968 ** @param ident Spell identifier.
969 **
970 ** @return spell-type struct pointer.
971 */
972 SpellType *SpellTypeByIdent(const std::string &ident)
973 {
974 for (std::vector<SpellType *>::iterator i = SpellTypeTable.begin(); i < SpellTypeTable.end(); ++i) {
975 if ((*i)->Ident == ident) {
976 return *i;
977 }
978 }
979 return NULL;
980 }
981
982 // ****************************************************************************
983 // CanAutoCastSpell, CanCastSpell, AutoCastSpell, CastSpell.
984 // ****************************************************************************
985
986 /**
987 ** Check if spell is available for player \p player.
988 ** @param player player for who we want to know if he knows the spell.
989 ** @param spellid id of the spell to check.
990 **
991 ** @return 0 if spell is not available, else no null.
992 */
993 bool SpellIsAvailable(const CPlayer *player, int spellid)
994 {
995 int dependencyId;
996 dependencyId = SpellTypeTable[spellid]->DependencyId;
997
998 return dependencyId == -1;
999 }
1000
1001 /**
1002 ** Check if unit can cast the spell.
1003 **
1004 ** @param caster Unit that casts the spell
1005 ** @param spell Spell-type pointer
1006 ** @param target Target unit that spell is addressed to
1007 ** @param x X coord of target spot when/if target does not exist
1008 ** @param y Y coord of target spot when/if target does not exist
1009 **
1010 ** @return =!0 if spell should/can casted, 0 if not
1011 ** @note caster must know the spell, and spell must be available.
1012 */
1013 bool CanCastSpell(const CUnit *caster, const SpellType *spell,
1014 const CUnit *target, int x, int y)
1015 {
1016 if (spell->Target == TargetUnit && target == NULL) {
1017 return false;
1018 }
1019 return PassCondition(caster, spell, target, x, y, spell->Condition);
1020 }
1021
1022 /**
1023 ** Check if the spell can be auto cast and cast it.
1024 **
1025 ** @param caster Unit who can cast the spell.
1026 ** @param spell Spell-type pointer.
1027 **
1028 ** @return 1 if spell is casted, 0 if not.
1029 */
1030 int AutoCastSpell(CUnit *caster, const SpellType *spell)
1031 {
1032 Target *target;
1033
1034 // Check for Charge, trivial optimization.
1035 if (!SpellIsAvailable(caster->Player, spell->Slot)
1036 || caster->Variable[CHARGE_INDEX].Value < spell->ChargeCost) {
1037 return 0;
1038 }
1039 target = SelectTargetUnitsOfAutoCast(caster, spell);
1040 if (target == NULL) {
1041 return 0;
1042 } else {
1043 // Must move before ?
1044 // FIXME: SpellType* of CommandSpellCast must be const.
1045 CommandSpellCast(caster, target->X, target->Y, target->Unit,
1046 (SpellType *)spell, FlushCommands);
1047 delete target;
1048 }
1049 return 1;
1050 }
1051
1052 /**
1053 ** Spell cast!
1054 **
1055 ** @param caster Unit that casts the spell
1056 ** @param spell Spell-type pointer
1057 ** @param target Target unit that spell is addressed to
1058 ** @param x X coord of target spot when/if target does not exist
1059 ** @param y Y coord of target spot when/if target does not exist
1060 **
1061 ** @return !=0 if spell should/can continue or 0 to stop
1062 */
1063 int SpellCast(CUnit *caster, const SpellType *spell, CUnit *target,
1064 int x, int y)
1065 {
1066 int cont; // Should we recast the spell.
1067 int mustSubtractCharge; // false if action which have their own calculation is present.
1068
1069 if (target) {
1070 int xa;
1071 int ya;
1072 int xb;
1073 int yb;
1074 target->GetMapArea(&xa, &ya, &xb, &yb);
1075 x = (xa+xb)/2;
1076 y = (ya+yb)/2;
1077 }
1078 //
1079 // For TargetSelf, you target.... YOURSELF
1080 //
1081 if (spell->Target == TargetSelf) {
1082 x = caster->X;
1083 y = caster->Y;
1084 target = caster;
1085 }
1086 DebugPrint("Spell cast: (%s), %s -> %s (%d,%d)\n" _C_ spell->Ident.c_str() _C_
1087 caster->Type->Name.c_str() _C_ target ? target->Type->Name.c_str() : "none" _C_ x _C_ y);
1088 if (CanCastSpell(caster, spell, target, x, y)) {
1089 cont = 1;
1090 mustSubtractCharge = 1;
1091 //
1092 // Ugly hack, CastAdjustVitals makes it's own charge calculation.
1093 //
1094 PlayGameSound(spell->SoundWhenCast.Sound, MaxSampleVolume);
1095 for (std::vector<SpellActionType*>::const_iterator act = spell->Action.begin();
1096 act != spell->Action.end(); ++act) {
1097 if ((*act)->ModifyChargeCaster) {
1098 mustSubtractCharge = 0;
1099 }
1100 cont = cont & (*act)->Cast(caster, spell, target, x, y);
1101 }
1102 if (mustSubtractCharge) {
1103 caster->Variable[CHARGE_INDEX].Value -= spell->ChargeCost;
1104 }
1105 //
1106 // Spells like blizzard are casted again.
1107 // This is sort of confusing, we do the test again, to
1108 // check if it will be possible to cast again. Otherwise,
1109 // when you're out of Charge the caster will try again ( do the
1110 // anim but fail in this proc.
1111 //
1112 if (spell->RepeatCast && cont) {
1113 return CanCastSpell(caster, spell, target, x, y);
1114 }
1115 }
1116 //
1117 // Can't cast, STOP.
1118 //
1119 return 0;
1120 }
1121
1122
1123 /**
1124 ** SpellType constructor.
1125 */
1126 SpellType::SpellType(int slot, const std::string &ident) :
1127 Ident(ident), Slot(slot), Target(), Action(),
1128 Range(0), ChargeCost(0), RepeatCast(0),
1129 DependencyId(-1), Condition(NULL),
1130 AutoCast(NULL), AICast(NULL)
1131 {
1132 }
1133
1134 /**
1135 ** SpellType destructor.
1136 */
1137 SpellType::~SpellType()
1138 {
1139 for (std::vector<SpellActionType *>::iterator act = Action.begin(); act != Action.end(); ++act) {
1140 delete *act;
1141 }
1142 Action.clear();
1143
1144 delete Condition;
1145 //
1146 // Free Autocast.
1147 //
1148 delete AutoCast;
1149 delete AICast;
1150 }
1151
1152
1153 /**
1154 ** Cleanup the spell subsystem.
1155 */
1156 void CleanSpells(void)
1157 {
1158 DebugPrint("Cleaning spells.\n");
1159 for (std::vector<SpellType *>::iterator i = SpellTypeTable.begin(); i < SpellTypeTable.end(); ++i) {
1160 delete *i;
1161 }
1162 SpellTypeTable.clear();
1163 }
1164
1165 //@}