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