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