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