2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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
44 static int do_upd_checking;
46 static void do_mob_land(struct lndstr *, int);
47 static void do_mob_plane(struct plnstr *, int);
48 static void do_mob_sect(struct sctstr *sp, int etus);
49 static void do_mob_ship(struct shpstr *, int);
52 sct_do_upd_mob(struct sctstr *sp)
56 if (do_upd_checking || update_running)
60 if (sp->sct_type == SCT_SANCT)
62 etus = game_tick_to_now(&sp->sct_access);
66 do_upd_checking = 1; /* avoid recursion */
67 do_mob_sect(sp, etus);
72 shp_do_upd_mob(struct shpstr *sp)
76 if (do_upd_checking || update_running)
80 etus = game_tick_to_now(&sp->shp_access);
84 do_upd_checking = 1; /* avoid recursion */
85 do_mob_ship(sp, etus);
90 lnd_do_upd_mob(struct lndstr *lp)
94 if (do_upd_checking || update_running)
98 etus = game_tick_to_now(&lp->lnd_access);
102 do_upd_checking = 1; /* avoid recursion */
103 do_mob_land(lp, etus);
108 pln_do_upd_mob(struct plnstr *pp)
112 if (do_upd_checking || update_running)
114 if (pp->pln_own == 0)
116 etus = game_tick_to_now(&pp->pln_access);
120 do_upd_checking = 1; /* avoid recursion */
121 do_mob_plane(pp, etus);
133 for (n = 0; NULL != (sp = getsectid(n)); n++) {
134 sp->sct_timestamp = now;
136 etus = game_reset_tick(&sp->sct_access);
138 etus = etu_per_update;
139 do_mob_sect(sp, etus);
144 do_mob_sect(struct sctstr *sp, int etus)
148 if (CANT_HAPPEN(etus < 0))
151 if (sp->sct_own == 0)
153 if (sp->sct_type == SCT_SANCT)
156 value = sp->sct_mobil + ((float)etus * sect_mob_scale);
157 if (value > sect_mob_max)
158 value = sect_mob_max;
159 sp->sct_mobil = value;
170 for (n = 0; NULL != (sp = getshipp(n)); n++) {
171 sp->shp_timestamp = now;
173 etus = game_reset_tick(&sp->shp_access);
175 etus = etu_per_update;
176 do_mob_ship(sp, etus);
181 do_mob_ship(struct shpstr *sp, int etus)
185 int can_add, have_fuel_for, total_add;
188 if (CANT_HAPPEN(etus < 0))
191 if (sp->shp_own == 0)
194 if (opt_FUEL == 0 || mchr[(int)sp->shp_type].m_fuelu == 0) {
195 value = sp->shp_mobil + (float)etus * ship_mob_scale;
196 if (value > ship_mob_max)
197 value = ship_mob_max;
198 sp->shp_mobil = (signed char)value;
200 if (sp->shp_mobil >= ship_mob_max) {
201 sp->shp_mobil = ship_mob_max;
204 can_add = ship_mob_max - sp->shp_mobil;
205 if (can_add > (float)etus * ship_mob_scale)
206 can_add = (float)etus * ship_mob_scale;
207 have_fuel_for = ldround(((double)sp->shp_fuel /
208 (double)mchr[(int)sp->shp_type].m_fuelu)
209 * (double)fuel_mult, 1);
211 if (can_add > have_fuel_for) {
213 need = can_add - have_fuel_for;
215 d *= mchr[(int)sp->shp_type].m_fuelu;
218 if (d - (int)d > 0.0)
221 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
223 sp->shp_fuel += newfuel * 5;
226 have_fuel_for = ldround(((double)sp->shp_fuel /
227 (double)mchr[(int)sp->shp_type].m_fuelu)
228 * (double)fuel_mult, 1);
230 if (can_add > have_fuel_for) {
232 need = can_add - have_fuel_for;
234 d *= mchr[(int)sp->shp_type].m_fuelu;
237 if (d - (int)d > 0.0)
240 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
242 sp->shp_fuel += newfuel * 50;
245 have_fuel_for = ldround(((double)sp->shp_fuel /
246 (double)mchr[(int)sp->shp_type].m_fuelu)
247 * (double)fuel_mult, 1);
249 if (can_add > have_fuel_for)
250 total_add = have_fuel_for;
254 d *= mchr[(int)sp->shp_type].m_fuelu;
256 sp->shp_fuel -= ldround(d, 1);
257 sp->shp_fuel = MIN(sp->shp_fuel, mchr[(int)sp->shp_type].m_fuelc);
258 sp->shp_mobil += total_add;
270 for (n = 0; NULL != (lp = getlandp(n)); n++) {
271 lp->lnd_timestamp = now;
273 etus = game_reset_tick(&lp->lnd_access);
275 etus = etu_per_update;
276 do_mob_land(lp, etus);
281 do_mob_land(struct lndstr *lp, int etus)
285 int can_add, have_fuel_for, total_add;
288 if (CANT_HAPPEN(etus < 0))
291 if (lp->lnd_own == 0)
294 if (opt_FUEL == 0 || lp->lnd_fuelu == 0) {
295 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
296 if (value > land_mob_max) {
297 if (lp->lnd_harden < land_mob_max && !opt_MOB_ACCESS) {
299 * Automatic fortification on excess mobility.
300 * Disabled for MOB_ACCESS, because it leads to
301 * excessively deep recursion and thus miserable
302 * performance as the number of land units grows.
304 * Provide mobility to be used in lnd_fortify()
305 * without overflowing lnd_mobil.
307 lp->lnd_mobil = land_mob_max;
308 lnd_fortify(lp, value - land_mob_max);
310 value = land_mob_max;
312 lp->lnd_mobil = value;
315 if (lp->lnd_mobil >= land_mob_max) {
316 lp->lnd_mobil = land_mob_max;
320 can_add = land_mob_max - lp->lnd_mobil;
322 if (can_add > (float)etus * land_mob_scale)
323 can_add = (float)etus * land_mob_scale;
325 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
327 if (can_add > have_fuel_for) {
329 need = can_add - have_fuel_for;
334 if (d - (int)d > 0.0)
337 newfuel = supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
339 lp->lnd_fuel += newfuel * 5;
342 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
344 if (can_add > have_fuel_for) {
346 need = can_add - have_fuel_for;
351 if (d - (int)d > 0.0)
354 newfuel = supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
356 lp->lnd_fuel += newfuel * 50;
359 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
361 if (can_add > have_fuel_for) {
362 total_add = have_fuel_for;
368 lp->lnd_fuel -= ldround(d, 1);
369 lp->lnd_fuel = MIN(lp->lnd_fuel, lp->lnd_fuelc);
370 lp->lnd_mobil += total_add;
371 /* No excess mobility here, hence no automatic fortification */
383 for (n = 0; NULL != (pp = getplanep(n)); n++) {
384 pp->pln_timestamp = now;
386 etus = game_reset_tick(&pp->pln_access);
388 etus = etu_per_update;
389 do_mob_plane(pp, etus);
394 do_mob_plane(struct plnstr *pp, int etus)
398 if (CANT_HAPPEN(etus < 0))
401 if (pp->pln_own == 0)
404 value = pp->pln_mobil + ((float)etus * plane_mob_scale);
405 if (value > plane_mob_max)
406 value = plane_mob_max;
407 pp->pln_mobil = value;