File: action_resource.cpp

package info (click to toggle)
boswars 2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 96,652 kB
  • sloc: cpp: 57,250; python: 1,715; sh: 25; makefile: 17
file content (376 lines) | stat: -rw-r--r-- 10,339 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//     ____                _       __               
//    / __ )____  _____   | |     / /___ ___________
//   / __  / __ \/ ___/   | | /| / / __ `/ ___/ ___/
//  / /_/ / /_/ (__  )    | |/ |/ / /_/ / /  (__  ) 
// /_____/\____/____/     |__/|__/\__,_/_/  /____/  
//                                              
//       A futuristic real-time strategy game.
//          This file is part of Bos Wars.
//
/**@name action_resource.cpp - The generic resource action. */
//
//      (c) Copyright 2001-2008 by Crestez Leonard and Jimmy Salmon
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; only version 2 of the License.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//      02111-1307, USA.
//

//@{

/*----------------------------------------------------------------------------
--  Includes
----------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "stratagus.h"
#include "player.h"
#include "unit.h"
#include "unittype.h"
#include "animation.h"
#include "actions.h"
#include "pathfinder.h"
#include "interface.h"
#include "sound.h"

/*----------------------------------------------------------------------------
--  Declarations
----------------------------------------------------------------------------*/

#define SUB_START_RESOURCE 0
#define SUB_MOVE_TO_RESOURCE 5
#define SUB_UNREACHABLE_RESOURCE 31
#define SUB_START_GATHERING 55
#define SUB_GATHER_RESOURCE 60

int AlliedUnitRecyclingEfficiency[MaxCosts] = {0, 50};
int EnemyUnitRecyclingEfficiency[MaxCosts] = {0, 40};

/*----------------------------------------------------------------------------
--  Functions
----------------------------------------------------------------------------*/

/**
**  Move unit to resource.
**
**  @param unit  Pointer to unit.
**
**  @return      -1 if unreachable, 0 if moving, 1 if reached.
*/
static int MoveToResource(CUnit *unit)
{
	switch (DoActionMove(unit)) { // reached end-point?
		case PF_UNREACHABLE:
			return -1;
		case PF_REACHED:
			break;
		default:
			// Goal gone or something.
			if (unit->Anim.Unbreakable || unit->Orders[0]->Goal->IsVisibleAsGoal(unit->Player)) {
				return 0;
			}
			break;
	}
	return 1;
}

/**
**  Start harvesting the resource.
**
**  @param unit  Pointer to unit.
**
**  @return      TRUE if ready, otherwise FALSE.
*/
static bool StartGathering(CUnit *unit)
{
	CUnit *goal = unit->Orders[0]->Goal;

	Assert(!unit->IX && !unit->IY);

	//
	// Target is gone, stop getting resources.
	//
	if (!goal->IsVisibleAsGoal(unit->Player)) {
		goal->RefsDecrease();
		// Find an alternative, but don't look too far.
		unit->Orders[0]->X = unit->Orders[0]->Y = -1;
		if ((goal = UnitFindResource(unit, unit->X, unit->Y, 10))) {
			unit->SubAction = SUB_START_RESOURCE;
			unit->Orders[0]->Goal = goal;
			goal->RefsIncrease();
		} else {
			unit->ClearAction();
			unit->Orders[0]->Goal = NoUnitP;
		}
		return false;
	}

	// 0 can happen, for example with flying units.
	Assert(MapDistanceBetweenUnits(unit, goal) <= 1);

	//
	// Update the heading of a harvesting unit to look straight at the resource.
	//
	UnitHeadingFromDeltaXY(unit,
		goal->X + (goal->Type->TileWidth - 1) / 2 - unit->X,
		goal->Y + (goal->Type->TileHeight - 1) / 2 - unit->Y);

	return true;
}

