]> git.pond.sub.org Git - empserver/blob - src/lib/subs/lndsub.c
unit: Drop ulist member chrp
[empserver] / src / lib / subs / lndsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  lndsub.c: Land unit subroutines
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Steve McClure, 1998-2000
32  *     Markus Armbruster, 2004-2014
33  */
34
35 #include <config.h>
36
37 #include <math.h>
38 #include <stdlib.h>
39 #include "chance.h"
40 #include "combat.h"
41 #include "damage.h"
42 #include "empobj.h"
43 #include "file.h"
44 #include "misc.h"
45 #include "mission.h"
46 #include "news.h"
47 #include "nsc.h"
48 #include "optlist.h"
49 #include "path.h"
50 #include "player.h"
51 #include "prototypes.h"
52 #include "unit.h"
53 #include "xy.h"
54
55 static void lnd_stays(natid, char *, struct ulist *);
56 static int lnd_hit_mine(struct lndstr *);
57 static int has_helpful_engineer(coord, coord, natid);
58
59 double
60 attack_val(int combat_mode, struct lndstr *lp)
61 {
62     int men;
63     double value;
64     struct lchrstr *lcp;
65
66     if (lp->lnd_effic < LAND_MINEFF) {
67         putland(lp->lnd_uid, lp);
68         return 0;
69     }
70
71     lcp = &lchr[(int)lp->lnd_type];
72
73 /* Spies always count as 1 during assaults.  If they are the only ones
74    in the assault, they get to sneak on anyway. */
75
76     if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
77         return 1;
78
79     men = lp->lnd_item[I_MILIT];
80     value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
81
82     switch (combat_mode) {
83     case A_ATTACK:
84         return value;
85     case A_ASSAULT:
86         if (!(lcp->l_flags & L_MARINE))
87             return assault_penalty * value;
88         break;
89     case A_BOARD:
90         if (!(lcp->l_flags & L_MARINE))
91             return assault_penalty * men;
92     }
93
94     return value;
95 }
96
97 double
98 defense_val(struct lndstr *lp)
99 {
100     int men;
101     double value;
102     struct lchrstr *lcp;
103
104     if (lp->lnd_effic < LAND_MINEFF) {
105         putland(lp->lnd_uid, lp);
106         return 0;
107     }
108
109     lcp = &lchr[(int)lp->lnd_type];
110
111     men = lp->lnd_item[I_MILIT];
112
113     if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
114         !(lcp->l_flags & L_MARINE))
115         return men;
116
117     value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
118     value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
119
120     /* If there are military on the unit, you get at least a 1
121        man defensive unit, except for spies */
122     if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
123         return 1;
124
125     return value;
126 }
127
128 int
129 lnd_reaction_range(struct lndstr *lp)
130 {
131     struct sctstr sect;
132
133     getsect(lp->lnd_x, lp->lnd_y, &sect);
134     if (sect.sct_type == SCT_HEADQ && sect.sct_effic >= 60)
135         return lchr[lp->lnd_type].l_rad + 1;
136     return lchr[lp->lnd_type].l_rad;
137 }
138
139 void
140 lnd_print(natid actor, struct ulist *llp, char *s)
141 {
142     if (actor == player->cnum)
143         pr("%s %s\n", prland(&llp->unit.land), s);
144     else
145         wu(0, actor, "%s %s\n", prland(&llp->unit.land), s);
146 }
147
148 int
149 lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
150                         /* attacking or assaulting or paratrooping? */
151                         /* number of casualties to take */
152 {
153     int eff_eq;
154     int n;
155     int biggest;
156     int civs;
157     coord ret_x, ret_y;
158     coord bx, by;
159     struct sctstr sect;
160     int ret_chance;
161     char buf[1024];
162     int taken;
163     int nowhere_to_go = 0;
164     double mobcost, bmcost;
165     signed char orig;
166     int mob;
167
168     taken = llp->unit.land.lnd_item[I_MILIT];
169     /* Spies always die */
170     if (lchr[llp->unit.land.lnd_type].l_flags & L_SPY)
171         llp->unit.land.lnd_effic = 0;
172     else {
173         eff_eq = ldround(cas * 100.0 /
174             lchr[llp->unit.land.lnd_type].l_item[I_MILIT], 1);
175         llp->unit.land.lnd_effic -= eff_eq;
176         lnd_submil(&llp->unit.land, cas);
177     }
178
179     if (llp->unit.land.lnd_effic < LAND_MINEFF) {
180         sprintf(buf, "dies %s %s!",
181                 combat_mode ? att_mode[combat_mode] : "defending",
182                 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
183                      llp->unit.land.lnd_own));
184         lnd_print(llp->unit.land.lnd_own, llp, buf);
185         lnd_put_one(llp);
186         /* Since we killed the unit, we killed all the mil on it */
187         return taken;
188     } else {
189         /* Ok, now, how many did we take off? (sould be the diff) */
190         taken = taken - llp->unit.land.lnd_item[I_MILIT];
191     }
192
193     if (llp->unit.land.lnd_effic >= llp->unit.land.lnd_retreat)
194         return taken;
195
196     /* we're being boarded */
197     if (llp->unit.land.lnd_ship >= 0 && combat_mode == A_DEFEND)
198         return taken;
199
200     /* we're being boarded */
201     if (llp->unit.land.lnd_land >= 0 && combat_mode == A_DEFEND)
202         return taken;
203
204     /* Have to make a retreat check */
205
206     ret_chance = llp->unit.land.lnd_retreat - llp->unit.land.lnd_effic;
207     if (pct_chance(ret_chance)) {
208         pr("\n");
209         lnd_print(llp->unit.land.lnd_own, llp, "fails morale check!");
210         llp->unit.land.lnd_mission = 0;
211         llp->unit.land.lnd_harden = 0;
212         if (llp->unit.land.lnd_ship >= 0 || llp->unit.land.lnd_land >= 0)
213             nowhere_to_go = 1;
214         else if (combat_mode == A_DEFEND) {
215             /*
216              * defending unit.. find a place to send it
217              * strategy: look for the most-populated
218              * adjacent sector that is owned by the unit
219              * owner. Charge mob..
220              */
221             biggest = -1;
222             for (n = 1; n <= 6; ++n) {
223                 ret_x = llp->unit.land.lnd_x + diroff[n][0];
224                 ret_y = llp->unit.land.lnd_y + diroff[n][1];
225                 getsect(ret_x, ret_y, &sect);
226                 if (sect.sct_own != llp->unit.land.lnd_own)
227                     continue;
228                 if (sect.sct_type == SCT_MOUNT)
229                     continue;
230                 mobcost = lnd_mobcost(&llp->unit.land, &sect);
231                 if (mobcost < 0)
232                     continue;
233                 civs = sect.sct_item[I_CIVIL];
234                 if (civs > biggest) {
235                     biggest = civs;
236                     bx = sect.sct_x;
237                     by = sect.sct_y;
238                     bmcost = mobcost;
239                 }
240             }
241             if (biggest < 0)
242                 nowhere_to_go = 1;
243             else {
244                 /* retreat to bx,by */
245                 llp->unit.land.lnd_x = bx;
246                 llp->unit.land.lnd_y = by;
247                 /* FIXME landmines */
248                 mob = llp->unit.land.lnd_mobil - (int)bmcost;
249                 if (mob < -127)
250                     mob = -127;
251                 orig = llp->unit.land.lnd_mobil;
252                 llp->unit.land.lnd_mobil = (signed char)mob;
253                 if (llp->unit.land.lnd_mobil > orig)
254                     llp->unit.land.lnd_mobil = -127;
255                 sprintf(buf, "retreats at %d%% efficiency to %s!",
256                         llp->unit.land.lnd_effic,
257                         xyas(bx, by, llp->unit.land.lnd_own));
258                 lnd_print(llp->unit.land.lnd_own, llp, buf);
259                 lnd_put_one(llp);
260             }
261         } else {                /* attacking from a sector */
262             sprintf(buf, "leaves the battlefield at %d%% efficiency",
263                     llp->unit.land.lnd_effic);
264             if ((llp->unit.land.lnd_mobil - (int)llp->mobil) < -127)
265                 llp->unit.land.lnd_mobil = -127;
266             else
267                 llp->unit.land.lnd_mobil -= (int)llp->mobil;
268             llp->mobil = 0.0;
269             lnd_print(llp->unit.land.lnd_own, llp, buf);
270             lnd_put_one(llp);
271         }
272     }
273     if (nowhere_to_go) {
274         /* nowhere to go.. take more casualties */
275         llp->unit.land.lnd_effic -= 10;
276         lnd_submil(&llp->unit.land,
277                    lchr[llp->unit.land.lnd_type].l_item[I_MILIT] / 10);
278         if (llp->unit.land.lnd_effic < LAND_MINEFF) {
279             lnd_print(llp->unit.land.lnd_own, llp,
280                       "has nowhere to retreat, and dies!");
281             lnd_put_one(llp);
282         } else
283             lnd_print(llp->unit.land.lnd_own, llp,
284                       "has nowhere to retreat and takes extra losses!");
285     }
286
287     return taken;
288 }
289
290 void
291 lnd_takemob(struct emp_qelem *list, double loss)
292 {
293     struct emp_qelem *qp, *next;
294     struct ulist *llp;
295     int new;
296     int mcost = ldround(combat_mob * loss, 1);
297
298     for (qp = list->q_forw; qp != list; qp = next) {
299         next = qp->q_forw;
300         llp = (struct ulist *)qp;
301 #if 0
302         if (chance(loss))
303             use_supply(&llp->unit.land);
304 #endif
305         new = llp->unit.land.lnd_mobil - mcost;
306         if (new < -127)
307             new = -127;
308         llp->unit.land.lnd_mobil = (signed char)new;
309     }
310 }
311
312 void
313 lnd_submil(struct lndstr *lp, int num)
314 {
315     int new = lp->lnd_item[I_MILIT] - num;
316     lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
317 }
318
319 int
320 lnd_spyval(struct lndstr *lp)
321 {
322     if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
323         return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0) + 2;
324     else
325         return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
326 }
327
328 void
329 intelligence_report(int destination, struct lndstr *lp, int spy,
330                     char *mess)
331 {
332     int vis = lnd_vis(lp);
333     char buf1[80], buf2[80], buf3[80];
334
335     if (destination == 0)
336         return;
337
338     if (lp->lnd_own == 0)
339         return;
340
341     memset(buf1, 0, sizeof(buf1));
342     memset(buf2, 0, sizeof(buf2));
343     memset(buf3, 0, sizeof(buf3));
344     if (chance((spy + vis) / 10.0)) {
345         if (destination == player->cnum)
346             pr("%s %s", mess, prland(lp));
347         else
348             sprintf(buf1, "%s %s", mess, prland(lp));
349
350         if (chance((spy + vis) / 20.0)) {
351             if (destination == player->cnum)
352                 pr(" (eff %d, mil %d",
353                    roundintby(lp->lnd_effic, 5),
354                    roundintby(lp->lnd_item[I_MILIT], 10));
355             else
356                 sprintf(buf2, " (eff %d, mil %d",
357                         roundintby(lp->lnd_effic, 5),
358                         roundintby(lp->lnd_item[I_MILIT], 10));
359
360             if (chance((spy + vis) / 20.0)) {
361                 int t;
362                 t = lp->lnd_tech - 20 + roll(40);
363                 t = MAX(t, 0);
364                 if (destination == player->cnum)
365                     pr(", tech %d)\n", t);
366                 else
367                     sprintf(buf3, ", tech %d)\n", t);
368             } else {
369                 if (destination == player->cnum)
370                     pr(")\n");
371                 else
372                     sprintf(buf3, ")\n");
373             }
374         } else {
375             if (destination == player->cnum)
376                 pr("\n");
377             else
378                 sprintf(buf2, "\n");
379         }
380     }
381
382     if (destination != player->cnum) {
383         wu(0, destination, "%s%s%s", buf1, buf2, buf3);
384     }
385 }
386
387 void
388 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
389 {
390     struct lndstr land;
391     int this_mot;
392     int mobtype = MOB_MOVE;     /* indeterminate */
393
394     emp_initque(list);
395     while (nxtitem(ni, &land)) {
396         /*
397          * It would be nice to let deities march foreign land units,
398          * but much of the code assumes that only the land unit's
399          * owner can march it.
400          */
401         if (!land.lnd_own || land.lnd_own != player->cnum)
402             continue;
403         if (opt_MARKET) {
404             if (ontradingblock(EF_LAND, &land)) {
405                 pr("unit #%d inelligible - it's for sale.\n",
406                    land.lnd_uid);
407                 continue;
408             }
409         }
410         /*
411          * The marching code gets confused when trains and non-trains
412          * march together.  Disallow for now.
413          */
414         this_mot = lnd_mobtype(&land);
415         if (this_mot != mobtype) {
416             if (mobtype == MOB_MOVE)
417                 mobtype = this_mot;
418             else if (mobtype == MOB_MARCH) {
419                 pr("%s is a train and can't march with the leader.\n",
420                    prland(&land));
421                 continue;
422             } else {
423                 pr("%s can't rail-march with the leading train.\n",
424                    prland(&land));
425                 continue;
426             }
427         }
428
429         land.lnd_mission = 0;
430         land.lnd_rflags = 0;
431         land.lnd_harden = 0;
432         memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
433         putland(land.lnd_uid, &land);
434         lnd_insque(&land, list);
435     }
436 }
437
438 /*
439  * Append LP to LIST.
440  * Return the new list link.
441  */
442 struct ulist *
443 lnd_insque(struct lndstr *lp, struct emp_qelem *list)
444 {
445     struct ulist *mlp = malloc(sizeof(struct ulist));
446
447     mlp->unit.land = *lp;
448     mlp->mobil = lp->lnd_mobil;
449     emp_insque(&mlp->queue, list);
450     return mlp;
451 }
452
453 /* This function assumes that the list was created by lnd_sel */
454 void
455 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
456         natid actor)
457 {
458     struct emp_qelem *qp;
459     struct emp_qelem *next;
460     struct ulist *llp;
461     struct lndstr *lp, *ldr = NULL;
462     struct sctstr sect;
463     char mess[128];
464
465     *minmobp = 9876.0;
466     *maxmobp = -9876.0;
467     for (qp = list->q_back; qp != list; qp = next) {
468         next = qp->q_back;
469         llp = (struct ulist *)qp;
470         lp = &llp->unit.land;
471         getland(lp->lnd_uid, lp);
472         if (lp->lnd_own != actor) {
473             mpr(actor, "%s was disbanded at %s\n",
474                 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
475             emp_remque(&llp->queue);
476             free(llp);
477             continue;
478         }
479         if (lp->lnd_ship >= 0) {
480             lnd_stays(actor, "is on a ship", llp);
481             continue;
482         }
483         if (lp->lnd_land >= 0) {
484             lnd_stays(actor, "is on a unit", llp);
485             continue;
486         }
487         if (!getsect(lp->lnd_x, lp->lnd_y, &sect)) {
488             lnd_stays(actor, "was sucked into the sky by a strange looking spaceland", llp);    /* heh -KHS */
489             continue;
490         }
491         if (!(lchr[lp->lnd_type].l_flags & L_SPY) &&
492             !(lchr[lp->lnd_type].l_flags & L_TRAIN) &&
493             lp->lnd_item[I_MILIT] == 0) {
494             lnd_stays(actor, "has no mil on it to guide it", llp);
495             continue;
496         }
497         switch (lnd_check_mar(lp, &sect)) {
498         case LND_STUCK_NOT:
499             break;
500         case LND_STUCK_NO_RAIL:
501             lnd_stays(actor, "is stuck off the rail system", llp);
502             continue;
503         default:
504             CANT_REACH();
505             /* fall through */
506         case LND_STUCK_IMPASSABLE:
507             lnd_stays(actor, "is stuck", llp);
508             continue;
509         }
510         if (relations_with(sect.sct_own, actor) != ALLIED &&
511             !(lchr[lp->lnd_type].l_flags & L_SPY) &&
512             sect.sct_own) {
513             sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
514             lnd_stays(actor, mess, llp);
515             continue;
516         }
517         if (!ldr)
518             ldr = lp;
519         else if (lp->lnd_x != ldr->lnd_x || lp->lnd_y != ldr->lnd_y) {
520             lnd_stays(actor, "is not with the leader", llp);
521             continue;
522         }
523         if (lp->lnd_mobil + 1 < (int)llp->mobil) {
524             llp->mobil = lp->lnd_mobil;
525         }
526         if (llp->mobil < *minmobp)
527             *minmobp = llp->mobil;
528         if (llp->mobil > *maxmobp)
529             *maxmobp = llp->mobil;
530     }
531 }
532
533 static void
534 lnd_mar_put_one(struct ulist *llp)
535 {
536     if (llp->mobil < -127)
537         llp->mobil = -127;
538     llp->unit.land.lnd_mobil = llp->mobil;
539     lnd_put_one(llp);
540 }
541
542 static void
543 lnd_mar_put(struct emp_qelem *list, natid actor)
544 {
545     struct emp_qelem *qp, *next;
546     struct ulist *llp;
547     struct lndstr *lp;
548
549     for (qp = list->q_back; qp != list; qp = next) {
550         next = qp->q_back;
551         llp = (struct ulist *)qp;
552         lp = &llp->unit.land;
553         mpr(actor, "%s stopped at %s\n",
554             prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
555         lnd_mar_put_one(llp);
556     }
557 }
558
559 void
560 lnd_put(struct emp_qelem *list)
561 {
562     struct emp_qelem *qp, *next;
563
564     for (qp = list->q_back; qp != list; qp = next) {
565         next = qp->q_back;
566         lnd_put_one((struct ulist *)qp);
567     }
568 }
569
570 void
571 lnd_put_one(struct ulist *llp)
572 {
573     putland(llp->unit.land.lnd_uid, &llp->unit.land);
574     emp_remque(&llp->queue);
575     free(llp);
576 }
577
578 /*
579  * Sweep landmines with engineers in LAND_LIST for ACTOR.
580  * If EXPLICIT is non-zero, this is for an explicit sweep command from
581  * a player.  Else it's an automatic "on the move" sweep.
582  * If TAKEMOB is non-zero, require and charge mobility.
583  */
584 void
585 lnd_sweep(struct emp_qelem *land_list, int explicit, int takemob,
586           natid actor)
587 {
588     struct emp_qelem *qp;
589     struct emp_qelem *next;
590     struct ulist *llp;
591     struct sctstr sect;
592     int mines, m, max, sshells, lshells;
593
594     for (qp = land_list->q_back; qp != land_list; qp = next) {
595         next = qp->q_back;
596         llp = (struct ulist *)qp;
597         if (!(lchr[llp->unit.land.lnd_type].l_flags & L_ENGINEER)) {
598             if (explicit)
599                 mpr(actor, "%s is not an engineer!\n",
600                     prland(&llp->unit.land));
601             continue;
602         }
603         if (takemob && llp->mobil < 0.0) {
604             if (explicit)
605                 lnd_stays(actor, "is out of mobility", llp);
606             continue;
607         }
608         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &sect);
609         if (!explicit && relations_with(sect.sct_oldown, actor) == ALLIED)
610             continue;
611         if (SCT_MINES_ARE_SEAMINES(&sect)) {
612             if (explicit)
613                 mpr(actor, "%s is in a %s sector.  No landmines there!\n",
614                     prland(&llp->unit.land), dchr[sect.sct_type].d_name);
615             continue;
616         }
617         if (takemob) {
618             llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
619             llp->unit.land.lnd_mobil = (int)llp->mobil;
620             llp->unit.land.lnd_harden = 0;
621         }
622         putland(llp->unit.land.lnd_uid, &llp->unit.land);
623         if (!(mines = sect.sct_mines))
624             continue;
625         max = lchr[llp->unit.land.lnd_type].l_item[I_SHELL];
626         lshells = llp->unit.land.lnd_item[I_SHELL];
627         sshells = sect.sct_item[I_SHELL];
628         for (m = 0; mines > 0 && m < max * 2; m++) {
629             if (chance(0.5 * lchr[llp->unit.land.lnd_type].l_att)) {
630                 mpr(actor, "Sweep...\n");
631                 mines--;
632                 if (lshells < max)
633                     ++lshells;
634                 else if (sshells < ITEM_MAX)
635                     ++sshells;
636             }
637         }
638         sect.sct_mines = mines;
639         llp->unit.land.lnd_item[I_SHELL] = lshells;
640         sect.sct_item[I_SHELL] = sshells;
641         putland(llp->unit.land.lnd_uid, &llp->unit.land);
642         putsect(&sect);
643     }
644 }
645
646 static int
647 contains_engineer(struct emp_qelem *list)
648 {
649     struct emp_qelem *qp;
650     struct emp_qelem *next;
651     struct ulist *llp;
652
653     for (qp = list->q_back; qp != list; qp = next) {
654         next = qp->q_back;
655         llp = (struct ulist *)qp;
656         if (lchr[llp->unit.land.lnd_type].l_flags & L_ENGINEER)
657             return 1;
658     }
659     return 0;
660 }
661
662 int
663 lnd_check_mines(struct emp_qelem *land_list)
664 {
665     struct emp_qelem *qp;
666     struct emp_qelem *next;
667     struct ulist *llp;
668     struct sctstr sect;
669     int stopping = 0;
670     int with_eng = contains_engineer(land_list);
671
672     for (qp = land_list->q_back; qp != land_list; qp = next) {
673         next = qp->q_back;
674         llp = (struct ulist *)qp;
675         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &sect);
676         if (SCT_LANDMINES(&sect) == 0)
677             continue;
678         if (relations_with(sect.sct_oldown, llp->unit.land.lnd_own)
679             == ALLIED)
680             continue;
681         if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
682             lnd_hit_mine(&llp->unit.land);
683             sect.sct_mines--;
684             putsect(&sect);
685             putland(llp->unit.land.lnd_uid, &llp->unit.land);
686             if (!llp->unit.land.lnd_own) {
687                 stopping = 1;
688                 emp_remque(qp);
689                 free(qp);
690             }
691         }
692     }
693     return stopping;
694 }
695
696 static void
697 lnd_stays(natid actor, char *str, struct ulist *llp)
698 {
699     mpr(actor, "%s %s & stays in %s\n",
700         prland(&llp->unit.land), str,
701         xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
702     lnd_mar_put_one(llp);
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', 1);
790                 dam += newdam;
791             } else {
792                 newdam = pln_damage(&plp->plane, 'p', 0);
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.
949  * If removing the land units in LIST would abandon their sector, ask
950  * the user to confirm.
951  * Return zero when abandonment was declined, else non-zero.
952  */
953 int lnd_abandon_askyn(struct emp_qelem *list)
954 {
955     struct ulist *llp;
956     struct sctstr sect;
957     struct emp_qelem *qp;
958
959     if (QEMPTY(list))
960         return 1;
961     llp = (struct ulist *)list->q_back;
962     getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &sect);
963     if (!abandon_askyn(&sect, I_CIVIL, 0, llp))
964         return 0;
965     if (!check_sect_ok(&sect))
966         return 0;
967     for (qp = list->q_back; qp != list; qp = qp->q_back) {
968         if (!check_land_ok(&((struct ulist *)qp)->unit.land))
969             return 0;
970     }
971     return 1;
972 }
973
974 int
975 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor)
976 {
977     struct sctstr sect, osect;
978     struct emp_qelem *qp;
979     struct emp_qelem *next;
980     struct ulist *llp;
981     coord dx;
982     coord dy;
983     coord newx;
984     coord newy;
985     int move;
986     int stopping = 0;
987     int visible;
988     char dp[80];
989     int rel;
990     int oldown;
991
992     if (CANT_HAPPEN(QEMPTY(list)))
993         return 1;
994
995     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
996         lnd_mar_put(list, actor);
997         return 1;
998     }
999     dx = diroff[dir][0];
1000     dy = diroff[dir][1];
1001
1002     llp = (struct ulist *)list->q_back;
1003     getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
1004     oldown = osect.sct_own;
1005     newx = xnorm(llp->unit.land.lnd_x + dx);
1006     newy = ynorm(llp->unit.land.lnd_y + dy);
1007     getsect(newx, newy, &sect);
1008     rel = sect.sct_own ? relations_with(sect.sct_own, actor) : ALLIED;
1009
1010     move = 0;
1011     for (qp = list->q_back; qp != list; qp = next) {
1012         next = qp->q_back;
1013         llp = (struct ulist *)qp;
1014         switch (lnd_check_mar(&llp->unit.land, &sect)) {
1015         case LND_STUCK_NOT:
1016             if (rel == ALLIED
1017                 || (lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
1018                 move = 1;
1019             break;
1020         case LND_STUCK_NO_RAIL:
1021             if (rel == ALLIED)
1022                 mpr(actor, "no rail system in %s\n",
1023                     xyas(newx, newy, actor));
1024             else
1025                 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1026             return 1;
1027         default:
1028             CANT_REACH();
1029             /* fall through */
1030         case LND_STUCK_IMPASSABLE:
1031             mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1032             return 1;
1033         }
1034     }
1035     if (!move) {
1036         mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1037         return 1;
1038     }
1039
1040     for (qp = list->q_back; qp != list; qp = next) {
1041         next = qp->q_back;
1042         llp = (struct ulist *)qp;
1043         if (rel != ALLIED
1044             && !(lchr[llp->unit.land.lnd_type].l_flags & L_SPY)) {
1045             sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
1046             lnd_stays(actor, dp, llp);
1047             continue;
1048         }
1049         if (llp->mobil <= 0.0) {
1050             lnd_stays(actor, "is out of mobility", llp);
1051             continue;
1052         }
1053         llp->unit.land.lnd_x = newx;
1054         llp->unit.land.lnd_y = newy;
1055         llp->mobil -= lnd_mobcost(&llp->unit.land, &sect);
1056         llp->unit.land.lnd_mobil = (int)llp->mobil;
1057         llp->unit.land.lnd_harden = 0;
1058         putland(llp->unit.land.lnd_uid, &llp->unit.land);
1059         putsect(&osect);
1060         getsect(osect.sct_x, osect.sct_y, &osect);
1061         if (osect.sct_own != oldown && oldown == actor) {
1062             /* It was your sector, now it's not.  Simple :) */
1063             mpr(actor, "You no longer own %s\n",
1064                 xyas(osect.sct_x, osect.sct_y, actor));
1065         }
1066         if (rel != ALLIED) {
1067             /* must be a spy */
1068             /* Always a 10% chance of getting caught. */
1069             if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1070                 if (rel == NEUTRAL || rel == FRIENDLY) {
1071                     wu(0, sect.sct_own,
1072                        "%s unit spotted in %s\n", cname(actor),
1073                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1074                     setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1075                 } else if (rel <= HOSTILE) {
1076                     wu(0, sect.sct_own,
1077                        "%s spy shot in %s\n", cname(actor),
1078                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1079                     mpr(actor, "%s was shot and killed.\n",
1080                         prland(&llp->unit.land));
1081                     llp->unit.land.lnd_effic = 0;
1082                     putland(llp->unit.land.lnd_uid, &llp->unit.land);
1083                     lnd_put_one(llp);
1084                 }
1085             }
1086         }
1087     }
1088     if (QEMPTY(list))
1089         return stopping;
1090     lnd_sweep(list, 0, 1, actor);
1091     stopping |= lnd_check_mines(list);
1092     if (QEMPTY(list))
1093         return stopping;
1094
1095     visible = 0;
1096     for (qp = list->q_back; qp != list; qp = next) {
1097         next = qp->q_back;
1098         llp = (struct ulist *)qp;
1099         if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1100             visible = 1;
1101     }
1102     if (visible)
1103         stopping |= lnd_interdict(list, newx, newy, actor);
1104
1105     return stopping;
1106 }
1107
1108 /*
1109  * Fire land unit support against VICTIM for ATTACKER, at X,Y.
1110  * If DEFENDING, this is defensive support, else offensive support.
1111  * Return total damage.
1112  */
1113 int
1114 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1115 {
1116     struct nstr_item ni;
1117     struct lndstr land;
1118     int dam, dam2;
1119     int dist;
1120     int range;
1121
1122     dam = 0;
1123     snxtitem_all(&ni, EF_LAND);
1124     while (nxtitem(&ni, &land)) {
1125         if ((land.lnd_x == x) && (land.lnd_y == y))
1126             continue;
1127         if (!feels_like_helping(land.lnd_own, attacker, victim))
1128             continue;
1129
1130         /* are we in range? */
1131         dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1132
1133         range = roundrange(lnd_fire_range(&land));
1134         if (dist > range)
1135             continue;
1136
1137         dam2 = lnd_fire(&land);
1138         putland(land.lnd_uid, &land);
1139         if (dam2 < 0)
1140             continue;
1141
1142         if (defending)
1143             nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1144         else
1145             nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1146         if (pct_chance(lnd_acc(&land) - 1))
1147             dam2 /= 2;
1148         dam += dam2;
1149         if (land.lnd_own != attacker)
1150             wu(0, land.lnd_own,
1151                "%s supported %s at %s\n",
1152                prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1153     }
1154     return dam;
1155 }
1156
1157 int
1158 lnd_can_attack(struct lndstr *lp)
1159 {
1160     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1161
1162     if (lcp->l_flags & L_SUPPLY)
1163         return 0;
1164
1165     return 1;
1166 }
1167
1168 /*
1169  * Increase fortification value of LP.
1170  * Fortification costs mobility.  Use up to MOB mobility.
1171  * Return actual fortification increase.
1172  */
1173 int
1174 lnd_fortify(struct lndstr *lp, int mob)
1175 {
1176     int hard_amt;
1177     double mob_used, mult;
1178
1179     if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1180         return 0;
1181
1182     mob_used = MIN(lp->lnd_mobil, mob);
1183     if (mob_used < 0)
1184         return 0;
1185
1186     mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1187         ? 1.5 : 1.0;
1188
1189     hard_amt = (int)(mob_used * mult);
1190     if (lp->lnd_harden + hard_amt > land_mob_max) {
1191         hard_amt = land_mob_max - lp->lnd_harden;
1192         mob_used = ceil(hard_amt / mult);
1193     }
1194
1195     lp->lnd_mobil -= (int)mob_used;
1196     lp->lnd_harden += hard_amt;
1197     lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1198
1199     return hard_amt;
1200 }
1201
1202 /*
1203  * Is there a engineer unit at X,Y that can help nation CN?
1204  */
1205 static int
1206 has_helpful_engineer(coord x, coord y, natid cn)
1207 {
1208     struct nstr_item ni;
1209     struct lndstr land;
1210
1211     snxtitem_xy(&ni, EF_LAND, x, y);
1212     while (nxtitem(&ni, &land)) {
1213         if (relations_with(land.lnd_own, cn) != ALLIED)
1214             continue;
1215         if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1216             return 1;
1217     }
1218
1219     return 0;
1220 }
1221
1222 /*
1223  * Set LP's tech to TLEV along with everything else that depends on it.
1224  */
1225 void
1226 lnd_set_tech(struct lndstr *lp, int tlev)
1227 {
1228     struct lchrstr *lcp = lchr + lp->lnd_type;
1229
1230     if (CANT_HAPPEN(tlev < lcp->l_tech))
1231         tlev = lcp->l_tech;
1232
1233     lp->lnd_tech = tlev;
1234 }