2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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
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)
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 = (float)(newetus * mult);
83 inewmob = (int)(newetus * mult);
84 if (newmob == inewmob || newetus > 7)
88 } while (newetus > 0);
93 *counter = *counter - left;
101 update_timestamps(time_t lastsavedtime)
103 struct shpstr *shipp;
104 struct sctstr *sectp;
105 struct lndstr *landp;
106 struct plnstr *planep;
111 timestamp_fixing = 1;
113 delta = now - lastsavedtime;
114 for (n = 0; (shipp = getshipp(n)); n++)
115 shipp->shp_access += delta;
116 for (n = 0; (sectp = getsectid(n)); n++)
117 sectp->sct_access += delta;
118 for (n = 0; (landp = getlandp(n)); n++)
119 landp->lnd_access += delta;
120 for (n = 0; (planep = getplanep(n)); n++)
121 planep->pln_access += delta;
122 timestamp_fixing = 0;
128 struct shpstr *shipp;
129 struct sctstr *sectp;
130 struct lndstr *landp;
131 struct plnstr *planep;
138 sectp = getsectid(n);
140 planep = getplanep(n);
143 shp_do_upd_mob(shipp);
145 sct_do_upd_mob(sectp);
147 lnd_do_upd_mob(landp);
149 pln_do_upd_mob(planep);
150 if (!shipp && !sectp && !landp && !planep)
158 sct_do_upd_mob(register struct sctstr *sp)
162 if (do_upd_checking || timestamp_fixing || update_pending)
164 if (sp->sct_own == 0)
166 if (sp->sct_type == SCT_SANCT)
168 if ((etus = increase_mob(&sp->sct_access, sect_mob_scale)) == 0)
171 do_mob_sect(sp, etus);
177 shp_do_upd_mob(register struct shpstr *sp)
181 if (do_upd_checking || timestamp_fixing || update_pending)
183 if (sp->shp_own == 0)
185 if ((etus = increase_mob(&sp->shp_access, ship_mob_scale)) == 0)
188 do_mob_ship(sp, etus);
193 lnd_do_upd_mob(register struct lndstr *lp)
197 if (do_upd_checking || timestamp_fixing || update_pending)
199 if (lp->lnd_own == 0)
201 if ((etus = increase_mob(&lp->lnd_access, land_mob_scale)) == 0)
205 do_mob_land(lp, etus);
210 pln_do_upd_mob(register struct plnstr *pp)
214 if (do_upd_checking || timestamp_fixing || update_pending)
216 if (pp->pln_own == 0)
218 if ((etus = increase_mob(&pp->pln_access, plane_mob_scale)) == 0)
222 do_mob_plane(pp, etus);
227 mob_sect(register int etus)
229 register struct sctstr *sp;
234 for (n = 0; NULL != (sp = getsectid(n)); n++) {
235 sp->sct_timestamp = now;
239 do_mob_sect(sp, etus);
244 do_mob_sect(register struct sctstr *sp, register int etus)
248 if (sp->sct_own == 0)
250 if (sp->sct_type == SCT_SANCT)
252 /* Do we have to even bother? */
253 if (sp->sct_mobil >= sect_mob_max) {
254 /* No, so set just in case and then return */
255 sp->sct_mobil = sect_mob_max;
258 value = sp->sct_mobil + ((float)etus * sect_mob_scale);
259 if (value > sect_mob_max)
260 value = sect_mob_max;
261 sp->sct_mobil = value;
265 mob_ship(register int etus)
267 register struct shpstr *sp;
272 for (n = 0; NULL != (sp = getshipp(n)); n++) {
273 sp->shp_timestamp = now;
277 do_mob_ship(sp, etus);
282 do_mob_ship(register struct shpstr *sp, register int etus)
286 int can_add, have_fuel_for, total_add;
289 if (sp->shp_own == 0)
292 /* Do we even have to bother updating this mobility? */
293 if (sp->shp_mobil >= ship_mob_max) {
294 /* No, so don't. Just set it to max (just in case) and
296 sp->shp_mobil = ship_mob_max;
300 if (opt_FUEL == 0) { /* only a bit to do ... */
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 = value;
305 return; /* so we ship the FUEL stuff */
308 /* opt_FUEL in force */
309 if (mchr[(int)sp->shp_type].m_fuelu == 0) {
310 value = sp->shp_mobil + ((float)etus * ship_mob_scale);
311 if (value > ship_mob_max)
312 value = ship_mob_max;
313 sp->shp_mobil = (s_char)value;
315 can_add = ship_mob_max - sp->shp_mobil;
316 if (can_add > ((float)etus * ship_mob_scale))
317 can_add = ((float)etus * ship_mob_scale);
318 have_fuel_for = ldround((((double)sp->shp_fuel /
319 (double)mchr[(int)sp->shp_type].
320 m_fuelu) * (double)fuel_mult), 1);
322 if (can_add > have_fuel_for) {
324 need = can_add - have_fuel_for;
326 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
327 d /= (double)fuel_mult;
329 if ((d - (int)d) > 0.0)
332 newfuel = supply_commod(sp->shp_own, sp->shp_x,
333 sp->shp_y, I_PETROL, need);
334 sp->shp_fuel += (u_char)(newfuel * 5);
337 have_fuel_for = ldround((((double)sp->shp_fuel /
338 (double)mchr[(int)sp->shp_type].
339 m_fuelu) * (double)fuel_mult), 1);
341 if (can_add > have_fuel_for) {
343 need = can_add - have_fuel_for;
345 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
346 d /= (double)fuel_mult;
348 if ((d - (int)d) > 0.0)
351 newfuel = supply_commod(sp->shp_own, sp->shp_x,
352 sp->shp_y, I_OIL, need);
353 sp->shp_fuel += (u_char)(newfuel * 50);
356 have_fuel_for = ldround((((double)sp->shp_fuel /
357 (double)mchr[(int)sp->shp_type].
358 m_fuelu) * (double)fuel_mult), 1);
360 if (can_add > have_fuel_for)
361 total_add = have_fuel_for;
364 d = (double)total_add;
365 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
366 d /= (double)fuel_mult;
367 sp->shp_fuel -= (u_char)ldround(d, 1);
368 sp->shp_fuel = (u_char)min(sp->shp_fuel,
369 mchr[(int)sp->shp_type].m_fuelc);
370 sp->shp_mobil += (s_char)total_add;
375 mob_land(register int etus)
377 register struct lndstr *lp;
382 for (n = 0; NULL != (lp = getlandp(n)); n++) {
383 lp->lnd_timestamp = now;
387 do_mob_land(lp, etus);
392 do_mob_land(register struct lndstr *lp, register int etus)
396 int can_add, have_fuel_for, total_add;
399 if (lp->lnd_own == 0)
402 if (lp->lnd_mobil >= land_mob_max) {
403 lp->lnd_mobil = land_mob_max;
404 if (lp->lnd_harden >= land_mob_max) {
405 lp->lnd_harden = land_mob_max;
410 if (opt_FUEL == 0) { /* just some bits and pieces */
411 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
412 if (value > land_mob_max) {
413 lnd_fortify(lp, value - land_mob_max);
414 value = land_mob_max;
416 lp->lnd_mobil = value;
421 /* opt_FUEL in force ... */
422 if (lp->lnd_fuelu == 0) {
423 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
424 if (value > land_mob_max) {
425 lnd_fortify(lp, value - land_mob_max);
426 value = land_mob_max;
428 lp->lnd_mobil = value;
432 can_add = land_mob_max - lp->lnd_mobil;
434 if (can_add > ((float)etus * land_mob_scale))
435 can_add = ((float)etus * land_mob_scale);
437 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
439 if (can_add > have_fuel_for) {
441 need = can_add - have_fuel_for;
443 d *= (double)lp->lnd_fuelu;
444 d /= (double)fuel_mult;
446 if ((d - (int)d) > 0.0)
449 newfuel = supply_commod(lp->lnd_own, lp->lnd_x,
450 lp->lnd_y, I_PETROL, need);
451 lp->lnd_fuel += (u_char)(newfuel * 5);
454 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
456 if (can_add > have_fuel_for) {
458 need = can_add - have_fuel_for;
460 d *= (double)lp->lnd_fuelu;
461 d /= (double)fuel_mult;
463 if ((d - (int)d) > 0.0)
466 newfuel = supply_commod(lp->lnd_own, lp->lnd_x,
467 lp->lnd_y, I_OIL, need);
468 lp->lnd_fuel += (u_char)(newfuel * 50);
471 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
473 if (can_add > have_fuel_for) {
474 total_add = have_fuel_for;
477 d = (double)total_add;
478 d *= (double)lp->lnd_fuelu;
479 d /= (double)fuel_mult;
480 lp->lnd_fuel -= (u_char)ldround(d, 1);
481 lp->lnd_fuel = (u_char)min(lp->lnd_fuel, lp->lnd_fuelc);
482 if (total_add + lp->lnd_mobil > land_mob_max) {
483 total_add = land_mob_max - lp->lnd_mobil;
485 /* no automatic fortification here, as it would cost fuel */
487 lp->lnd_mobil += (s_char)total_add;
492 mob_plane(register int etus)
494 register struct plnstr *pp;
499 for (n = 0; NULL != (pp = getplanep(n)); n++) {
500 pp->pln_timestamp = now;
504 do_mob_plane(pp, etus);
509 do_mob_plane(register struct plnstr *pp, register int etus)
513 if (pp->pln_own == 0)
515 if (pp->pln_mobil >= plane_mob_max) {
516 pp->pln_mobil = plane_mob_max;
520 value = pp->pln_mobil + ((float)etus * plane_mob_scale);
521 if (value > plane_mob_max)
522 value = plane_mob_max;
523 pp->pln_mobil = value;