]> git.pond.sub.org Git - empserver/blob - src/lib/subs/lndsub.c
Update copyright notice.
[empserver] / src / lib / subs / lndsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 = llp->lcp->l_item[I_SHELL];
709         lshells = llp->land.lnd_item[I_SHELL];
710         sshells = sect.sct_item[I_SHELL];
711         for (m = 0; mines > 0 && m < max * 2; m++) {
712             if (chance(0.5 * llp->lcp->l_att)) {
713                 mpr(actor, "Sweep...\n");
714                 mines--;
715                 if (lshells < max)
716                     ++lshells;
717                 else if (sshells < ITEM_MAX)
718                     ++sshells;
719             }
720         }
721         sect.sct_mines = mines;
722         llp->land.lnd_item[I_SHELL] = lshells;
723         sect.sct_item[I_SHELL] = sshells;
724         putland(llp->land.lnd_uid, &llp->land);
725         putsect(&sect);
726     }
727 }
728
729 static int
730 contains_engineer(struct emp_qelem *list)
731 {
732     struct emp_qelem *qp;
733     struct emp_qelem *next;
734     struct llist *llp;
735
736     for (qp = list->q_back; qp != list; qp = next) {
737         next = qp->q_back;
738         llp = (struct llist *)qp;
739         if (llp->lcp->l_flags & L_ENGINEER)
740             return 1;
741     }
742     return 0;
743 }
744
745 int
746 lnd_check_mines(struct emp_qelem *land_list)
747 {
748     struct emp_qelem *qp;
749     struct emp_qelem *next;
750     struct llist *llp;
751     struct sctstr sect;
752     int stopping = 0;
753     int with_eng = contains_engineer(land_list);
754
755     for (qp = land_list->q_back; qp != land_list; qp = next) {
756         next = qp->q_back;
757         llp = (struct llist *)qp;
758         getsect(llp->land.lnd_x, llp->land.lnd_y, &sect);
759         if (sect.sct_oldown == llp->land.lnd_own)
760             continue;
761         if (sect.sct_type == SCT_BSPAN)
762             continue;
763         if (!sect.sct_mines)
764             continue;
765         if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
766             lnd_hit_mine(&llp->land, llp->lcp);
767             sect.sct_mines--;
768             putsect(&sect);
769             putland(llp->land.lnd_uid, (s_char *)&llp->land);
770             if (!llp->land.lnd_own) {
771                 stopping = 1;
772                 emp_remque(qp);
773                 free((s_char *)qp);
774             }
775         }
776     }
777     return stopping;
778 }
779
780 void
781 lnd_list(struct emp_qelem *land_list)
782 {
783     struct emp_qelem *qp;
784     struct emp_qelem *next;
785     struct llist *llp;
786     struct lndstr *lnd;
787
788     pr("lnd#     land type       x,y    a  eff  sh gun xl  mu tech retr fuel\n");
789
790     for (qp = land_list->q_back; qp != land_list; qp = next) {
791         next = qp->q_back;
792         llp = (struct llist *)qp;
793         lnd = &llp->land;
794         pr("%4d ", lnd->lnd_uid);
795         pr("%-16.16s ", llp->lcp->l_name);
796         prxy("%4d,%-4d ", lnd->lnd_x, lnd->lnd_y, llp->land.lnd_own);
797         pr("%1c", lnd->lnd_army);
798         pr("%4d%%", lnd->lnd_effic);
799         pr("%4d", lnd->lnd_item[I_SHELL]);
800         pr("%4d", lnd->lnd_item[I_GUN]);
801         count_land_planes(lnd);
802         pr("%3d", lnd->lnd_nxlight);
803         pr("%4d", lnd->lnd_mobil);
804         pr("%4d", lnd->lnd_tech);
805         pr("%4d%%", lnd->lnd_retreat);
806         pr("%5d\n", lnd->lnd_fuel);
807     }
808 }
809
810 void
811 lnd_mess(s_char *str, struct llist *llp)
812 {
813     mpr(llp->land.lnd_own, "%s %s & stays in %s\n",
814         prland(&llp->land),
815         str, xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
816     if (llp->mobil < -127)
817         llp->mobil = -127;
818     llp->land.lnd_mobil = llp->mobil;
819     putland(llp->land.lnd_uid, &llp->land);
820     emp_remque((struct emp_qelem *)llp);
821     free((s_char *)llp);
822 }
823
824 static int
825 lnd_count(struct emp_qelem *list)
826 {
827     struct emp_qelem *qp;
828     struct emp_qelem *next;
829     struct llist *llp;
830     int count = 0;
831
832     for (qp = list->q_back; qp != list; qp = next) {
833         next = qp->q_back;
834         llp = (struct llist *)qp;
835         ++count;
836     }
837     return count;
838 }
839
840 static int
841 lnd_damage(struct emp_qelem *list, int totdam)
842 {
843     struct emp_qelem *qp;
844     struct emp_qelem *next;
845     struct llist *llp;
846     int dam;
847     int count;
848
849     if (!totdam || !(count = lnd_count(list)))
850         return 0;
851     dam = ldround(((double)totdam / (double)count), 1);
852     for (qp = list->q_back; qp != list; qp = next) {
853         next = qp->q_back;
854         llp = (struct llist *)qp;
855         /* have to get it again because of collateral damage */
856         getland(llp->land.lnd_uid, &llp->land);
857         landdamage(&llp->land, dam);
858         putland(llp->land.lnd_uid, &llp->land);
859         if (!llp->land.lnd_own) {
860             emp_remque(qp);
861             free((s_char *)qp);
862         }
863     }
864     return dam;
865 }
866
867 static int
868 lnd_easiest_target(struct emp_qelem *list)
869 {
870     struct emp_qelem *qp;
871     struct emp_qelem *next;
872     struct llist *llp;
873     int hard;
874     int easiest = 9876;         /* things start great for victim */
875     int count = 0;
876
877     for (qp = list->q_back; qp != list; qp = next) {
878         next = qp->q_back;
879         llp = (struct llist *)qp;
880         hard = lnd_hardtarget(&llp->land);
881         if (hard < easiest)
882             easiest = hard;     /* things get worse for victim */
883         ++count;
884     }
885     return easiest - count;
886 }
887
888 static int
889 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
890                          natid victim)
891 {
892     int dam;
893     struct emp_qelem msl_list, *qp, *newqp;
894
895     msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
896
897     dam = msl_launch_mindam(&msl_list, newx, newy,
898                             lnd_easiest_target(list), EF_LAND,
899                             lnd_count(list) * 20, "troops", victim,
900                             MI_INTERDICT);
901     if (dam) {
902         mpr(victim, "missile interdiction mission does %d damage!\n", dam);
903         collateral_damage(newx, newy, dam, 0);
904     }
905     qp = msl_list.q_forw;
906     while (qp != msl_list.q_forw) {
907         newqp = qp->q_forw;
908         emp_remque(qp);
909         free(qp);
910         qp = newqp;
911     }
912     return dam;
913 }
914
915 #if 0
916 /* Steve M. - commented out for now until abuse is decided upon */
917 /* risner: allow forts to interdict land units. */
918 static int
919 lnd_fort_interdiction(struct emp_qelem *list,
920                       coord newx, coord newy, natid victim)
921 {
922     struct nstr_sect ns;
923     struct sctstr fsect;
924     int trange;
925     double range, range2, guneff;
926     int shell, gun;
927     int dam;
928     int totdam = 0;
929     int i;
930
931     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
932     while (nxtsct(&ns, &fsect)) {
933         if (fsect.sct_own == 0)
934             continue;
935         if (fsect.sct_own == victim)
936             continue;
937         if (fsect.sct_type != SCT_FORTR)
938             continue;
939         if (getrel(getnatp(fsect.sct_own), victim) >= NEUTRAL)
940             continue;
941         gun = fsect.sct_item[I_GUN];
942         if (gun < 1)
943             continue;
944         range = tfactfire(fsect.sct_own, (double)min(gun, 7));
945         if (fsect.sct_effic > 59)
946             range++;
947         range2 = roundrange(range);
948         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
949         if (trange > range2)
950             continue;
951         if (fsect.sct_item[I_MILIT] < 5)
952             continue;
953         shell = fsect.sct_item[I_SHELL];
954         if (shell < 1)
955             shell += supply_commod(fsect.sct_own,
956                                    fsect.sct_x, fsect.sct_y, I_SHELL, 1);
957         if (shell < 1)
958             continue;
959         shell--;
960         fsect.sct_item[I_SHELL] = shell;
961         putsect(&fsect);
962         if (gun > 7)
963             gun = 7;
964         guneff = landgun((int)fsect.sct_effic, gun);
965         dam = (int)guneff;
966         totdam += dam;
967         mpr(victim, "Incoming fire does %d damage!\n", dam);
968         wu(0, fsect.sct_own,
969            "%s fires at %s land units in %s for %d!\n",
970            xyas(fsect.sct_x, fsect.sct_y,
971                 fsect.sct_own),
972            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
973         nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
974     }
975     if (totdam > 0)
976         return lnd_damage(list, totdam);
977     return 0;
978 }
979 #endif
980
981 int
982 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
983 {
984     int stopping = 0;
985
986 #if 0
987     if (!opt_NO_FORT_FIRE)
988 /* Steve M. - commented out for now until abuse is decided upon */
989         stopping |= lnd_fort_interdiction(list, newx, newy, victim);
990 #endif
991
992     stopping |=
993         lnd_damage(list,
994                    unit_interdict(newx, newy, victim, "land units",
995                                   lnd_easiest_target(list), MI_INTERDICT));
996
997     stopping |=
998         lnd_damage(list,
999                    lnd_missile_interdiction(list, newx, newy, victim));
1000     return stopping;
1001 }
1002
1003 /* high value of hardtarget is harder to hit */
1004 int
1005 lnd_hardtarget(struct lndstr *lp)
1006 {
1007     struct sctstr sect;
1008
1009     getsect(lp->lnd_x, lp->lnd_y, &sect);
1010     return (int)(((double)lp->lnd_effic / 100.0) *
1011                  (10 + dchr[sect.sct_type].d_dstr * 2 +
1012                   (double)lp->lnd_spd / 2.0 - lp->lnd_vis));
1013 }
1014
1015 int
1016 lnd_hit_mine(struct lndstr *lp, struct lchrstr *lcp)
1017 {
1018     double m;
1019
1020     mpr(lp->lnd_own, "Blammo! Landmines detected in %s! ",
1021         xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
1022
1023     nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
1024
1025     m = roll(20) + 10;
1026     if (lcp->l_flags & L_ENGINEER)
1027         m /= 2.0;
1028
1029     landdamage(lp, ldround(m, 1));
1030
1031     return (int)m;
1032 }
1033
1034 double
1035 lnd_mobcost(struct lndstr *lp, struct sctstr *sp, int mobtype)
1036 {
1037     double mobcost;
1038     double smobcost;
1039
1040     /* supply unit's speed depends on their eff, since
1041        that is their purpose */
1042     if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
1043         mobcost = lp->lnd_effic * 0.01 * lp->lnd_spd;
1044     else
1045         mobcost = lp->lnd_spd;
1046     if (mobcost < 0.01)
1047         mobcost = 0.01;
1048
1049 /* sector_mcost now takes 2 different arguments, a sector pointer, and
1050    whether or not to figure in the highway bonus, rail bonus or none.
1051    bridge heads, bridges and highways have built-in highways bonus
1052    because they are a 1, and this will discount that. */
1053
1054     smobcost = (double)sector_mcost(sp, mobtype);
1055     if (smobcost < 0.01)
1056         smobcost = 0.01;
1057
1058 /* marching through 0 mobility conquered sectors takes lots of mobility,
1059    unless you are a train.  Capturing railways is a good thing. */
1060
1061     if (sp->sct_own != sp->sct_oldown && sp->sct_mobil <= 0 &&
1062         smobcost < LND_MINMOBCOST && mobtype != MOB_RAIL)
1063         smobcost = LND_MINMOBCOST;
1064
1065     mobcost = smobcost * 5.0 * 480.0 /
1066         (mobcost + techfact(lp->lnd_tech, mobcost));
1067
1068     return mobcost;
1069 }
1070
1071 int
1072 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
1073                    int together)
1074 {
1075     struct sctstr sect, osect;
1076     struct emp_qelem *qp;
1077     struct emp_qelem *qp2;
1078     struct emp_qelem *next;
1079     struct llist *llp;
1080     coord dx;
1081     coord dy;
1082     coord newx;
1083     coord newy;
1084     int stopping = 0;
1085     int visible = 0;
1086     int stop;
1087     s_char dp[80];
1088     int rel;
1089     int oldown;
1090
1091     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
1092         lnd_put(list, actor);
1093         return 1;
1094     }
1095     dx = diroff[dir][0];
1096     dy = diroff[dir][1];
1097     for (qp = list->q_back; qp != list; qp = next) {
1098         next = qp->q_back;
1099         llp = (struct llist *)qp;
1100         getsect(llp->land.lnd_x, llp->land.lnd_y, &osect);
1101         oldown = osect.sct_own;
1102         newx = xnorm(llp->land.lnd_x + dx);
1103         newy = ynorm(llp->land.lnd_y + dy);
1104         getsect(newx, newy, &sect);
1105         rel = getrel(getnatp(sect.sct_own), player->cnum);
1106         if ((sect.sct_own != actor && rel != ALLIED &&
1107              !(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
1108              sect.sct_own) || (sect.sct_type == SCT_WATER ||
1109                                sect.sct_type == SCT_SANCT ||
1110                                sect.sct_type == SCT_WASTE)) {
1111             if (together) {
1112                 pr("can't go to %s\n", xyas(newx, newy, actor));
1113                 return 1;
1114             } else {
1115                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
1116                 lnd_mess(dp, llp);
1117                 continue;
1118             }
1119         }
1120
1121         if (!(lchr[(int)llp->land.lnd_type].l_flags & L_SPY))
1122             visible = 1;
1123         if (sect.sct_rail == 0 &&
1124             lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) {
1125             if (together) {
1126                 pr("no rail system in %s\n", xyas(newx, newy, actor));
1127                 return 1;
1128             } else {
1129                 sprintf(dp, "has no rail system in %s",
1130                         xyas(newx, newy, actor));
1131                 lnd_mess(dp, llp);
1132                 continue;
1133             }
1134         }
1135         /* Note we check would_abandon first because we don't want
1136            to always have to do these checks */
1137         if (would_abandon(&osect, I_CIVIL, 0, &(llp->land))) {
1138             stop = 0;
1139             if (!want_to_abandon(&osect, I_CIVIL, 0, &(llp->land))) {
1140                 stop = 1;
1141             }
1142             /* now check stuff */
1143             if (!check_sect_ok(&sect))
1144                 return 1;
1145             if (!check_sect_ok(&osect))
1146                 return 1;
1147             for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
1148                 if (!check_land_ok(&(((struct llist *)qp2)->land)))
1149                     return 1;
1150             }
1151             if (stop) {
1152                 lnd_mess("stops", llp);
1153                 continue;
1154             }
1155         }
1156         if (llp->mobil <= 0.0) {
1157             lnd_mess("is out of mobility", llp);
1158             continue;
1159         }
1160         llp->land.lnd_x = newx;
1161         llp->land.lnd_y = newy;
1162         if (lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) {
1163             llp->mobil -= lnd_mobcost(&llp->land, &sect, MOB_RAIL);
1164         } else {
1165             llp->mobil -= lnd_mobcost(&llp->land, &sect, MOB_ROAD);
1166         }
1167         llp->land.lnd_mobil = (int)llp->mobil;
1168         llp->land.lnd_harden = 0;
1169         putland(llp->land.lnd_uid, &llp->land);
1170         putsect(&osect);
1171         getsect(osect.sct_x, osect.sct_y, &osect);
1172         if (osect.sct_own != oldown && oldown == player->cnum) {
1173             /* It was your sector, now it's not.  Simple :) */
1174             pr("You no longer own %s\n",
1175                xyas(osect.sct_x, osect.sct_y, player->cnum));
1176         }
1177         if (rel != ALLIED && sect.sct_own != actor && sect.sct_own) {   /* must be a spy */
1178             /* Always a 10% chance of getting caught. */
1179             if (chance(LND_SPY_DETECT_CHANCE(llp->land.lnd_effic))) {
1180                 if (rel == NEUTRAL || rel == FRIENDLY) {
1181                     wu(0, sect.sct_own,
1182                        "%s unit spotted in %s\n", cname(player->cnum),
1183                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1184                     setrel(sect.sct_own, llp->land.lnd_own, HOSTILE);
1185                 } else if (rel == HOSTILE || rel == AT_WAR ||
1186                            rel == SITZKRIEG || rel == MOBILIZATION) {
1187                     wu(0, sect.sct_own,
1188                        "%s spy shot in %s\n", cname(player->cnum),
1189                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1190                     pr("%s was shot and killed.\n", prland(&llp->land));
1191                     llp->land.lnd_effic = 0;
1192                     putland(llp->land.lnd_uid, &llp->land);
1193                     lnd_delete(llp, 0);
1194                 }
1195             }
1196         }
1197     }
1198     if (QEMPTY(list))
1199         return stopping;
1200     lnd_sweep(list, 0, 1, actor);
1201     stopping |= lnd_check_mines(list);
1202     if (QEMPTY(list))
1203         return stopping;
1204     if (visible)
1205         stopping |= lnd_interdict(list, newx, newy, actor);
1206     return stopping;
1207 }
1208
1209 /*
1210  * find all artillery units belonging
1211  * to the attacker or defender that can fire.
1212  * Each arty unit adds +1%/damage point
1213  *
1214  */
1215 int
1216 lnd_support(natid victim, natid attacker, coord x, coord y)
1217 {
1218     struct nstr_item ni;
1219     struct lndstr land;
1220     int rel, rel2;
1221     double dam = 0.0;
1222     int dist;
1223     int shell;
1224     int gun;
1225     double range, range2;
1226
1227     snxtitem_all(&ni, EF_LAND);
1228     while (nxtitem(&ni, (s_char *)&land)) {
1229         if (land.lnd_frg == 0)
1230             continue;
1231         if ((land.lnd_x == x) && (land.lnd_y == y))
1232             continue;
1233         if (land.lnd_ship >= 0)
1234             continue;
1235         if (land.lnd_land >= 0)
1236             continue;
1237         if (land.lnd_mission > 0)
1238             continue;
1239         if (land.lnd_effic < LAND_MINFIREEFF)
1240             continue;
1241         /* Do we have mil? */
1242         if (land.lnd_item[I_MILIT] <= 0)
1243             continue;
1244         rel = getrel(getnatp(land.lnd_own), attacker);
1245         rel2 = getrel(getnatp(land.lnd_own), victim);
1246         if ((land.lnd_own != attacker) &&
1247             ((rel != ALLIED) || (rel2 != AT_WAR)))
1248             continue;
1249
1250         /* do we have supplies? */
1251         if (!has_supply(&land))
1252             continue;
1253
1254         /* are we in range? */
1255         dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1256
1257         range = techfact((int)land.lnd_tech, (double)land.lnd_frg / 2.0);
1258
1259         range2 = roundrange(range);
1260         if (dist > range2)
1261             continue;
1262
1263         shell = land.lnd_item[I_SHELL];
1264         gun = land.lnd_item[I_GUN];
1265
1266         if (shell == 0 || gun == 0)
1267             continue;
1268
1269         use_supply(&land);
1270         nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1271         if (roll(100) < land.lnd_acc) {
1272             dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1273                                land.lnd_ammo, shell) / 2;
1274         } else {
1275             dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1276                                land.lnd_ammo, shell);
1277         }
1278         if (land.lnd_own != attacker)
1279             wu(0, land.lnd_own,
1280                "%s supported %s at %s\n",
1281                prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1282     }
1283     return (int)dam;
1284 }
1285
1286 s_char *
1287 lnd_path(int together, struct lndstr *lp, s_char *buf)
1288 {
1289     coord destx;
1290     coord desty;
1291     struct sctstr d_sect, sect;
1292     s_char *cp;
1293     double dummy;
1294
1295     if (!sarg_xy(buf, &destx, &desty))
1296         return 0;
1297     if (!together) {
1298         pr("Cannot go to a destination sector if not all starting in the same sector\n");
1299         return 0;
1300     }
1301     if (!getsect(destx, desty, &d_sect)) {
1302         pr("%d,%d is not a sector\n", destx, desty);
1303         return 0;
1304     }
1305     getsect(lp->lnd_x, lp->lnd_y, &sect);
1306     if (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
1307         cp = (s_char *)BestLandPath(buf, &sect, &d_sect, &dummy, MOB_RAIL);
1308     else
1309         cp = (s_char *)BestLandPath(buf, &sect, &d_sect, &dummy, MOB_ROAD);
1310     if (!cp) {
1311         pr("No owned path from %s to %s!\n",
1312            xyas(lp->lnd_x, lp->lnd_y, player->cnum),
1313            xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
1314         return 0;
1315     }
1316     pr("Using path '%s'\n", cp);
1317     return cp;
1318 }
1319
1320 int
1321 lnd_can_attack(struct lndstr *lp)
1322 {
1323     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1324
1325 /*      if (lcp->l_flags & L_SUPPLY ||
1326             lcp->l_flags & L_SECURITY ||
1327             lcp->l_flags & L_FLAK ||
1328             lcp->l_frg)
1329                 return 0; */
1330     if (lcp->l_flags & L_SUPPLY)
1331         return 0;
1332
1333     return 1;
1334 }
1335
1336 /*
1337  * Increase fortification value of LP.
1338  * Fortification costs mobility.  Use up to HARD_AMT mobility.
1339  * Return actual fortification increase.
1340  */
1341 int
1342 lnd_fortify (struct lndstr *lp, int hard_amt)
1343 {
1344     int mob_used;
1345     int eng;
1346
1347     if ((lp->lnd_ship >= 0) || lp->lnd_land >= 0)
1348         return 0;
1349
1350     hard_amt = min(lp->lnd_mobil, hard_amt);
1351
1352     if ((lp->lnd_harden + hard_amt) > land_mob_max)
1353         hard_amt = land_mob_max - lp->lnd_harden;
1354
1355     eng = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own);
1356
1357     if (eng)
1358         hard_amt = ((float)hard_amt * 1.5);
1359
1360     if ((lp->lnd_harden + hard_amt) > land_mob_max)
1361         hard_amt = land_mob_max - lp->lnd_harden;
1362
1363     /* Ok, set the mobility used */
1364     mob_used = hard_amt;
1365
1366     /* Now, if an engineer helped, it's really only 2/3rds of
1367        that */
1368     if (eng)
1369         mob_used = (int)((float)mob_used / 1.5);
1370
1371     /* If we increased it, but not much, we gotta take at least 1
1372        mob point. */
1373     if (mob_used <= 0 && hard_amt > 0)
1374         mob_used = 1;
1375
1376     lp->lnd_mobil -= mob_used;
1377     if (lp->lnd_mobil < 0)
1378         lp->lnd_mobil = 0;
1379
1380     lp->lnd_harden += hard_amt;
1381     lp->lnd_harden = min(lp->lnd_harden, land_mob_max);
1382
1383     return hard_amt;
1384 }
1385
1386 /*
1387  * Set LP's tech to TLEV along with everything else that depends on it.
1388  */
1389 void
1390 lnd_set_tech(struct lndstr *lp, int tlev)
1391 {
1392     struct lchrstr *lcp = lchr + lp->lnd_type;
1393     int tech_diff = tlev - lcp->l_tech;
1394
1395     if (CANT_HAPPEN(tech_diff < 0)) {
1396       tlev -= tech_diff;
1397       tech_diff = 0;
1398     }
1399
1400     lp->lnd_tech = tlev;
1401     lp->lnd_att = (float)LND_ATTDEF(lcp->l_att, tech_diff);
1402     lp->lnd_def = (float)LND_ATTDEF(lcp->l_def, tech_diff);
1403     lp->lnd_vul = (int)LND_VUL(lcp->l_vul, tech_diff);
1404     lp->lnd_spd = (int)LND_SPD(lcp->l_spd, tech_diff);
1405     lp->lnd_vis = (int)LND_VIS(lcp->l_vis, tech_diff);
1406     lp->lnd_spy = (int)LND_SPY(lcp->l_spy, tech_diff);
1407     lp->lnd_rad = (int)LND_RAD(lcp->l_rad, tech_diff);
1408     lp->lnd_frg = (int)LND_FRG(lcp->l_frg, tech_diff);
1409     lp->lnd_acc = (int)LND_ACC(lcp->l_acc, tech_diff);
1410     lp->lnd_dam = (int)LND_DAM(lcp->l_dam, tech_diff);
1411     lp->lnd_ammo = (int)LND_AMM(lcp->l_ammo, lcp->l_dam, tech_diff);
1412     lp->lnd_aaf = (int)LND_AAF(lcp->l_aaf, tech_diff);
1413     lp->lnd_fuelc = (int)LND_FC(lcp->l_fuelc, tech_diff);
1414     lp->lnd_fuelu = (int)LND_FU(lcp->l_fuelu, tech_diff);
1415     lp->lnd_maxlight = (int)LND_XPL(lcp->l_nxlight, tech_diff);
1416     lp->lnd_maxland = (int)LND_MXL(lcp->l_mxland, tech_diff);
1417 }