Debian Codegraph @

engine/action/action_resource.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_resource.cpp - The generic resource action. */
11 //
12 // (c) Copyright 2001-2008 by Crestez Leonard 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 "player.h"
41 #include "unit.h"
42 #include "unittype.h"
43 #include "animation.h"
44 #include "actions.h"
45 #include "pathfinder.h"
46 #include "interface.h"
47 #include "sound.h"
48
49 /*----------------------------------------------------------------------------
50 -- Declarations
51 ----------------------------------------------------------------------------*/
52
53 #define SUB_START_RESOURCE 0
54 #define SUB_MOVE_TO_RESOURCE 5
55 #define SUB_UNREACHABLE_RESOURCE 31
56 #define SUB_START_GATHERING 55
57 #define SUB_GATHER_RESOURCE 60
58
59 int AlliedUnitRecyclingEfficiency[MaxCosts] = {0, 50};
60 int EnemyUnitRecyclingEfficiency[MaxCosts] = {0, 40};
61
62 /*----------------------------------------------------------------------------
63 -- Functions
64 ----------------------------------------------------------------------------*/
65
66 /**
67 ** Move unit to resource.
68 **
69 ** @param unit Pointer to unit.
70 **
71 ** @return -1 if unreachable, 0 if moving, 1 if reached.
72 */
73 static int MoveToResource(CUnit *unit)
74 {
75 switch (DoActionMove(unit)) { // reached end-point?
76 case PF_UNREACHABLE:
77 return -1;
78 case PF_REACHED:
79 break;
80 default:
81 // Goal gone or something.
82 if (unit->Anim.Unbreakable || unit->Orders[0]->Goal->IsVisibleAsGoal(unit->Player)) {
83 return 0;
84 }
85 break;
86 }
87 return 1;
88 }
89
90 /**
91 ** Start harvesting the resource.
92 **
93 ** @param unit Pointer to unit.
94 **
95 ** @return TRUE if ready, otherwise FALSE.
96 */
97 static bool StartGathering(CUnit *unit)
98 {
99 CUnit *goal = unit->Orders[0]->Goal;
100
101 Assert(!unit->IX && !unit->IY);
102
103 //
104 // Target is gone, stop getting resources.
105 //
106 if (!goal->IsVisibleAsGoal(unit->Player)) {
107 goal->RefsDecrease();
108 // Find an alternative, but don't look too far.
109 unit->Orders[0]->X = unit->Orders[0]->Y = -1;
110 if ((goal = UnitFindResource(unit, unit->X, unit->Y, 10))) {
111 unit->SubAction = SUB_START_RESOURCE;
112 unit->Orders[0]->Goal = goal;
113 goal->RefsIncrease();
114 } else {
115 unit->ClearAction();
116 unit->Orders[0]->Goal = NoUnitP;
117 }
118 return false;
119 }
120
121 // 0 can happen, for example with flying units.
122 Assert(MapDistanceBetweenUnits(unit, goal) <= 1);
123
124 //
125 // Update the heading of a harvesting unit to look straight at the resource.
126 //
127 UnitHeadingFromDeltaXY(unit,
128 goal->X + (goal->Type->TileWidth - 1) / 2 - unit->X,
129 goal->Y + (goal->Type->TileHeight - 1) / 2 - unit->Y);
130
131 return true;
132 }
133
134 /**
135 ** Animate a unit that is harvesting
136 **
137 ** @param unit Unit to animate
138 */
139 static void AnimateActionHarvest(CUnit *unit)
140 {
141 Assert(unit->Type->Animations->Harvest);
142 UnitShowAnimation(unit, unit->Type->Animations->Harvest);
143 }
144
145 /**
146 ** Check which type of resource was harvested from a unit,
147 ** so that the caller can look for more of the same.
148 **
149 ** @param exhausted Unit from which resources were harvested.
150 **
151 ** @return Either enum CostType, or -1 if the resource
152 ** type could not be identified.
153 */
154 static int HarvestedResourceType(const CUnit *exhausted)
155 {
156 int found = -1;
157 for (int res = 0; res < MaxCosts; res++) {
158 // exhausted->ResourcesHeld[res] is presumably zero
159 // already so check the unit type instead.
160 if (exhausted->Type->ProductionCosts[res] > 0) {
161 if (found == -1) {
162 found = res;
163 } else {
164 // It contains more than one type of
165 // resource. We could find the
166 // largest number but the amounts are
167 // not really comparable.
168 return -1;
169 }
170 }
171 }
172
173 // The unit type contains just one type of resource, or none
174 // at all. In the latter case, found == -1 still.
175 return found;
176 }
177
178 /**
179 ** Find something else to do when the resource is exhausted.
180 ** This is called from GatherResource when the resource is empty.
181 **
182 ** @param unit Harvester unit
183 */
184 static void FindNewResource(CUnit *unit)
185 {
186 const int res = HarvestedResourceType(unit->Orders[0]->Goal);
187 unit->Orders[0]->Goal->RefsDecrease();
188 unit->Orders[0]->Goal = NoUnitP;
189
190 unit->Orders[0]->X = unit->Orders[0]->Y = -1;
191 if ((unit->Orders[0]->Goal = UnitFindResource(unit, unit->X, unit->Y, 10, res))) {
192 DebugPrint("Unit %d found another resource.\n" _C_ unit->Slot);
193 unit->SubAction = SUB_START_RESOURCE;
194 unit->State = 0;
195 unit->Orders[0]->Goal->RefsIncrease();
196 } else {
197 DebugPrint("Unit %d did not find another resource.\n" _C_ unit->Slot);
198 unit->ClearAction();
199 unit->State = 0;
200 }
201 }
202
203 /**
204 ** Gather the resource
205 **
206 ** @param unit Harvester unit
207 */
208 static void GatherResource(CUnit *unit)
209 {
210 CUnit *source = unit->Orders[0]->Goal;
211 int amount[MaxCosts] = {1, 1};
212 bool visible = source->IsAliveOnMap();
213 int i;
214 const int totalEfficiency[MaxCosts] = {100, 100};
215 const int *efficiency = totalEfficiency;
216
217 AnimateActionHarvest(unit);
218
219 // Calculate how much we can harvest.
220 if (visible && UnitHoldsResources(source)) {
221 // Determine efficiency of recycling buildings
222 if (unit->Player == source->Player || unit->Player->IsAllied(source)) {
223 efficiency = AlliedUnitRecyclingEfficiency;
224 } else if (unit->Player->IsEnemy(source)) {
225 efficiency = EnemyUnitRecyclingEfficiency;
226 }
227
228 // Update ResourcesHeld based on HP
229 // Source might have been healed or attacked
230 // Is this the best place for this?
231 if (efficiency != totalEfficiency) {
232 int hp = source->Variable[HP_INDEX].Max *
233 source->ResourcesHeld[1] / source->Type->ProductionCosts[1];
234 if (hp != 0 && hp != source->Variable[HP_INDEX].Value) {
235 for (i = 0; i < MaxCosts; ++i) {
236 source->ResourcesHeld[i] = source->Type->ProductionCosts[i] *
237 source->Variable[HP_INDEX].Value / source->Variable[HP_INDEX].Max;
238 }
239 }
240 }
241
242 // Calculate new resource amounts
243 CalculateRequestedAmount(unit->Type, source->ResourcesHeld, amount);
244 for (i = 0; i < MaxCosts; ++i) {
245 unit->Player->ProductionRate[i] -= unit->Data.Harvest.CurrentProduction[i];
246 unit->Data.Harvest.CurrentProduction[i] = amount[i] * efficiency[i] / 100;
247 unit->Player->ProductionRate[i] += unit->Data.Harvest.CurrentProduction[i];
248 source->ResourcesHeld[i] -= amount[i];
249 }
250
251 // Recycling loses hit points
252 if (efficiency != totalEfficiency) {
253 source->Variable[HP_INDEX].Value = source->Variable[HP_INDEX].Max *
254 source->ResourcesHeld[1] / source->Type->ProductionCosts[1];
255 if (source->Variable[HP_INDEX].Value == 0 && source->ResourcesHeld[1] != 0) {
256 source->Variable[HP_INDEX].Value = 1;
257 }
258 }
259 }
260
261 //
262 // End of resource: destroy the resource.
263 //
264 if (!visible || !UnitHoldsResources(source)) {
265 if (unit->Anim.Unbreakable) {
266 // Wait until the animation finishes
267 return;
268 }
269
270 DebugPrint("Resource is destroyed for unit %d\n" _C_ unit->Slot);
271
272 for (i = 0; i < MaxCosts; ++i) {
273 unit->Player->ProductionRate[i] -= unit->Data.Harvest.CurrentProduction[i];
274 unit->Data.Harvest.CurrentProduction[i] = 0;
275 }
276
277 if (unit->OrderCount == 1) {
278 // Find a new resource to harvest
279 FindNewResource(unit);
280 } else {
281 unit->Orders[0]->Goal->RefsDecrease();
282 unit->Orders[0]->Goal = NoUnitP;
283 unit->ClearAction();
284 }
285
286 // Don't destroy the resource twice.
287 // This only happens when it's empty.
288 if (visible) {
289 LetUnitDie(source);
290 }
291 }
292 }
293
294 /**
295 ** Give up on gathering.
296 **
297 ** @param unit Pointer to unit.
298 */
299 static void ResourceGiveUp(CUnit *unit)
300 {
301 DebugPrint("Unit %d gave up on resource gathering.\n" _C_ unit->Slot);
302 if (unit->Orders[0]->Goal) {
303 unit->Orders[0]->Goal->RefsDecrease();
304 unit->Orders[0]->Goal = NULL;
305 }
306 unit->ClearAction();
307 }
308
309 /**
310 ** Control the unit action: getting a resource.
311 **
312 ** This is the generic function for harvesting resources
313 **
314 ** @param unit Pointer to unit.
315 */
316 void HandleActionResource(CUnit *unit)
317 {
318 if (unit->Wait) {
319 unit->Wait--;
320 return;
321 }
322
323 // Let's start mining.
324 if (unit->SubAction == SUB_START_RESOURCE) {
325 if (unit->Orders[0]->Goal->Type->CanHarvestFrom) {
326 NewResetPath(unit);
327 unit->SubAction = SUB_MOVE_TO_RESOURCE;
328 } else {
329 ResourceGiveUp(unit);
330 return;
331 }
332 }
333
334 // Move to the resource location.
335 if (unit->SubAction >= SUB_MOVE_TO_RESOURCE &&
336 unit->SubAction < SUB_UNREACHABLE_RESOURCE) {
337 int ret = MoveToResource(unit);
338 // -1 failure, 0 not yet reached, 1 reached
339 if (ret == -1) {
340 // Can't Reach
341 unit->SubAction++;
342 unit->Wait = 10;
343 return;
344 } else if (ret == 1) {
345 // Reached
346 unit->SubAction = SUB_START_GATHERING;
347 memset(unit->Data.Harvest.CurrentProduction, 0, sizeof(unit->Data.Harvest.CurrentProduction));
348 } else {
349 // Move along.
350 return;
351 }
352 }
353
354 // Resource seems to be unreachable
355 if (unit->SubAction == SUB_UNREACHABLE_RESOURCE) {
356 ResourceGiveUp(unit);
357 return;
358 }
359
360 // Start gathering the resource
361 if (unit->SubAction == SUB_START_GATHERING) {
362 if (StartGathering(unit)) {
363 unit->SubAction = SUB_GATHER_RESOURCE;
364 } else {
365 return;
366 }
367 }
368
369 // Gather the resource.
370 if (unit->SubAction == SUB_GATHER_RESOURCE) {
371 GatherResource(unit);
372 return;
373 }
374 }
375
376 //@}