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