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;
54 static void do_mob_land(register struct lndstr *, register int);
55 static void do_mob_plane(register struct plnstr *, register int);
56 static void do_mob_sect(register struct sctstr *sp, register int etus);
57 static void do_mob_ship(register struct shpstr *, register int);
60 increase_mob(time_t * counter, float mult)
66 extern int updating_mob;
73 secs = now - *counter;
74 if (secs < 1 || secs < s_p_etu)
76 newetus = (int)(secs / s_p_etu);
79 left = (secs % s_p_etu);
81 newmob = (float)(newetus * mult);
82 inewmob = (int)(newetus * mult);
83 if (newmob == inewmob || newetus > 7)
87 } while (newetus > 0);
92 *counter = *counter - 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(register struct sctstr *sp)
159 extern float sect_mob_scale;
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)
179 extern float ship_mob_scale;
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)
196 extern float land_mob_scale;
199 if (do_upd_checking || timestamp_fixing || update_pending)
201 if (lp->lnd_own == 0)
203 if ((etus = increase_mob(&lp->lnd_access, land_mob_scale)) == 0)
207 do_mob_land(lp, etus);
212 pln_do_upd_mob(register struct plnstr *pp)
214 extern float plane_mob_scale;
217 if (do_upd_checking || timestamp_fixing || update_pending)
219 if (pp->pln_own == 0)
221 if ((etus = increase_mob(&pp->pln_access, plane_mob_scale)) == 0)
225 do_mob_plane(pp, etus);
230 mob_sect(register int etus)
232 register struct sctstr *sp;
237 for (n = 0; NULL != (sp = getsectid(n)); n++) {
238 sp->sct_timestamp = now;
242 do_mob_sect(sp, etus);
247 do_mob_sect(register struct sctstr *sp, register int etus)
249 extern float sect_mob_scale;
250 extern int sect_mob_max;
253 if (sp->sct_own == 0)
255 if (sp->sct_type == SCT_SANCT)
257 /* Do we have to even bother? */
258 if (sp->sct_mobil >= sect_mob_max) {
259 /* No, so set just in case and then return */
260 sp->sct_mobil = sect_mob_max;
263 value = sp->sct_mobil + ((float)etus * sect_mob_scale);
264 if (value > sect_mob_max)
265 value = sect_mob_max;
266 sp->sct_mobil = value;
270 mob_ship(register int etus)
272 register struct shpstr *sp;
277 for (n = 0; NULL != (sp = getshipp(n)); n++) {
278 sp->shp_timestamp = now;
282 do_mob_ship(sp, etus);
287 do_mob_ship(register struct shpstr *sp, register int etus)
289 extern int ship_mob_max;
290 extern float ship_mob_scale;
293 int can_add, have_fuel_for, total_add;
295 extern int fuel_mult;
297 if (sp->shp_own == 0)
300 /* Do we even have to bother updating this mobility? */
301 if (sp->shp_mobil >= ship_mob_max) {
302 /* No, so don't. Just set it to max (just in case) and
304 sp->shp_mobil = ship_mob_max;
308 if (opt_FUEL == 0) { /* only a bit to do ... */
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 = value;
313 return; /* so we ship the FUEL stuff */
316 /* opt_FUEL in force */
317 if (mchr[(int)sp->shp_type].m_fuelu == 0) {
318 value = sp->shp_mobil + ((float)etus * ship_mob_scale);
319 if (value > ship_mob_max)
320 value = ship_mob_max;
321 sp->shp_mobil = (s_char)value;
323 can_add = ship_mob_max - sp->shp_mobil;
324 if (can_add > ((float)etus * ship_mob_scale))
325 can_add = ((float)etus * ship_mob_scale);
326 have_fuel_for = ldround((((double)sp->shp_fuel /
327 (double)mchr[(int)sp->shp_type].
328 m_fuelu) * (double)fuel_mult), 1);
330 if (can_add > have_fuel_for) {
332 need = can_add - have_fuel_for;
334 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
335 d /= (double)fuel_mult;
337 if ((d - (int)d) > 0.0)
340 newfuel = supply_commod(sp->shp_own, sp->shp_x,
341 sp->shp_y, I_PETROL, need);
342 sp->shp_fuel += (u_char)(newfuel * 5);
345 have_fuel_for = ldround((((double)sp->shp_fuel /
346 (double)mchr[(int)sp->shp_type].
347 m_fuelu) * (double)fuel_mult), 1);
349 if (can_add > have_fuel_for) {
351 need = can_add - have_fuel_for;
353 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
354 d /= (double)fuel_mult;
356 if ((d - (int)d) > 0.0)
359 newfuel = supply_commod(sp->shp_own, sp->shp_x,
360 sp->shp_y, I_OIL, need);
361 sp->shp_fuel += (u_char)(newfuel * 50);
364 have_fuel_for = ldround((((double)sp->shp_fuel /
365 (double)mchr[(int)sp->shp_type].
366 m_fuelu) * (double)fuel_mult), 1);
368 if (can_add > have_fuel_for)
369 total_add = have_fuel_for;
372 d = (double)total_add;
373 d *= (double)mchr[(int)sp->shp_type].m_fuelu;
374 d /= (double)fuel_mult;
375 sp->shp_fuel -= (u_char)ldround(d, 1);
376 sp->shp_fuel = (u_char)min(sp->shp_fuel,
377 mchr[(int)sp->shp_type].m_fuelc);
378 sp->shp_mobil += (s_char)total_add;
383 mob_land(register int etus)
385 register struct lndstr *lp;
390 for (n = 0; NULL != (lp = getlandp(n)); n++) {
391 lp->lnd_timestamp = now;
395 do_mob_land(lp, etus);
400 do_mob_land(register struct lndstr *lp, register int etus)
402 extern int land_mob_max;
403 extern float land_mob_scale;
406 int can_add, have_fuel_for, total_add;
408 extern int fuel_mult;
410 if (lp->lnd_own == 0)
413 if (lp->lnd_mobil >= land_mob_max) {
414 lp->lnd_mobil = land_mob_max;
415 if (lp->lnd_harden >= land_mob_max) {
416 lp->lnd_harden = land_mob_max;
422 * Give damaged units a break. When at low
423 * efficiency, units can go to -100 mob when
424 * marching 1 step, making them slower than
425 * normal mil. This helps take the curse off.
426 * If MOB_ACCESS is on, we don't do this, as it would
427 * be too much of an advantage. So, we just add double
428 * the small amount of mob if we are < 0 instead.
431 if (!opt_MOB_ACCESS) {
432 if (lp->lnd_mobil < 0)
436 if (opt_FUEL == 0) { /* just some bits and pieces */
437 if (opt_MOB_ACCESS) {
438 if (lp->lnd_mobil < 0)
440 lp->lnd_mobil + (2 * ((float)etus * land_mob_scale));
442 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
444 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
446 if (value > land_mob_max) {
447 lnd_fortify(lp, value - land_mob_max);
448 value = land_mob_max;
450 lp->lnd_mobil = value;
455 /* opt_FUEL in force ... */
456 if (lp->lnd_fuelu == 0) {
457 if (opt_MOB_ACCESS) {
458 if (lp->lnd_mobil < 0)
460 lp->lnd_mobil + (2 * ((float)etus * land_mob_scale));
462 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
464 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
466 if (value > land_mob_max) {
467 lnd_fortify(lp, value - land_mob_max);
468 value = land_mob_max;
470 lp->lnd_mobil = value;
474 can_add = land_mob_max - lp->lnd_mobil;
476 if (can_add > ((float)etus * land_mob_scale))
477 can_add = ((float)etus * land_mob_scale);
479 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
481 if (can_add > have_fuel_for) {
483 need = can_add - have_fuel_for;
485 d *= (double)lp->lnd_fuelu;
486 d /= (double)fuel_mult;
488 if ((d - (int)d) > 0.0)
491 newfuel = supply_commod(lp->lnd_own, lp->lnd_x,
492 lp->lnd_y, I_PETROL, need);
493 lp->lnd_fuel += (u_char)(newfuel * 5);
496 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
498 if (can_add > have_fuel_for) {
500 need = can_add - have_fuel_for;
502 d *= (double)lp->lnd_fuelu;
503 d /= (double)fuel_mult;
505 if ((d - (int)d) > 0.0)
508 newfuel = supply_commod(lp->lnd_own, lp->lnd_x,
509 lp->lnd_y, I_OIL, need);
510 lp->lnd_fuel += (u_char)(newfuel * 50);
513 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
515 if (can_add > have_fuel_for) {
516 total_add = have_fuel_for;
519 d = (double)total_add;
520 d *= (double)lp->lnd_fuelu;
521 d /= (double)fuel_mult;
522 lp->lnd_fuel -= (u_char)ldround(d, 1);
523 lp->lnd_fuel = (u_char)min(lp->lnd_fuel, lp->lnd_fuelc);
524 if (total_add + lp->lnd_mobil > land_mob_max) {
525 total_add = land_mob_max - lp->lnd_mobil;
527 /* no automatic fortification here, as it would cost fuel */
529 if (opt_MOB_ACCESS) {
530 if (lp->lnd_mobil < 0)
531 lp->lnd_mobil += (s_char)total_add;
533 lp->lnd_mobil += (s_char)total_add;
538 mob_plane(register int etus)
540 register struct plnstr *pp;
545 for (n = 0; NULL != (pp = getplanep(n)); n++) {
546 pp->pln_timestamp = now;
550 do_mob_plane(pp, etus);
555 do_mob_plane(register struct plnstr *pp, register int etus)
557 extern int plane_mob_max;
558 extern float plane_mob_scale;
561 if (pp->pln_own == 0)
563 if (pp->pln_mobil >= plane_mob_max) {
564 pp->pln_mobil = plane_mob_max;
568 value = pp->pln_mobil + ((float)etus * plane_mob_scale);
569 if (value > plane_mob_max)
570 value = plane_mob_max;
571 pp->pln_mobil = value;