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