]> git.pond.sub.org Git - empserver/blob - src/lib/subs/lndsub.c
subs: Don't hardcode impassable sector types
[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 (((struct lchrstr *)llp->chrp)->l_flags & L_SPY)
171         llp->unit.land.lnd_effic = 0;
172     else {
173         eff_eq = ldround(cas * 100.0 /
174             ((struct lchrstr *)llp->chrp)->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                    ((struct lchrstr *)llp->chrp)->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->chrp = (struct empobj_chr *)&lchr[lp->lnd_type];
448     mlp->unit.land = *lp;
449     mlp->mobil = lp->lnd_mobil;
450     emp_insque(&mlp->queue, list);
451     return mlp;
452 }
453
454 /* This function assumes that the list was created by lnd_sel */
455 void
456 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
457         int *togetherp, natid actor)
458 {
459     struct emp_qelem *qp;
460     struct emp_qelem *next;
461     struct ulist *llp;
462     struct lndstr *lp;
463     struct sctstr sect;
464     coord allx;
465     coord ally;
466     int first = 1;
467     char mess[128];
468
469     *minmobp = 9876.0;
470     *maxmobp = -9876.0;
471     *togetherp = 1;
472     for (qp = list->q_back; qp != list; qp = next) {
473         next = qp->q_back;
474         llp = (struct ulist *)qp;
475         lp = &llp->unit.land;
476         getland(lp->lnd_uid, lp);
477         if (lp->lnd_own != actor) {
478             mpr(actor, "%s was disbanded at %s\n",
479                 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
480             emp_remque(&llp->queue);
481             free(llp);
482             continue;
483         }
484         if (lp->lnd_ship >= 0) {
485             lnd_stays(actor, "is on a ship", llp);
486             continue;
487         }
488         if (lp->lnd_land >= 0) {
489             lnd_stays(actor, "is on a unit", llp);
490             continue;
491         }
492         if (!getsect(lp->lnd_x, lp->lnd_y, &sect)) {
493             lnd_stays(actor, "was sucked into the sky by a strange looking spaceland", llp);    /* heh -KHS */
494             continue;
495         }
496         if (!(lchr[lp->lnd_type].l_flags & L_SPY) &&
497             !(lchr[lp->lnd_type].l_flags & L_TRAIN) &&
498             lp->lnd_item[I_MILIT] == 0) {
499             lnd_stays(actor, "has no mil on it to guide it", llp);
500             continue;
501         }
502         if (relations_with(sect.sct_own, actor) != ALLIED &&
503             !(lchr[lp->lnd_type].l_flags & L_SPY) &&
504             sect.sct_own) {
505             sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
506             lnd_stays(actor, mess, llp);
507             continue;
508         }
509         if (first) {
510             allx = lp->lnd_x;
511             ally = lp->lnd_y;
512             first = 0;
513         }
514         if (lp->lnd_x != allx || lp->lnd_y != ally)
515             *togetherp = 0;
516         if (lp->lnd_mobil + 1 < (int)llp->mobil) {
517             llp->mobil = lp->lnd_mobil;
518         }
519         if (llp->mobil < *minmobp)
520             *minmobp = llp->mobil;
521         if (llp->mobil > *maxmobp)
522             *maxmobp = llp->mobil;
523     }
524 }
525
526 static void
527 lnd_mar_put_one(struct ulist *llp)
528 {
529     if (llp->mobil < -127)
530         llp->mobil = -127;
531     llp->unit.land.lnd_mobil = llp->mobil;
532     lnd_put_one(llp);
533 }
534
535 static void
536 lnd_mar_put(struct emp_qelem *list, natid actor)
537 {
538     struct emp_qelem *qp, *next;
539     struct ulist *llp;
540     struct lndstr *lp;
541
542     for (qp = list->q_back; qp != list; qp = next) {
543         next = qp->q_back;
544         llp = (struct ulist *)qp;
545         lp = &llp->unit.land;
546         mpr(actor, "%s stopped at %s\n",
547             prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
548         lnd_mar_put_one(llp);
549     }
550 }
551
552 void
553 lnd_put(struct emp_qelem *list)
554 {
555     struct emp_qelem *qp, *next;
556
557     for (qp = list->q_back; qp != list; qp = next) {
558         next = qp->q_back;
559         lnd_put_one((struct ulist *)qp);
560     }
561 }
562
563 void
564 lnd_put_one(struct ulist *llp)
565 {
566     putland(llp->unit.land.lnd_uid, &llp->unit.land);
567     emp_remque(&llp->queue);
568     free(llp);
569 }
570
571 /*
572  * Sweep landmines with engineers in LAND_LIST for ACTOR.
573  * If EXPLICIT is non-zero, this is for an explicit sweep command from
574  * a player.  Else it's an automatic "on the move" sweep.
575  * If TAKEMOB is non-zero, require and charge mobility.
576  */
577 void
578 lnd_sweep(struct emp_qelem *land_list, int explicit, int takemob,
579           natid actor)
580 {
581     struct emp_qelem *qp;
582     struct emp_qelem *next;
583     struct ulist *llp;
584     struct sctstr sect;
585     int mines, m, max, sshells, lshells;
586
587     for (qp = land_list->q_back; qp != land_list; qp = next) {
588         next = qp->q_back;
589         llp = (struct ulist *)qp;
590         if (!(((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)) {
591             if (explicit)
592                 mpr(actor, "%s is not an engineer!\n",
593                     prland(&llp->unit.land));
594             continue;
595         }
596         if (takemob && llp->mobil < 0.0) {
597             if (explicit)
598                 lnd_stays(actor, "is out of mobility", llp);
599             continue;
600         }
601         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &sect);
602         if (!explicit && relations_with(sect.sct_oldown, actor) == ALLIED)
603             continue;
604         if (SCT_MINES_ARE_SEAMINES(&sect)) {
605             if (explicit)
606                 mpr(actor, "%s is in a %s sector.  No landmines there!\n",
607                     prland(&llp->unit.land), dchr[sect.sct_type].d_name);
608             continue;
609         }
610         if (takemob) {
611             llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
612             llp->unit.land.lnd_mobil = (int)llp->mobil;
613             llp->unit.land.lnd_harden = 0;
614         }
615         putland(llp->unit.land.lnd_uid, &llp->unit.land);
616         if (!(mines = sect.sct_mines))
617             continue;
618         max = ((struct lchrstr *)llp->chrp)->l_item[I_SHELL];
619         lshells = llp->unit.land.lnd_item[I_SHELL];
620         sshells = sect.sct_item[I_SHELL];
621         for (m = 0; mines > 0 && m < max * 2; m++) {
622             if (chance(0.5 * ((struct lchrstr *)llp->chrp)->l_att)) {
623                 mpr(actor, "Sweep...\n");
624                 mines--;
625                 if (lshells < max)
626                     ++lshells;
627                 else if (sshells < ITEM_MAX)
628                     ++sshells;
629             }
630         }
631         sect.sct_mines = mines;
632         llp->unit.land.lnd_item[I_SHELL] = lshells;
633         sect.sct_item[I_SHELL] = sshells;
634         putland(llp->unit.land.lnd_uid, &llp->unit.land);
635         putsect(&sect);
636     }
637 }
638
639 static int
640 contains_engineer(struct emp_qelem *list)
641 {
642     struct emp_qelem *qp;
643     struct emp_qelem *next;
644     struct ulist *llp;
645
646     for (qp = list->q_back; qp != list; qp = next) {
647         next = qp->q_back;
648         llp = (struct ulist *)qp;
649         if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
650             return 1;
651     }
652     return 0;
653 }
654
655 int
656 lnd_check_mines(struct emp_qelem *land_list)
657 {
658     struct emp_qelem *qp;
659     struct emp_qelem *next;
660     struct ulist *llp;
661     struct sctstr sect;
662     int stopping = 0;
663     int with_eng = contains_engineer(land_list);
664
665     for (qp = land_list->q_back; qp != land_list; qp = next) {
666         next = qp->q_back;
667         llp = (struct ulist *)qp;
668         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &sect);
669         if (SCT_LANDMINES(&sect) == 0)
670             continue;
671         if (relations_with(sect.sct_oldown, llp->unit.land.lnd_own)
672             == ALLIED)
673             continue;
674         if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
675             lnd_hit_mine(&llp->unit.land);
676             sect.sct_mines--;
677             putsect(&sect);
678             putland(llp->unit.land.lnd_uid, &llp->unit.land);
679             if (!llp->unit.land.lnd_own) {
680                 stopping = 1;
681                 emp_remque(qp);
682                 free(qp);
683             }
684         }
685     }
686     return stopping;
687 }
688
689 static void
690 lnd_stays(natid actor, char *str, struct ulist *llp)
691 {
692     mpr(actor, "%s %s & stays in %s\n",
693         prland(&llp->unit.land), str,
694         xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
695     lnd_mar_put_one(llp);
696 }
697
698 static int
699 lnd_count(struct emp_qelem *list)
700 {
701     struct emp_qelem *qp;
702     struct emp_qelem *next;
703     int count = 0;
704
705     for (qp = list->q_back; qp != list; qp = next) {
706         next = qp->q_back;
707         ++count;
708     }
709     return count;
710 }
711
712 static int
713 lnd_damage(struct emp_qelem *list, int totdam)
714 {
715     struct emp_qelem *qp;
716     struct emp_qelem *next;
717     struct ulist *llp;
718     int dam;
719     int count;
720
721     if (!totdam || !(count = lnd_count(list)))
722         return 0;
723     dam = ldround((double)totdam / count, 1);
724     for (qp = list->q_back; qp != list; qp = next) {
725         next = qp->q_back;
726         llp = (struct ulist *)qp;
727         /* land unit might have changed (launched SAMs, collateral dmg) */
728         getland(llp->unit.land.lnd_uid, &llp->unit.land);
729         landdamage(&llp->unit.land, dam);
730         putland(llp->unit.land.lnd_uid, &llp->unit.land);
731         if (!llp->unit.land.lnd_own) {
732             emp_remque(qp);
733             free(qp);
734         }
735     }
736     return dam;
737 }
738
739 static int
740 lnd_easiest_target(struct emp_qelem *list)
741 {
742     struct emp_qelem *qp;
743     struct emp_qelem *next;
744     struct ulist *llp;
745     int hard;
746     int easiest = 9876;         /* things start great for victim */
747     int count = 0;
748
749     for (qp = list->q_back; qp != list; qp = next) {
750         next = qp->q_back;
751         llp = (struct ulist *)qp;
752         hard = lnd_hardtarget(&llp->unit.land);
753         if (hard < easiest)
754             easiest = hard;     /* things get worse for victim */
755         ++count;
756     }
757     return easiest - count;
758 }
759
760 static int
761 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
762                          natid victim)
763 {
764     int mindam = lnd_count(list) * 20;
765     int hardtarget = lnd_easiest_target(list);
766     int dam, newdam, sublaunch;
767     int stopping = 0;
768     struct plist *plp;
769     struct emp_qelem msl_list, *qp, *newqp;
770
771     msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
772
773     dam = 0;
774     for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
775         newqp = qp->q_back;
776         plp = (struct plist *)qp;
777
778         if (dam < mindam && mission_pln_equip(plp, NULL, 'p') >= 0) {
779             if (msl_launch(&plp->plane, EF_LAND, "troops",
780                            newx, newy, victim, &sublaunch) < 0)
781                 goto use_up_msl;
782             stopping = 1;
783             if (msl_hit(&plp->plane, hardtarget, EF_LAND,
784                         N_LND_MISS, N_LND_SMISS, sublaunch, victim)) {
785                 newdam = pln_damage(&plp->plane, 'p', 1);
786                 dam += newdam;
787             } else {
788                 newdam = pln_damage(&plp->plane, 'p', 0);
789                 collateral_damage(newx, newy, newdam);
790             }
791         use_up_msl:
792             plp->plane.pln_effic = 0;
793             putplane(plp->plane.pln_uid, &plp->plane);
794         }
795         emp_remque(qp);
796         free(qp);
797     }
798
799     if (dam) {
800         mpr(victim, "missile interdiction mission does %d damage!\n", dam);
801         collateral_damage(newx, newy, dam);
802         lnd_damage(list, dam);
803     }
804     return stopping;
805 }
806
807 #if 0
808 /* Steve M. - commented out for now until abuse is decided upon */
809 /* risner: allow forts to interdict land units. */
810 static int
811 lnd_fort_interdiction(struct emp_qelem *list,
812                       coord newx, coord newy, natid victim)
813 {
814     struct nstr_sect ns;
815     struct sctstr fsect;
816     int trange, range;
817     int dam;
818     int stopping = 0;
819     int totdam = 0;
820
821     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
822     while (nxtsct(&ns, &fsect)) {
823         if (fsect.sct_own == 0)
824             continue;
825         if (relations_with(fsect.sct_own, victim) >= NEUTRAL)
826             continue;
827         range = roundrange(fortrange(&fsect));
828         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
829         if (trange > range)
830             continue;
831         dam = fort_fire(&fsect);
832         putsect(&fsect);
833         if (dam < 0)
834             continue;
835         stopping = 1;
836         totdam += dam;
837         mpr(victim, "Incoming fire does %d damage!\n", dam);
838         wu(0, fsect.sct_own,
839            "%s fires at %s land units in %s for %d!\n",
840            xyas(fsect.sct_x, fsect.sct_y,
841                 fsect.sct_own),
842            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
843         nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
844     }
845     if (totdam > 0)
846         lnd_damage(list, totdam);
847     return stopping;
848 }
849 #endif
850
851 static int
852 lnd_mission_interdiction(struct emp_qelem *list, coord x, coord y,
853                          natid victim)
854 {
855     int dam;
856
857     dam = unit_interdict(x, y, victim, "land units",
858                          lnd_easiest_target(list),
859                          MI_INTERDICT);
860     if (dam >= 0)
861         lnd_damage(list, dam);
862     return dam >= 0;
863 }
864
865 int
866 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
867 {
868     int stopping = 0;
869
870 #if 0
871     if (!opt_NO_FORT_FIRE)
872 /* Steve M. - commented out for now until abuse is decided upon */
873         stopping |= lnd_fort_interdiction(list, newx, newy, victim);
874 #endif
875
876     stopping |= lnd_mission_interdiction(list, newx, newy, victim);
877     stopping |= lnd_missile_interdiction(list, newx, newy, victim);
878     return stopping;
879 }
880
881 /* high value of hardtarget is harder to hit */
882 int
883 lnd_hardtarget(struct lndstr *lp)
884 {
885     struct sctstr sect;
886
887     getsect(lp->lnd_x, lp->lnd_y, &sect);
888     return (int)((lp->lnd_effic / 100.0) *
889                  (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
890                   - lnd_vis(lp)));
891 }
892
893 static int
894 lnd_hit_mine(struct lndstr *lp)
895 {
896     int m;
897
898     mpr(lp->lnd_own, "Blammo! Landmines detected in %s!\n",
899         xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
900
901     nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
902
903     m = MINE_LDAMAGE();
904     if (lchr[lp->lnd_type].l_flags & L_ENGINEER)
905         m /= 2;
906
907     landdamage(lp, m);
908     return m;
909 }
910
911 double
912 lnd_pathcost(struct lndstr *lp, double pathcost)
913 {
914     double effspd;
915
916     effspd = lnd_spd(lp);
917     if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
918         effspd *= lp->lnd_effic * 0.01;
919
920     /*
921      * The return value must be PATHCOST times a factor that depends
922      * only on the land unit.  Anything else breaks path finding.  In
923      * particular, you can't add or enforce a minimum cost here.  Do
924      * it in sector_mcost().
925      */
926     return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
927 }
928
929 int
930 lnd_mobtype(struct lndstr *lp)
931 {
932     return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
933         ? MOB_RAIL : MOB_MARCH;
934 }
935
936 double
937 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
938 {
939     return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
940 }
941
942 int
943 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
944                    int together)
945 {
946     struct sctstr sect, osect;
947     struct emp_qelem *qp;
948     struct emp_qelem *qp2;
949     struct emp_qelem *next;
950     struct ulist *llp;
951     struct emp_qelem cur, done;
952     coord dx;
953     coord dy;
954     coord newx;
955     coord newy;
956     int stopping = 0;
957     int visible;
958     int stop;
959     char dp[80];
960     int rel;
961     int oldown;
962
963     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
964         lnd_mar_put(list, actor);
965         return 1;
966     }
967     dx = diroff[dir][0];
968     dy = diroff[dir][1];
969     for (qp = list->q_back; qp != list; qp = next) {
970         next = qp->q_back;
971         llp = (struct ulist *)qp;
972         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
973         oldown = osect.sct_own;
974         newx = xnorm(llp->unit.land.lnd_x + dx);
975         newy = ynorm(llp->unit.land.lnd_y + dy);
976         getsect(newx, newy, &sect);
977         rel = relations_with(sect.sct_own, actor);
978         if ((rel != ALLIED && sect.sct_own
979              && !(lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
980             || dchr[sect.sct_type].d_mob0 < 0) {
981             if (together) {
982                 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
983                 return 1;
984             } else {
985                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
986                 lnd_stays(actor, dp, llp);
987                 continue;
988             }
989         }
990         if (!SCT_HAS_RAIL(&sect)
991             && lnd_mobtype(&llp->unit.land) == MOB_RAIL) {
992             if (together) {
993                 mpr(actor, "no rail system in %s\n",
994                     xyas(newx, newy, actor));
995                 return 1;
996             } else {
997                 sprintf(dp, "has no rail system in %s",
998                         xyas(newx, newy, actor));
999                 lnd_stays(actor, dp, llp);
1000                 continue;
1001             }
1002         }
1003         /* Note we check would_abandon first because we don't want
1004            to always have to do these checks */
1005         if (would_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
1006             stop = 0;
1007             if (!want_to_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
1008                 stop = 1;
1009             }
1010             /* now check stuff */
1011             if (!check_sect_ok(&sect))
1012                 return 1;
1013             if (!check_sect_ok(&osect))
1014                 return 1;
1015             for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
1016                 if (!check_land_ok(&((struct ulist *)qp2)->unit.land))
1017                     return 1;
1018             }
1019             if (stop) {
1020                 lnd_stays(actor, "stops", llp);
1021                 continue;
1022             }
1023         }
1024         if (llp->mobil <= 0.0) {
1025             lnd_stays(actor, "is out of mobility", llp);
1026             continue;
1027         }
1028         llp->unit.land.lnd_x = newx;
1029         llp->unit.land.lnd_y = newy;
1030         llp->mobil -= lnd_mobcost(&llp->unit.land, &sect);
1031         llp->unit.land.lnd_mobil = (int)llp->mobil;
1032         llp->unit.land.lnd_harden = 0;
1033         putland(llp->unit.land.lnd_uid, &llp->unit.land);
1034         putsect(&osect);
1035         getsect(osect.sct_x, osect.sct_y, &osect);
1036         if (osect.sct_own != oldown && oldown == actor) {
1037             /* It was your sector, now it's not.  Simple :) */
1038             mpr(actor, "You no longer own %s\n",
1039                 xyas(osect.sct_x, osect.sct_y, actor));
1040         }
1041         if (rel != ALLIED && sect.sct_own) {    /* must be a spy */
1042             /* Always a 10% chance of getting caught. */
1043             if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1044                 if (rel == NEUTRAL || rel == FRIENDLY) {
1045                     wu(0, sect.sct_own,
1046                        "%s unit spotted in %s\n", cname(actor),
1047                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1048                     setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1049                 } else if (rel <= HOSTILE) {
1050                     wu(0, sect.sct_own,
1051                        "%s spy shot in %s\n", cname(actor),
1052                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1053                     mpr(actor, "%s was shot and killed.\n",
1054                         prland(&llp->unit.land));
1055                     llp->unit.land.lnd_effic = 0;
1056                     putland(llp->unit.land.lnd_uid, &llp->unit.land);
1057                     lnd_put_one(llp);
1058                 }
1059             }
1060         }
1061     }
1062     if (QEMPTY(list))
1063         return stopping;
1064     lnd_sweep(list, 0, 1, actor);
1065     stopping |= lnd_check_mines(list);
1066     if (QEMPTY(list))
1067         return stopping;
1068
1069     /* interdict land units sector by sector */
1070     emp_initque(&cur);
1071     emp_initque(&done);
1072     while (!QEMPTY(list)) {
1073         llp = (struct ulist *)list->q_back;
1074         newx = llp->unit.land.lnd_x;
1075         newy = llp->unit.land.lnd_y;
1076         /* move units in NEWX,NEWY to cur */
1077         visible = 0;
1078         for (qp = list->q_back; qp != list; qp = next) {
1079             next = qp->q_back;
1080             llp = (struct ulist *)qp;
1081             if (llp->unit.land.lnd_x == newx && llp->unit.land.lnd_y == newy) {
1082                 emp_remque(qp);
1083                 emp_insque(qp, &cur);
1084                 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1085                     visible = 1;
1086             }
1087         }
1088         /* interdict them */
1089         if (visible)
1090             stopping |= lnd_interdict(&cur, newx, newy, actor);
1091         /* move survivors to done */
1092         for (qp = cur.q_back; qp != &cur; qp = next) {
1093             next = qp->q_back;
1094             emp_remque(qp);
1095             emp_insque(qp, &done);
1096         }
1097     }
1098     /* assign surviving land units back to list */
1099     emp_insque(list, &done);
1100     emp_remque(&done);
1101
1102     return stopping;
1103 }
1104
1105 /*
1106  * Fire land unit support against VICTIM for ATTACKER, at X,Y.
1107  * If DEFENDING, this is defensive support, else offensive support.
1108  * Return total damage.
1109  */
1110 int
1111 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1112 {
1113     struct nstr_item ni;
1114     struct lndstr land;
1115     int dam, dam2;
1116     int dist;
1117     int range;
1118
1119     dam = 0;
1120     snxtitem_all(&ni, EF_LAND);
1121     while (nxtitem(&ni, &land)) {
1122         if ((land.lnd_x == x) && (land.lnd_y == y))
1123             continue;
1124         if (!feels_like_helping(land.lnd_own, attacker, victim))
1125             continue;
1126
1127         /* are we in range? */
1128         dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1129
1130         range = roundrange(lnd_fire_range(&land));
1131         if (dist > range)
1132             continue;
1133
1134         dam2 = lnd_fire(&land);
1135         putland(land.lnd_uid, &land);
1136         if (dam2 < 0)
1137             continue;
1138
1139         if (defending)
1140             nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1141         else
1142             nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1143         if (pct_chance(lnd_acc(&land) - 1))
1144             dam2 /= 2;
1145         dam += dam2;
1146         if (land.lnd_own != attacker)
1147             wu(0, land.lnd_own,
1148                "%s supported %s at %s\n",
1149                prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1150     }
1151     return dam;
1152 }
1153
1154 int
1155 lnd_can_attack(struct lndstr *lp)
1156 {
1157     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1158
1159     if (lcp->l_flags & L_SUPPLY)
1160         return 0;
1161
1162     return 1;
1163 }
1164
1165 /*
1166  * Increase fortification value of LP.
1167  * Fortification costs mobility.  Use up to MOB mobility.
1168  * Return actual fortification increase.
1169  */
1170 int
1171 lnd_fortify(struct lndstr *lp, int mob)
1172 {
1173     int hard_amt;
1174     double mob_used, mult;
1175
1176     if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1177         return 0;
1178
1179     mob_used = MIN(lp->lnd_mobil, mob);
1180     if (mob_used < 0)
1181         return 0;
1182
1183     mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1184         ? 1.5 : 1.0;
1185
1186     hard_amt = (int)(mob_used * mult);
1187     if (lp->lnd_harden + hard_amt > land_mob_max) {
1188         hard_amt = land_mob_max - lp->lnd_harden;
1189         mob_used = ceil(hard_amt / mult);
1190     }
1191
1192     lp->lnd_mobil -= (int)mob_used;
1193     lp->lnd_harden += hard_amt;
1194     lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1195
1196     return hard_amt;
1197 }
1198
1199 /*
1200  * Is there a engineer unit at X,Y that can help nation CN?
1201  */
1202 static int
1203 has_helpful_engineer(coord x, coord y, natid cn)
1204 {
1205     struct nstr_item ni;
1206     struct lndstr land;
1207
1208     snxtitem_xy(&ni, EF_LAND, x, y);
1209     while (nxtitem(&ni, &land)) {
1210         if (relations_with(land.lnd_own, cn) != ALLIED)
1211             continue;
1212         if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1213             return 1;
1214     }
1215
1216     return 0;
1217 }
1218
1219 /*
1220  * Set LP's tech to TLEV along with everything else that depends on it.
1221  */
1222 void
1223 lnd_set_tech(struct lndstr *lp, int tlev)
1224 {
1225     struct lchrstr *lcp = lchr + lp->lnd_type;
1226
1227     if (CANT_HAPPEN(tlev < lcp->l_tech))
1228         tlev = lcp->l_tech;
1229
1230     lp->lnd_tech = tlev;
1231 }