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)
243 /* Do we have to even bother? */
244 if (sp->sct_mobil >= sect_mob_max) {
245 /* No, so set just in case and then return */
246 sp->sct_mobil = sect_mob_max;
249 value = sp->sct_mobil + ((float)etus * sect_mob_scale);
250 if (value > sect_mob_max)
251 value = sect_mob_max;
252 sp->sct_mobil = value;
263 for (n = 0; NULL != (sp = getshipp(n)); n++) {
264 sp->shp_timestamp = now;
268 do_mob_ship(sp, etus);
273 do_mob_ship(struct shpstr *sp, int etus)
277 int can_add, have_fuel_for, total_add;
280 if (sp->shp_own == 0)
283 /* Do we even have to bother updating this mobility? */
284 if (sp->shp_mobil >= ship_mob_max) {
285 /* No, so don't. Just set it to max (just in case) and
287 sp->shp_mobil = ship_mob_max;
291 if (opt_FUEL == 0) { /* only a bit to do ... */
292 value = sp->shp_mobil + ((float)etus * ship_mob_scale);
293 if (value > ship_mob_max)
294 value = ship_mob_max;
295 sp->shp_mobil = value;
296 return; /* so we ship the FUEL stuff */
299 /* opt_FUEL in force */
300 if (mchr[(int)sp->shp_type].m_fuelu == 0) {
301 value = sp->shp_mobil + (float)etus * ship_mob_scale;
302 if (value > ship_mob_max)
303 value = ship_mob_max;
304 sp->shp_mobil = (signed char)value;
306 can_add = ship_mob_max - sp->shp_mobil;
307 if (can_add > (float)etus * ship_mob_scale)
308 can_add = (float)etus * ship_mob_scale;
309 have_fuel_for = ldround(((double)sp->shp_fuel /
310 (double)mchr[(int)sp->shp_type].m_fuelu)
311 * (double)fuel_mult, 1);
313 if (can_add > have_fuel_for) {
315 need = can_add - have_fuel_for;
317 d *= mchr[(int)sp->shp_type].m_fuelu;
320 if (d - (int)d > 0.0)
323 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
325 sp->shp_fuel += newfuel * 5;
328 have_fuel_for = ldround(((double)sp->shp_fuel /
329 (double)mchr[(int)sp->shp_type].m_fuelu)
330 * (double)fuel_mult, 1);
332 if (can_add > have_fuel_for) {
334 need = can_add - have_fuel_for;
336 d *= mchr[(int)sp->shp_type].m_fuelu;
339 if (d - (int)d > 0.0)
342 newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
344 sp->shp_fuel += newfuel * 50;
347 have_fuel_for = ldround(((double)sp->shp_fuel /
348 (double)mchr[(int)sp->shp_type].m_fuelu)
349 * (double)fuel_mult, 1);
351 if (can_add > have_fuel_for)
352 total_add = have_fuel_for;
356 d *= mchr[(int)sp->shp_type].m_fuelu;
358 sp->shp_fuel -= ldround(d, 1);
359 sp->shp_fuel = MIN(sp->shp_fuel, mchr[(int)sp->shp_type].m_fuelc);
360 sp->shp_mobil += total_add;
372 for (n = 0; NULL != (lp = getlandp(n)); n++) {
373 lp->lnd_timestamp = now;
377 do_mob_land(lp, etus);
382 do_mob_land(struct lndstr *lp, int etus)
386 int can_add, have_fuel_for, total_add;
389 if (lp->lnd_own == 0)
392 if (lp->lnd_mobil >= land_mob_max) {
393 lp->lnd_mobil = land_mob_max;
394 if (lp->lnd_harden >= land_mob_max) {
395 lp->lnd_harden = land_mob_max;
400 if (opt_FUEL == 0 || lp->lnd_fuelu == 0) {
401 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
402 if (value > land_mob_max) {
404 * provide mobility to be used in lnd_fortify()
405 * without overflowing lnd_mobil
407 lp->lnd_mobil = land_mob_max;
409 lnd_fortify(lp, value - land_mob_max);
410 lp->lnd_mobil = land_mob_max;
412 lp->lnd_mobil = value;
416 can_add = land_mob_max - lp->lnd_mobil;
418 if (can_add > (float)etus * land_mob_scale)
419 can_add = (float)etus * land_mob_scale;
421 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
423 if (can_add > have_fuel_for) {
425 need = can_add - have_fuel_for;
430 if (d - (int)d > 0.0)
433 newfuel = supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
435 lp->lnd_fuel += newfuel * 5;
438 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
440 if (can_add > have_fuel_for) {
442 need = can_add - have_fuel_for;
447 if (d - (int)d > 0.0)
450 newfuel = supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
452 lp->lnd_fuel += newfuel * 50;
455 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
457 if (can_add > have_fuel_for) {
458 total_add = have_fuel_for;
464 lp->lnd_fuel -= ldround(d, 1);
465 lp->lnd_fuel = MIN(lp->lnd_fuel, lp->lnd_fuelc);
466 lp->lnd_mobil += total_add;
467 /* No excess mobility here, hence no automatic fortification */
479 for (n = 0; NULL != (pp = getplanep(n)); n++) {
480 pp->pln_timestamp = now;
484 do_mob_plane(pp, etus);
489 do_mob_plane(struct plnstr *pp, int etus)
493 if (pp->pln_own == 0)
495 if (pp->pln_mobil >= plane_mob_max) {
496 pp->pln_mobil = plane_mob_max;
500 value = pp->pln_mobil + ((float)etus * plane_mob_scale);
501 if (value > plane_mob_max)
502 value = plane_mob_max;
503 pp->pln_mobil = value;