]> git.pond.sub.org Git - empserver/blob - src/lib/subs/lndsub.c
(lnd_mar_one_sector): When units in different sectors march, only
[empserver] / src / lib / subs / lndsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  lndsub.c: Land unit subroutines
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1998-2000
33  */
34
35 #include <math.h>
36 #include "misc.h"
37 #include "player.h"
38 #include "file.h"
39 #include "sect.h"
40 #include "path.h"
41 #include "news.h"
42 #include "nat.h"
43 #include "xy.h"
44 #include "land.h"
45 #include "ship.h"
46 #include "nsc.h"
47 #include "mission.h"
48 #include "plane.h"
49 #include "combat.h"
50 #include "damage.h"
51 #include "optlist.h"
52 #include "prototypes.h"
53
54 int
55 attack_val(int combat_mode, struct lndstr *lp)
56 {
57     int men;
58     int value;
59     struct lchrstr *lcp;
60
61     if (((int)lp->lnd_effic) < LAND_MINEFF) {
62         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
63         lp->lnd_own = 0;
64         putland(lp->lnd_uid, lp);
65         return 0;
66     }
67
68     lcp = &lchr[(int)lp->lnd_type];
69
70 /* Spies always count as 1 during assaults.  If they are the only ones
71    in the assault, they get to sneak on anyway. */
72
73     if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
74         return 1;
75
76     men = total_mil(lp);
77
78     value = ldround(((double)men * (double)lp->lnd_att), 1);
79
80     value = (int)((double)value * ((double)lp->lnd_effic / 100.0));
81
82     switch (combat_mode) {
83     case A_ATTACK:
84         return value;
85     case A_ASSAULT:
86         if (!(lcp->l_flags & L_MARINE))
87             return (int)(assault_penalty * value);
88         break;
89     case A_BOARD:
90         if (!(lcp->l_flags & L_MARINE))
91             return (int)(assault_penalty * men);
92     }
93
94     return value;
95 }
96
97 int
98 defense_val(struct lndstr *lp)
99 {
100     int men;
101     double value;
102     struct lchrstr *lcp;
103
104     if (((int)lp->lnd_effic) < LAND_MINEFF) {
105         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
106         lp->lnd_own = 0;
107         putland(lp->lnd_uid, lp);
108         return 0;
109     }
110
111     lcp = &lchr[(int)lp->lnd_type];
112
113     men = total_mil(lp);
114
115     if (men < 0)
116         men = 0;
117     if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
118         !(lcp->l_flags & L_MARINE))
119         return men;
120
121     value = men * lp->lnd_def;
122
123     value *=
124         ((double)land_mob_max + lp->lnd_harden) / (double)land_mob_max;
125     value = (int)((double)value * ((double)lp->lnd_effic / 100.0));
126     value = (int)ldround(value, 1);
127
128     /* If there are military on the unit, you get at least a 1
129        man defensive unit, except for spies */
130     if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
131         return 1;
132
133     return (int)(value);
134 }
135
136 int
137 total_mil(struct lndstr *lp)
138 {
139     struct lchrstr *lcp;
140     double men;
141
142     lcp = &lchr[(int)lp->lnd_type];
143
144     men = lnd_getmil(lp);
145 /*      men *= ((double)lp->lnd_effic)/100.0;*/
146
147     return ldround(men, 1);
148 }
149
150 void
151 lnd_print(struct llist *llp, s_char *s)
152 {
153     if (llp->land.lnd_own == player->cnum)
154         pr("%s %s\n", prland(&llp->land), s);
155     else
156         wu(0, llp->land.lnd_own, "%s %s\n", prland(&llp->land), s);
157 }
158
159 void
160 lnd_delete(struct llist *llp, s_char *s)
161 {
162     if (s)
163         lnd_print(llp, s);
164     putland(llp->land.lnd_uid, &llp->land);
165     emp_remque((struct emp_qelem *)llp);
166     free((s_char *)llp);
167 }
168
169 int
170 lnd_take_casualty(int combat_mode, struct llist *llp, int cas)
171                         /* attacking or assaulting or paratrooping? */
172                         /* number of casualties to take */
173 {
174     int eff_eq;
175     int n;
176     int biggest;
177     int civs;
178     int nowned;
179     coord ret_x, ret_y;
180     coord bx, by;
181     struct sctstr sect;
182     int ret_chance;
183     s_char buf[1024];
184     int taken;
185     int nowhere_to_go = 0;
186     struct sctstr rsect;
187     double mobcost;
188     s_char orig;
189     int mob;
190
191
192
193     taken = lnd_getmil(&(llp->land));
194     /* Spies always die */
195     if (llp->lcp->l_flags & L_SPY) {
196         eff_eq = 100;
197         llp->land.lnd_effic = 0;
198     } else {
199         eff_eq =
200             ldround((((double)cas * 100.0) / (double)llp->lcp->l_mil), 1);
201         llp->land.lnd_effic -= eff_eq;
202         lnd_submil(&(llp->land), cas);
203     }
204
205     if (llp->land.lnd_effic < LAND_MINEFF) {
206         sprintf(buf, "dies %s %s!",
207                 combat_mode ? att_mode[combat_mode] : (s_char *)
208                 "defending", xyas(llp->land.lnd_x, llp->land.lnd_y,
209                                   llp->land.lnd_own));
210         lnd_delete(llp, buf);
211         /* Since we killed the unit, we killed all the mil on it */
212         return taken;
213     } else {
214         /* Ok, now, how many did we take off? (sould be the diff) */
215         taken = taken - lnd_getmil(&(llp->land));
216     }
217
218     if (llp->land.lnd_effic >= llp->land.lnd_retreat)
219         return taken;
220
221     /* we're being boarded */
222     if (llp->land.lnd_ship >= 0 && combat_mode == A_DEFEND)
223         return taken;
224
225     /* we're being boarded */
226     if (llp->land.lnd_land >= 0 && combat_mode == A_DEFEND)
227         return taken;
228
229     /* Have to make a retreat check */
230
231     ret_chance = llp->land.lnd_retreat - llp->land.lnd_effic;
232     if (roll(100) < ret_chance) {
233         pr("\n");
234         lnd_print(llp, "fails morale check!");
235         llp->land.lnd_mission = 0;
236         llp->land.lnd_harden = 0;
237         if (llp->land.lnd_ship >= 0 || llp->land.lnd_land >= 0)
238             nowhere_to_go = 1;
239         else if (combat_mode == A_DEFEND) {
240             /*
241              * defending unit.. find a place to send it
242              * strategy: look for the most-populated 
243              * adjacent sector that is owned by the unit
244              * player->owner. Charge mob..
245              */
246             biggest = -1;
247             nowned = 0;
248             for (n = 1; n <= 6; ++n) {
249                 ret_x = llp->land.lnd_x + diroff[n][0];
250                 ret_y = llp->land.lnd_y + diroff[n][1];
251                 getsect(ret_x, ret_y, &sect);
252                 if (sect.sct_own != llp->land.lnd_own)
253                     continue;
254                 if (sect.sct_type == SCT_MOUNT)
255                     continue;
256                 ++nowned;
257                 civs = sect.sct_item[I_CIVIL];
258                 if (civs > biggest) {
259                     biggest = civs;
260                     bx = sect.sct_x;
261                     by = sect.sct_y;
262                 }
263             }
264             if (!nowned)
265                 nowhere_to_go = 1;
266             else {
267                 /* retreat to bx,by */
268                 llp->land.lnd_x = bx;
269                 llp->land.lnd_y = by;
270                 getsect(bx, by, &rsect);
271                 mobcost = lnd_mobcost(&llp->land, &rsect, MOB_ROAD);
272                 mob = llp->land.lnd_mobil - (int)mobcost;
273                 if (mob < -127)
274                     mob = -127;
275                 orig = llp->land.lnd_mobil;
276                 llp->land.lnd_mobil = (s_char)mob;
277                 if (llp->land.lnd_mobil > orig)
278                     llp->land.lnd_mobil = (-127);
279                 sprintf(buf, "retreats at %d%% efficiency to %s!",
280                         llp->land.lnd_effic,
281                         xyas(bx, by, llp->land.lnd_own));
282                 lnd_delete(llp, buf);
283             }
284         } else {                /* attacking from a sector */
285             sprintf(buf, "leaves the battlefield at %d%% efficiency",
286                     llp->land.lnd_effic);
287             if ((llp->land.lnd_mobil - (int)llp->mobil) < -127)
288                 llp->land.lnd_mobil = -127;
289             else
290                 llp->land.lnd_mobil -= (int)llp->mobil;
291             llp->mobil = 0.0;
292             lnd_delete(llp, buf);
293         }
294     }
295     if (nowhere_to_go) {
296         /* nowhere to go.. take more casualties */
297         llp->land.lnd_effic -= 10;
298         lnd_submil(&llp->land, llp->lcp->l_mil / 10);
299         if (llp->land.lnd_effic < LAND_MINEFF)
300             lnd_delete(llp, "has nowhere to retreat, and dies!");
301         else
302             lnd_print(llp,
303                       "has nowhere to retreat and takes extra losses!");
304     }
305
306     return taken;
307 }
308
309 void
310 lnd_takemob(struct emp_qelem *list, double loss)
311 {
312     struct emp_qelem *qp, *next;
313     struct llist *llp;
314     int new;
315     int mcost = ldround(combat_mob * loss, 1);
316
317     for (qp = list->q_forw; qp != list; qp = next) {
318         next = qp->q_forw;
319         llp = (struct llist *)qp;
320 /*
321                 if (chance(loss))
322                         use_supply(&llp->land);
323                 if (llp->land.lnd_mission == MI_RESERVE)
324                         new = llp->land.lnd_mobil - mcost/2;
325                 else
326  */
327         new = llp->land.lnd_mobil - mcost;
328         if (new < -127)
329             new = -127;
330         llp->land.lnd_mobil = (s_char)new;
331     }
332 }
333 int
334 lnd_getmil(struct lndstr *lp)
335 {
336     return lp->lnd_item[I_MILIT];
337 }
338
339 void
340 lnd_submil(struct lndstr *lp, int num)
341 {
342     int new = lp->lnd_item[I_MILIT] - num;
343     lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
344 }
345
346 int
347 lnd_spyval(struct lndstr *lp)
348 {
349     if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
350         return (lp->lnd_spy * (lp->lnd_effic / 100.0)) + 2;
351     else
352         return (lp->lnd_spy * (lp->lnd_effic / 100.0));
353 }
354
355 int
356 intelligence_report(int destination, struct lndstr *lp, int spy,
357                     s_char *mess)
358 {
359     struct lchrstr *lcp;
360     s_char buf1[80], buf2[80], buf3[80];
361     double estimate = 0.0;      /* estimated defense value */
362
363     if (destination == 0)
364         return 0;
365
366     if (lp->lnd_own == 0)
367         return 0;
368
369     lcp = &lchr[(int)lp->lnd_type];
370
371     memset(buf1, 0, sizeof(buf1));
372     memset(buf2, 0, sizeof(buf2));
373     memset(buf3, 0, sizeof(buf3));
374     if (chance((double)(spy + lp->lnd_vis) / 10.0)) {
375         if (destination == player->cnum)
376             pr("%s %s", mess, prland(lp));
377         else
378             sprintf(buf1, "%s %s", mess, prland(lp));
379
380         estimate = lnd_getmil(lp);
381
382         if (chance((double)(spy + lp->lnd_vis) / 20.0)) {
383
384             if (destination == player->cnum)
385                 pr(" (eff %d, mil %d", roundintby(lp->lnd_effic, 5),
386                    roundintby(lnd_getmil(lp), 10));
387             else
388                 sprintf(buf2, " (eff %d, mil %d",
389                         roundintby(lp->lnd_effic, 5),
390                         roundintby(lnd_getmil(lp), 10));
391             estimate = lnd_getmil(lp) * lp->lnd_effic / 100.0;
392
393             if (chance((double)(spy + lp->lnd_vis) / 20.0)) {
394                 int t;
395                 t = lp->lnd_tech - 20 + roll(40);
396                 t = max(t, 0);
397                 if (destination == player->cnum)
398                     pr(", tech %d)\n", t);
399                 else
400                     sprintf(buf3, ", tech %d)\n", t);
401             } else {
402                 if (destination == player->cnum)
403                     pr(")\n");
404                 else
405                     sprintf(buf3, ")\n");
406             }
407         } else {
408             if (destination == player->cnum)
409                 pr("\n");
410             else
411                 sprintf(buf2, "\n");
412         }
413     }
414
415     if (destination != player->cnum) {
416         wu(0, destination, "%s%s%s", buf1, buf2, buf3);
417     }
418
419     if (lp->lnd_ship < 0 || lcp->l_flags & L_MARINE)
420         estimate *= lp->lnd_def;
421
422     return (int)estimate;
423 }
424
425 /* Used by the spy command to count land units in a sector.  If used
426    for anything else, you may want to reconsider, because this doesn't
427    always count spies. :) */
428 int
429 count_sect_units(struct sctstr *sp)
430 {
431     int count = 0;
432     struct nstr_item ni;
433     struct lndstr land;
434
435     snxtitem_all(&ni, EF_LAND);
436     while (nxtitem(&ni, (s_char *)&land)) {
437         if (!land.lnd_own)
438             continue;
439         if (land.lnd_x != sp->sct_x || land.lnd_y != sp->sct_y)
440             continue;
441         /* Don't always see spies */
442         if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
443             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
444                 continue;
445         }
446         /* Got here, report it */
447         ++count;
448     }
449
450     return count;
451 }
452
453 void
454 count_units(struct shpstr *sp)
455 {
456     struct nstr_item ni;
457     struct lndstr land;
458     int nland = 0;
459
460     if (sp->shp_effic < SHIP_MINEFF)
461         return;
462
463     snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
464     while (nxtitem(&ni, (s_char *)&land)) {
465         if (land.lnd_own == 0)
466             continue;
467         if (land.lnd_ship == sp->shp_uid)
468             nland++;
469     }
470
471     if (sp->shp_nland != nland) {
472         sp->shp_nland = nland;
473         putship(sp->shp_uid, sp);
474     }
475 }
476
477 void
478 lnd_count_units(struct lndstr *lp)
479 {
480     struct nstr_item ni;
481     struct lndstr land;
482     int nland = 0;
483
484     if (lp->lnd_effic < LAND_MINEFF)
485         return;
486
487     snxtitem_xy(&ni, EF_LAND, lp->lnd_x, lp->lnd_y);
488     while (nxtitem(&ni, (s_char *)&land)) {
489         if (land.lnd_own == 0)
490             continue;
491         if (land.lnd_land == lp->lnd_uid)
492             nland++;
493     }
494
495     if (lp->lnd_nland != nland) {
496         lp->lnd_nland = nland;
497         putland(lp->lnd_uid, lp);
498     }
499 }
500
501 void
502 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
503
504
505 /*      int     wantflags;
506         int     nowantflags;
507 */
508 {
509     struct lndstr land;
510     struct lchrstr *lcp;
511     struct llist *llp;
512
513     emp_initque(list);
514     while (nxtitem(ni, (s_char *)&land)) {
515         if (!player->owner)
516             continue;
517         if (opt_MARKET) {
518             if (ontradingblock(EF_LAND, (int *)&land)) {
519                 pr("unit #%d inelligible - it's for sale.\n",
520                    land.lnd_uid);
521                 continue;
522             }
523         }
524         lcp = &lchr[(int)land.lnd_type];
525 /*              if (wantflags && (lcp->m_flags & wantflags) != wantflags)
526                         continue;
527                 if (nowantflags && lcp->m_flags & nowantflags)
528                         continue;
529 */
530         /* This abuse is better fixed by building a unit with the normal negative
531            mobility that everything else is built with */
532 /* Just so that the player can't build a bunch of land units, and them
533    march them a few minutes later... */
534 /*
535                 if (opt_MOB_ACCESS) {
536                   if (land.lnd_effic < 11 &&
537                     land.lnd_mobil < etu_per_update) {
538                     pr("Land unit #%d needs at least %d mob to march.\n",
539                         land.lnd_uid, etu_per_update);
540                     continue;
541                   }
542                 }
543 */
544         land.lnd_mission = 0;
545         land.lnd_rflags = 0;
546         land.lnd_harden = 0;
547         memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
548         putland(land.lnd_uid, &land);
549         llp = (struct llist *)malloc(sizeof(struct llist));
550         llp->lcp = lcp;
551         llp->land = land;
552         llp->mobil = (double)land.lnd_mobil;
553         emp_insque(&llp->queue, list);
554     }
555 }
556
557 /* This function assumes that the list was created by lnd_sel */
558 void
559 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
560         int *togetherp, natid actor)
561 {
562     struct emp_qelem *qp;
563     struct emp_qelem *next;
564     struct llist *llp;
565     struct sctstr sect;
566     struct lndstr land;
567     coord allx;
568     coord ally;
569     int first = 1;
570     s_char mess[128];
571     int rel;
572
573     *minmobp = 9876.0;
574     *maxmobp = -9876.0;
575     *togetherp = 1;
576     for (qp = list->q_back; qp != list; qp = next) {
577         next = qp->q_back;
578         llp = (struct llist *)qp;
579         getland(llp->land.lnd_uid, &land);
580         if (land.lnd_own != actor) {
581             mpr(actor, "%s was disbanded at %s\n",
582                 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
583             emp_remque((struct emp_qelem *)llp);
584             free((s_char *)llp);
585             continue;
586         }
587         if (land.lnd_ship >= 0) {
588             lnd_mess("is on a ship", llp);
589             continue;
590         }
591         if (land.lnd_land >= 0) {
592             lnd_mess("is on a unit", llp);
593             continue;
594         }
595         if (!getsect(land.lnd_x, land.lnd_y, &sect)) {
596             lnd_mess("was sucked into the sky by a strange looking spaceland", llp);    /* heh -KHS */
597             continue;
598         }
599         if (!(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
600             !(lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) &&
601             lnd_getmil(&llp->land) == 0) {
602             lnd_mess("has no mil on it to guide it", llp);
603             continue;
604         }
605         rel = getrel(getnatp(sect.sct_own), player->cnum);
606         if (sect.sct_own != land.lnd_own && rel != ALLIED &&
607             !(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
608             sect.sct_own) {
609             sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
610             lnd_mess(mess, llp);
611             continue;
612         }
613         if (first) {
614             allx = land.lnd_x;
615             ally = land.lnd_y;
616             first = 0;
617         }
618         if (land.lnd_x != allx || land.lnd_y != ally)
619             *togetherp = 0;
620         if (land.lnd_mobil + 1 < (int)llp->mobil) {
621             llp->mobil = (double)land.lnd_mobil;
622         }
623         if (llp->mobil < *minmobp)
624             *minmobp = llp->mobil;
625         if (llp->mobil > *maxmobp)
626             *maxmobp = llp->mobil;
627         llp->land = land;
628     }
629 }
630
631 void
632 lnd_put(struct emp_qelem *list, natid actor)
633 {
634     struct emp_qelem *qp;
635     struct emp_qelem *newqp;
636     struct llist *llp;
637
638     qp = list->q_back;
639     while (qp != list) {
640         llp = (struct llist *)qp;
641         if (actor) {
642             mpr(actor, "%s stopped at %s\n", prland(&llp->land),
643                 xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
644             if (llp->mobil < -127)
645                 llp->mobil = -127;
646             llp->land.lnd_mobil = llp->mobil;
647         }
648         putland(llp->land.lnd_uid, &llp->land);
649         newqp = qp->q_back;
650         emp_remque(qp);
651         free((s_char *)qp);
652         qp = newqp;
653     }
654 }
655
656 void
657 lnd_sweep(struct emp_qelem *land_list, int verbose, int takemob,
658           natid actor)
659 {
660     struct emp_qelem *qp;
661     struct emp_qelem *next;
662     struct llist *llp;
663     struct sctstr sect;
664     int mines, m, max, sshells, lshells;
665     double mobcost;
666
667     for (qp = land_list->q_back; qp != land_list; qp = next) {
668         next = qp->q_back;
669         llp = (struct llist *)qp;
670         if (!(llp->lcp->l_flags & L_ENGINEER)) {
671             if (verbose)
672                 mpr(actor, "%s is not an engineer!\n", prland(&llp->land));
673             continue;
674         }
675         if (takemob && llp->mobil < 0.0) {
676             if (verbose)
677                 lnd_mess("is out of mobility", llp);
678             continue;
679         }
680         getsect(llp->land.lnd_x, llp->land.lnd_y, &sect);
681         if (sect.sct_oldown == llp->land.lnd_own) {
682             if (verbose)
683                 mpr(actor,
684                     "%s is in a sector completely owned by you.  Don't bother digging up mines there!\n",
685                     prland(&llp->land));
686             continue;
687         }
688         if (sect.sct_type == SCT_BSPAN) {
689             if (verbose)
690                 mpr(actor, "%s is on a bridge.  No mines there!\n",
691                     prland(&llp->land));
692             continue;
693         }
694         if (takemob) {
695 /*                      mobcost = llp->land.lnd_effic * 0.01 * llp->lcp->l_spd;*/
696             mobcost = llp->land.lnd_spd;
697             mobcost = 480.0 / (mobcost +
698                                techfact(llp->land.lnd_tech, mobcost));
699             llp->mobil -= mobcost;
700             llp->land.lnd_mobil = (int)llp->mobil;
701             llp->land.lnd_harden = 0;
702         }
703         putland(llp->land.lnd_uid, &llp->land);
704         if (!(mines = sect.sct_mines))
705             continue;
706         max = llp->lcp->l_item[I_SHELL];
707         lshells = llp->land.lnd_item[I_SHELL];
708         sshells = sect.sct_item[I_SHELL];
709         for (m = 0; mines > 0 && m < max * 2; m++) {
710             if (chance(0.5 * llp->lcp->l_att)) {
711                 mpr(actor, "Sweep...\n");
712                 mines--;
713                 if (lshells < max)
714                     ++lshells;
715                 else if (sshells < ITEM_MAX)
716                     ++sshells;
717             }
718         }
719         sect.sct_mines = mines;
720         llp->land.lnd_item[I_SHELL] = lshells;
721         sect.sct_item[I_SHELL] = sshells;
722         putland(llp->land.lnd_uid, &llp->land);
723         putsect(&sect);
724     }
725 }
726
727 static int
728 contains_engineer(struct emp_qelem *list)
729 {
730     struct emp_qelem *qp;
731     struct emp_qelem *next;
732     struct llist *llp;
733
734     for (qp = list->q_back; qp != list; qp = next) {
735         next = qp->q_back;
736         llp = (struct llist *)qp;
737         if (llp->lcp->l_flags & L_ENGINEER)
738             return 1;
739     }
740     return 0;
741 }
742
743 int
744 lnd_check_mines(struct emp_qelem *land_list)
745 {
746     struct emp_qelem *qp;
747     struct emp_qelem *next;
748     struct llist *llp;
749     struct sctstr sect;
750     int stopping = 0;
751     int with_eng = contains_engineer(land_list);
752
753     for (qp = land_list->q_back; qp != land_list; qp = next) {
754         next = qp->q_back;
755         llp = (struct llist *)qp;
756         getsect(llp->land.lnd_x, llp->land.lnd_y, &sect);
757         if (sect.sct_oldown == llp->land.lnd_own)
758             continue;
759         if (sect.sct_type == SCT_BSPAN)
760             continue;
761         if (!sect.sct_mines)
762             continue;
763         if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
764             lnd_hit_mine(&llp->land, llp->lcp);
765             sect.sct_mines--;
766             putsect(&sect);
767             putland(llp->land.lnd_uid, (s_char *)&llp->land);
768             if (!llp->land.lnd_own) {
769                 stopping = 1;
770                 emp_remque(qp);
771                 free((s_char *)qp);
772             }
773         }
774     }
775     return stopping;
776 }
777
778 void
779 lnd_list(struct emp_qelem *land_list)
780 {
781     struct emp_qelem *qp;
782     struct emp_qelem *next;
783     struct llist *llp;
784     struct lndstr *lnd;
785
786     pr("lnd#     land type       x,y    a  eff  sh gun xl  mu tech retr fuel\n");
787
788     for (qp = land_list->q_back; qp != land_list; qp = next) {
789         next = qp->q_back;
790         llp = (struct llist *)qp;
791         lnd = &llp->land;
792         pr("%4d ", lnd->lnd_uid);
793         pr("%-16.16s ", llp->lcp->l_name);
794         prxy("%4d,%-4d ", lnd->lnd_x, lnd->lnd_y, llp->land.lnd_own);
795         pr("%1c", lnd->lnd_army);
796         pr("%4d%%", lnd->lnd_effic);
797         pr("%4d", lnd->lnd_item[I_SHELL]);
798         pr("%4d", lnd->lnd_item[I_GUN]);
799         count_land_planes(lnd);
800         pr("%3d", lnd->lnd_nxlight);
801         pr("%4d", lnd->lnd_mobil);
802         pr("%4d", lnd->lnd_tech);
803         pr("%4d%%", lnd->lnd_retreat);
804         pr("%5d\n", lnd->lnd_fuel);
805     }
806 }
807
808 void
809 lnd_mess(s_char *str, struct llist *llp)
810 {
811     mpr(llp->land.lnd_own, "%s %s & stays in %s\n",
812         prland(&llp->land),
813         str, xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
814     if (llp->mobil < -127)
815         llp->mobil = -127;
816     llp->land.lnd_mobil = llp->mobil;
817     putland(llp->land.lnd_uid, &llp->land);
818     emp_remque((struct emp_qelem *)llp);
819     free((s_char *)llp);
820 }
821
822 static int
823 lnd_count(struct emp_qelem *list)
824 {
825     struct emp_qelem *qp;
826     struct emp_qelem *next;
827     struct llist *llp;
828     int count = 0;
829
830     for (qp = list->q_back; qp != list; qp = next) {
831         next = qp->q_back;
832         llp = (struct llist *)qp;
833         ++count;
834     }
835     return count;
836 }
837
838 static int
839 lnd_damage(struct emp_qelem *list, int totdam)
840 {
841     struct emp_qelem *qp;
842     struct emp_qelem *next;
843     struct llist *llp;
844     int dam;
845     int count;
846
847     if (!totdam || !(count = lnd_count(list)))
848         return 0;
849     dam = ldround(((double)totdam / (double)count), 1);
850     for (qp = list->q_back; qp != list; qp = next) {
851         next = qp->q_back;
852         llp = (struct llist *)qp;
853         /* have to get it again because of collateral damage */
854         getland(llp->land.lnd_uid, &llp->land);
855         landdamage(&llp->land, dam);
856         putland(llp->land.lnd_uid, &llp->land);
857         if (!llp->land.lnd_own) {
858             emp_remque(qp);
859             free((s_char *)qp);
860         }
861     }
862     return dam;
863 }
864
865 static int
866 lnd_easiest_target(struct emp_qelem *list)
867 {
868     struct emp_qelem *qp;
869     struct emp_qelem *next;
870     struct llist *llp;
871     int hard;
872     int easiest = 9876;         /* things start great for victim */
873     int count = 0;
874
875     for (qp = list->q_back; qp != list; qp = next) {
876         next = qp->q_back;
877         llp = (struct llist *)qp;
878         hard = lnd_hardtarget(&llp->land);
879         if (hard < easiest)
880             easiest = hard;     /* things get worse for victim */
881         ++count;
882     }
883     return easiest - count;
884 }
885
886 static int
887 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
888                          natid victim)
889 {
890     int dam;
891     struct emp_qelem msl_list, *qp, *newqp;
892
893     msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
894
895     dam = msl_launch_mindam(&msl_list, newx, newy,
896                             lnd_easiest_target(list), EF_LAND,
897                             lnd_count(list) * 20, "troops", victim,
898                             MI_INTERDICT);
899     if (dam) {
900         mpr(victim, "missile interdiction mission does %d damage!\n", dam);
901         collateral_damage(newx, newy, dam, 0);
902     }
903     qp = msl_list.q_forw;
904     while (qp != msl_list.q_forw) {
905         newqp = qp->q_forw;
906         emp_remque(qp);
907         free(qp);
908         qp = newqp;
909     }
910     return dam;
911 }
912
913 #if 0
914 /* Steve M. - commented out for now until abuse is decided upon */
915 /* risner: allow forts to interdict land units. */
916 static int
917 lnd_fort_interdiction(struct emp_qelem *list,
918                       coord newx, coord newy, natid victim)
919 {
920     struct nstr_sect ns;
921     struct sctstr fsect;
922     int trange;
923     double range, range2, guneff;
924     int shell, gun;
925     int dam;
926     int totdam = 0;
927     int i;
928
929     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
930     while (nxtsct(&ns, &fsect)) {
931         if (fsect.sct_own == 0)
932             continue;
933         if (fsect.sct_own == victim)
934             continue;
935         if (fsect.sct_type != SCT_FORTR)
936             continue;
937         if (getrel(getnatp(fsect.sct_own), victim) >= NEUTRAL)
938             continue;
939         gun = fsect.sct_item[I_GUN];
940         if (gun < 1)
941             continue;
942         range = tfactfire(fsect.sct_own, (double)min(gun, 7));
943         if (fsect.sct_effic > 59)
944             range++;
945         range2 = roundrange(range);
946         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
947         if (trange > range2)
948             continue;
949         if (fsect.sct_item[I_MILIT] < 5)
950             continue;
951         shell = fsect.sct_item[I_SHELL];
952         if (shell < 1)
953             shell += supply_commod(fsect.sct_own,
954                                    fsect.sct_x, fsect.sct_y, I_SHELL, 1);
955         if (shell < 1)
956             continue;
957         shell--;
958         fsect.sct_item[I_SHELL] = shell;
959         putsect(&fsect);
960         if (gun > 7)
961             gun = 7;
962         guneff = landgun((int)fsect.sct_effic, gun);
963         dam = (int)guneff;
964         totdam += dam;
965         mpr(victim, "Incoming fire does %d damage!\n", dam);
966         wu(0, fsect.sct_own,
967            "%s fires at %s land units in %s for %d!\n",
968            xyas(fsect.sct_x, fsect.sct_y,
969                 fsect.sct_own),
970            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
971         nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
972     }
973     if (totdam > 0)
974         return lnd_damage(list, totdam);
975     return 0;
976 }
977 #endif
978
979 int
980 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
981 {
982     int stopping = 0;
983
984 #if 0
985     if (!opt_NO_FORT_FIRE)
986 /* Steve M. - commented out for now until abuse is decided upon */
987         stopping |= lnd_fort_interdiction(list, newx, newy, victim);
988 #endif
989
990     stopping |=
991         lnd_damage(list,
992                    unit_interdict(newx, newy, victim, "land units",
993                                   lnd_easiest_target(list), MI_INTERDICT));
994
995     stopping |=
996         lnd_damage(list,
997                    lnd_missile_interdiction(list, newx, newy, victim));
998     return stopping;
999 }
1000
1001 /* high value of hardtarget is harder to hit */
1002 int
1003 lnd_hardtarget(struct lndstr *lp)
1004 {
1005     struct sctstr sect;
1006
1007     getsect(lp->lnd_x, lp->lnd_y, &sect);
1008     return (int)(((double)lp->lnd_effic / 100.0) *
1009                  (10 + dchr[sect.sct_type].d_dstr * 2 +
1010                   (double)lp->lnd_spd / 2.0 - lp->lnd_vis));
1011 }
1012
1013 int
1014 lnd_hit_mine(struct lndstr *lp, struct lchrstr *lcp)
1015 {
1016     double m;
1017
1018     mpr(lp->lnd_own, "Blammo! Landmines detected in %s! ",
1019         xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
1020
1021     nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
1022
1023     m = roll(20) + 10;
1024     if (lcp->l_flags & L_ENGINEER)
1025         m /= 2.0;
1026
1027     landdamage(lp, ldround(m, 1));
1028
1029     return (int)m;
1030 }
1031
1032 double
1033 lnd_mobcost(struct lndstr *lp, struct sctstr *sp, int mobtype)
1034 {
1035     double mobcost;
1036     double smobcost;
1037
1038     /* supply unit's speed depends on their eff, since
1039        that is their purpose */
1040     if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
1041         mobcost = lp->lnd_effic * 0.01 * lp->lnd_spd;
1042     else
1043         mobcost = lp->lnd_spd;
1044     if (mobcost < 0.01)
1045         mobcost = 0.01;
1046
1047 /* sector_mcost now takes 2 different arguments, a sector pointer, and
1048    whether or not to figure in the highway bonus, rail bonus or none.
1049    bridge heads, bridges and highways have built-in highways bonus
1050    because they are a 1, and this will discount that. */
1051
1052     smobcost = (double)sector_mcost(sp, mobtype);
1053     if (smobcost < 0.01)
1054         smobcost = 0.01;
1055
1056 /* marching through 0 mobility conquered sectors takes lots of mobility,
1057    unless you are a train.  Capturing railways is a good thing. */
1058
1059     if (sp->sct_own != sp->sct_oldown && sp->sct_mobil <= 0 &&
1060         smobcost < LND_MINMOBCOST && mobtype != MOB_RAIL)
1061         smobcost = LND_MINMOBCOST;
1062
1063     mobcost = smobcost * 5.0 * 480.0 /
1064         (mobcost + techfact(lp->lnd_tech, mobcost));
1065
1066     return mobcost;
1067 }
1068
1069 int
1070 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
1071                    int together)
1072 {
1073     struct sctstr sect, osect;
1074     struct emp_qelem *qp;
1075     struct emp_qelem *qp2;
1076     struct emp_qelem *next;
1077     struct llist *llp;
1078     struct emp_qelem cur, done;
1079     coord dx;
1080     coord dy;
1081     coord newx;
1082     coord newy;
1083     int stopping = 0;
1084     int visible;
1085     int stop;
1086     s_char dp[80];
1087     int rel;
1088     int oldown;
1089
1090     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
1091         lnd_put(list, actor);
1092         return 1;
1093     }
1094     dx = diroff[dir][0];
1095     dy = diroff[dir][1];
1096     for (qp = list->q_back; qp != list; qp = next) {
1097         next = qp->q_back;
1098         llp = (struct llist *)qp;
1099         getsect(llp->land.lnd_x, llp->land.lnd_y, &osect);
1100         oldown = osect.sct_own;
1101         newx = xnorm(llp->land.lnd_x + dx);
1102         newy = ynorm(llp->land.lnd_y + dy);
1103         getsect(newx, newy, &sect);
1104         rel = getrel(getnatp(sect.sct_own), player->cnum);
1105         if ((sect.sct_own != actor && rel != ALLIED &&
1106              !(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
1107              sect.sct_own) || (sect.sct_type == SCT_WATER ||
1108                                sect.sct_type == SCT_SANCT ||
1109                                sect.sct_type == SCT_WASTE)) {
1110             if (together) {
1111                 pr("can't go to %s\n", xyas(newx, newy, actor));
1112                 return 1;
1113             } else {
1114                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
1115                 lnd_mess(dp, llp);
1116                 continue;
1117             }
1118         }
1119         if (sect.sct_rail == 0 &&
1120             lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) {
1121             if (together) {
1122                 pr("no rail system in %s\n", xyas(newx, newy, actor));
1123                 return 1;
1124             } else {
1125                 sprintf(dp, "has no rail system in %s",
1126                         xyas(newx, newy, actor));
1127                 lnd_mess(dp, llp);
1128                 continue;
1129             }
1130         }
1131         /* Note we check would_abandon first because we don't want
1132            to always have to do these checks */
1133         if (would_abandon(&osect, I_CIVIL, 0, &(llp->land))) {
1134             stop = 0;
1135             if (!want_to_abandon(&osect, I_CIVIL, 0, &(llp->land))) {
1136                 stop = 1;
1137             }
1138             /* now check stuff */
1139             if (!check_sect_ok(&sect))
1140                 return 1;
1141             if (!check_sect_ok(&osect))
1142                 return 1;
1143             for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
1144                 if (!check_land_ok(&(((struct llist *)qp2)->land)))
1145                     return 1;
1146             }
1147             if (stop) {
1148                 lnd_mess("stops", llp);
1149                 continue;
1150             }
1151         }
1152         if (llp->mobil <= 0.0) {
1153             lnd_mess("is out of mobility", llp);
1154             continue;
1155         }
1156         llp->land.lnd_x = newx;
1157         llp->land.lnd_y = newy;
1158         if (lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) {
1159             llp->mobil -= lnd_mobcost(&llp->land, &sect, MOB_RAIL);
1160         } else {
1161             llp->mobil -= lnd_mobcost(&llp->land, &sect, MOB_ROAD);
1162         }
1163         llp->land.lnd_mobil = (int)llp->mobil;
1164         llp->land.lnd_harden = 0;
1165         putland(llp->land.lnd_uid, &llp->land);
1166         putsect(&osect);
1167         getsect(osect.sct_x, osect.sct_y, &osect);
1168         if (osect.sct_own != oldown && oldown == player->cnum) {
1169             /* It was your sector, now it's not.  Simple :) */
1170             pr("You no longer own %s\n",
1171                xyas(osect.sct_x, osect.sct_y, player->cnum));
1172         }
1173         if (rel != ALLIED && sect.sct_own != actor && sect.sct_own) {   /* must be a spy */
1174             /* Always a 10% chance of getting caught. */
1175             if (chance(LND_SPY_DETECT_CHANCE(llp->land.lnd_effic))) {
1176                 if (rel == NEUTRAL || rel == FRIENDLY) {
1177                     wu(0, sect.sct_own,
1178                        "%s unit spotted in %s\n", cname(player->cnum),
1179                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1180                     setrel(sect.sct_own, llp->land.lnd_own, HOSTILE);
1181                 } else if (rel == HOSTILE || rel == AT_WAR ||
1182                            rel == SITZKRIEG || rel == MOBILIZATION) {
1183                     wu(0, sect.sct_own,
1184                        "%s spy shot in %s\n", cname(player->cnum),
1185                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1186                     pr("%s was shot and killed.\n", prland(&llp->land));
1187                     llp->land.lnd_effic = 0;
1188                     putland(llp->land.lnd_uid, &llp->land);
1189                     lnd_delete(llp, 0);
1190                 }
1191             }
1192         }
1193     }
1194     if (QEMPTY(list))
1195         return stopping;
1196     lnd_sweep(list, 0, 1, actor);
1197     stopping |= lnd_check_mines(list);
1198     if (QEMPTY(list))
1199         return stopping;
1200
1201     /* interdict land units sector by sector */
1202     emp_initque(&cur);
1203     emp_initque(&done);
1204     while (!QEMPTY(list)) {
1205         llp = (struct llist *)list->q_back;
1206         newx = llp->land.lnd_x;
1207         newy = llp->land.lnd_y;
1208         /* move units in NEWX,NEWY to cur */
1209         visible = 0;
1210         for (qp = list->q_back; qp != list; qp = next) {
1211             next = qp->q_back;
1212             llp = (struct llist *)qp;
1213             if (llp->land.lnd_x == newx && llp->land.lnd_y == newy) {
1214                 emp_remque(qp);
1215                 emp_insque(qp, &cur);
1216                 if (!(lchr[(int)llp->land.lnd_type].l_flags & L_SPY))
1217                     visible = 1;
1218             }
1219         }
1220         /* interdict them */
1221         if (visible)
1222             stopping |= lnd_interdict(&cur, newx, newy, actor);
1223         /* move survivors to done */
1224         for (qp = cur.q_back; qp != &cur; qp = next) {
1225             next = qp->q_back;
1226             llp = (struct llist *)qp;
1227             emp_remque(qp);
1228             emp_insque(qp, &done);
1229         }
1230     }
1231     /* assign surviving land units back to list */
1232     emp_insque(list, &done);
1233     emp_remque(&done);
1234
1235     return stopping;
1236 }
1237
1238 /*
1239  * find all artillery units belonging
1240  * to the attacker or defender that can fire.
1241  * Each arty unit adds +1%/damage point
1242  *
1243  */
1244 int
1245 lnd_support(natid victim, natid attacker, coord x, coord y)
1246 {
1247     struct nstr_item ni;
1248     struct lndstr land;
1249     int rel, rel2;
1250     double dam = 0.0;
1251     int dist;
1252     int shell;
1253     int gun;
1254     double range, range2;
1255
1256     snxtitem_all(&ni, EF_LAND);
1257     while (nxtitem(&ni, (s_char *)&land)) {
1258         if (land.lnd_frg == 0)
1259             continue;
1260         if ((land.lnd_x == x) && (land.lnd_y == y))
1261             continue;
1262         if (land.lnd_ship >= 0)
1263             continue;
1264         if (land.lnd_land >= 0)
1265             continue;
1266         if (land.lnd_mission > 0)
1267             continue;
1268         if (land.lnd_effic < LAND_MINFIREEFF)
1269             continue;
1270         /* Do we have mil? */
1271         if (land.lnd_item[I_MILIT] <= 0)
1272             continue;
1273         rel = getrel(getnatp(land.lnd_own), attacker);
1274         rel2 = getrel(getnatp(land.lnd_own), victim);
1275         if ((land.lnd_own != attacker) &&
1276             ((rel != ALLIED) || (rel2 != AT_WAR)))
1277             continue;
1278
1279         /* do we have supplies? */
1280         if (!has_supply(&land))
1281             continue;
1282
1283         /* are we in range? */
1284         dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1285
1286         range = techfact((int)land.lnd_tech, (double)land.lnd_frg / 2.0);
1287
1288         range2 = roundrange(range);
1289         if (dist > range2)
1290             continue;
1291
1292         shell = land.lnd_item[I_SHELL];
1293         gun = land.lnd_item[I_GUN];
1294
1295         if (shell == 0 || gun == 0)
1296             continue;
1297
1298         use_supply(&land);
1299         nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1300         if (roll(100) < land.lnd_acc) {
1301             dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1302                                land.lnd_ammo, shell) / 2;
1303         } else {
1304             dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1305                                land.lnd_ammo, shell);
1306         }
1307         if (land.lnd_own != attacker)
1308             wu(0, land.lnd_own,
1309                "%s supported %s at %s\n",
1310                prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1311     }
1312     return (int)dam;
1313 }
1314
1315 s_char *
1316 lnd_path(int together, struct lndstr *lp, s_char *buf)
1317 {
1318     coord destx;
1319     coord desty;
1320     struct sctstr d_sect, sect;
1321     s_char *cp;
1322     double dummy;
1323
1324     if (!sarg_xy(buf, &destx, &desty))
1325         return 0;
1326     if (!together) {
1327         pr("Cannot go to a destination sector if not all starting in the same sector\n");
1328         return 0;
1329     }
1330     if (!getsect(destx, desty, &d_sect)) {
1331         pr("%d,%d is not a sector\n", destx, desty);
1332         return 0;
1333     }
1334     getsect(lp->lnd_x, lp->lnd_y, &sect);
1335     if (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
1336         cp = (s_char *)BestLandPath(buf, &sect, &d_sect, &dummy, MOB_RAIL);
1337     else
1338         cp = (s_char *)BestLandPath(buf, &sect, &d_sect, &dummy, MOB_ROAD);
1339     if (!cp) {
1340         pr("No owned path from %s to %s!\n",
1341            xyas(lp->lnd_x, lp->lnd_y, player->cnum),
1342            xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
1343         return 0;
1344     }
1345     pr("Using path '%s'\n", cp);
1346     return cp;
1347 }
1348
1349 int
1350 lnd_can_attack(struct lndstr *lp)
1351 {
1352     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1353
1354 /*      if (lcp->l_flags & L_SUPPLY ||
1355             lcp->l_flags & L_SECURITY ||
1356             lcp->l_flags & L_FLAK ||
1357             lcp->l_frg)
1358                 return 0; */
1359     if (lcp->l_flags & L_SUPPLY)
1360         return 0;
1361
1362     return 1;
1363 }
1364
1365 /*
1366  * Increase fortification value of LP.
1367  * Fortification costs mobility.  Use up to HARD_AMT mobility.
1368  * Return actual fortification increase.
1369  */
1370 int
1371 lnd_fortify (struct lndstr *lp, int hard_amt)
1372 {
1373     int mob_used;
1374     int eng;
1375
1376     if ((lp->lnd_ship >= 0) || lp->lnd_land >= 0)
1377         return 0;
1378
1379     hard_amt = min(lp->lnd_mobil, hard_amt);
1380
1381     if ((lp->lnd_harden + hard_amt) > land_mob_max)
1382         hard_amt = land_mob_max - lp->lnd_harden;
1383
1384     eng = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own);
1385
1386     if (eng)
1387         hard_amt = ((float)hard_amt * 1.5);
1388
1389     if ((lp->lnd_harden + hard_amt) > land_mob_max)
1390         hard_amt = land_mob_max - lp->lnd_harden;
1391
1392     /* Ok, set the mobility used */
1393     mob_used = hard_amt;
1394
1395     /* Now, if an engineer helped, it's really only 2/3rds of
1396        that */
1397     if (eng)
1398         mob_used = (int)((float)mob_used / 1.5);
1399
1400     /* If we increased it, but not much, we gotta take at least 1
1401        mob point. */
1402     if (mob_used <= 0 && hard_amt > 0)
1403         mob_used = 1;
1404
1405     lp->lnd_mobil -= mob_used;
1406     if (lp->lnd_mobil < 0)
1407         lp->lnd_mobil = 0;
1408
1409     lp->lnd_harden += hard_amt;
1410     lp->lnd_harden = min(lp->lnd_harden, land_mob_max);
1411
1412     return hard_amt;
1413 }
1414
1415 /*
1416  * Set LP's tech to TLEV along with everything else that depends on it.
1417  */
1418 void
1419 lnd_set_tech(struct lndstr *lp, int tlev)
1420 {
1421     struct lchrstr *lcp = lchr + lp->lnd_type;
1422     int tech_diff = tlev - lcp->l_tech;
1423
1424     if (CANT_HAPPEN(tech_diff < 0)) {
1425       tlev -= tech_diff;
1426       tech_diff = 0;
1427     }
1428
1429     lp->lnd_tech = tlev;
1430     lp->lnd_att = (float)LND_ATTDEF(lcp->l_att, tech_diff);
1431     lp->lnd_def = (float)LND_ATTDEF(lcp->l_def, tech_diff);
1432     lp->lnd_vul = (int)LND_VUL(lcp->l_vul, tech_diff);
1433     lp->lnd_spd = (int)LND_SPD(lcp->l_spd, tech_diff);
1434     lp->lnd_vis = (int)LND_VIS(lcp->l_vis, tech_diff);
1435     lp->lnd_spy = (int)LND_SPY(lcp->l_spy, tech_diff);
1436     lp->lnd_rad = (int)LND_RAD(lcp->l_rad, tech_diff);
1437     lp->lnd_frg = (int)LND_FRG(lcp->l_frg, tech_diff);
1438     lp->lnd_acc = (int)LND_ACC(lcp->l_acc, tech_diff);
1439     lp->lnd_dam = (int)LND_DAM(lcp->l_dam, tech_diff);
1440     lp->lnd_ammo = (int)LND_AMM(lcp->l_ammo, lcp->l_dam, tech_diff);
1441     lp->lnd_aaf = (int)LND_AAF(lcp->l_aaf, tech_diff);
1442     lp->lnd_fuelc = (int)LND_FC(lcp->l_fuelc, tech_diff);
1443     lp->lnd_fuelu = (int)LND_FU(lcp->l_fuelu, tech_diff);
1444     lp->lnd_maxlight = (int)LND_XPL(lcp->l_nxlight, tech_diff);
1445     lp->lnd_maxland = (int)LND_MXL(lcp->l_mxland, tech_diff);
1446 }