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 timestamp_fixing;
51 extern int update_pending;
52 int do_upd_checking = 0;
56 increase_mob(time_t * counter, float mult)
62 extern int updating_mob;
69 secs = now - *counter;
70 if (secs < 1 || secs < s_p_etu)
72 newetus = (int)(secs / s_p_etu);
75 left = (secs % s_p_etu);
77 newmob = (float)(newetus * mult);
78 inewmob = (int)(newetus * mult);
79 if (newmob == inewmob || newetus > 7)
83 } while (newetus > 0);
88 *counter = *counter - left;
96 update_timestamps(time_t lastsavedtime)
100 struct lndstr *landp;
101 struct plnstr *planep;
106 timestamp_fixing = 1;
108 delta = now - lastsavedtime;
109 for (n = 0; (shipp = getshipp(n)); n++)
110 shipp->shp_access += delta;
111 for (n = 0; (sectp = getsectid(n)); n++)
112 sectp->sct_access += delta;
113 for (n = 0; (landp = getlandp(n)); n++)
114 landp->lnd_access += delta;
115 for (n = 0; (planep = getplanep(n)); n++)
116 planep->pln_access += delta;
117 timestamp_fixing = 0;
123 struct shpstr *shipp;
124 struct sctstr *sectp;
125 struct lndstr *landp;
126 struct plnstr *planep;
133 sectp = getsectid(n);
135 planep = getplanep(n);
138 shp_do_upd_mob(shipp);
140 sct_do_upd_mob(sectp);
142 lnd_do_upd_mob(landp);
144 pln_do_upd_mob(planep);
145 if (!shipp && !sectp && !landp && !planep)
153 sct_do_upd_mob(register struct sctstr *sp)
155 extern float sect_mob_scale;
158 if (do_upd_checking || timestamp_fixing || update_pending)
160 if (sp->sct_own == 0)
162 if (sp->sct_type == SCT_SANCT)
164 if ((etus = increase_mob(&sp->sct_access, sect_mob_scale)) == 0)
167 do_mob_sect(sp, etus);
173 shp_do_upd_mob(register struct shpstr *sp)
175 extern float ship_mob_scale;
178 if (do_upd_checking || timestamp_fixing || update_pending)
180 if (sp->shp_own == 0)
182 if ((etus = increase_mob(&sp->shp_access, ship_mob_scale)) == 0)
185 do_mob_ship(sp, etus);
190 lnd_do_upd_mob(register struct lndstr *lp)
192 extern float land_mob_scale;
195 if (do_upd_checking || timestamp_fixing || update_pending)
197 if (lp->lnd_own == 0)
199 if ((etus = increase_mob(&lp->lnd_access, land_mob_scale)) == 0)
203 do_mob_land(lp, etus);
208 pln_do_upd_mob(register struct plnstr *pp)
210 extern float plane_mob_scale;
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);
226 mob_sect(register int etus)
228 register struct sctstr *sp;
233 for (n = 0; NULL != (sp = getsectid(n)); n++) {
234 sp->sct_timestamp = now;
238 do_mob_sect(sp, etus);
243 do_mob_sect(register struct sctstr *sp, register int etus)
245 extern float sect_mob_scale;
246 extern int sect_mob_max;
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)
285 extern int ship_mob_max;
286 extern float ship_mob_scale;
289 int can_add, have_fuel_for, total_add;
291 extern int fuel_mult;
293 if (sp->shp_own == 0)
296 /* Do we even have to bother updating this mobility? */
297 if (sp->shp_mobil >= ship_mob_max) {
298 /* No, so don't. Just set it to max (just in case) and
300 sp->shp_mobil = ship_mob_max;
304 if (opt_FUEL == 0) { /* only a bit to do ... */
305 value = sp->shp_mobil + ((float)etus * ship_mob_scale);
306 if (value > ship_mob_max)
307 value = ship_mob_max;
308 sp->shp_mobil = value;
309 return; /* so we ship the FUEL stuff */
312 /* opt_FUEL in force */
313 if (mchr[(int)sp->shp_type].m_fuelu == 0) {
314 value = sp->shp_mobil + ((float)etus * ship_mob_scale);
315 if (value > ship_mob_max)
316 value = ship_mob_max;
317 sp->shp_mobil = (s_char)value;
319 can_add = ship_mob_max - sp->shp_mobil;
320 if (can_add > ((float)etus * ship_mob_scale))
321 can_add = ((float)etus * ship_mob_scale);
322 have_fuel_for = ldround((((double)sp->shp_fuel /
323 (double)mchr[(int)sp->shp_type].
324 m_fuelu) * (double)fuel_mult), 1);
326 if (can_add > have_fuel_for) {
328 need = can_add - have_fuel_for;
330 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
331 d /= (double)fuel_mult;
333 if ((d - (int)d) > 0.0)
336 newfuel = supply_commod(sp->shp_own, sp->shp_x,
337 sp->shp_y, I_PETROL, need);
338 sp->shp_fuel += (u_char)(newfuel * 5);
341 have_fuel_for = ldround((((double)sp->shp_fuel /
342 (double)mchr[(int)sp->shp_type].
343 m_fuelu) * (double)fuel_mult), 1);
345 if (can_add > have_fuel_for) {
347 need = can_add - have_fuel_for;
349 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
350 d /= (double)fuel_mult;
352 if ((d - (int)d) > 0.0)
355 newfuel = supply_commod(sp->shp_own, sp->shp_x,
356 sp->shp_y, I_OIL, need);
357 sp->shp_fuel += (u_char)(newfuel * 50);
360 have_fuel_for = ldround((((double)sp->shp_fuel /
361 (double)mchr[(int)sp->shp_type].
362 m_fuelu) * (double)fuel_mult), 1);
364 if (can_add > have_fuel_for)
365 total_add = have_fuel_for;
368 d = (double)total_add;
369 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
370 d /= (double)fuel_mult;
371 sp->shp_fuel -= (u_char)ldround(d, 1);
372 sp->shp_fuel = (u_char)min(sp->shp_fuel,
373 mchr[(int)sp->shp_type].m_fuelc);
374 sp->shp_mobil += (s_char)total_add;
379 mob_land(register int etus)
381 register struct lndstr *lp;
386 for (n = 0; NULL != (lp = getlandp(n)); n++) {
387 lp->lnd_timestamp = now;
391 do_mob_land(lp, etus);
396 do_mob_land(register struct lndstr *lp, register int etus)
398 extern int land_mob_max;
399 extern float land_mob_scale;
402 int can_add, have_fuel_for, total_add;
404 extern int fuel_mult;
406 if (lp->lnd_own == 0)
409 if (lp->lnd_mobil >= land_mob_max) {
410 lp->lnd_mobil = land_mob_max;
415 * Give damaged units a break. When at low
416 * efficiency, units can go to -100 mob when
417 * marching 1 step, making them slower than
418 * normal mil. This helps take the curse off.
419 * If MOB_ACCESS is on, we don't do this, as it would
420 * be too much of an advantage. So, we just add double
421 * the small amount of mob if we are < 0 instead.
424 if (!opt_MOB_ACCESS) {
425 if (lp->lnd_mobil < 0)
429 if (opt_FUEL == 0) { /* just some bits and pieces */
430 if (opt_MOB_ACCESS) {
431 if (lp->lnd_mobil < 0)
433 lp->lnd_mobil + (2 * ((float)etus * land_mob_scale));
435 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
437 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
439 if (value > land_mob_max)
440 value = land_mob_max;
441 lp->lnd_mobil = value;
446 /* opt_FUEL in force ... */
447 if (lp->lnd_fuelu == 0) {
448 if (opt_MOB_ACCESS) {
449 if (lp->lnd_mobil < 0)
451 lp->lnd_mobil + (2 * ((float)etus * land_mob_scale));
453 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
455 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
457 if (value > land_mob_max)
458 value = land_mob_max;
459 lp->lnd_mobil = value;
463 can_add = land_mob_max - lp->lnd_mobil;
465 if (can_add > ((float)etus * land_mob_scale))
466 can_add = ((float)etus * land_mob_scale);
468 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
470 if (can_add > have_fuel_for) {
472 need = can_add - have_fuel_for;
474 d *= (double)lp->lnd_fuelu;
475 d /= (double)fuel_mult;
477 if ((d - (int)d) > 0.0)
480 newfuel = supply_commod(lp->lnd_own, lp->lnd_x,
481 lp->lnd_y, I_PETROL, need);
482 lp->lnd_fuel += (u_char)(newfuel * 5);
485 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
487 if (can_add > have_fuel_for) {
489 need = can_add - have_fuel_for;
491 d *= (double)lp->lnd_fuelu;
492 d /= (double)fuel_mult;
494 if ((d - (int)d) > 0.0)
497 newfuel = supply_commod(lp->lnd_own, lp->lnd_x,
498 lp->lnd_y, I_OIL, need);
499 lp->lnd_fuel += (u_char)(newfuel * 50);
502 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
504 if (can_add > have_fuel_for) {
505 total_add = have_fuel_for;
508 d = (double)total_add;
509 d *= (double)lp->lnd_fuelu;
510 d /= (double)fuel_mult;
511 lp->lnd_fuel -= (u_char)ldround(d, 1);
512 lp->lnd_fuel = (u_char)min(lp->lnd_fuel, lp->lnd_fuelc);
513 if (total_add + lp->lnd_mobil > land_mob_max) {
514 total_add = land_mob_max - lp->lnd_mobil;
517 if (opt_MOB_ACCESS) {
518 if (lp->lnd_mobil < 0)
519 lp->lnd_mobil += (s_char)total_add;
521 lp->lnd_mobil += (s_char)total_add;
526 mob_plane(register int etus)
528 register struct plnstr *pp;
533 for (n = 0; NULL != (pp = getplanep(n)); n++) {
534 pp->pln_timestamp = now;
538 do_mob_plane(pp, etus);
543 do_mob_plane(register struct plnstr *pp, register int etus)
545 extern int plane_mob_max;
546 extern float plane_mob_scale;
549 if (pp->pln_own == 0)
551 if (pp->pln_mobil >= plane_mob_max) {
552 pp->pln_mobil = plane_mob_max;
556 value = pp->pln_mobil + ((float)etus * plane_mob_scale);
557 if (value > plane_mob_max)
558 value = plane_mob_max;
559 pp->pln_mobil = value;