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