Debian Codegraph @

engine/action/action_still.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 action_still.cpp - The stand still action. */
11 //
12 // (c) Copyright 1998-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
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "stratagus.h"
39 #include "missile.h"
40 #include "unittype.h"
41 #include "animation.h"
42 #include "actions.h"
43 #include "unit.h"
44 #include "unit_cache.h"
45 #include "map.h"
46 #include "pathfinder.h"
47 #include "spells.h"
48 #include "player.h"
49
50 /*----------------------------------------------------------------------------
51 -- Functions
52 ----------------------------------------------------------------------------*/
53
54 /**
55 ** Auto cast a spell if possible
56 **
57 ** @return true if a spell was auto cast, false otherwise
58 */
59 static bool AutoCast(CUnit *unit)
60 {
61 if (unit->AutoCastSpell) {
62 for (int i = 0; i < (int)SpellTypeTable.size(); ++i) {
63 if (unit->AutoCastSpell[i] && AutoCastSpell(unit, SpellTypeTable[i])) {
64 return true;
65 }
66 }
67 }
68 return false;
69 }
70
71 /**
72 ** Try to find a repairable unit around and return it.
73 **
74 ** @param unit unit which can repair.
75 ** @param range range to find a repairable unit.
76 **
77 ** @return unit to repair if found, NoUnitP otherwise
78 **
79 ** @todo FIXME: find the best unit (most damaged, ...).
80 */
81 static CUnit *UnitToRepairInRange(CUnit *unit, int range)
82 {
83 CUnit *table[UnitMax];
84 int n;
85
86 n = UnitCache.Select(unit->X - range, unit->Y - range,
87 unit->X + unit->Type->TileWidth + range,
88 unit->Y + unit->Type->TileHeight + range,
89 table, UnitMax);
90 for (int i = 0; i < n; ++i) {
91 if (table[i]->IsTeamed(unit) &&
92 table[i]->Type->RepairHP &&
93 table[i]->Variable[HP_INDEX].Value < table[i]->Variable[HP_INDEX].Max &&
94 table[i]->IsVisibleAsGoal(unit->Player)) {
95 return table[i];
96 }
97 }
98 return NoUnitP;
99 }
100
101 /**
102 ** Auto repair a unit if possible
103 **
104 ** @return true if the unit is repairing, false otherwise
105 */
106 bool AutoRepair(CUnit *unit)
107 {
108 if (unit->AutoRepair && unit->Type->Variable[AUTOREPAIRRANGE_INDEX].Value) {
109 CUnit *repairedUnit = UnitToRepairInRange(unit,
110 unit->Type->Variable[AUTOREPAIRRANGE_INDEX].Value);
111 if (repairedUnit != NoUnitP) {
112 COrder order = *unit->Orders[0];
113 CommandRepair(unit, -1, -1, repairedUnit, FlushCommands);
114 unit->SavedOrder = order;
115 return true;
116 }
117 }
118 return false;
119 }
120
121 /**
122 ** Auto attack nearby units if possible
123 */
124 static void AutoAttack(CUnit *unit, bool stand_ground)
125 {
126 CUnit *temp;
127 CUnit *goal;
128
129 if (unit->Wait) {
130 unit->Wait--;
131 return;
132 }
133
134 // Cowards don't attack unless ordered.
135 if (unit->Type->CanAttack && !unit->Type->Coward) {
136 // Normal units react in reaction range.
137 if (CanMove(unit) && !unit->Removed && !stand_ground) {
138 if ((goal = AttackUnitsInReactRange(unit))) {
139 // Weak goal, can choose other unit, come back after attack
140 CommandAttack(unit, goal->X, goal->Y, NULL, FlushCommands);
141 Assert(unit->SavedOrder.Action == UnitActionStill);
142 Assert(!unit->SavedOrder.Goal);
143 unit->SavedOrder.Action = UnitActionAttack;
144 unit->SavedOrder.Range = 0;
145 unit->SavedOrder.X = unit->X;
146 unit->SavedOrder.Y = unit->Y;
147 unit->SavedOrder.Goal = NoUnitP;
148 } else {
149 unit->Wait = 15;
150 }
151 // Removed units can only attack in AttackRange, from bunker
152 } else if ((goal = AttackUnitsInRange(unit))) {
153 temp = unit->Orders[0]->Goal;
154 if (temp && temp->Orders[0]->Action == UnitActionDie) {
155 temp->RefsDecrease();
156 unit->Orders[0]->Goal = temp = NoUnitP;
157 }
158 if (!unit->SubAction || temp != goal) {
159 // New target.
160 if (temp) {
161 temp->RefsDecrease();
162 }
163 unit->Orders[0]->Goal = goal;
164 goal->RefsIncrease();
165 unit->State = 0;
166 unit->SubAction = 1; // Mark attacking.
167 UnitHeadingFromDeltaXY(unit,
168 goal->X + (goal->Type->TileWidth - 1) / 2 - unit->X,
169 goal->Y + (goal->Type->TileHeight - 1) / 2 - unit->Y);
170 }
171 return;
172 }
173 } else {
174 unit->Wait = 15;
175 }
176
177 if (unit->SubAction) { // was attacking.
178 if ((temp = unit->Orders[0]->Goal)) {
179 temp->RefsDecrease();
180 unit->Orders[0]->Goal = NoUnitP;
181 }
182 unit->SubAction = unit->State = 0; // No attacking, restart
183 }
184 Assert(!unit->Orders[0]->Goal);
185 }
186
187 /**
188 ** Unit stands still or stand ground.
189 **
190 ** @param unit Unit pointer for action.
191 ** @param stand_ground true if unit is standing ground.
192 */
193 void ActionStillGeneric(CUnit *unit, bool stand_ground)
194 {
195 // If unit is not bunkered and removed, wait
196 if (unit->Removed && (!unit->Container ||
197 !unit->Container->Type->CanTransport ||
198 !unit->Container->Type->AttackFromTransporter ||
199 unit->Type->Missile.Missile->Class == MissileClassNone)) {
200 // If unit is in building or transporter it is removed.
201 return;
202 }
203
204 // Animations
205 if (unit->SubAction) { // attacking unit in attack range.
206 AnimateActionAttack(unit);
207 } else {
208 UnitShowAnimation(unit, unit->Type->Animations->Still);
209 }
210
211 if (unit->Anim.Unbreakable) { // animation can't be aborted here
212 return;
213 }
214
215 if (AutoCast(unit) || AutoRepair(unit)) {
216 return;
217 }
218
219 AutoAttack(unit, stand_ground);
220 }
221
222 /**
223 ** Unit stands still!
224 **
225 ** @param unit Unit pointer for still action.
226 */
227 void HandleActionStill(CUnit *unit)
228 {
229 ActionStillGeneric(unit, false);
230 }
231
232 //@}