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
46 static int timestamp_fixing;
47 static int do_upd_checking = 0;
49 static void do_mob_land(struct lndstr *, int);
50 static void do_mob_plane(struct plnstr *, int);
51 static void do_mob_sect(struct sctstr *sp, int etus);
52 static void do_mob_ship(struct shpstr *, int);
55 increase_mob(time_t *counter, float mult)
66 secs = now - *counter;
67 if (secs < 1 || secs < s_p_etu)
69 newetus = (int)(secs / s_p_etu);
72 left = (secs % s_p_etu);
74 newmob = newetus * mult;
75 inewmob = (int)newmob;
76 if (newmob == inewmob || newetus > 7)
80 } while (newetus > 0);
84 *counter = now - left;
92 update_timestamps(time_t lastsavedtime)
97 struct plnstr *planep;
102 timestamp_fixing = 1;
104 delta = now - lastsavedtime;
105 for (n = 0; (shipp = getshipp(n)); n++)
106 shipp->shp_access += delta;
107 for (n = 0; (sectp = getsectid(n)); n++)
108 sectp->sct_access += delta;
109 for (n = 0; (landp = getlandp(n)); n++)
110 landp->lnd_access += delta;
111 for (n = 0; (planep = getplanep(n)); n++)
112 planep->pln_access += delta;
113 timestamp_fixing = 0;
119 struct shpstr *shipp;
120 struct sctstr *sectp;
121 struct lndstr *landp;
122 struct plnstr *planep;
129 sectp = getsectid(n);
131 planep = getplanep(n);
134 shp_do_upd_mob(shipp);
136 sct_do_upd_mob(sectp);
138 lnd_do_upd_mob(landp);
140 pln_do_upd_mob(planep);
141 if (!shipp && !sectp && !landp && !planep)
149 sct_do_upd_mob(struct sctstr *sp)
153 if (do_upd_checking || timestamp_fixing || update_pending)
155 if (sp->sct_own == 0)
157 if (sp->sct_type == SCT_SANCT)
159 if ((etus = increase_mob(&sp->sct_access, sect_mob_scale)) == 0)
162 do_mob_sect(sp, etus);
168 shp_do_upd_mob(struct shpstr *sp)
172 if (do_upd_checking || timestamp_fixing || update_pending)
174 if (sp->shp_own == 0)
176 if ((etus = increase_mob(&sp->shp_access, ship_mob_scale)) == 0)
179 do_mob_ship(sp, etus);
184 lnd_do_upd_mob(struct lndstr *lp)
188 if (do_upd_checking || timestamp_fixing || update_pending)
190 if (lp->lnd_own == 0)
192 if ((etus = increase_mob(&lp->lnd_access, land_mob_scale)) == 0)
196 do_mob_land(lp, etus);
201 pln_do_upd_mob(struct plnstr *pp)
205 if (do_upd_checking || timestamp_fixing || update_pending)
207 if (pp->pln_own == 0)
209 if ((etus = increase_mob(&pp->pln_access, plane_mob_scale)) == 0)
213 do_mob_plane(pp, etus);
225 for (n = 0; NULL != (sp = getsectid(n)); n++) {
226 sp->sct_timestamp = now;
230 do_mob_sect(sp, etus);
235 do_mob_sect(struct sctstr *sp, int etus)
239 if (sp->sct_own == 0)
241 if (sp->sct_type == SCT_SANCT)
244 value = sp->sct_mobil + ((float)etus * sect_mob_scale);
245 if (value > sect_mob_max)
246 value = sect_mob_max;
247 sp->sct_mobil = value;
258 for (n = 0; NULL != (sp = getshipp(n)); n++) {
259 sp->shp_timestamp = now;
263 do_mob_ship(sp, etus);
268 do_mob_ship(struct shpstr *sp, int etus)
272 int can_add, have_fuel_for, total_add;
275 if (sp->shp_own == 0)
278 if (opt_FUEL == 0 || mchr[(int)sp->shp_type].m_fuelu == 0) {
279 value = sp->shp_mobil + (float)etus * ship_mob_scale;
280 if (value > ship_mob_max)
281 value = ship_mob_max;
282 sp->shp_mobil = (signed char)value;
284 if (sp->shp_mobil >= ship_mob_max) {
285 sp->shp_mobil = ship_mob_max;
288 can_add = ship_mob_max - sp->shp_mobil;
289 if (can_add > (float)etus * ship_mob_scale)
290 can_add = (float)etus * ship_mob_scale;
291 have_fuel_for = ldround(((double)sp->shp_fuel /
292 (double)mchr[(int)sp->shp_type].m_fuelu)
293 * (double)fuel_mult, 1);
295 if (can_add > have_fuel_for) {
297 need = can_add - have_fuel_for;
299 d *= mchr[(int)sp->shp_type].m_fuelu;
302 if (d - (int)d > 0.0)
305 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
307 sp->shp_fuel += newfuel * 5;
310 have_fuel_for = ldround(((double)sp->shp_fuel /
311 (double)mchr[(int)sp->shp_type].m_fuelu)
312 * (double)fuel_mult, 1);
314 if (can_add > have_fuel_for) {
316 need = can_add - have_fuel_for;
318 d *= mchr[(int)sp->shp_type].m_fuelu;
321 if (d - (int)d > 0.0)
324 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
326 sp->shp_fuel += newfuel * 50;
329 have_fuel_for = ldround(((double)sp->shp_fuel /
330 (double)mchr[(int)sp->shp_type].m_fuelu)
331 * (double)fuel_mult, 1);
333 if (can_add > have_fuel_for)
334 total_add = have_fuel_for;
338 d *= mchr[(int)sp->shp_type].m_fuelu;
340 sp->shp_fuel -= ldround(d, 1);
341 sp->shp_fuel = MIN(sp->shp_fuel, mchr[(int)sp->shp_type].m_fuelc);
342 sp->shp_mobil += total_add;
354 for (n = 0; NULL != (lp = getlandp(n)); n++) {
355 lp->lnd_timestamp = now;
359 do_mob_land(lp, etus);
364 do_mob_land(struct lndstr *lp, int etus)
368 int can_add, have_fuel_for, total_add;
371 if (lp->lnd_own == 0)
374 if (opt_FUEL == 0 || lp->lnd_fuelu == 0) {
375 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
376 if (value > land_mob_max) {
377 if (lp->lnd_harden < land_mob_max && !opt_MOB_ACCESS) {
379 * Automatic fortification on excess mobility.
380 * Disabled for MOB_ACCESS, because it leads to
381 * excessively deep recursion and thus miserable
382 * performance as the number of land units grows.
384 * Provide mobility to be used in lnd_fortify()
385 * without overflowing lnd_mobil.
387 lp->lnd_mobil = land_mob_max;
388 lnd_fortify(lp, value - land_mob_max);
390 value = land_mob_max;
392 lp->lnd_mobil = value;
395 if (lp->lnd_mobil >= land_mob_max) {
396 lp->lnd_mobil = land_mob_max;
400 can_add = land_mob_max - lp->lnd_mobil;
402 if (can_add > (float)etus * land_mob_scale)
403 can_add = (float)etus * land_mob_scale;
405 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
407 if (can_add > have_fuel_for) {
409 need = can_add - have_fuel_for;
414 if (d - (int)d > 0.0)
417 newfuel = supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
419 lp->lnd_fuel += newfuel * 5;
422 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
424 if (can_add > have_fuel_for) {
426 need = can_add - have_fuel_for;
431 if (d - (int)d > 0.0)
434 newfuel = supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
436 lp->lnd_fuel += newfuel * 50;
439 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
441 if (can_add > have_fuel_for) {
442 total_add = have_fuel_for;
448 lp->lnd_fuel -= ldround(d, 1);
449 lp->lnd_fuel = MIN(lp->lnd_fuel, lp->lnd_fuelc);
450 lp->lnd_mobil += total_add;
451 /* No excess mobility here, hence no automatic fortification */
463 for (n = 0; NULL != (pp = getplanep(n)); n++) {
464 pp->pln_timestamp = now;
468 do_mob_plane(pp, etus);
473 do_mob_plane(struct plnstr *pp, int etus)
477 if (pp->pln_own == 0)
480 value = pp->pln_mobil + ((float)etus * plane_mob_scale);
481 if (value > plane_mob_max)
482 value = plane_mob_max;
483 pp->pln_mobil = value;