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