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