]> git.pond.sub.org Git - empserver/blob - src/lib/subs/lndsub.c
Sectors need space for items, deliveries and distribution thresholds.
[empserver] / src / lib / subs / lndsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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  */
34
35 #include <math.h>
36 #include "misc.h"
37 #include "player.h"
38 #include "file.h"
39 #include "var.h"
40 #include "sect.h"
41 #include "path.h"
42 #include "news.h"
43 #include "treaty.h"
44 #include "nat.h"
45 #include "xy.h"
46 #include "land.h"
47 #include "ship.h"
48 #include "nsc.h"
49 #include "mission.h"
50 #include "plane.h"
51 #include "combat.h"
52 #include "damage.h"
53 #include "optlist.h"
54 #include "prototypes.h"
55
56 int
57 attack_val(int combat_mode, struct lndstr *lp)
58 {
59     int men;
60     int value;
61     struct lchrstr *lcp;
62
63     if (((int)lp->lnd_effic) < LAND_MINEFF) {
64         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
65         lp->lnd_own = 0;
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 = total_mil(lp);
79
80     value = ldround(((double)men * (double)lp->lnd_att), 1);
81
82     value = (int)((double)value * ((double)lp->lnd_effic / 100.0));
83
84     switch (combat_mode) {
85     case A_ATTACK:
86         return value;
87     case A_ASSAULT:
88         if (!(lcp->l_flags & L_MARINE))
89             return (int)(assault_penalty * value);
90         break;
91     case A_BOARD:
92         if (!(lcp->l_flags & L_MARINE))
93             return (int)(assault_penalty * men);
94     }
95
96     return value;
97 }
98
99 int
100 defense_val(struct lndstr *lp)
101 {
102     int men;
103     double value;
104     struct lchrstr *lcp;
105
106     if (((int)lp->lnd_effic) < LAND_MINEFF) {
107         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
108         lp->lnd_own = 0;
109         putland(lp->lnd_uid, lp);
110         return 0;
111     }
112
113     lcp = &lchr[(int)lp->lnd_type];
114
115     men = total_mil(lp);
116
117     if (men < 0)
118         men = 0;
119     if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
120         !(lcp->l_flags & L_MARINE))
121         return men;
122
123     value = men * lp->lnd_def;
124
125     value *=
126         ((double)land_mob_max + lp->lnd_harden) / (double)land_mob_max;
127     value = (int)((double)value * ((double)lp->lnd_effic / 100.0));
128     value = (int)ldround(value, 1);
129
130     /* If there are military on the unit, you get at least a 1
131        man defensive unit, except for spies */
132     if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
133         return 1;
134
135     return (int)(value);
136 }
137
138 int
139 total_mil(struct lndstr *lp)
140 {
141     struct lchrstr *lcp;
142     double men;
143
144     lcp = &lchr[(int)lp->lnd_type];
145
146     men = lnd_getmil(lp);
147 /*      men *= ((double)lp->lnd_effic)/100.0;*/
148
149     return ldround(men, 1);
150 }
151
152 void
153 lnd_print(struct llist *llp, s_char *s)
154 {
155     if (llp->land.lnd_own == player->cnum)
156         pr("%s %s\n", prland(&llp->land), s);
157     else
158         wu(0, llp->land.lnd_own, "%s %s\n", prland(&llp->land), s);
159 }
160
161 void
162 lnd_delete(struct llist *llp, s_char *s)
163 {
164     if (s)
165         lnd_print(llp, s);
166     putland(llp->land.lnd_uid, &llp->land);
167     emp_remque((struct emp_qelem *)llp);
168     free((s_char *)llp);
169 }
170
171 int
172 lnd_take_casualty(int combat_mode, struct llist *llp, int cas)
173                         /* attacking or assaulting or paratrooping? */
174                         /* number of casualties to take */
175 {
176     int eff_eq;
177     int n;
178     int biggest;
179     int civs;
180     int nowned;
181     coord ret_x, ret_y;
182     coord bx, by;
183     struct sctstr sect;
184     int ret_chance;
185     s_char buf[1024];
186     int taken;
187     int nowhere_to_go = 0;
188     struct sctstr rsect;
189     double mobcost;
190     s_char orig;
191     int mob;
192
193
194
195     taken = lnd_getmil(&(llp->land));
196     /* Spies always die */
197     if (llp->lcp->l_flags & L_SPY) {
198         eff_eq = 100;
199         llp->land.lnd_effic = 0;
200     } else {
201         eff_eq =
202             ldround((((double)cas * 100.0) / (double)llp->lcp->l_mil), 1);
203         llp->land.lnd_effic -= eff_eq;
204         lnd_submil(&(llp->land), cas);
205     }
206
207     if (llp->land.lnd_effic < LAND_MINEFF) {
208         sprintf(buf, "dies %s %s!",
209                 combat_mode ? att_mode[combat_mode] : (s_char *)
210                 "defending", xyas(llp->land.lnd_x, llp->land.lnd_y,
211                                   llp->land.lnd_own));
212         lnd_delete(llp, buf);
213         /* Since we killed the unit, we killed all the mil on it */
214         return taken;
215     } else {
216         /* Ok, now, how many did we take off? (sould be the diff) */
217         taken = taken - lnd_getmil(&(llp->land));
218     }
219
220     if (llp->land.lnd_effic >= llp->land.lnd_retreat)
221         return taken;
222
223     /* we're being boarded */
224     if (llp->land.lnd_ship >= 0 && combat_mode == A_DEFEND)
225         return taken;
226
227     /* we're being boarded */
228     if (llp->land.lnd_land >= 0 && combat_mode == A_DEFEND)
229         return taken;
230
231     /* Have to make a retreat check */
232
233     ret_chance = llp->land.lnd_retreat - llp->land.lnd_effic;
234     if (roll(100) < ret_chance) {
235         pr("\n");
236         lnd_print(llp, "fails morale check!");
237         llp->land.lnd_mission = 0;
238         llp->land.lnd_harden = 0;
239         if (llp->land.lnd_ship >= 0 || llp->land.lnd_land >= 0)
240             nowhere_to_go = 1;
241         else if (combat_mode == A_DEFEND) {
242             /*
243              * defending unit.. find a place to send it
244              * strategy: look for the most-populated 
245              * adjacent sector that is owned by the unit
246              * player->owner. Charge mob..
247              */
248             biggest = -1;
249             nowned = 0;
250             for (n = 1; n <= 6; ++n) {
251                 ret_x = llp->land.lnd_x + diroff[n][0];
252                 ret_y = llp->land.lnd_y + diroff[n][1];
253                 getsect(ret_x, ret_y, &sect);
254                 if (sect.sct_own != llp->land.lnd_own)
255                     continue;
256                 if (sect.sct_type == SCT_MOUNT)
257                     continue;
258                 ++nowned;
259                 civs = sect.sct_item[I_CIVIL];
260                 if (civs > biggest) {
261                     biggest = civs;
262                     bx = sect.sct_x;
263                     by = sect.sct_y;
264                 }
265             }
266             if (!nowned)
267                 nowhere_to_go = 1;
268             else {
269                 /* retreat to bx,by */
270                 llp->land.lnd_x = bx;
271                 llp->land.lnd_y = by;
272                 getsect(bx, by, &rsect);
273                 mobcost = lnd_mobcost(&llp->land, &rsect, MOB_ROAD);
274                 mob = llp->land.lnd_mobil - (int)mobcost;
275                 if (mob < -127)
276                     mob = -127;
277                 orig = llp->land.lnd_mobil;
278                 llp->land.lnd_mobil = (s_char)mob;
279                 if (llp->land.lnd_mobil > orig)
280                     llp->land.lnd_mobil = (-127);
281                 sprintf(buf, "retreats at %d%% efficiency to %s!",
282                         llp->land.lnd_effic,
283                         xyas(bx, by, llp->land.lnd_own));
284                 lnd_delete(llp, buf);
285             }
286         } else {                /* attacking from a sector */
287             sprintf(buf, "leaves the battlefield at %d%% efficiency",
288                     llp->land.lnd_effic);
289             if ((llp->land.lnd_mobil - (int)llp->mobil) < -127)
290                 llp->land.lnd_mobil = -127;
291             else
292                 llp->land.lnd_mobil -= (int)llp->mobil;
293             llp->mobil = 0.0;
294             lnd_delete(llp, buf);
295         }
296     }
297     if (nowhere_to_go) {
298         /* nowhere to go.. take more casualties */
299         llp->land.lnd_effic -= 10;
300         lnd_submil(&llp->land, llp->lcp->l_mil / 10);
301         if (llp->land.lnd_effic < LAND_MINEFF)
302             lnd_delete(llp, "has nowhere to retreat, and dies!");
303         else
304             lnd_print(llp,
305                       "has nowhere to retreat and takes extra losses!");
306     }
307
308     return taken;
309 }
310
311 void
312 lnd_takemob(struct emp_qelem *list, double loss)
313 {
314     struct emp_qelem *qp, *next;
315     struct llist *llp;
316     int new;
317     int mcost = ldround(combat_mob * loss, 1);
318
319     for (qp = list->q_forw; qp != list; qp = next) {
320         next = qp->q_forw;
321         llp = (struct llist *)qp;
322 /*
323                 if (chance(loss))
324                         use_supply(&llp->land);
325                 if (llp->land.lnd_mission == MI_RESERVE)
326                         new = llp->land.lnd_mobil - mcost/2;
327                 else
328  */
329         new = llp->land.lnd_mobil - mcost;
330         if (new < -127)
331             new = -127;
332         llp->land.lnd_mobil = (s_char)new;
333     }
334 }
335 int
336 lnd_getmil(struct lndstr *lp)
337 {
338     return lp->lnd_item[I_MILIT];
339 }
340
341 void
342 lnd_submil(struct lndstr *lp, int num)
343 {
344     int new = lp->lnd_item[I_MILIT] - num;
345     lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
346 }
347
348 int
349 lnd_spyval(struct lndstr *lp)
350 {
351     if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
352         return (lp->lnd_spy * (lp->lnd_effic / 100.0)) + 2;
353     else
354         return (lp->lnd_spy * (lp->lnd_effic / 100.0));
355 }
356
357 int
358 intelligence_report(int destination, struct lndstr *lp, int spy,
359                     s_char *mess)
360 {
361     struct lchrstr *lcp;
362     s_char buf1[80], buf2[80], buf3[80];
363     double estimate = 0.0;      /* estimated defense value */
364
365     if (destination == 0)
366         return 0;
367
368     if (lp->lnd_own == 0)
369         return 0;
370
371     lcp = &lchr[(int)lp->lnd_type];
372
373     memset(buf1, 0, sizeof(buf1));
374     memset(buf2, 0, sizeof(buf2));
375     memset(buf3, 0, sizeof(buf3));
376     if (chance((double)(spy + lp->lnd_vis) / 10.0)) {
377         if (destination == player->cnum)
378             pr("%s %s", mess, prland(lp));
379         else
380             sprintf(buf1, "%s %s", mess, prland(lp));
381
382         estimate = lnd_getmil(lp);
383
384         if (chance((double)(spy + lp->lnd_vis) / 20.0)) {
385
386             if (destination == player->cnum)
387                 pr(" (eff %d, mil %d", roundintby(lp->lnd_effic, 5),
388                    roundintby(lnd_getmil(lp), 10));
389             else
390                 sprintf(buf2, " (eff %d, mil %d",
391                         roundintby(lp->lnd_effic, 5),
392                         roundintby(lnd_getmil(lp), 10));
393             estimate = lnd_getmil(lp) * lp->lnd_effic / 100.0;
394
395             if (chance((double)(spy + lp->lnd_vis) / 20.0)) {
396                 int t;
397                 t = lp->lnd_tech - 20 + roll(40);
398                 t = max(t, 0);
399                 if (destination == player->cnum)
400                     pr(", tech %d)\n", t);
401                 else
402                     sprintf(buf3, ", tech %d)\n", t);
403             } else {
404                 if (destination == player->cnum)
405                     pr(")\n");
406                 else
407                     sprintf(buf3, ")\n");
408             }
409         } else {
410             if (destination == player->cnum)
411                 pr("\n");
412             else
413                 sprintf(buf2, "\n");
414         }
415     }
416
417     if (destination != player->cnum) {
418         wu(0, destination, "%s%s%s", buf1, buf2, buf3);
419     }
420
421     if (lp->lnd_ship < 0 || lcp->l_flags & L_MARINE)
422         estimate *= lp->lnd_def;
423
424     return (int)estimate;
425 }
426
427 /* Used by the spy command to count land units in a sector.  If used
428    for anything else, you may want to reconsider, because this doesn't
429    always count spies. :) */
430 int
431 count_sect_units(struct sctstr *sp)
432 {
433     int count = 0;
434     struct nstr_item ni;
435     struct lndstr land;
436
437     snxtitem_all(&ni, EF_LAND);
438     while (nxtitem(&ni, (s_char *)&land)) {
439         if (!land.lnd_own)
440             continue;
441         if (land.lnd_x != sp->sct_x || land.lnd_y != sp->sct_y)
442             continue;
443         /* Don't always see spies */
444         if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
445             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
446                 continue;
447         }
448         /* Got here, report it */
449         ++count;
450     }
451
452     return count;
453 }
454
455 void
456 count_units(struct shpstr *sp)
457 {
458     struct nstr_item ni;
459     struct lndstr land;
460     int nland = 0;
461
462     if (sp->shp_effic < SHIP_MINEFF)
463         return;
464
465     snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
466     while (nxtitem(&ni, (s_char *)&land)) {
467         if (land.lnd_own == 0)
468             continue;
469         if (land.lnd_ship == sp->shp_uid)
470             nland++;
471     }
472
473     if (sp->shp_nland != nland) {
474         sp->shp_nland = nland;
475         putship(sp->shp_uid, sp);
476     }
477 }
478
479 void
480 lnd_count_units(struct lndstr *lp)
481 {
482     struct nstr_item ni;
483     struct lndstr land;
484     int nland = 0;
485
486     if (lp->lnd_effic < LAND_MINEFF)
487         return;
488
489     snxtitem_xy(&ni, EF_LAND, lp->lnd_x, lp->lnd_y);
490     while (nxtitem(&ni, (s_char *)&land)) {
491         if (land.lnd_own == 0)
492             continue;
493         if (land.lnd_land == lp->lnd_uid)
494             nland++;
495     }
496
497     if (lp->lnd_nland != nland) {
498         lp->lnd_nland = nland;
499         putland(lp->lnd_uid, lp);
500     }
501 }
502
503 void
504 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
505
506
507 /*      int     wantflags;
508         int     nowantflags;
509 */
510 {
511     struct lndstr land;
512     struct lchrstr *lcp;
513     struct llist *llp;
514
515     emp_initque(list);
516     while (nxtitem(ni, (s_char *)&land)) {
517         if (!player->owner)
518             continue;
519         if (opt_MARKET) {
520             if (ontradingblock(EF_LAND, (int *)&land)) {
521                 pr("unit #%d inelligible - it's for sale.\n",
522                    land.lnd_uid);
523                 continue;
524             }
525         }
526         lcp = &lchr[(int)land.lnd_type];
527 /*              if (wantflags && (lcp->m_flags & wantflags) != wantflags)
528                         continue;
529                 if (nowantflags && lcp->m_flags & nowantflags)
530                         continue;
531 */
532         /* This abuse is better fixed by building a unit with the normal negative
533            mobility that everything else is built with */
534 /* Just so that the player can't build a bunch of land units, and them
535    march them a few minutes later... */
536 /*
537                 if (opt_MOB_ACCESS) {
538                   if (land.lnd_effic < 11 &&
539                     land.lnd_mobil < etu_per_update) {
540                     pr("Land unit #%d needs at least %d mob to march.\n",
541                         land.lnd_uid, etu_per_update);
542                     continue;
543                   }
544                 }
545 */
546         land.lnd_mission = 0;
547         land.lnd_rflags = 0;
548         land.lnd_harden = 0;
549         memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
550         putland(land.lnd_uid, &land);
551         llp = (struct llist *)malloc(sizeof(struct llist));
552         llp->lcp = lcp;
553         llp->land = land;
554         llp->mobil = (double)land.lnd_mobil;
555         emp_insque(&llp->queue, list);
556     }
557 }
558
559 /* This function assumes that the list was created by lnd_sel */
560 void
561 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
562         int *togetherp, natid actor)
563 {
564     struct emp_qelem *qp;
565     struct emp_qelem *next;
566     struct llist *llp;
567     struct sctstr sect;
568     struct lndstr land;
569     coord allx;
570     coord ally;
571     int first = 1;
572     s_char mess[128];
573     int rel;
574
575     *minmobp = 9876.0;
576     *maxmobp = -9876.0;
577     *togetherp = 1;
578     for (qp = list->q_back; qp != list; qp = next) {
579         next = qp->q_back;
580         llp = (struct llist *)qp;
581         getland(llp->land.lnd_uid, &land);
582         if (land.lnd_own != actor) {
583             mpr(actor, "%s was disbanded at %s\n",
584                 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
585             emp_remque((struct emp_qelem *)llp);
586             free((s_char *)llp);
587             continue;
588         }
589         if (land.lnd_ship >= 0) {
590             lnd_mess("is on a ship", llp);
591             continue;
592         }
593         if (land.lnd_land >= 0) {
594             lnd_mess("is on a unit", llp);
595             continue;
596         }
597         if (!getsect(land.lnd_x, land.lnd_y, &sect)) {
598             lnd_mess("was sucked into the sky by a strange looking spaceland", llp);    /* heh -KHS */
599             continue;
600         }
601         if (!(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
602             !(lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) &&
603             lnd_getmil(&llp->land) == 0) {
604             lnd_mess("has no mil on it to guide it", llp);
605             continue;
606         }
607         rel = getrel(getnatp(sect.sct_own), player->cnum);
608         if (sect.sct_own != land.lnd_own && rel != ALLIED &&
609             !(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
610             sect.sct_own) {
611             sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
612             lnd_mess(mess, llp);
613             continue;
614         }
615         if (first) {
616             allx = land.lnd_x;
617             ally = land.lnd_y;
618             first = 0;
619         }
620         if (land.lnd_x != allx || land.lnd_y != ally)
621             *togetherp = 0;
622         if (land.lnd_mobil + 1 < (int)llp->mobil) {
623             llp->mobil = (double)land.lnd_mobil;
624         }
625         if (llp->mobil < *minmobp)
626             *minmobp = llp->mobil;
627         if (llp->mobil > *maxmobp)
628             *maxmobp = llp->mobil;
629         llp->land = land;
630     }
631 }
632
633 void
634 lnd_put(struct emp_qelem *list, natid actor)
635 {
636     register struct emp_qelem *qp;
637     register struct emp_qelem *newqp;
638     struct llist *llp;
639
640     qp = list->q_back;
641     while (qp != list) {
642         llp = (struct llist *)qp;
643         if (actor) {
644             mpr(actor, "%s stopped at %s\n", prland(&llp->land),
645                 xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
646             if (llp->mobil < -127)
647                 llp->mobil = -127;
648             llp->land.lnd_mobil = llp->mobil;
649         }
650         putland(llp->land.lnd_uid, &llp->land);
651         newqp = qp->q_back;
652         emp_remque(qp);
653         free((s_char *)qp);
654         qp = newqp;
655     }
656 }
657
658 void
659 lnd_sweep(struct emp_qelem *land_list, int verbose, int takemob,
660           natid actor)
661 {
662     struct emp_qelem *qp;
663     struct emp_qelem *next;
664     struct llist *llp;
665     struct sctstr sect;
666     int mines, m, max, sshells, lshells;
667     double mobcost;
668
669     for (qp = land_list->q_back; qp != land_list; qp = next) {
670         next = qp->q_back;
671         llp = (struct llist *)qp;
672         if (!(llp->lcp->l_flags & L_ENGINEER)) {
673             if (verbose)
674                 mpr(actor, "%s is not an engineer!\n", prland(&llp->land));
675             continue;
676         }
677         if (takemob && llp->mobil < 0.0) {
678             if (verbose)
679                 lnd_mess("is out of mobility", llp);
680             continue;
681         }
682         getsect(llp->land.lnd_x, llp->land.lnd_y, &sect);
683         if (sect.sct_oldown == llp->land.lnd_own) {
684             if (verbose)
685                 mpr(actor,
686                     "%s is in a sector completely owned by you.  Don't bother digging up mines there!\n",
687                     prland(&llp->land));
688             continue;
689         }
690         if (sect.sct_type == SCT_BSPAN) {
691             if (verbose)
692                 mpr(actor, "%s is on a bridge.  No mines there!\n",
693                     prland(&llp->land));
694             continue;
695         }
696         if (takemob) {
697 /*                      mobcost = llp->land.lnd_effic * 0.01 * llp->lcp->l_spd;*/
698             mobcost = llp->land.lnd_spd;
699             mobcost = 480.0 / (mobcost +
700                                techfact(llp->land.lnd_tech, mobcost));
701             llp->mobil -= mobcost;
702             llp->land.lnd_mobil = (int)llp->mobil;
703             llp->land.lnd_harden = 0;
704         }
705         putland(llp->land.lnd_uid, &llp->land);
706         if (!(mines = sect.sct_mines))
707             continue;
708         max = vl_find(V_SHELL, llp->lcp->l_vtype,
709                       llp->lcp->l_vamt, (int)llp->lcp->l_nv);
710         lshells = llp->land.lnd_item[I_SHELL];
711         sshells = sect.sct_item[I_SHELL];
712         for (m = 0; mines > 0 && m < max * 2; m++) {
713             if (chance(0.5 * llp->lcp->l_att)) {
714                 mpr(actor, "Sweep...\n");
715                 mines--;
716                 if (lshells < max)
717                     ++lshells;
718                 else
719                     ++sshells;
720             }
721         }
722         sect.sct_mines = mines;
723         llp->land.lnd_item[I_SHELL] = lshells;
724         sect.sct_item[I_SHELL] = sshells;
725         putland(llp->land.lnd_uid, &llp->land);
726         putsect(&sect);
727     }
728 }
729
730 static int
731 contains_engineer(struct emp_qelem *list)
732 {
733     struct emp_qelem *qp;
734     struct emp_qelem *next;
735     struct llist *llp;
736
737     for (qp = list->q_back; qp != list; qp = next) {
738         next = qp->q_back;
739         llp = (struct llist *)qp;
740         if (llp->lcp->l_flags & L_ENGINEER)
741             return 1;
742     }
743     return 0;
744 }
745
746 int
747 lnd_check_mines(struct emp_qelem *land_list)
748 {
749     struct emp_qelem *qp;
750     struct emp_qelem *next;
751     struct llist *llp;
752     struct sctstr sect;
753     int stopping = 0;
754     int with_eng = contains_engineer(land_list);
755
756     for (qp = land_list->q_back; qp != land_list; qp = next) {
757         next = qp->q_back;
758         llp = (struct llist *)qp;
759         getsect(llp->land.lnd_x, llp->land.lnd_y, &sect);
760         if (sect.sct_oldown == llp->land.lnd_own)
761             continue;
762         if (sect.sct_type == SCT_BSPAN)
763             continue;
764         if (!sect.sct_mines)
765             continue;
766         if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
767             lnd_hit_mine(&llp->land, llp->lcp);
768             sect.sct_mines--;
769             putsect(&sect);
770             putland(llp->land.lnd_uid, (s_char *)&llp->land);
771             if (!llp->land.lnd_own) {
772                 stopping = 1;
773                 emp_remque(qp);
774                 free((s_char *)qp);
775             }
776         }
777     }
778     return stopping;
779 }
780
781 void
782 lnd_list(struct emp_qelem *land_list)
783 {
784     struct emp_qelem *qp;
785     struct emp_qelem *next;
786     struct llist *llp;
787     struct lndstr *lnd;
788     int vec[I_MAX + 1];
789
790     pr("lnd#     land type       x,y    a  eff  sh gun xl  mu tech retr fuel\n");
791
792     for (qp = land_list->q_back; qp != land_list; qp = next) {
793         next = qp->q_back;
794         llp = (struct llist *)qp;
795         lnd = &llp->land;
796         pr("%4d ", lnd->lnd_uid);
797         pr("%-16.16s ", llp->lcp->l_name);
798         prxy("%4d,%-4d ", lnd->lnd_x, lnd->lnd_y, llp->land.lnd_own);
799         pr("%1c", lnd->lnd_army);
800         pr("%4d%%", lnd->lnd_effic);
801         getvec(VT_ITEM, vec, (s_char *)lnd, EF_LAND);
802         pr("%4d", vec[I_SHELL]);
803         pr("%4d", vec[I_GUN]);
804         count_land_planes(lnd);
805         pr("%3d", lnd->lnd_nxlight);
806         pr("%4d", lnd->lnd_mobil);
807         pr("%4d", lnd->lnd_tech);
808         pr("%4d%%", lnd->lnd_retreat);
809         pr("%5d\n", lnd->lnd_fuel);
810     }
811 }
812
813 void
814 lnd_mess(s_char *str, struct llist *llp)
815 {
816     mpr(llp->land.lnd_own, "%s %s & stays in %s\n",
817         prland(&llp->land),
818         str, xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
819     if (llp->mobil < -127)
820         llp->mobil = -127;
821     llp->land.lnd_mobil = llp->mobil;
822     putland(llp->land.lnd_uid, &llp->land);
823     emp_remque((struct emp_qelem *)llp);
824     free((s_char *)llp);
825 }
826
827 static int
828 lnd_count(struct emp_qelem *list)
829 {
830     struct emp_qelem *qp;
831     struct emp_qelem *next;
832     struct llist *llp;
833     int count = 0;
834
835     for (qp = list->q_back; qp != list; qp = next) {
836         next = qp->q_back;
837         llp = (struct llist *)qp;
838         ++count;
839     }
840     return count;
841 }
842
843 static int
844 lnd_damage(struct emp_qelem *list, int totdam)
845 {
846     struct emp_qelem *qp;
847     struct emp_qelem *next;
848     struct llist *llp;
849     int dam;
850     int count;
851
852     if (!totdam || !(count = lnd_count(list)))
853         return 0;
854     dam = ldround(((double)totdam / (double)count), 1);
855     for (qp = list->q_back; qp != list; qp = next) {
856         next = qp->q_back;
857         llp = (struct llist *)qp;
858         /* have to get it again because of collateral damage */
859         getland(llp->land.lnd_uid, &llp->land);
860         landdamage(&llp->land, dam);
861         putland(llp->land.lnd_uid, &llp->land);
862         if (!llp->land.lnd_own) {
863             emp_remque(qp);
864             free((s_char *)qp);
865         }
866     }
867     return dam;
868 }
869
870 static int
871 lnd_easiest_target(struct emp_qelem *list)
872 {
873     struct emp_qelem *qp;
874     struct emp_qelem *next;
875     struct llist *llp;
876     int hard;
877     int easiest = 9876;         /* things start great for victim */
878     int count = 0;
879
880     for (qp = list->q_back; qp != list; qp = next) {
881         next = qp->q_back;
882         llp = (struct llist *)qp;
883         hard = lnd_hardtarget(&llp->land);
884         if (hard < easiest)
885             easiest = hard;     /* things get worse for victim */
886         ++count;
887     }
888     return easiest - count;
889 }
890
891 static int
892 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
893                          natid victim)
894 {
895     int dam;
896     struct emp_qelem msl_list, *qp, *newqp;
897
898     msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
899
900     dam = msl_launch_mindam(&msl_list, newx, newy,
901                             lnd_easiest_target(list), EF_LAND,
902                             lnd_count(list) * 20, "troops", victim,
903                             MI_INTERDICT);
904     if (dam) {
905         mpr(victim, "missile interdiction mission does %d damage!\n", dam);
906         collateral_damage(newx, newy, dam, 0);
907     }
908     qp = msl_list.q_forw;
909     while (qp != msl_list.q_forw) {
910         newqp = qp->q_forw;
911         emp_remque(qp);
912         free(qp);
913         qp = newqp;
914     }
915     return dam;
916 }
917
918 #if 0
919 /* Steve M. - commented out for now until abuse is decided upon */
920 /* risner: allow forts to interdict land units. */
921 static int
922 lnd_fort_interdiction(struct emp_qelem *list,
923                       coord newx, coord newy, natid victim)
924 {
925     struct nstr_sect ns;
926     struct sctstr fsect;
927     int trange;
928     double range, range2, guneff;
929     int shell, gun;
930     int dam;
931     int totdam = 0;
932     int i;
933
934     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
935     while (nxtsct(&ns, &fsect)) {
936         if (fsect.sct_own == 0)
937             continue;
938         if (fsect.sct_own == victim)
939             continue;
940         if (fsect.sct_type != SCT_FORTR)
941             continue;
942         if (getrel(getnatp(fsect.sct_own), victim) >= NEUTRAL)
943             continue;
944         gun = fsect.sct_item[I_GUN];
945         if (gun < 1)
946             continue;
947         range = tfactfire(fsect.sct_own, (double)min(gun, 7));
948         if (fsect.sct_effic > 59)
949             range++;
950         range2 = roundrange(range);
951         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
952         if (trange > range2)
953             continue;
954         if (fsect.sct_item[I_MILIT] < 5)
955             continue;
956         shell = fsect.sct_item[I_SHELL];
957         if (shell < 1)
958             shell += supply_commod(fsect.sct_own,
959                                    fsect.sct_x, fsect.sct_y, I_SHELL, 1);
960         if (shell < 1)
961             continue;
962         shell--;
963         fsect.sct_item[I_SHELL] = shell;
964         putsect(&fsect);
965         if (gun > 7)
966             gun = 7;
967         guneff = landgun((int)fsect.sct_effic, gun);
968         dam = (int)guneff;
969         totdam += dam;
970         mpr(victim, "Incoming fire does %d damage!\n", dam);
971         wu(0, fsect.sct_own,
972            "%s fires at %s land units in %s for %d!\n",
973            xyas(fsect.sct_x, fsect.sct_y,
974                 fsect.sct_own),
975            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
976         nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
977     }
978     if (totdam > 0)
979         return lnd_damage(list, totdam);
980     return 0;
981 }
982 #endif
983
984 int
985 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
986 {
987     int stopping = 0;
988
989 #if 0
990     if (!opt_NO_FORT_FIRE)
991 /* Steve M. - commented out for now until abuse is decided upon */
992         stopping |= lnd_fort_interdiction(list, newx, newy, victim);
993 #endif
994
995     stopping |=
996         lnd_damage(list,
997                    unit_interdict(newx, newy, victim, "land units",
998                                   lnd_easiest_target(list), MI_INTERDICT));
999
1000     stopping |=
1001         lnd_damage(list,
1002                    lnd_missile_interdiction(list, newx, newy, victim));
1003     return stopping;
1004 }
1005
1006 /* high value of hardtarget is harder to hit */
1007 int
1008 lnd_hardtarget(struct lndstr *lp)
1009 {
1010     struct sctstr sect;
1011
1012     getsect(lp->lnd_x, lp->lnd_y, &sect);
1013     return (int)(((double)lp->lnd_effic / 100.0) *
1014                  (10 + dchr[sect.sct_type].d_dstr * 2 +
1015                   (double)lp->lnd_spd / 2.0 - lp->lnd_vis));
1016 }
1017
1018 int
1019 lnd_hit_mine(struct lndstr *lp, struct lchrstr *lcp)
1020 {
1021     double m;
1022
1023     mpr(lp->lnd_own, "Blammo! Landmines detected in %s! ",
1024         xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
1025
1026     nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
1027
1028     m = roll(20) + 10;
1029     if (lcp->l_flags & L_ENGINEER)
1030         m /= 2.0;
1031
1032     landdamage(lp, ldround(m, 1));
1033
1034     return (int)m;
1035 }
1036
1037 double
1038 lnd_mobcost(struct lndstr *lp, struct sctstr *sp, int mobtype)
1039 {
1040     double mobcost;
1041     double smobcost;
1042
1043     /* supply unit's speed depends on their eff, since
1044        that is their purpose */
1045     if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
1046         mobcost = lp->lnd_effic * 0.01 * lp->lnd_spd;
1047     else
1048         mobcost = lp->lnd_spd;
1049     if (mobcost < 0.01)
1050         mobcost = 0.01;
1051
1052 /* sector_mcost now takes 2 different arguments, a sector pointer, and
1053    whether or not to figure in the highway bonus, rail bonus or none.
1054    bridge heads, bridges and highways have built-in highways bonus
1055    because they are a 1, and this will discount that. */
1056
1057     smobcost = (double)sector_mcost(sp, mobtype);
1058     if (smobcost < 0.01)
1059         smobcost = 0.01;
1060
1061 /* marching through 0 mobility conquered sectors takes lots of mobility,
1062    unless you are a train.  Capturing railways is a good thing. */
1063
1064     if (sp->sct_own != sp->sct_oldown && sp->sct_mobil <= 0 &&
1065         smobcost < LND_MINMOBCOST && mobtype != MOB_RAIL)
1066         smobcost = LND_MINMOBCOST;
1067
1068     mobcost = smobcost * 5.0 * 480.0 /
1069         (mobcost + techfact(lp->lnd_tech, mobcost));
1070
1071     return mobcost;
1072 }
1073
1074 int
1075 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
1076                    int together)
1077 {
1078     struct sctstr sect, osect;
1079     struct emp_qelem *qp;
1080     struct emp_qelem *qp2;
1081     struct emp_qelem *next;
1082     struct llist *llp;
1083     coord dx;
1084     coord dy;
1085     coord newx;
1086     coord newy;
1087     int stopping = 0;
1088     int visible = 0;
1089     int stop;
1090     s_char dp[80];
1091     int rel;
1092     int oldown;
1093
1094     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
1095         lnd_put(list, actor);
1096         return 1;
1097     }
1098     dx = diroff[dir][0];
1099     dy = diroff[dir][1];
1100     for (qp = list->q_back; qp != list; qp = next) {
1101         next = qp->q_back;
1102         llp = (struct llist *)qp;
1103         getsect(llp->land.lnd_x, llp->land.lnd_y, &osect);
1104         oldown = osect.sct_own;
1105         newx = xnorm(llp->land.lnd_x + dx);
1106         newy = ynorm(llp->land.lnd_y + dy);
1107         getsect(newx, newy, &sect);
1108         rel = getrel(getnatp(sect.sct_own), player->cnum);
1109         if ((sect.sct_own != actor && rel != ALLIED &&
1110              !(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
1111              sect.sct_own) || (sect.sct_type == SCT_WATER ||
1112                                sect.sct_type == SCT_SANCT ||
1113                                sect.sct_type == SCT_WASTE)) {
1114             if (together) {
1115                 pr("can't go to %s\n", xyas(newx, newy, actor));
1116                 return 1;
1117             } else {
1118                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
1119                 lnd_mess(dp, llp);
1120                 continue;
1121             }
1122         }
1123
1124         if (!(lchr[(int)llp->land.lnd_type].l_flags & L_SPY))
1125             visible = 1;
1126         if (sect.sct_rail == 0 &&
1127             lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) {
1128             if (together) {
1129                 pr("no rail system in %s\n", xyas(newx, newy, actor));
1130                 return 1;
1131             } else {
1132                 sprintf(dp, "has no rail system in %s",
1133                         xyas(newx, newy, actor));
1134                 lnd_mess(dp, llp);
1135                 continue;
1136             }
1137         }
1138         /* Note we check would_abandon first because we don't want
1139            to always have to do these checks */
1140         if (would_abandon(&osect, V_CIVIL, 0, &(llp->land))) {
1141             stop = 0;
1142             if (!want_to_abandon(&osect, V_CIVIL, 0, &(llp->land))) {
1143                 stop = 1;
1144             }
1145             /* now check stuff */
1146             if (!check_sect_ok(&sect))
1147                 return 1;
1148             if (!check_sect_ok(&osect))
1149                 return 1;
1150             for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
1151                 if (!check_land_ok(&(((struct llist *)qp2)->land)))
1152                     return 1;
1153             }
1154             if (stop) {
1155                 lnd_mess("stops", llp);
1156                 continue;
1157             }
1158         }
1159         if (llp->mobil <= 0.0) {
1160             lnd_mess("is out of mobility", llp);
1161             continue;
1162         }
1163         llp->land.lnd_x = newx;
1164         llp->land.lnd_y = newy;
1165         if (lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) {
1166             llp->mobil -= lnd_mobcost(&llp->land, &sect, MOB_RAIL);
1167         } else {
1168             llp->mobil -= lnd_mobcost(&llp->land, &sect, MOB_ROAD);
1169         }
1170         llp->land.lnd_mobil = (int)llp->mobil;
1171         llp->land.lnd_harden = 0;
1172         putland(llp->land.lnd_uid, &llp->land);
1173         putsect(&osect);
1174         getsect(osect.sct_x, osect.sct_y, &osect);
1175         if (osect.sct_own != oldown && oldown == player->cnum) {
1176             /* It was your sector, now it's not.  Simple :) */
1177             pr("You no longer own %s\n",
1178                xyas(osect.sct_x, osect.sct_y, player->cnum));
1179         }
1180         if (rel != ALLIED && sect.sct_own != actor && sect.sct_own) {   /* must be a spy */
1181             /* Always a 10% chance of getting caught. */
1182             if (chance(LND_SPY_DETECT_CHANCE(llp->land.lnd_effic))) {
1183                 if (rel == NEUTRAL || rel == FRIENDLY) {
1184                     wu(0, sect.sct_own,
1185                        "%s unit spotted in %s\n", cname(player->cnum),
1186                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1187                     setrel(sect.sct_own, llp->land.lnd_own, HOSTILE);
1188                 } else if (rel == HOSTILE || rel == AT_WAR ||
1189                            rel == SITZKRIEG || rel == MOBILIZATION) {
1190                     wu(0, sect.sct_own,
1191                        "%s spy shot in %s\n", cname(player->cnum),
1192                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1193                     pr("%s was shot and killed.\n", prland(&llp->land));
1194                     llp->land.lnd_effic = 0;
1195                     putland(llp->land.lnd_uid, &llp->land);
1196                     lnd_delete(llp, 0);
1197                 }
1198             }
1199         }
1200     }
1201     if (QEMPTY(list))
1202         return stopping;
1203     lnd_sweep(list, 0, 1, actor);
1204     stopping |= lnd_check_mines(list);
1205     if (QEMPTY(list))
1206         return stopping;
1207     if (visible)
1208         stopping |= lnd_interdict(list, newx, newy, actor);
1209     return stopping;
1210 }
1211
1212 /*
1213  * find all artillery units belonging
1214  * to the attacker or defender that can fire.
1215  * Each arty unit adds +1%/damage point
1216  *
1217  */
1218 int
1219 lnd_support(natid victim, natid attacker, coord x, coord y)
1220 {
1221     struct nstr_item ni;
1222     struct lndstr land;
1223     int rel, rel2;
1224     double dam = 0.0;
1225     int dist;
1226     int shell;
1227     int gun;
1228     double range, range2;
1229
1230     snxtitem_all(&ni, EF_LAND);
1231     while (nxtitem(&ni, (s_char *)&land)) {
1232         if (land.lnd_frg == 0)
1233             continue;
1234         if ((land.lnd_x == x) && (land.lnd_y == y))
1235             continue;
1236         if (land.lnd_ship >= 0)
1237             continue;
1238         if (land.lnd_land >= 0)
1239             continue;
1240         if (land.lnd_mission > 0)
1241             continue;
1242         if (land.lnd_effic < LAND_MINFIREEFF)
1243             continue;
1244         /* Do we have mil? */
1245         if (land.lnd_item[I_MILIT] <= 0)
1246             continue;
1247         rel = getrel(getnatp(land.lnd_own), attacker);
1248         rel2 = getrel(getnatp(land.lnd_own), victim);
1249         if ((land.lnd_own != attacker) &&
1250             ((rel != ALLIED) || (rel2 != AT_WAR)))
1251             continue;
1252
1253         /* do we have supplies? */
1254         if (!has_supply(&land))
1255             continue;
1256
1257         /* are we in range? */
1258         dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1259
1260         range = techfact((int)land.lnd_tech, (double)land.lnd_frg / 2.0);
1261
1262         range2 = roundrange(range);
1263         if (dist > range2)
1264             continue;
1265
1266         shell = land.lnd_item[I_SHELL];
1267         gun = land.lnd_item[I_GUN];
1268
1269         if (shell == 0 || gun == 0)
1270             continue;
1271
1272         use_supply(&land);
1273         nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1274         if (roll(100) < land.lnd_acc) {
1275             dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1276                                land.lnd_ammo, shell) / 2;
1277         } else {
1278             dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1279                                land.lnd_ammo, shell);
1280         }
1281         if (land.lnd_own != attacker)
1282             wu(0, land.lnd_own,
1283                "%s supported %s at %s\n",
1284                prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1285     }
1286     return (int)dam;
1287 }
1288
1289 s_char *
1290 lnd_path(int together, struct lndstr *lp, s_char *buf)
1291 {
1292     coord destx;
1293     coord desty;
1294     struct sctstr d_sect, sect;
1295     s_char *cp;
1296     double dummy;
1297
1298     if (!sarg_xy(buf, &destx, &desty))
1299         return 0;
1300     if (!together) {
1301         pr("Cannot go to a destination sector if not all starting in the same sector\n");
1302         return 0;
1303     }
1304     if (!getsect(destx, desty, &d_sect)) {
1305         pr("%d,%d is not a sector\n", destx, desty);
1306         return 0;
1307     }
1308     getsect(lp->lnd_x, lp->lnd_y, &sect);
1309     if (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
1310         cp = (s_char *)BestLandPath(buf, &sect, &d_sect, &dummy, MOB_RAIL);
1311     else
1312         cp = (s_char *)BestLandPath(buf, &sect, &d_sect, &dummy, MOB_ROAD);
1313     if (!cp) {
1314         pr("No owned path from %s to %s!\n",
1315            xyas(lp->lnd_x, lp->lnd_y, player->cnum),
1316            xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
1317         return 0;
1318     }
1319     pr("Using path '%s'\n", cp);
1320     return cp;
1321 }
1322
1323 int
1324 lnd_can_attack(struct lndstr *lp)
1325 {
1326     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1327
1328 /*      if (lcp->l_flags & L_SUPPLY ||
1329             lcp->l_flags & L_SECURITY ||
1330             lcp->l_flags & L_FLAK ||
1331             lcp->l_frg)
1332                 return 0; */
1333     if (lcp->l_flags & L_SUPPLY)
1334         return 0;
1335
1336     return 1;
1337 }
1338
1339 /*
1340  * Increase fortification value of LP.
1341  * Fortification costs mobility.  Use up to HARD_AMT mobility.
1342  * Return actual fortification increase.
1343  */
1344 int
1345 lnd_fortify (struct lndstr *lp, int hard_amt)
1346 {
1347     int mob_used;
1348     int eng;
1349
1350     if ((lp->lnd_ship >= 0) || lp->lnd_land >= 0)
1351         return 0;
1352
1353     hard_amt = min(lp->lnd_mobil, hard_amt);
1354
1355     if ((lp->lnd_harden + hard_amt) > land_mob_max)
1356         hard_amt = land_mob_max - lp->lnd_harden;
1357
1358     eng = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own);
1359
1360     if (eng)
1361         hard_amt = ((float)hard_amt * 1.5);
1362
1363     if ((lp->lnd_harden + hard_amt) > land_mob_max)
1364         hard_amt = land_mob_max - lp->lnd_harden;
1365
1366     /* Ok, set the mobility used */
1367     mob_used = hard_amt;
1368
1369     /* Now, if an engineer helped, it's really only 2/3rds of
1370        that */
1371     if (eng)
1372         mob_used = (int)((float)mob_used / 1.5);
1373
1374     /* If we increased it, but not much, we gotta take at least 1
1375        mob point. */
1376     if (mob_used <= 0 && hard_amt > 0)
1377         mob_used = 1;
1378
1379     lp->lnd_mobil -= mob_used;
1380     if (lp->lnd_mobil < 0)
1381         lp->lnd_mobil = 0;
1382
1383     lp->lnd_harden += hard_amt;
1384     lp->lnd_harden = min(lp->lnd_harden, land_mob_max);
1385
1386     return hard_amt;
1387 }