/**
**  Animate a unit that is harvesting
**
**  @param unit  Unit to animate
*/
static void AnimateActionHarvest(CUnit *unit)
{
	Assert(unit->Type->Animations->Harvest);
	UnitShowAnimation(unit, unit->Type->Animations->Harvest);
}

/**
**  Check which type of resource was harvested from a unit,
**  so that the caller can look for more of the same.
**
**  @param exhausted  Unit from which resources were harvested.
**
**  @return           Either enum CostType, or -1 if the resource
**                    type could not be identified.
*/
static int HarvestedResourceType(const CUnit *exhausted)
{
	int found = -1;
	for (int res = 0; res < MaxCosts; res++) {
		// exhausted->ResourcesHeld[res] is presumably zero
		// already so check the unit type instead.
		if (exhausted->Type->ProductionCosts[res] > 0) {
			if (found == -1) {
				found = res;
			} else {
				// It contains more than one type of
				// resource.  We could find the
				// largest number but the amounts are
				// not really comparable.
				return -1;
			}
		}
	}

	// The unit type contains just one type of resource, or none
	// at all.  In the latter case, found == -1 still.
	return found;
}

/**
**  Find something else to do when the resource is exhausted.
**  This is called from GatherResource when the resource is empty.
**
**  @param unit  Harvester unit
*/
static void FindNewResource(CUnit *unit)
{
	const int res = HarvestedResourceType(unit->Orders[0]->Goal);
	unit->Orders[0]->Goal->RefsDecrease();
	unit->Orders[0]->Goal = NoUnitP;

	unit->Orders[0]->X = unit->Orders[0]->Y = -1;
	if ((unit->Orders[0]->Goal = UnitFindResource(unit, unit->X, unit->Y, 10, res))) {
		DebugPrint("Unit %d found another resource.\n" _C_ unit->Slot);
		unit->SubAction = SUB_START_RESOURCE;
		unit->State = 0;
		unit->Orders[0]->Goal->RefsIncrease();
	} else {
		DebugPrint("Unit %d did not find another resource.\n" _C_ unit->Slot);
		unit->ClearAction();
		unit->State = 0;
	}
}

/**
**  Gather the resource
**
**  @param unit  Harvester unit
*/
static void GatherResource(CUnit *unit)
{
	CUnit *source = unit->Orders[0]->Goal;
	int amount[MaxCosts] = {1, 1};
	bool visible = source->IsAliveOnMap();
	int i;
	const int totalEfficiency[MaxCosts] = {100, 100};
	const int *efficiency = totalEfficiency;

	AnimateActionHarvest(unit);

	// Calculate how much we can harvest.
	if (visible && UnitHoldsResources(source)) {
		// Determine efficiency of recycling buildings
		if (unit->Player == source->Player || unit->Player->IsAllied(source)) {
			efficiency = AlliedUnitRecyclingEfficiency;
		} else if (unit->Player->IsEnemy(source)) {
			efficiency = EnemyUnitRecyclingEfficiency;
		}

		// Update ResourcesHeld based on HP
		// Source might have been healed or attacked
		// Is this the best place for this?
		if (efficiency != totalEfficiency) {
			int hp = source->Variable[HP_INDEX].Max *
				source->ResourcesHeld[1] / source->Type->ProductionCosts[1];
			if (hp != 0 && hp != source->Variable[HP_INDEX].Value) {
				for (i = 0; i < MaxCosts; ++i) {
					source->ResourcesHeld[i] = source->Type->ProductionCosts[i] *
						source->Variable[HP_INDEX].Value / source->Variable[HP_INDEX].Max;
				}
			}
		}

		// Calculate new resource amounts
		CalculateRequestedAmount(unit->Type, source->ResourcesHeld, amount);
		for (i = 0; i < MaxCosts; ++i) {
			unit->Player->ProductionRate[i] -= unit->Data.Harvest.CurrentProduction[i];
			unit->Data.Harvest.CurrentProduction[i] = amount[i] * efficiency[i] / 100;
			unit->Player->ProductionRate[i] += unit->Data.Harvest.CurrentProduction[i];
			source->ResourcesHeld[i] -= amount[i];
		}

		// Recycling loses hit points
		if (efficiency != totalEfficiency) {
			source->Variable[HP_INDEX].Value = source->Variable[HP_INDEX].Max *
				source->ResourcesHeld[1] / source->Type->ProductionCosts[1];
			if (source->Variable[HP_INDEX].Value == 0 && source->ResourcesHeld[1] != 0) {
				source->Variable[HP_INDEX].Value = 1;
			}
		}
	}

	//
	// End of resource: destroy the resource.
	//
	if (!visible || !UnitHoldsResources(source)) {
		if (unit->Anim.Unbreakable) {
			// Wait until the animation finishes
			return;
		}

		DebugPrint("Resource is destroyed for unit %d\n" _C_ unit->Slot);

		for (i = 0; i < MaxCosts; ++i) {
			unit->Player->ProductionRate[i] -= unit->Data.Harvest.CurrentProduction[i];
			unit->Data.Harvest.CurrentProduction[i] = 0;
		}

		if (unit->OrderCount == 1) {
			// Find a new resource to harvest
			FindNewResource(unit);
		} else {
			unit->Orders[0]->Goal->RefsDecrease();
			unit->Orders[0]->Goal = NoUnitP;
			unit->ClearAction();
		}

		// Don't destroy the resource twice.
		// This only happens when it's empty.
		if (visible) {
			LetUnitDie(source);
		}
	}
}

