2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
54 static int timestamp_fixing;
55 static int do_upd_checking = 0;
57 static void do_mob_land(struct lndstr *, int);
58 static void do_mob_plane(struct plnstr *, int);
59 static void do_mob_sect(struct sctstr *sp, int etus);
60 static void do_mob_ship(struct shpstr *, int);
63 increase_mob(time_t *counter, float mult)
74 secs = now - *counter;
75 if (secs < 1 || secs < s_p_etu)
77 newetus = (int)(secs / s_p_etu);
80 left = (secs % s_p_etu);
82 newmob = newetus * mult;
83 inewmob = (int)newmob;
84 if (newmob == inewmob || newetus > 7)
88 } while (newetus > 0);
92 *counter = now - left;
100 update_timestamps(time_t lastsavedtime)
102 struct shpstr *shipp;
103 struct sctstr *sectp;
104 struct lndstr *landp;
105 struct plnstr *planep;
110 timestamp_fixing = 1;
112 delta = now - lastsavedtime;
113 for (n = 0; (shipp = getshipp(n)); n++)
114 shipp->shp_access += delta;
115 for (n = 0; (sectp = getsectid(n)); n++)
116 sectp->sct_access += delta;
117 for (n = 0; (landp = getlandp(n)); n++)
118 landp->lnd_access += delta;
119 for (n = 0; (planep = getplanep(n)); n++)
120 planep->pln_access += delta;
121 timestamp_fixing = 0;
127 struct shpstr *shipp;
128 struct sctstr *sectp;
129 struct lndstr *landp;
130 struct plnstr *planep;
137 sectp = getsectid(n);
139 planep = getplanep(n);
142 shp_do_upd_mob(shipp);
144 sct_do_upd_mob(sectp);
146 lnd_do_upd_mob(landp);
148 pln_do_upd_mob(planep);
149 if (!shipp && !sectp && !landp && !planep)
157 sct_do_upd_mob(struct sctstr *sp)
161 if (do_upd_checking || timestamp_fixing || update_pending)
163 if (sp->sct_own == 0)
165 if (sp->sct_type == SCT_SANCT)
167 if ((etus = increase_mob(&sp->sct_access, sect_mob_scale)) == 0)
170 do_mob_sect(sp, etus);
176 shp_do_upd_mob(struct shpstr *sp)
180 if (do_upd_checking || timestamp_fixing || update_pending)
182 if (sp->shp_own == 0)
184 if ((etus = increase_mob(&sp->shp_access, ship_mob_scale)) == 0)
187 do_mob_ship(sp, etus);
192 lnd_do_upd_mob(struct lndstr *lp)
196 if (do_upd_checking || timestamp_fixing || update_pending)
198 if (lp->lnd_own == 0)
200 if ((etus = increase_mob(&lp->lnd_access, land_mob_scale)) == 0)
204 do_mob_land(lp, etus);
209 pln_do_upd_mob(struct plnstr *pp)
213 if (do_upd_checking || timestamp_fixing || update_pending)
215 if (pp->pln_own == 0)
217 if ((etus = increase_mob(&pp->pln_access, plane_mob_scale)) == 0)
221 do_mob_plane(pp, etus);
233 for (n = 0; NULL != (sp = getsectid(n)); n++) {
234 sp->sct_timestamp = now;
238 do_mob_sect(sp, etus);
243 do_mob_sect(struct sctstr *sp, int etus)
247 if (sp->sct_own == 0)
249 if (sp->sct_type == SCT_SANCT)
251 /* Do we have to even bother? */
252 if (sp->sct_mobil >= sect_mob_max) {
253 /* No, so set just in case and then return */
254 sp->sct_mobil = sect_mob_max;
257 value = sp->sct_mobil + ((float)etus * sect_mob_scale);
258 if (value > sect_mob_max)
259 value = sect_mob_max;
260 sp->sct_mobil = value;
271 for (n = 0; NULL != (sp = getshipp(n)); n++) {
272 sp->shp_timestamp = now;
276 do_mob_ship(sp, etus);
281 do_mob_ship(struct shpstr *sp, int etus)
285 int can_add, have_fuel_for, total_add;
288 if (sp->shp_own == 0)
291 /* Do we even have to bother updating this mobility? */
292 if (sp->shp_mobil >= ship_mob_max) {
293 /* No, so don't. Just set it to max (just in case) and
295 sp->shp_mobil = ship_mob_max;
299 if (opt_FUEL == 0) { /* only a bit to do ... */
300 value = sp->shp_mobil + ((float)etus * ship_mob_scale);
301 if (value > ship_mob_max)
302 value = ship_mob_max;
303 sp->shp_mobil = value;
304 return; /* so we ship the FUEL stuff */
307 /* opt_FUEL in force */
308 if (mchr[(int)sp->shp_type].m_fuelu == 0) {
309 value = sp->shp_mobil + ((float)etus * ship_mob_scale);
310 if (value > ship_mob_max)
311 value = ship_mob_max;
312 sp->shp_mobil = (signed char)value;
314 can_add = ship_mob_max - sp->shp_mobil;
315 if (can_add > ((float)etus * ship_mob_scale))
316 can_add = ((float)etus * ship_mob_scale);
317 have_fuel_for = ldround((((double)sp->shp_fuel /
318 (double)mchr[(int)sp->shp_type].m_fuelu)
319 * (double)fuel_mult), 1);
321 if (can_add > have_fuel_for) {
323 need = can_add - have_fuel_for;
325 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
326 d /= (double)fuel_mult;
328 if ((d - (int)d) > 0.0)
331 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
333 sp->shp_fuel += newfuel * 5;
336 have_fuel_for = ldround((((double)sp->shp_fuel /
337 (double)mchr[(int)sp->shp_type].m_fuelu)
338 * (double)fuel_mult), 1);
340 if (can_add > have_fuel_for) {
342 need = can_add - have_fuel_for;
344 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
345 d /= (double)fuel_mult;
347 if ((d - (int)d) > 0.0)
350 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
352 sp->shp_fuel += newfuel * 50;
355 have_fuel_for = ldround((((double)sp->shp_fuel /
356 (double)mchr[(int)sp->shp_type].m_fuelu)
357 * (double)fuel_mult), 1);
359 if (can_add > have_fuel_for)
360 total_add = have_fuel_for;
363 d = (double)total_add;
364 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
365 d /= (double)fuel_mult;
366 sp->shp_fuel -= ldround(d, 1);
367 sp->shp_fuel = MIN(sp->shp_fuel, mchr[(int)sp->shp_type].m_fuelc);
368 sp->shp_mobil += total_add;
380 for (n = 0; NULL != (lp = getlandp(n)); n++) {
381 lp->lnd_timestamp = now;
385 do_mob_land(lp, etus);
390 do_mob_land(struct lndstr *lp, int etus)
394 int can_add, have_fuel_for, total_add;
397 if (lp->lnd_own == 0)
400 if (lp->lnd_mobil >= land_mob_max) {
401 lp->lnd_mobil = land_mob_max;
402 if (lp->lnd_harden >= land_mob_max) {
403 lp->lnd_harden = land_mob_max;
408 if (opt_FUEL == 0) { /* just some bits and pieces */
409 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
410 if (value > land_mob_max) {
411 lnd_fortify(lp, value - land_mob_max);
412 value = land_mob_max;
414 lp->lnd_mobil = value;
419 /* opt_FUEL in force ... */
420 if (lp->lnd_fuelu == 0) {
421 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
422 if (value > land_mob_max) {
423 lnd_fortify(lp, value - land_mob_max);
424 value = land_mob_max;
426 lp->lnd_mobil = value;
430 can_add = land_mob_max - lp->lnd_mobil;
432 if (can_add > ((float)etus * land_mob_scale))
433 can_add = ((float)etus * land_mob_scale);
435 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
437 if (can_add > have_fuel_for) {
439 need = can_add - have_fuel_for;
441 d *= (double)lp->lnd_fuelu;
442 d /= (double)fuel_mult;
444 if ((d - (int)d) > 0.0)
447 newfuel = supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
449 lp->lnd_fuel += newfuel * 5;
452 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
454 if (can_add > have_fuel_for) {
456 need = can_add - have_fuel_for;
458 d *= (double)lp->lnd_fuelu;
459 d /= (double)fuel_mult;
461 if ((d - (int)d) > 0.0)
464 newfuel = supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
466 lp->lnd_fuel += newfuel * 50;
469 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
471 if (can_add > have_fuel_for) {
472 total_add = have_fuel_for;
475 d = (double)total_add;
476 d *= (double)lp->lnd_fuelu;
477 d /= (double)fuel_mult;
478 lp->lnd_fuel -= ldround(d, 1);
479 lp->lnd_fuel = MIN(lp->lnd_fuel, lp->lnd_fuelc);
480 lp->lnd_mobil += total_add;
481 /* No excess mobility here, hence no automatic fortification */
493 for (n = 0; NULL != (pp = getplanep(n)); n++) {
494 pp->pln_timestamp = now;
498 do_mob_plane(pp, etus);
503 do_mob_plane(struct plnstr *pp, int etus)
507 if (pp->pln_own == 0)
509 if (pp->pln_mobil >= plane_mob_max) {
510 pp->pln_mobil = plane_mob_max;
514 value = pp->pln_mobil + ((float)etus * plane_mob_scale);
515 if (value > plane_mob_max)
516 value = plane_mob_max;
517 pp->pln_mobil = value;