2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * mobility.c: Add mobility to each of the items which accumulate mobility.
30 * Known contributors to this file:
32 * Steve McClure, 1998-1999
33 * Markus Armbruster, 2004-2007
45 static int do_upd_checking;
47 static void do_mob_land(struct lndstr *, int);
48 static void do_mob_plane(struct plnstr *, int);
49 static void do_mob_sect(struct sctstr *sp, int etus);
50 static void do_mob_ship(struct shpstr *, int);
53 sct_do_upd_mob(struct sctstr *sp)
57 if (do_upd_checking || update_running)
61 if (sp->sct_type == SCT_SANCT)
63 etus = game_tick_to_now(&sp->sct_access);
67 do_upd_checking = 1; /* avoid recursion */
68 do_mob_sect(sp, etus);
73 shp_do_upd_mob(struct shpstr *sp)
77 if (do_upd_checking || update_running)
81 etus = game_tick_to_now(&sp->shp_access);
85 do_upd_checking = 1; /* avoid recursion */
86 do_mob_ship(sp, etus);
91 lnd_do_upd_mob(struct lndstr *lp)
95 if (do_upd_checking || update_running)
99 etus = game_tick_to_now(&lp->lnd_access);
103 do_upd_checking = 1; /* avoid recursion */
104 do_mob_land(lp, etus);
109 pln_do_upd_mob(struct plnstr *pp)
113 if (do_upd_checking || update_running)
115 if (pp->pln_own == 0)
117 etus = game_tick_to_now(&pp->pln_access);
121 do_upd_checking = 1; /* avoid recursion */
122 do_mob_plane(pp, etus);
134 for (n = 0; NULL != (sp = getsectid(n)); n++) {
135 sp->sct_timestamp = now;
137 etus = game_reset_tick(&sp->sct_access);
139 etus = etu_per_update;
140 do_mob_sect(sp, etus);
145 do_mob_sect(struct sctstr *sp, int etus)
149 if (CANT_HAPPEN(etus < 0))
152 if (sp->sct_own == 0)
154 if (sp->sct_type == SCT_SANCT)
157 value = sp->sct_mobil + ((float)etus * sect_mob_scale);
158 if (value > sect_mob_max)
159 value = sect_mob_max;
160 sp->sct_mobil = value;
171 for (n = 0; NULL != (sp = getshipp(n)); n++) {
172 sp->shp_timestamp = now;
174 etus = game_reset_tick(&sp->shp_access);
176 etus = etu_per_update;
177 do_mob_ship(sp, etus);
182 do_mob_ship(struct shpstr *sp, int etus)
186 int can_add, have_fuel_for, total_add;
189 if (CANT_HAPPEN(etus < 0))
192 if (sp->shp_own == 0)
195 if (opt_FUEL == 0 || mchr[(int)sp->shp_type].m_fuelu == 0) {
196 value = sp->shp_mobil + (float)etus * ship_mob_scale;
197 if (value > ship_mob_max)
198 value = ship_mob_max;
199 sp->shp_mobil = (signed char)value;
201 if (sp->shp_mobil >= ship_mob_max) {
202 sp->shp_mobil = ship_mob_max;
205 can_add = ship_mob_max - sp->shp_mobil;
206 if (can_add > (float)etus * ship_mob_scale)
207 can_add = (float)etus * ship_mob_scale;
208 have_fuel_for = ldround(((double)sp->shp_fuel /
209 (double)mchr[(int)sp->shp_type].m_fuelu)
210 * (double)fuel_mult, 1);
212 if (can_add > have_fuel_for) {
214 need = can_add - have_fuel_for;
216 d *= mchr[(int)sp->shp_type].m_fuelu;
219 if (d - (int)d > 0.0)
222 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
224 sp->shp_fuel += newfuel * 5;
227 have_fuel_for = ldround(((double)sp->shp_fuel /
228 (double)mchr[(int)sp->shp_type].m_fuelu)
229 * (double)fuel_mult, 1);
231 if (can_add > have_fuel_for) {
233 need = can_add - have_fuel_for;
235 d *= mchr[(int)sp->shp_type].m_fuelu;
238 if (d - (int)d > 0.0)
241 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
243 sp->shp_fuel += newfuel * 50;
246 have_fuel_for = ldround(((double)sp->shp_fuel /
247 (double)mchr[(int)sp->shp_type].m_fuelu)
248 * (double)fuel_mult, 1);
250 if (can_add > have_fuel_for)
251 total_add = have_fuel_for;
255 d *= mchr[(int)sp->shp_type].m_fuelu;
257 sp->shp_fuel -= ldround(d, 1);
258 sp->shp_fuel = MIN(sp->shp_fuel, mchr[(int)sp->shp_type].m_fuelc);
259 sp->shp_mobil += total_add;
271 for (n = 0; NULL != (lp = getlandp(n)); n++) {
272 lp->lnd_timestamp = now;
274 etus = game_reset_tick(&lp->lnd_access);
276 etus = etu_per_update;
277 do_mob_land(lp, etus);
282 do_mob_land(struct lndstr *lp, int etus)
286 int can_add, have_fuel_for, total_add;
289 if (CANT_HAPPEN(etus < 0))
292 if (lp->lnd_own == 0)
295 if (opt_FUEL == 0 || lp->lnd_fuelu == 0) {
296 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
297 if (value > land_mob_max) {
298 if (lp->lnd_harden < land_mob_max && !opt_MOB_ACCESS) {
300 * Automatic fortification on excess mobility.
301 * Disabled for MOB_ACCESS, because it leads to
302 * excessively deep recursion and thus miserable
303 * performance as the number of land units grows.
305 * Provide mobility to be used in lnd_fortify()
306 * without overflowing lnd_mobil.
308 lp->lnd_mobil = land_mob_max;
309 lnd_fortify(lp, value - land_mob_max);
311 value = land_mob_max;
313 lp->lnd_mobil = value;
316 if (lp->lnd_mobil >= land_mob_max) {
317 lp->lnd_mobil = land_mob_max;
321 can_add = land_mob_max - lp->lnd_mobil;
323 if (can_add > (float)etus * land_mob_scale)
324 can_add = (float)etus * land_mob_scale;
326 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
328 if (can_add > have_fuel_for) {
330 need = can_add - have_fuel_for;
335 if (d - (int)d > 0.0)
338 newfuel = supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
340 lp->lnd_fuel += newfuel * 5;
343 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
345 if (can_add > have_fuel_for) {
347 need = can_add - have_fuel_for;
352 if (d - (int)d > 0.0)
355 newfuel = supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
357 lp->lnd_fuel += newfuel * 50;
360 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
362 if (can_add > have_fuel_for) {
363 total_add = have_fuel_for;
369 lp->lnd_fuel -= ldround(d, 1);
370 lp->lnd_fuel = MIN(lp->lnd_fuel, lp->lnd_fuelc);
371 lp->lnd_mobil += total_add;
372 /* No excess mobility here, hence no automatic fortification */
384 for (n = 0; NULL != (pp = getplanep(n)); n++) {
385 pp->pln_timestamp = now;
387 etus = game_reset_tick(&pp->pln_access);
389 etus = etu_per_update;
390 do_mob_plane(pp, etus);
395 do_mob_plane(struct plnstr *pp, int etus)
399 if (CANT_HAPPEN(etus < 0))
402 if (pp->pln_own == 0)
405 value = pp->pln_mobil + ((float)etus * plane_mob_scale);
406 if (value > plane_mob_max)
407 value = plane_mob_max;
408 pp->pln_mobil = value;