/**
**  Give up on gathering.
**
**  @param unit  Pointer to unit.
*/
static void ResourceGiveUp(CUnit *unit)
{
	DebugPrint("Unit %d gave up on resource gathering.\n" _C_ unit->Slot);
	if (unit->Orders[0]->Goal) {
		unit->Orders[0]->Goal->RefsDecrease();
		unit->Orders[0]->Goal = NULL;
	}
	unit->ClearAction();
}

/**
**  Control the unit action: getting a resource.
**
**  This is the generic function for harvesting resources
**
**  @param unit  Pointer to unit.
*/
void HandleActionResource(CUnit *unit)
{
	if (unit->Wait) {
		unit->Wait--;
		return;
	}

	// Let's start mining.
	if (unit->SubAction == SUB_START_RESOURCE) {
		if (unit->Orders[0]->Goal->Type->CanHarvestFrom) {
			NewResetPath(unit);
			unit->SubAction = SUB_MOVE_TO_RESOURCE;
		} else {
			ResourceGiveUp(unit);
			return;
		}
	}

	// Move to the resource location.
	if (unit->SubAction >= SUB_MOVE_TO_RESOURCE &&
			unit->SubAction < SUB_UNREACHABLE_RESOURCE) {
		int ret = MoveToResource(unit);
		// -1 failure, 0 not yet reached, 1 reached
		if (ret == -1) {
			// Can't Reach
			unit->SubAction++;
			unit->Wait = 10;
			return;
		} else if (ret == 1) {
			// Reached
			unit->SubAction = SUB_START_GATHERING;
			memset(unit->Data.Harvest.CurrentProduction, 0, sizeof(unit->Data.Harvest.CurrentProduction));
		} else {
			// Move along.
			return;
		}
	}

	// Resource seems to be unreachable
	if (unit->SubAction == SUB_UNREACHABLE_RESOURCE) {
		ResourceGiveUp(unit);
		return;
	}

	// Start gathering the resource
	if (unit->SubAction == SUB_START_GATHERING) {
		if (StartGathering(unit)) {
			unit->SubAction = SUB_GATHER_RESOURCE;
		} else {
			return;
		}
	}

	// Gather the resource.
	if (unit->SubAction == SUB_GATHER_RESOURCE) {
		GatherResource(unit);
		return;
	}
}

//@}