2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * 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
50 extern int update_pending;
54 static int timestamp_fixing;
55 static int do_upd_checking = 0;
57 static void do_mob_land(register struct lndstr *, register int);
58 static void do_mob_plane(register struct plnstr *, register int);
59 static void do_mob_sect(register struct sctstr *sp, register int etus);
60 static void do_mob_ship(register struct shpstr *, register int);
63 increase_mob(time_t * counter, float mult)
68 extern int updating_mob;
75 secs = now - *counter;
76 if (secs < 1 || secs < s_p_etu)
78 newetus = (int)(secs / s_p_etu);
81 left = (secs % s_p_etu);
83 newmob = (float)(newetus * mult);
84 inewmob = (int)(newetus * mult);
85 if (newmob == inewmob || newetus > 7)
89 } while (newetus > 0);
94 *counter = *counter - left;
102 update_timestamps(time_t lastsavedtime)
104 struct shpstr *shipp;
105 struct sctstr *sectp;
106 struct lndstr *landp;
107 struct plnstr *planep;
112 timestamp_fixing = 1;
114 delta = now - lastsavedtime;
115 for (n = 0; (shipp = getshipp(n)); n++)
116 shipp->shp_access += delta;
117 for (n = 0; (sectp = getsectid(n)); n++)
118 sectp->sct_access += delta;
119 for (n = 0; (landp = getlandp(n)); n++)
120 landp->lnd_access += delta;
121 for (n = 0; (planep = getplanep(n)); n++)
122 planep->pln_access += delta;
123 timestamp_fixing = 0;
129 struct shpstr *shipp;
130 struct sctstr *sectp;
131 struct lndstr *landp;
132 struct plnstr *planep;
139 sectp = getsectid(n);
141 planep = getplanep(n);
144 shp_do_upd_mob(shipp);
146 sct_do_upd_mob(sectp);
148 lnd_do_upd_mob(landp);
150 pln_do_upd_mob(planep);
151 if (!shipp && !sectp && !landp && !planep)
159 sct_do_upd_mob(register struct sctstr *sp)
163 if (do_upd_checking || timestamp_fixing || update_pending)
165 if (sp->sct_own == 0)
167 if (sp->sct_type == SCT_SANCT)
169 if ((etus = increase_mob(&sp->sct_access, sect_mob_scale)) == 0)
172 do_mob_sect(sp, etus);
178 shp_do_upd_mob(register struct shpstr *sp)
182 if (do_upd_checking || timestamp_fixing || update_pending)
184 if (sp->shp_own == 0)
186 if ((etus = increase_mob(&sp->shp_access, ship_mob_scale)) == 0)
189 do_mob_ship(sp, etus);
194 lnd_do_upd_mob(register struct lndstr *lp)
198 if (do_upd_checking || timestamp_fixing || update_pending)
200 if (lp->lnd_own == 0)
202 if ((etus = increase_mob(&lp->lnd_access, land_mob_scale)) == 0)
206 do_mob_land(lp, etus);
211 pln_do_upd_mob(register struct plnstr *pp)
215 if (do_upd_checking || timestamp_fixing || update_pending)
217 if (pp->pln_own == 0)
219 if ((etus = increase_mob(&pp->pln_access, plane_mob_scale)) == 0)
223 do_mob_plane(pp, etus);
228 mob_sect(register int etus)
230 register struct sctstr *sp;
235 for (n = 0; NULL != (sp = getsectid(n)); n++) {
236 sp->sct_timestamp = now;
240 do_mob_sect(sp, etus);
245 do_mob_sect(register struct sctstr *sp, register int etus)
249 if (sp->sct_own == 0)
251 if (sp->sct_type == SCT_SANCT)
253 /* Do we have to even bother? */
254 if (sp->sct_mobil >= sect_mob_max) {
255 /* No, so set just in case and then return */
256 sp->sct_mobil = sect_mob_max;
259 value = sp->sct_mobil + ((float)etus * sect_mob_scale);
260 if (value > sect_mob_max)
261 value = sect_mob_max;
262 sp->sct_mobil = value;
266 mob_ship(register int etus)
268 register struct shpstr *sp;
273 for (n = 0; NULL != (sp = getshipp(n)); n++) {
274 sp->shp_timestamp = now;
278 do_mob_ship(sp, etus);
283 do_mob_ship(register struct shpstr *sp, register int etus)
287 int can_add, have_fuel_for, total_add;
290 if (sp->shp_own == 0)
293 /* Do we even have to bother updating this mobility? */
294 if (sp->shp_mobil >= ship_mob_max) {
295 /* No, so don't. Just set it to max (just in case) and
297 sp->shp_mobil = ship_mob_max;
301 if (opt_FUEL == 0) { /* only a bit to do ... */
302 value = sp->shp_mobil + ((float)etus * ship_mob_scale);
303 if (value > ship_mob_max)
304 value = ship_mob_max;
305 sp->shp_mobil = value;
306 return; /* so we ship the FUEL stuff */
309 /* opt_FUEL in force */
310 if (mchr[(int)sp->shp_type].m_fuelu == 0) {
311 value = sp->shp_mobil + ((float)etus * ship_mob_scale);
312 if (value > ship_mob_max)
313 value = ship_mob_max;
314 sp->shp_mobil = (s_char)value;
316 can_add = ship_mob_max - sp->shp_mobil;
317 if (can_add > ((float)etus * ship_mob_scale))
318 can_add = ((float)etus * ship_mob_scale);
319 have_fuel_for = ldround((((double)sp->shp_fuel /
320 (double)mchr[(int)sp->shp_type].
321 m_fuelu) * (double)fuel_mult), 1);
323 if (can_add > have_fuel_for) {
325 need = can_add - have_fuel_for;
327 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
328 d /= (double)fuel_mult;
330 if ((d - (int)d) > 0.0)
333 newfuel = supply_commod(sp->shp_own, sp->shp_x,
334 sp->shp_y, I_PETROL, need);
335 sp->shp_fuel += (u_char)(newfuel * 5);
338 have_fuel_for = ldround((((double)sp->shp_fuel /
339 (double)mchr[(int)sp->shp_type].
340 m_fuelu) * (double)fuel_mult), 1);
342 if (can_add > have_fuel_for) {
344 need = can_add - have_fuel_for;
346 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
347 d /= (double)fuel_mult;
349 if ((d - (int)d) > 0.0)
352 newfuel = supply_commod(sp->shp_own, sp->shp_x,
353 sp->shp_y, I_OIL, need);
354 sp->shp_fuel += (u_char)(newfuel * 50);
357 have_fuel_for = ldround((((double)sp->shp_fuel /
358 (double)mchr[(int)sp->shp_type].
359 m_fuelu) * (double)fuel_mult), 1);
361 if (can_add > have_fuel_for)
362 total_add = have_fuel_for;
365 d = (double)total_add;
366 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
367 d /= (double)fuel_mult;
368 sp->shp_fuel -= (u_char)ldround(d, 1);
369 sp->shp_fuel = (u_char)min(sp->shp_fuel,
370 mchr[(int)sp->shp_type].m_fuelc);
371 sp->shp_mobil += (s_char)total_add;
376 mob_land(register int etus)
378 register struct lndstr *lp;
383 for (n = 0; NULL != (lp = getlandp(n)); n++) {
384 lp->lnd_timestamp = now;
388 do_mob_land(lp, etus);
393 do_mob_land(register struct lndstr *lp, register int etus)
397 int can_add, have_fuel_for, total_add;
400 if (lp->lnd_own == 0)
403 if (lp->lnd_mobil >= land_mob_max) {
404 lp->lnd_mobil = land_mob_max;
405 if (lp->lnd_harden >= land_mob_max) {
406 lp->lnd_harden = land_mob_max;
411 if (opt_FUEL == 0) { /* just some bits and pieces */
412 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
413 if (value > land_mob_max) {
414 lnd_fortify(lp, value - land_mob_max);
415 value = land_mob_max;
417 lp->lnd_mobil = value;
422 /* opt_FUEL in force ... */
423 if (lp->lnd_fuelu == 0) {
424 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
425 if (value > land_mob_max) {
426 lnd_fortify(lp, value - land_mob_max);
427 value = land_mob_max;
429 lp->lnd_mobil = value;
433 can_add = land_mob_max - lp->lnd_mobil;
435 if (can_add > ((float)etus * land_mob_scale))
436 can_add = ((float)etus * land_mob_scale);
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;
444 d *= (double)lp->lnd_fuelu;
445 d /= (double)fuel_mult;
447 if ((d - (int)d) > 0.0)
450 newfuel = supply_commod(lp->lnd_own, lp->lnd_x,
451 lp->lnd_y, I_PETROL, need);
452 lp->lnd_fuel += (u_char)(newfuel * 5);
455 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
457 if (can_add > have_fuel_for) {
459 need = can_add - have_fuel_for;
461 d *= (double)lp->lnd_fuelu;
462 d /= (double)fuel_mult;
464 if ((d - (int)d) > 0.0)
467 newfuel = supply_commod(lp->lnd_own, lp->lnd_x,
468 lp->lnd_y, I_OIL, need);
469 lp->lnd_fuel += (u_char)(newfuel * 50);
472 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
474 if (can_add > have_fuel_for) {
475 total_add = have_fuel_for;
478 d = (double)total_add;
479 d *= (double)lp->lnd_fuelu;
480 d /= (double)fuel_mult;
481 lp->lnd_fuel -= (u_char)ldround(d, 1);
482 lp->lnd_fuel = (u_char)min(lp->lnd_fuel, lp->lnd_fuelc);
483 if (total_add + lp->lnd_mobil > land_mob_max) {
484 total_add = land_mob_max - lp->lnd_mobil;
486 /* no automatic fortification here, as it would cost fuel */
488 lp->lnd_mobil += (s_char)total_add;
493 mob_plane(register int etus)
495 register struct plnstr *pp;
500 for (n = 0; NULL != (pp = getplanep(n)); n++) {
501 pp->pln_timestamp = now;
505 do_mob_plane(pp, etus);
510 do_mob_plane(register struct plnstr *pp, register int etus)
514 if (pp->pln_own == 0)
516 if (pp->pln_mobil >= plane_mob_max) {
517 pp->pln_mobil = plane_mob_max;
521 value = pp->pln_mobil + ((float)etus * plane_mob_scale);
522 if (value > plane_mob_max)
523 value = plane_mob_max;
524 pp->pln_mobil = value;