Debian Codegraph @

engine/action/action_follow.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_follow.cpp - The follow action. */
11 //
12 // (c) Copyright 2001-2007 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 #include <string.h>
38
39 #include "stratagus.h"
40 #include "unit.h"
41 #include "unittype.h"
42 #include "pathfinder.h"
43 #include "map.h"
44 #include "interface.h"
45 #include "actions.h"
46
47 /*----------------------------------------------------------------------------
48 -- Variables
49 ----------------------------------------------------------------------------*/
50
51 /*----------------------------------------------------------------------------
52 -- Function
53 ----------------------------------------------------------------------------*/
54
55 /**
56 ** Unit follow action:
57 **
58 ** @param unit Pointer to unit.
59 */
60 void HandleActionFollow(CUnit *unit)
61 {
62 CUnit *goal;
63
64 if (unit->Wait) {
65 unit->Wait--;
66 return;
67 }
68
69 //
70 // Reached target
71 //
72 if (unit->SubAction == 128) {
73 goal = unit->Orders[0]->Goal;
74 if (!goal || !goal->IsVisibleAsGoal(unit->Player)) {
75 DebugPrint("Goal gone\n");
76 if (goal) {
77 goal->RefsDecrease();
78 }
79 unit->Orders[0]->Goal = NoUnitP;
80 unit->ClearAction();
81 return;
82 }
83
84 if (goal->X == unit->Orders[0]->X && goal->Y == unit->Orders[0]->Y) {
85 // Move to the next order
86 if (unit->OrderCount > 1) {
87 goal->RefsDecrease();
88 unit->Orders[0]->Goal = NoUnitP;
89 unit->ClearAction();
90 return;
91 }
92
93 // Reset frame to still frame while we wait
94 // FIXME: Unit doesn't animate.
95 unit->Frame = unit->Type->StillFrame;
96 UnitUpdateHeading(unit);
97 unit->Wait = 10;
98 if (unit->Orders[0]->Range > 1) {
99 unit->Orders[0]->Range = 1;
100 unit->SubAction = 0;
101 }
102 return;
103 }
104
105 unit->SubAction = 0;
106 }
107
108 if (!unit->SubAction) { // first entry
109 unit->SubAction = 1;
110 NewResetPath(unit);
111 Assert(unit->State == 0);
112 }
113
114 switch (DoActionMove(unit)) { // reached end-point?
115 case PF_UNREACHABLE:
116 //
117 // Some tries to reach the goal
118 //
119 if (unit->Orders[0]->Range <= Map.Info.MapWidth ||
120 unit->Orders[0]->Range <= Map.Info.MapHeight) {
121 unit->Orders[0]->Range++;
122 break;
123 }
124 // FALL THROUGH
125 case PF_REACHED:
126 if (!(goal = unit->Orders[0]->Goal)) { // goal has died
127 unit->ClearAction();
128 return;
129 }
130 unit->Orders[0]->X = goal->X;
131 unit->Orders[0]->Y = goal->Y;
132 unit->SubAction = 128;
133
134 // FALL THROUGH
135 default:
136 break;
137 }
138
139 //
140 // Target destroyed?
141 //
142 if ((goal = unit->Orders[0]->Goal) && !goal->IsVisibleAsGoal(unit->Player)) {
143 DebugPrint("Goal gone\n");
144 unit->Orders[0]->X = goal->X + goal->Type->TileWidth / 2;
145 unit->Orders[0]->Y = goal->Y + goal->Type->TileHeight / 2;
146 unit->Orders[0]->Goal = NoUnitP;
147 goal->RefsDecrease();
148 goal = NoUnitP;
149 NewResetPath(unit);
150 }
151
152 if (!unit->Anim.Unbreakable) {
153 //
154 // If our leader is dead or stops or attacks:
155 // Attack any enemy in reaction range.
156 // If don't set the goal, the unit can than choose a
157 // better goal if moving nearer to enemy.
158 //
159 if (unit->Type->CanAttack &&
160 (!goal || goal->Orders[0]->Action == UnitActionAttack ||
161 goal->Orders[0]->Action == UnitActionStill)) {
162 goal = AttackUnitsInReactRange(unit);
163 if (goal) {
164 CommandAttack(unit, goal->X, goal->Y, NULL, FlushCommands);
165 // Save current command to come back.
166 unit->SavedOrder = *unit->Orders[0];
167 // This stops the follow command and the attack is executed
168 unit->ClearAction();
169 unit->Orders[0]->Goal = NoUnitP;
170 }
171 }
172 }
173 }
174
175 //@}