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