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