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