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