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