Debian Codegraph @

engine/action/action_patrol.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_patrol.cpp - The patrol action. */
11 //
12 // (c) Copyright 1998-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
38 #include "stratagus.h"
39 #include "unit.h"
40 #include "unittype.h"
41 #include "actions.h"
42 #include "pathfinder.h"
43 #include "map.h"
44
45 /*----------------------------------------------------------------------------
46 -- Functions
47 ----------------------------------------------------------------------------*/
48
49 extern bool AutoRepair(CUnit *unit);
50
51
52 /**
53 ** Swap the patrol points.
54 */
55 static void SwapPatrolPoints(CUnit *unit)
56 {
57 int tmp;
58
59 tmp = unit->Orders[0]->Arg1.Patrol.X;
60 unit->Orders[0]->Arg1.Patrol.X = unit->Orders[0]->X;
61 unit->Orders[0]->X = tmp;
62 tmp = unit->Orders[0]->Arg1.Patrol.Y;
63 unit->Orders[0]->Arg1.Patrol.Y = unit->Orders[0]->Y;
64 unit->Orders[0]->Y = tmp;
65
66 NewResetPath(unit);
67 }
68
69 /**
70 ** Unit Patrol:
71 ** The unit patrols between two points.
72 ** Any enemy unit in reaction range is attacked.
73 ** @todo FIXME:
74 ** Should do some tries to reach the end-points.
75 ** Should support patrol between more points!
76 ** Patrol between units.
77 **
78 ** @param unit Patroling unit pointer.
79 */
80 void HandleActionPatrol(CUnit *unit)
81 {
82 if (unit->Wait) {
83 unit->Wait--;
84 return;
85 }
86
87 if (!unit->SubAction) { // first entry.
88 NewResetPath(unit);
89 unit->SubAction = 1;
90 }
91
92 switch (DoActionMove(unit)) {
93 case PF_FAILED:
94 unit->SubAction = 1;
95 break;
96 case PF_UNREACHABLE:
97 // Increase range and try again
98 unit->SubAction = 1;
99 if (unit->Orders[0]->Range <= Map.Info.MapWidth ||
100 unit->Orders[0]->Range <= Map.Info.MapHeight) {
101 unit->Orders[0]->Range++;
102 break;
103 }
104 // FALL THROUGH
105 case PF_REACHED:
106 unit->SubAction = 1;
107 unit->Orders[0]->Range = 0;
108 SwapPatrolPoints(unit);
109 break;
110 case PF_WAIT:
111 // Wait for a while then give up
112 unit->SubAction++;
113 if (unit->SubAction == 5) {
114 unit->SubAction = 1;
115 unit->Orders[0]->Range = 0;
116 SwapPatrolPoints(unit);
117 }
118 break;
119 default: // moving
120 unit->SubAction = 1;
121 break;
122 }
123
124 if (!unit->Anim.Unbreakable) {
125 //
126 // Attack any enemy in reaction range.
127 // If don't set the goal, the unit can then choose a
128 // better goal if moving nearer to enemy.
129 //
130 if (unit->Type->CanAttack) {
131 const CUnit *goal = AttackUnitsInReactRange(unit);
132 if (goal) {
133 DebugPrint("Patrol attack %d\n" _C_ UnitNumber(goal));
134 CommandAttack(unit, goal->X, goal->Y, NULL, FlushCommands);
135 // Save current command to come back.
136 unit->SavedOrder = *unit->Orders[0];
137 unit->ClearAction();
138 unit->Orders[0]->Goal = NoUnitP;
139 return;
140 }
141 }
142
143 // Look for something to auto repair
144 if (AutoRepair(unit)) {
145 return;
146 }
147 }
148 }
149
150 //@}