]> git.pond.sub.org Git - empserver/blob - src/lib/subs/lndsub.c
2e00d6f38740bbe9a986d66af1e9587de741ecbd
[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 void
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
337     if (destination == 0)
338         return;
339
340     if (lp->lnd_own == 0)
341         return;
342
343     lcp = &lchr[(int)lp->lnd_type];
344
345     memset(buf1, 0, sizeof(buf1));
346     memset(buf2, 0, sizeof(buf2));
347     memset(buf3, 0, sizeof(buf3));
348     if (chance((spy + lp->lnd_vis) / 10.0)) {
349         if (destination == player->cnum)
350             pr("%s %s", mess, prland(lp));
351         else
352             sprintf(buf1, "%s %s", mess, prland(lp));
353
354         if (chance((spy + lp->lnd_vis) / 20.0)) {
355             if (destination == player->cnum)
356                 pr(" (eff %d, mil %d",
357                    roundintby(lp->lnd_effic, 5),
358                    roundintby(lp->lnd_item[I_MILIT], 10));
359             else
360                 sprintf(buf2, " (eff %d, mil %d",
361                         roundintby(lp->lnd_effic, 5),
362                         roundintby(lp->lnd_item[I_MILIT], 10));
363
364             if (chance((spy + lp->lnd_vis) / 20.0)) {
365                 int t;
366                 t = lp->lnd_tech - 20 + roll(40);
367                 t = MAX(t, 0);
368                 if (destination == player->cnum)
369                     pr(", tech %d)\n", t);
370                 else
371                     sprintf(buf3, ", tech %d)\n", t);
372             } else {
373                 if (destination == player->cnum)
374                     pr(")\n");
375                 else
376                     sprintf(buf3, ")\n");
377             }
378         } else {
379             if (destination == player->cnum)
380                 pr("\n");
381             else
382                 sprintf(buf2, "\n");
383         }
384     }
385
386     if (destination != player->cnum) {
387         wu(0, destination, "%s%s%s", buf1, buf2, buf3);
388     }
389 }
390
391 /* Used by the spy command to count land units in a sector.  If used
392    for anything else, you may want to reconsider, because this doesn't
393    always count spies. :) */
394 int
395 count_sect_units(struct sctstr *sp)
396 {
397     int count = 0;
398     struct nstr_item ni;
399     struct lndstr land;
400
401     snxtitem_all(&ni, EF_LAND);
402     while (nxtitem(&ni, &land)) {
403         if (!land.lnd_own)
404             continue;
405         if (land.lnd_x != sp->sct_x || land.lnd_y != sp->sct_y)
406             continue;
407         /* Don't always see spies */
408         if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
409             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
410                 continue;
411         }
412         /* Got here, report it */
413         ++count;
414     }
415
416     return count;
417 }
418
419 void
420 count_units(struct shpstr *sp)
421 {
422     struct nstr_item ni;
423     struct lndstr land;
424     int nland = 0;
425
426     if (sp->shp_effic < SHIP_MINEFF)
427         return;
428
429     snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
430     while (nxtitem(&ni, &land)) {
431         if (land.lnd_own == 0)
432             continue;
433         if (land.lnd_ship == sp->shp_uid)
434             nland++;
435     }
436
437     if (sp->shp_nland != nland) {
438         sp->shp_nland = nland;
439         putship(sp->shp_uid, sp);
440     }
441 }
442
443 void
444 lnd_count_units(struct lndstr *lp)
445 {
446     struct nstr_item ni;
447     struct lndstr land;
448     int nland = 0;
449
450     if (lp->lnd_effic < LAND_MINEFF)
451         return;
452
453     snxtitem_xy(&ni, EF_LAND, lp->lnd_x, lp->lnd_y);
454     while (nxtitem(&ni, &land)) {
455         if (land.lnd_own == 0)
456             continue;
457         if (land.lnd_land == lp->lnd_uid)
458             nland++;
459     }
460
461     if (lp->lnd_nland != nland) {
462         lp->lnd_nland = nland;
463         putland(lp->lnd_uid, lp);
464     }
465 }
466
467 void
468 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
469 {
470     struct lndstr land;
471     struct lchrstr *lcp;
472     struct ulist *llp;
473     int this_mot;
474     int mobtype = MOB_MOVE;     /* indeterminate */
475
476     emp_initque(list);
477     while (nxtitem(ni, &land)) {
478         if (!player->owner)
479             continue;
480         if (opt_MARKET) {
481             if (ontradingblock(EF_LAND, &land)) {
482                 pr("unit #%d inelligible - it's for sale.\n",
483                    land.lnd_uid);
484                 continue;
485             }
486         }
487         /*
488          * The marching code gets confused when trains and non-trains
489          * march together.  Disallow for now.
490          */
491         this_mot = lnd_mobtype(&land);
492         if (this_mot != mobtype) {
493             if (mobtype == MOB_MOVE)
494                 mobtype = this_mot;
495             else if (mobtype == MOB_MARCH) {
496                 pr("%s is a train and can't march with the leader.\n",
497                    prland(&land));
498                 continue;
499             } else {
500                 pr("%s can't rail-march with the leading train.\n",
501                    prland(&land));
502                 continue;
503             }
504         }
505
506         lcp = &lchr[(int)land.lnd_type];
507         land.lnd_mission = 0;
508         land.lnd_rflags = 0;
509         land.lnd_harden = 0;
510         memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
511         putland(land.lnd_uid, &land);
512         llp = malloc(sizeof(struct ulist));
513         llp->chrp = (struct empobj_chr *)lcp;
514         llp->unit.land = land;
515         llp->mobil = land.lnd_mobil;
516         emp_insque(&llp->queue, list);
517     }
518 }
519
520 /* This function assumes that the list was created by lnd_sel */
521 void
522 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
523         int *togetherp, natid actor)
524 {
525     struct emp_qelem *qp;
526     struct emp_qelem *next;
527     struct ulist *llp;
528     struct sctstr sect;
529     struct lndstr land;
530     coord allx;
531     coord ally;
532     int first = 1;
533     char mess[128];
534     int rel;
535
536     *minmobp = 9876.0;
537     *maxmobp = -9876.0;
538     *togetherp = 1;
539     for (qp = list->q_back; qp != list; qp = next) {
540         next = qp->q_back;
541         llp = (struct ulist *)qp;
542         getland(llp->unit.land.lnd_uid, &land);
543         if (land.lnd_own != actor) {
544             mpr(actor, "%s was disbanded at %s\n",
545                 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
546             emp_remque((struct emp_qelem *)llp);
547             free(llp);
548             continue;
549         }
550         if (land.lnd_ship >= 0) {
551             lnd_mess("is on a ship", llp);
552             continue;
553         }
554         if (land.lnd_land >= 0) {
555             lnd_mess("is on a unit", llp);
556             continue;
557         }
558         if (!getsect(land.lnd_x, land.lnd_y, &sect)) {
559             lnd_mess("was sucked into the sky by a strange looking spaceland", llp);    /* heh -KHS */
560             continue;
561         }
562         if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
563             !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_TRAIN) &&
564             llp->unit.land.lnd_item[I_MILIT] == 0) {
565             lnd_mess("has no mil on it to guide it", llp);
566             continue;
567         }
568         rel = getrel(getnatp(sect.sct_own), player->cnum);
569         if (sect.sct_own != land.lnd_own && rel != ALLIED &&
570             !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
571             sect.sct_own) {
572             sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
573             lnd_mess(mess, llp);
574             continue;
575         }
576         if (first) {
577             allx = land.lnd_x;
578             ally = land.lnd_y;
579             first = 0;
580         }
581         if (land.lnd_x != allx || land.lnd_y != ally)
582             *togetherp = 0;
583         if (land.lnd_mobil + 1 < (int)llp->mobil) {
584             llp->mobil = land.lnd_mobil;
585         }
586         if (llp->mobil < *minmobp)
587             *minmobp = llp->mobil;
588         if (llp->mobil > *maxmobp)
589             *maxmobp = llp->mobil;
590         llp->unit.land = land;
591     }
592 }
593
594 void
595 lnd_sweep(struct emp_qelem *land_list, int verbose, int takemob,
596           natid actor)
597 {
598     struct emp_qelem *qp;
599     struct emp_qelem *next;
600     struct ulist *llp;
601     struct sctstr sect;
602     int mines, m, max, sshells, lshells;
603
604     for (qp = land_list->q_back; qp != land_list; qp = next) {
605         next = qp->q_back;
606         llp = (struct ulist *)qp;
607         if (!(((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)) {
608             if (verbose)
609                 mpr(actor, "%s is not an engineer!\n",
610                     prland(&llp->unit.land));
611             continue;
612         }
613         if (takemob && llp->mobil < 0.0) {
614             if (verbose)
615                 lnd_mess("is out of mobility", llp);
616             continue;
617         }
618         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &sect);
619         if (sect.sct_oldown == llp->unit.land.lnd_own) {
620             if (verbose)
621                 mpr(actor,
622                     "%s is in a sector completely owned by you.  Don't bother digging up mines there!\n",
623                     prland(&llp->unit.land));
624             continue;
625         }
626         if (sect.sct_type == SCT_BSPAN) {
627             if (verbose)
628                 mpr(actor, "%s is on a bridge.  No mines there!\n",
629                     prland(&llp->unit.land));
630             continue;
631         }
632         if (takemob) {
633             llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
634             llp->unit.land.lnd_mobil = (int)llp->mobil;
635             llp->unit.land.lnd_harden = 0;
636         }
637         putland(llp->unit.land.lnd_uid, &llp->unit.land);
638         if (!(mines = sect.sct_mines))
639             continue;
640         max = ((struct lchrstr *)llp->chrp)->l_item[I_SHELL];
641         lshells = llp->unit.land.lnd_item[I_SHELL];
642         sshells = sect.sct_item[I_SHELL];
643         for (m = 0; mines > 0 && m < max * 2; m++) {
644             if (chance(0.5 * ((struct lchrstr *)llp->chrp)->l_att)) {
645                 mpr(actor, "Sweep...\n");
646                 mines--;
647                 if (lshells < max)
648                     ++lshells;
649                 else if (sshells < ITEM_MAX)
650                     ++sshells;
651             }
652         }
653         sect.sct_mines = mines;
654         llp->unit.land.lnd_item[I_SHELL] = lshells;
655         sect.sct_item[I_SHELL] = sshells;
656         putland(llp->unit.land.lnd_uid, &llp->unit.land);
657         putsect(&sect);
658     }
659 }
660
661 static int
662 contains_engineer(struct emp_qelem *list)
663 {
664     struct emp_qelem *qp;
665     struct emp_qelem *next;
666     struct ulist *llp;
667
668     for (qp = list->q_back; qp != list; qp = next) {
669         next = qp->q_back;
670         llp = (struct ulist *)qp;
671         if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
672             return 1;
673     }
674     return 0;
675 }
676
677 int
678 lnd_check_mines(struct emp_qelem *land_list)
679 {
680     struct emp_qelem *qp;
681     struct emp_qelem *next;
682     struct ulist *llp;
683     struct sctstr sect;
684     int stopping = 0;
685     int with_eng = contains_engineer(land_list);
686
687     for (qp = land_list->q_back; qp != land_list; qp = next) {
688         next = qp->q_back;
689         llp = (struct ulist *)qp;
690         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &sect);
691         if (sect.sct_oldown == llp->unit.land.lnd_own)
692             continue;
693         if (sect.sct_type == SCT_BSPAN)
694             continue;
695         if (!sect.sct_mines)
696             continue;
697         if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
698             lnd_hit_mine(&llp->unit.land, ((struct lchrstr *)llp->chrp));
699             sect.sct_mines--;
700             putsect(&sect);
701             putland(llp->unit.land.lnd_uid, &llp->unit.land);
702             if (!llp->unit.land.lnd_own) {
703                 stopping = 1;
704                 emp_remque(qp);
705                 free(qp);
706             }
707         }
708     }
709     return stopping;
710 }
711
712 static void
713 lnd_mess(char *str, struct ulist *llp)
714 {
715     mpr(llp->unit.land.lnd_own, "%s %s & stays in %s\n",
716         prland(&llp->unit.land),
717         str, xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
718                   llp->unit.land.lnd_own));
719     if (llp->mobil < -127)
720         llp->mobil = -127;
721     llp->unit.land.lnd_mobil = llp->mobil;
722     putland(llp->unit.land.lnd_uid, &llp->unit.land);
723     emp_remque((struct emp_qelem *)llp);
724     free(llp);
725 }
726
727 static int
728 lnd_count(struct emp_qelem *list)
729 {
730     struct emp_qelem *qp;
731     struct emp_qelem *next;
732     int count = 0;
733
734     for (qp = list->q_back; qp != list; qp = next) {
735         next = qp->q_back;
736         ++count;
737     }
738     return count;
739 }
740
741 static int
742 lnd_damage(struct emp_qelem *list, int totdam)
743 {
744     struct emp_qelem *qp;
745     struct emp_qelem *next;
746     struct ulist *llp;
747     int dam;
748     int count;
749
750     if (!totdam || !(count = lnd_count(list)))
751         return 0;
752     dam = ldround((double)totdam / count, 1);
753     for (qp = list->q_back; qp != list; qp = next) {
754         next = qp->q_back;
755         llp = (struct ulist *)qp;
756         /* have to get it again because of collateral damage */
757         getland(llp->unit.land.lnd_uid, &llp->unit.land);
758         landdamage(&llp->unit.land, dam);
759         putland(llp->unit.land.lnd_uid, &llp->unit.land);
760         if (!llp->unit.land.lnd_own) {
761             emp_remque(qp);
762             free(qp);
763         }
764     }
765     return dam;
766 }
767
768 static int
769 lnd_easiest_target(struct emp_qelem *list)
770 {
771     struct emp_qelem *qp;
772     struct emp_qelem *next;
773     struct ulist *llp;
774     int hard;
775     int easiest = 9876;         /* things start great for victim */
776     int count = 0;
777
778     for (qp = list->q_back; qp != list; qp = next) {
779         next = qp->q_back;
780         llp = (struct ulist *)qp;
781         hard = lnd_hardtarget(&llp->unit.land);
782         if (hard < easiest)
783             easiest = hard;     /* things get worse for victim */
784         ++count;
785     }
786     return easiest - count;
787 }
788
789 static int
790 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
791                          natid victim)
792 {
793     int dam;
794     struct emp_qelem msl_list, *qp, *newqp;
795
796     msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
797
798     dam = msl_launch_mindam(&msl_list, newx, newy,
799                             lnd_easiest_target(list), EF_LAND,
800                             lnd_count(list) * 20, "troops", victim,
801                             MI_INTERDICT);
802     if (dam) {
803         mpr(victim, "missile interdiction mission does %d damage!\n", dam);
804         collateral_damage(newx, newy, dam, 0);
805     }
806     qp = msl_list.q_forw;
807     while (qp != msl_list.q_forw) {
808         newqp = qp->q_forw;
809         emp_remque(qp);
810         free(qp);
811         qp = newqp;
812     }
813     return dam;
814 }
815
816 #if 0
817 /* Steve M. - commented out for now until abuse is decided upon */
818 /* risner: allow forts to interdict land units. */
819 static int
820 lnd_fort_interdiction(struct emp_qelem *list,
821                       coord newx, coord newy, natid victim)
822 {
823     struct nstr_sect ns;
824     struct sctstr fsect;
825     int trange, range;
826     double guneff;
827     int shell, gun;
828     int dam;
829     int totdam = 0;
830     int i;
831
832     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
833     while (nxtsct(&ns, &fsect)) {
834         if (fsect.sct_own == 0)
835             continue;
836         if (fsect.sct_own == victim)
837             continue;
838         if (getrel(getnatp(fsect.sct_own), victim) >= NEUTRAL)
839             continue;
840         range = roundrange(fortrange(&fsect));
841         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
842         if (trange > range)
843             continue;
844         dam = fort_fire(&fsect);
845         putsect(&fsect);
846         if (dam < 0)
847             continue;
848         totdam += dam;
849         mpr(victim, "Incoming fire does %d damage!\n", dam);
850         wu(0, fsect.sct_own,
851            "%s fires at %s land units in %s for %d!\n",
852            xyas(fsect.sct_x, fsect.sct_y,
853                 fsect.sct_own),
854            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
855         nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
856     }
857     if (totdam > 0)
858         return lnd_damage(list, totdam);
859     return 0;
860 }
861 #endif
862
863 int
864 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
865 {
866     int stopping = 0;
867
868 #if 0
869     if (!opt_NO_FORT_FIRE)
870 /* Steve M. - commented out for now until abuse is decided upon */
871         stopping |= lnd_fort_interdiction(list, newx, newy, victim);
872 #endif
873
874     stopping |=
875         lnd_damage(list,
876                    unit_interdict(newx, newy, victim, "land units",
877                                   lnd_easiest_target(list), MI_INTERDICT));
878
879     stopping |=
880         lnd_damage(list,
881                    lnd_missile_interdiction(list, newx, newy, victim));
882     return stopping;
883 }
884
885 /* high value of hardtarget is harder to hit */
886 int
887 lnd_hardtarget(struct lndstr *lp)
888 {
889     struct sctstr sect;
890
891     getsect(lp->lnd_x, lp->lnd_y, &sect);
892     return (int)((lp->lnd_effic / 100.0) *
893                  (10 + dchr[sect.sct_type].d_dstr * 2 + lp->lnd_spd / 2.0
894                   - lp->lnd_vis));
895 }
896
897 static int
898 lnd_hit_mine(struct lndstr *lp, struct lchrstr *lcp)
899 {
900     int m;
901
902     mpr(lp->lnd_own, "Blammo! Landmines detected in %s! ",
903         xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
904
905     nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
906
907     m = MINE_LDAMAGE();
908     if (lcp->l_flags & L_ENGINEER)
909         m /= 2;
910
911     landdamage(lp, m);
912     return m;
913 }
914
915 double
916 lnd_pathcost(struct lndstr *lp, double pathcost)
917 {
918     double effspd;
919
920     effspd = lp->lnd_spd;
921     if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
922         effspd *= lp->lnd_effic * 0.01;
923
924     /*
925      * The return value must be PATHCOST times a factor that depends
926      * only on the land unit.  Anything else breaks path finding.  In
927      * particular, you can't add or enforce a minimum cost here.  Do
928      * it in sector_mcost().
929      */
930     return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
931 }
932
933 int
934 lnd_mobtype(struct lndstr *lp)
935 {
936     return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
937         ? MOB_RAIL : MOB_MARCH;
938 }
939
940 double
941 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
942 {
943     return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
944 }
945
946 int
947 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
948                    int together)
949 {
950     struct sctstr sect, osect;
951     struct emp_qelem *qp;
952     struct emp_qelem *qp2;
953     struct emp_qelem *next;
954     struct ulist *llp;
955     struct emp_qelem cur, done;
956     coord dx;
957     coord dy;
958     coord newx;
959     coord newy;
960     int stopping = 0;
961     int visible;
962     int stop;
963     char dp[80];
964     int rel;
965     int oldown;
966
967     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
968         unit_put(list, actor);
969         return 1;
970     }
971     dx = diroff[dir][0];
972     dy = diroff[dir][1];
973     for (qp = list->q_back; qp != list; qp = next) {
974         next = qp->q_back;
975         llp = (struct ulist *)qp;
976         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
977         oldown = osect.sct_own;
978         newx = xnorm(llp->unit.land.lnd_x + dx);
979         newy = ynorm(llp->unit.land.lnd_y + dy);
980         getsect(newx, newy, &sect);
981         rel = getrel(getnatp(sect.sct_own), player->cnum);
982         if ((sect.sct_own != actor && rel != ALLIED &&
983              !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
984              sect.sct_own) || (sect.sct_type == SCT_WATER ||
985                                sect.sct_type == SCT_SANCT ||
986                                sect.sct_type == SCT_WASTE)) {
987             if (together) {
988                 pr("can't go to %s\n", xyas(newx, newy, actor));
989                 return 1;
990             } else {
991                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
992                 lnd_mess(dp, llp);
993                 continue;
994             }
995         }
996         if ((!intrchr[INT_RAIL].in_enable || sect.sct_rail == 0)
997             && lnd_mobtype(&llp->unit.land) == MOB_RAIL) {
998             if (together) {
999                 pr("no rail system in %s\n", xyas(newx, newy, actor));
1000                 return 1;
1001             } else {
1002                 sprintf(dp, "has no rail system in %s",
1003                         xyas(newx, newy, actor));
1004                 lnd_mess(dp, llp);
1005                 continue;
1006             }
1007         }
1008         /* Note we check would_abandon first because we don't want
1009            to always have to do these checks */
1010         if (would_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
1011             stop = 0;
1012             if (!want_to_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
1013                 stop = 1;
1014             }
1015             /* now check stuff */
1016             if (!check_sect_ok(&sect))
1017                 return 1;
1018             if (!check_sect_ok(&osect))
1019                 return 1;
1020             for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
1021                 if (!check_land_ok(&((struct ulist *)qp2)->unit.land))
1022                     return 1;
1023             }
1024             if (stop) {
1025                 lnd_mess("stops", llp);
1026                 continue;
1027             }
1028         }
1029         if (llp->mobil <= 0.0) {
1030             lnd_mess("is out of mobility", llp);
1031             continue;
1032         }
1033         llp->unit.land.lnd_x = newx;
1034         llp->unit.land.lnd_y = newy;
1035         llp->mobil -= lnd_mobcost(&llp->unit.land, &sect);
1036         llp->unit.land.lnd_mobil = (int)llp->mobil;
1037         llp->unit.land.lnd_harden = 0;
1038         putland(llp->unit.land.lnd_uid, &llp->unit.land);
1039         putsect(&osect);
1040         getsect(osect.sct_x, osect.sct_y, &osect);
1041         if (osect.sct_own != oldown && oldown == player->cnum) {
1042             /* It was your sector, now it's not.  Simple :) */
1043             pr("You no longer own %s\n",
1044                xyas(osect.sct_x, osect.sct_y, player->cnum));
1045         }
1046         if (rel != ALLIED && sect.sct_own != actor && sect.sct_own) {   /* must be a spy */
1047             /* Always a 10% chance of getting caught. */
1048             if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1049                 if (rel == NEUTRAL || rel == FRIENDLY) {
1050                     wu(0, sect.sct_own,
1051                        "%s unit spotted in %s\n", cname(player->cnum),
1052                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1053                     setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1054                 } else if (rel == HOSTILE || rel == AT_WAR ||
1055                            rel == SITZKRIEG || rel == MOBILIZATION) {
1056                     wu(0, sect.sct_own,
1057                        "%s spy shot in %s\n", cname(player->cnum),
1058                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1059                     pr("%s was shot and killed.\n", prland(&llp->unit.land));
1060                     llp->unit.land.lnd_effic = 0;
1061                     putland(llp->unit.land.lnd_uid, &llp->unit.land);
1062                     lnd_delete(llp, 0);
1063                 }
1064             }
1065         }
1066     }
1067     if (QEMPTY(list))
1068         return stopping;
1069     lnd_sweep(list, 0, 1, actor);
1070     stopping |= lnd_check_mines(list);
1071     if (QEMPTY(list))
1072         return stopping;
1073
1074     /* interdict land units sector by sector */
1075     emp_initque(&cur);
1076     emp_initque(&done);
1077     while (!QEMPTY(list)) {
1078         llp = (struct ulist *)list->q_back;
1079         newx = llp->unit.land.lnd_x;
1080         newy = llp->unit.land.lnd_y;
1081         /* move units in NEWX,NEWY to cur */
1082         visible = 0;
1083         for (qp = list->q_back; qp != list; qp = next) {
1084             next = qp->q_back;
1085             llp = (struct ulist *)qp;
1086             if (llp->unit.land.lnd_x == newx && llp->unit.land.lnd_y == newy) {
1087                 emp_remque(qp);
1088                 emp_insque(qp, &cur);
1089                 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1090                     visible = 1;
1091             }
1092         }
1093         /* interdict them */
1094         if (visible)
1095             stopping |= lnd_interdict(&cur, newx, newy, actor);
1096         /* move survivors to done */
1097         for (qp = cur.q_back; qp != &cur; qp = next) {
1098             next = qp->q_back;
1099             llp = (struct ulist *)qp;
1100             emp_remque(qp);
1101             emp_insque(qp, &done);
1102         }
1103     }
1104     /* assign surviving land units back to list */
1105     emp_insque(list, &done);
1106     emp_remque(&done);
1107
1108     return stopping;
1109 }
1110
1111 /*
1112  * find all artillery units belonging
1113  * to the attacker or defender that can fire.
1114  * Each arty unit adds +1%/damage point
1115  */
1116 int
1117 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1118 {
1119     struct nstr_item ni;
1120     struct lndstr land;
1121     int rel, rel2;
1122     int dam, dam2;
1123     int dist;
1124     int range;
1125
1126     dam = 0;
1127     snxtitem_all(&ni, EF_LAND);
1128     while (nxtitem(&ni, &land)) {
1129         if ((land.lnd_x == x) && (land.lnd_y == y))
1130             continue;
1131         rel = getrel(getnatp(land.lnd_own), attacker);
1132         rel2 = getrel(getnatp(land.lnd_own), victim);
1133         if ((land.lnd_own != attacker) &&
1134             ((rel != ALLIED) || (rel2 != AT_WAR)))
1135             continue;
1136
1137         /* are we in range? */
1138         dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1139
1140         range = roundrange(lnd_fire_range(&land));
1141         if (dist > range)
1142             continue;
1143
1144         dam2 = lnd_fire(&land);
1145         putland(land.lnd_uid, &land);
1146         if (dam2 < 0)
1147             continue;
1148
1149         if (defending)
1150             nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1151         else
1152             nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1153         if (roll(100) < land.lnd_acc)
1154             dam2 /= 2;
1155         dam += dam2;
1156         if (land.lnd_own != attacker)
1157             wu(0, land.lnd_own,
1158                "%s supported %s at %s\n",
1159                prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1160     }
1161     return dam;
1162 }
1163
1164 int
1165 lnd_can_attack(struct lndstr *lp)
1166 {
1167     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1168
1169     if (lcp->l_flags & L_SUPPLY)
1170         return 0;
1171
1172     return 1;
1173 }
1174
1175 /*
1176  * Increase fortification value of LP.
1177  * Fortification costs mobility.  Use up to MOB mobility.
1178  * Return actual fortification increase.
1179  */
1180 int
1181 lnd_fortify(struct lndstr *lp, int mob)
1182 {
1183     int hard_amt;
1184     double mob_used, mult;
1185
1186     if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1187         return 0;
1188
1189     mob_used = MIN(lp->lnd_mobil, mob);
1190     if (mob_used < 0)
1191         return 0;
1192
1193     mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1194         ? 1.5 : 1.0;
1195
1196     hard_amt = (int)(mob_used * mult);
1197     if (lp->lnd_harden + hard_amt > land_mob_max) {
1198         hard_amt = land_mob_max - lp->lnd_harden;
1199         mob_used = ceil(hard_amt / mult);
1200     }
1201
1202     lp->lnd_mobil -= (int)mob_used;
1203     lp->lnd_harden += hard_amt;
1204     lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1205
1206     return hard_amt;
1207 }
1208
1209 /*
1210  * Is there a engineer unit at X,Y that can help nation CN?
1211  */
1212 static int
1213 has_helpful_engineer(coord x, coord y, natid cn)
1214 {
1215     struct nstr_item ni;
1216     struct lndstr land;
1217
1218     snxtitem_xy(&ni, EF_LAND, x, y);
1219     while (nxtitem(&ni, &land)) {
1220         if (land.lnd_own != cn && getrel(getnatp(land.lnd_own), cn) != ALLIED)
1221             continue;
1222         if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1223             return 1;
1224     }
1225
1226     return 0;
1227 }
1228
1229 /*
1230  * Set LP's tech to TLEV along with everything else that depends on it.
1231  */
1232 void
1233 lnd_set_tech(struct lndstr *lp, int tlev)
1234 {
1235     struct lchrstr *lcp = lchr + lp->lnd_type;
1236
1237     if (CANT_HAPPEN(tlev < lcp->l_tech))
1238         tlev = 0;
1239
1240     lp->lnd_tech = tlev;
1241     lp->lnd_att = l_att(lcp, tlev);
1242     lp->lnd_def = l_def(lcp, tlev);
1243     lp->lnd_vul = l_vul(lcp, tlev);
1244     lp->lnd_spd = l_spd(lcp, tlev);
1245     lp->lnd_vis = lcp->l_vis;
1246     lp->lnd_spy = lcp->l_spy;
1247     lp->lnd_rad = lcp->l_rad;
1248     lp->lnd_frg = l_frg(lcp, tlev);
1249     lp->lnd_acc = l_acc(lcp, tlev);
1250     lp->lnd_dam = l_dam(lcp, tlev);
1251     lp->lnd_ammo = lcp->l_ammo;
1252     lp->lnd_aaf = lcp->l_aaf;
1253     lp->lnd_fuelc = lcp->l_fuelc;
1254     lp->lnd_fuelu = lcp->l_fuelu;
1255     lp->lnd_maxlight = lcp->l_nxlight;
1256     lp->lnd_maxland = lcp->l_nland;
1257 }