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