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;
419 * Give damaged units a break. When at low
420 * efficiency, units can go to -100 mob when
421 * marching 1 step, making them slower than
422 * normal mil. This helps take the curse off.
423 * If MOB_ACCESS is on, we don't do this, as it would
424 * be too much of an advantage. So, we just add double
425 * the small amount of mob if we are < 0 instead.
428 if (!opt_MOB_ACCESS) {
429 if (lp->lnd_mobil < 0)
433 if (opt_FUEL == 0) { /* just some bits and pieces */
434 if (opt_MOB_ACCESS) {
435 if (lp->lnd_mobil < 0)
437 lp->lnd_mobil + (2 * ((float)etus * land_mob_scale));
439 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
441 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
443 if (value > land_mob_max)
444 value = land_mob_max;
445 lp->lnd_mobil = value;
450 /* opt_FUEL in force ... */
451 if (lp->lnd_fuelu == 0) {
452 if (opt_MOB_ACCESS) {
453 if (lp->lnd_mobil < 0)
455 lp->lnd_mobil + (2 * ((float)etus * land_mob_scale));
457 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
459 value = lp->lnd_mobil + ((float)etus * land_mob_scale);
461 if (value > land_mob_max)
462 value = land_mob_max;
463 lp->lnd_mobil = value;
467 can_add = land_mob_max - lp->lnd_mobil;
469 if (can_add > ((float)etus * land_mob_scale))
470 can_add = ((float)etus * land_mob_scale);
472 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
474 if (can_add > have_fuel_for) {
476 need = can_add - have_fuel_for;
478 d *= (double)lp->lnd_fuelu;
479 d /= (double)fuel_mult;
481 if ((d - (int)d) > 0.0)
484 newfuel = supply_commod(lp->lnd_own, lp->lnd_x,
485 lp->lnd_y, I_PETROL, need);
486 lp->lnd_fuel += (u_char)(newfuel * 5);
489 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
491 if (can_add > have_fuel_for) {
493 need = can_add - have_fuel_for;
495 d *= (double)lp->lnd_fuelu;
496 d /= (double)fuel_mult;
498 if ((d - (int)d) > 0.0)
501 newfuel = supply_commod(lp->lnd_own, lp->lnd_x,
502 lp->lnd_y, I_OIL, need);
503 lp->lnd_fuel += (u_char)(newfuel * 50);
506 have_fuel_for = (lp->lnd_fuel / lp->lnd_fuelu) * fuel_mult;
508 if (can_add > have_fuel_for) {
509 total_add = have_fuel_for;
512 d = (double)total_add;
513 d *= (double)lp->lnd_fuelu;
514 d /= (double)fuel_mult;
515 lp->lnd_fuel -= (u_char)ldround(d, 1);
516 lp->lnd_fuel = (u_char)min(lp->lnd_fuel, lp->lnd_fuelc);
517 if (total_add + lp->lnd_mobil > land_mob_max) {
518 total_add = land_mob_max - lp->lnd_mobil;
521 if (opt_MOB_ACCESS) {
522 if (lp->lnd_mobil < 0)
523 lp->lnd_mobil += (s_char)total_add;
525 lp->lnd_mobil += (s_char)total_add;
530 mob_plane(register int etus)
532 register struct plnstr *pp;
537 for (n = 0; NULL != (pp = getplanep(n)); n++) {
538 pp->pln_timestamp = now;
542 do_mob_plane(pp, etus);
547 do_mob_plane(register struct plnstr *pp, register int etus)
549 extern int plane_mob_max;
550 extern float plane_mob_scale;
553 if (pp->pln_own == 0)
555 if (pp->pln_mobil >= plane_mob_max) {
556 pp->pln_mobil = plane_mob_max;
560 value = pp->pln_mobil + ((float)etus * plane_mob_scale);
561 if (value > plane_mob_max)
562 value = plane_mob_max;
563 pp->pln_mobil = value;