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