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