| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name action_spellcast.cpp - The spell cast action. */ |
| 11 |
// |
| 12 |
// (c) Copyright 2000-2008 by Vladi Belperchinov-Shabanski 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 |
** This is inherited from action_attack.c, actually spell casting will |
| 31 |
** be considered a `special' case attack action... //Vladi |
| 32 |
*/ |
| 33 |
|
| 34 |
//@{ |
| 35 |
|
| 36 |
/*---------------------------------------------------------------------------- |
| 37 |
-- Includes |
| 38 |
----------------------------------------------------------------------------*/ |
| 39 |
|
| 40 |
#include <stdio.h> |
| 41 |
#include <stdlib.h> |
| 42 |
|
| 43 |
#include "stratagus.h" |
| 44 |
#include "video.h" |
| 45 |
#include "unittype.h" |
| 46 |
#include "animation.h" |
| 47 |
#include "player.h" |
| 48 |
#include "unit.h" |
| 49 |
#include "missile.h" |
| 50 |
#include "actions.h" |
| 51 |
#include "pathfinder.h" |
| 52 |
#include "sound.h" |
| 53 |
#include "map.h" |
| 54 |
#include "spells.h" |
| 55 |
#include "interface.h" |
| 56 |
|
| 57 |
/*---------------------------------------------------------------------------- |
| 58 |
-- Functions |
| 59 |
----------------------------------------------------------------------------*/ |
| 60 |
|
| 61 |
#if 0 |
| 62 |
|
| 63 |
/** |
| 64 |
** Animate unit spell cast (it is attack really)! |
| 65 |
** |
| 66 |
** @param unit Unit, for that spell cast/attack animation is played. |
| 67 |
*/ |
| 68 |
void AnimateActionSpellCast(CUnit *unit) |
| 69 |
{ |
| 70 |
int flags; |
| 71 |
|
| 72 |
if (unit->Type->Animations) { |
| 73 |
Assert(unit->Type->Animations->Attack); |
| 74 |
|
| 75 |
flags = UnitShowAnimation(unit, unit->Type->Animations->Attack); |
| 76 |
|
| 77 |
if (flags & AnimationMissile) { // FIXME: should cast spell ? |
| 78 |
FireMissile(unit); // we should not get here ?? |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
#endif |
| 84 |
|
| 85 |
/** |
| 86 |
** Handle moving to the target. |
| 87 |
** |
| 88 |
** @param unit Unit, for that the spell cast is handled. |
| 89 |
*/ |
| 90 |
static void SpellMoveToTarget(CUnit *unit) |
| 91 |
{ |
| 92 |
CUnit *goal; |
| 93 |
int err; |
| 94 |
|
| 95 |
// Unit can't move |
| 96 |
err = 1; |
| 97 |
if (CanMove(unit)) { |
| 98 |
err = DoActionMove(unit); |
| 99 |
if (unit->Anim.Unbreakable) { |
| 100 |
return; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// when reached DoActionMove changes unit action |
| 105 |
// FIXME: use return codes from pathfinder |
| 106 |
goal = unit->Orders[0]->Goal; |
| 107 |
|
| 108 |
if (goal && MapDistanceBetweenUnits(unit, goal) <= |
| 109 |
unit->Orders[0]->Range) { |
| 110 |
|
| 111 |
// there is goal and it is in range |
| 112 |
unit->State = 0; |
| 113 |
UnitHeadingFromDeltaXY(unit, |
| 114 |
goal->X + (goal->Type->TileWidth - 1) / 2 - unit->X, |
| 115 |
goal->Y + (goal->Type->TileHeight - 1) / 2 - unit->Y); |
| 116 |
unit->SubAction++; // cast the spell |
| 117 |
return; |
| 118 |
} else if (!goal && MapDistanceToUnit(unit->Orders[0]->X, |
| 119 |
unit->Orders[0]->Y, unit) <= unit->Orders[0]->Range) { |
| 120 |
// there is no goal and target spot is in range |
| 121 |
UnitHeadingFromDeltaXY(unit, |
| 122 |
unit->Orders[0]->X + unit->Orders[0]->Arg1.Spell->Range - unit->X, |
| 123 |
unit->Orders[0]->Y + unit->Orders[0]->Arg1.Spell->Range - unit->Y); |
| 124 |
unit->SubAction++; // cast the spell |
| 125 |
return; |
| 126 |
} else if (err == PF_UNREACHABLE) { |
| 127 |
// |
| 128 |
// goal/spot unreachable and out of range -- give up |
| 129 |
// |
| 130 |
unit->ClearAction(); |
| 131 |
unit->State = 0; |
| 132 |
|
| 133 |
if (unit->Orders[0]->Goal) { // Release references |
| 134 |
unit->Orders[0]->Goal->RefsDecrease(); |
| 135 |
unit->Orders[0]->Goal = NoUnitP; |
| 136 |
} |
| 137 |
} |
| 138 |
Assert(!unit->Type->Vanishes && !unit->Destroyed); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
** Unit casts a spell! |
| 143 |
** |
| 144 |
** @param unit Unit, for that the spell cast is handled. |
| 145 |
*/ |
| 146 |
void HandleActionSpellCast(CUnit *unit) |
| 147 |
{ |
| 148 |
const SpellType *spell; |
| 149 |
|
| 150 |
if (unit->Wait) { |
| 151 |
unit->Wait--; |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
switch (unit->SubAction) { |
| 156 |
case 0: |
| 157 |
// |
| 158 |
// Check if we can cast the spell. |
| 159 |
// |
| 160 |
spell = unit->Orders[0]->Arg1.Spell; |
| 161 |
if (!CanCastSpell(unit, spell, unit->Orders[0]->Goal, |
| 162 |
unit->Orders[0]->X, unit->Orders[0]->Y)) { |
| 163 |
|
| 164 |
// |
| 165 |
// Notify player about this problem |
| 166 |
// |
| 167 |
if (unit->Variable[CHARGE_INDEX].Value < spell->ChargeCost) { |
| 168 |
unit->Player->Notify(NotifyYellow, unit->X, unit->Y, |
| 169 |
_("%s: not charged enough for this action: %s"), |
| 170 |
unit->Type->Name.c_str(), spell->Name.c_str()); |
| 171 |
} else { |
| 172 |
unit->Player->Notify(NotifyYellow, unit->X, unit->Y, |
| 173 |
_("%s: can't cast spell: %s"), |
| 174 |
unit->Type->Name.c_str(), spell->Name.c_str()); |
| 175 |
} |
| 176 |
|
| 177 |
if (unit->Player->AiEnabled) { |
| 178 |
DebugPrint("FIXME: do we need an AI callback?\n"); |
| 179 |
} |
| 180 |
unit->ClearAction(); |
| 181 |
if (unit->Orders[0]->Goal) { |
| 182 |
unit->Orders[0]->Goal->RefsDecrease(); |
| 183 |
unit->Orders[0]->Goal = NoUnitP; |
| 184 |
} |
| 185 |
return; |
| 186 |
} |
| 187 |
// FIXME FIXME FIXME: Check if already in range and skip straight to 2(casting) |
| 188 |
NewResetPath(unit); |
| 189 |
unit->ReCast = 0; // repeat spell on next pass? (defaults to `no') |
| 190 |
unit->SubAction = 1; |
| 191 |
// FALL THROUGH |
| 192 |
case 1: // Move to the target. |
| 193 |
if ((spell = unit->Orders[0]->Arg1.Spell)->Range != INFINITE_RANGE) { |
| 194 |
SpellMoveToTarget(unit); |
| 195 |
break; |
| 196 |
} else { |
| 197 |
unit->SubAction = 2; |
| 198 |
} |
| 199 |
// FALL THROUGH |
| 200 |
case 2: // Cast spell on the target. |
| 201 |
// FIXME: should use AnimateActionSpellCast here |
| 202 |
if (unit->Type->Animations->Attack) { |
| 203 |
UnitShowAnimation(unit, unit->Type->Animations->Attack); |
| 204 |
if (unit->Anim.Unbreakable) { // end of animation |
| 205 |
return; |
| 206 |
} |
| 207 |
} else { |
| 208 |
// FIXME: what todo, if unit/goal is removed? |
| 209 |
if (unit->Orders[0]->Goal && !unit->Orders[0]->Goal->IsVisibleAsGoal(unit->Player)) { |
| 210 |
unit->ReCast = 0; |
| 211 |
} else { |
| 212 |
spell = unit->Orders[0]->Arg1.Spell; |
| 213 |
unit->ReCast = SpellCast(unit, spell, unit->Orders[0]->Goal, |
| 214 |
unit->Orders[0]->X, unit->Orders[0]->Y); |
| 215 |
} |
| 216 |
} |
| 217 |
if (!unit->ReCast && unit->Orders[0]->Action != UnitActionDie) { |
| 218 |
unit->ClearAction(); |
| 219 |
if (unit->Orders[0]->Goal) { |
| 220 |
unit->Orders[0]->Goal->RefsDecrease(); |
| 221 |
unit->Orders[0]->Goal = NoUnitP; |
| 222 |
} |
| 223 |
} |
| 224 |
break; |
| 225 |
|
| 226 |
default: |
| 227 |
unit->SubAction = 0; // Reset path, than move to target |
| 228 |
break; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
//@} |