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