2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * lndsub.c: Land unit subroutines
30 * Known contributors to this file:
32 * Steve McClure, 1998-2000
54 #include "prototypes.h"
56 static void lnd_mess(char *, struct llist *);
57 static int lnd_hit_mine(struct lndstr *, struct lchrstr *);
60 attack_val(int combat_mode, struct lndstr *lp)
66 if (lp->lnd_effic < LAND_MINEFF) {
67 putland(lp->lnd_uid, lp);
71 lcp = &lchr[(int)lp->lnd_type];
73 /* Spies always count as 1 during assaults. If they are the only ones
74 in the assault, they get to sneak on anyway. */
76 if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
79 men = lp->lnd_item[I_MILIT];
80 value = men * lp->lnd_att * lp->lnd_effic / 100.0;
82 switch (combat_mode) {
86 if (!(lcp->l_flags & L_MARINE))
87 return (int)(assault_penalty * value);
90 if (!(lcp->l_flags & L_MARINE))
91 return (int)(assault_penalty * men);
98 defense_val(struct lndstr *lp)
104 if (lp->lnd_effic < LAND_MINEFF) {
105 putland(lp->lnd_uid, lp);
109 lcp = &lchr[(int)lp->lnd_type];
111 men = lp->lnd_item[I_MILIT];
113 if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
114 !(lcp->l_flags & L_MARINE))
117 value = men * lp->lnd_def * lp->lnd_effic / 100.0;
118 value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
120 /* If there are military on the unit, you get at least a 1
121 man defensive unit, except for spies */
122 if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
129 lnd_print(struct llist *llp, char *s)
131 if (llp->land.lnd_own == player->cnum)
132 pr("%s %s\n", prland(&llp->land), s);
134 wu(0, llp->land.lnd_own, "%s %s\n", prland(&llp->land), s);
138 lnd_delete(struct llist *llp, char *s)
142 putland(llp->land.lnd_uid, &llp->land);
143 emp_remque((struct emp_qelem *)llp);
148 lnd_take_casualty(int combat_mode, struct llist *llp, int cas)
149 /* attacking or assaulting or paratrooping? */
150 /* number of casualties to take */
163 int nowhere_to_go = 0;
165 double mobcost, bmcost;
169 taken = llp->land.lnd_item[I_MILIT];
170 /* Spies always die */
171 if (llp->lcp->l_flags & L_SPY) {
173 llp->land.lnd_effic = 0;
175 eff_eq = ldround(cas * 100.0 / llp->lcp->l_mil, 1);
176 llp->land.lnd_effic -= eff_eq;
177 lnd_submil(&llp->land, cas);
180 if (llp->land.lnd_effic < LAND_MINEFF) {
181 sprintf(buf, "dies %s %s!",
182 combat_mode ? att_mode[combat_mode] : "defending",
183 xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
184 lnd_delete(llp, buf);
185 /* Since we killed the unit, we killed all the mil on it */
188 /* Ok, now, how many did we take off? (sould be the diff) */
189 taken = taken - llp->land.lnd_item[I_MILIT];
192 if (llp->land.lnd_effic >= llp->land.lnd_retreat)
195 /* we're being boarded */
196 if (llp->land.lnd_ship >= 0 && combat_mode == A_DEFEND)
199 /* we're being boarded */
200 if (llp->land.lnd_land >= 0 && combat_mode == A_DEFEND)
203 /* Have to make a retreat check */
205 ret_chance = llp->land.lnd_retreat - llp->land.lnd_effic;
206 if (roll(100) < ret_chance) {
208 lnd_print(llp, "fails morale check!");
209 llp->land.lnd_mission = 0;
210 llp->land.lnd_harden = 0;
211 if (llp->land.lnd_ship >= 0 || llp->land.lnd_land >= 0)
213 else if (combat_mode == A_DEFEND) {
215 * defending unit.. find a place to send it
216 * strategy: look for the most-populated
217 * adjacent sector that is owned by the unit
218 * player->owner. Charge mob..
222 for (n = 1; n <= 6; ++n) {
223 ret_x = llp->land.lnd_x + diroff[n][0];
224 ret_y = llp->land.lnd_y + diroff[n][1];
225 getsect(ret_x, ret_y, §);
226 if (sect.sct_own != llp->land.lnd_own)
228 if (sect.sct_type == SCT_MOUNT)
230 mobcost = lnd_mobcost(&llp->land, &rsect);
234 civs = sect.sct_item[I_CIVIL];
235 if (civs > biggest) {
245 /* retreat to bx,by */
246 llp->land.lnd_x = bx;
247 llp->land.lnd_y = by;
248 getsect(bx, by, &rsect);
249 mob = llp->land.lnd_mobil - (int)bmcost;
252 orig = llp->land.lnd_mobil;
253 llp->land.lnd_mobil = (signed char)mob;
254 if (llp->land.lnd_mobil > orig)
255 llp->land.lnd_mobil = -127;
256 sprintf(buf, "retreats at %d%% efficiency to %s!",
258 xyas(bx, by, llp->land.lnd_own));
259 lnd_delete(llp, buf);
261 } else { /* attacking from a sector */
262 sprintf(buf, "leaves the battlefield at %d%% efficiency",
263 llp->land.lnd_effic);
264 if ((llp->land.lnd_mobil - (int)llp->mobil) < -127)
265 llp->land.lnd_mobil = -127;
267 llp->land.lnd_mobil -= (int)llp->mobil;
269 lnd_delete(llp, buf);
273 /* nowhere to go.. take more casualties */
274 llp->land.lnd_effic -= 10;
275 lnd_submil(&llp->land, llp->lcp->l_mil / 10);
276 if (llp->land.lnd_effic < LAND_MINEFF)
277 lnd_delete(llp, "has nowhere to retreat, and dies!");
280 "has nowhere to retreat and takes extra losses!");
287 lnd_takemob(struct emp_qelem *list, double loss)
289 struct emp_qelem *qp, *next;
292 int mcost = ldround(combat_mob * loss, 1);
294 for (qp = list->q_forw; qp != list; qp = next) {
296 llp = (struct llist *)qp;
299 use_supply(&llp->land);
300 if (llp->land.lnd_mission == MI_RESERVE)
301 new = llp->land.lnd_mobil - mcost/2;
304 new = llp->land.lnd_mobil - mcost;
307 llp->land.lnd_mobil = (signed char)new;
312 lnd_submil(struct lndstr *lp, int num)
314 int new = lp->lnd_item[I_MILIT] - num;
315 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
319 lnd_spyval(struct lndstr *lp)
321 if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
322 return lp->lnd_spy * (lp->lnd_effic / 100.0) + 2;
324 return lp->lnd_spy * (lp->lnd_effic / 100.0);
328 intelligence_report(int destination, struct lndstr *lp, int spy,
332 char buf1[80], buf2[80], buf3[80];
333 double estimate = 0.0; /* estimated defense value */
335 if (destination == 0)
338 if (lp->lnd_own == 0)
341 lcp = &lchr[(int)lp->lnd_type];
343 memset(buf1, 0, sizeof(buf1));
344 memset(buf2, 0, sizeof(buf2));
345 memset(buf3, 0, sizeof(buf3));
346 if (chance((spy + lp->lnd_vis) / 10.0)) {
347 if (destination == player->cnum)
348 pr("%s %s", mess, prland(lp));
350 sprintf(buf1, "%s %s", mess, prland(lp));
352 estimate = lp->lnd_item[I_MILIT];
354 if (chance((spy + lp->lnd_vis) / 20.0)) {
355 if (destination == player->cnum)
356 pr(" (eff %d, mil %d",
357 roundintby(lp->lnd_effic, 5),
358 roundintby(lp->lnd_item[I_MILIT], 10));
360 sprintf(buf2, " (eff %d, mil %d",
361 roundintby(lp->lnd_effic, 5),
362 roundintby(lp->lnd_item[I_MILIT], 10));
363 estimate = lp->lnd_item[I_MILIT] * lp->lnd_effic / 100.0;
365 if (chance((spy + lp->lnd_vis) / 20.0)) {
367 t = lp->lnd_tech - 20 + roll(40);
369 if (destination == player->cnum)
370 pr(", tech %d)\n", t);
372 sprintf(buf3, ", tech %d)\n", t);
374 if (destination == player->cnum)
377 sprintf(buf3, ")\n");
380 if (destination == player->cnum)
387 if (destination != player->cnum) {
388 wu(0, destination, "%s%s%s", buf1, buf2, buf3);
391 if (lp->lnd_ship < 0 || lcp->l_flags & L_MARINE)
392 estimate *= lp->lnd_def;
394 return (int)estimate;
397 /* Used by the spy command to count land units in a sector. If used
398 for anything else, you may want to reconsider, because this doesn't
399 always count spies. :) */
401 count_sect_units(struct sctstr *sp)
407 snxtitem_all(&ni, EF_LAND);
408 while (nxtitem(&ni, &land)) {
411 if (land.lnd_x != sp->sct_x || land.lnd_y != sp->sct_y)
413 /* Don't always see spies */
414 if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
415 if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
418 /* Got here, report it */
426 count_units(struct shpstr *sp)
432 if (sp->shp_effic < SHIP_MINEFF)
435 snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
436 while (nxtitem(&ni, &land)) {
437 if (land.lnd_own == 0)
439 if (land.lnd_ship == sp->shp_uid)
443 if (sp->shp_nland != nland) {
444 sp->shp_nland = nland;
445 putship(sp->shp_uid, sp);
450 lnd_count_units(struct lndstr *lp)
456 if (lp->lnd_effic < LAND_MINEFF)
459 snxtitem_xy(&ni, EF_LAND, lp->lnd_x, lp->lnd_y);
460 while (nxtitem(&ni, &land)) {
461 if (land.lnd_own == 0)
463 if (land.lnd_land == lp->lnd_uid)
467 if (lp->lnd_nland != nland) {
468 lp->lnd_nland = nland;
469 putland(lp->lnd_uid, lp);
474 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
486 while (nxtitem(ni, &land)) {
490 if (ontradingblock(EF_LAND, &land)) {
491 pr("unit #%d inelligible - it's for sale.\n",
496 lcp = &lchr[(int)land.lnd_type];
497 /* if (wantflags && (lcp->m_flags & wantflags) != wantflags)
499 if (nowantflags && lcp->m_flags & nowantflags)
502 /* This abuse is better fixed by building a unit with the normal negative
503 mobility that everything else is built with */
504 /* Just so that the player can't build a bunch of land units, and them
505 march them a few minutes later... */
507 if (opt_MOB_ACCESS) {
508 if (land.lnd_effic < 11 &&
509 land.lnd_mobil < etu_per_update) {
510 pr("Land unit #%d needs at least %d mob to march.\n",
511 land.lnd_uid, etu_per_update);
516 land.lnd_mission = 0;
519 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
520 putland(land.lnd_uid, &land);
521 llp = malloc(sizeof(struct llist));
524 llp->mobil = land.lnd_mobil;
525 emp_insque(&llp->queue, list);
529 /* This function assumes that the list was created by lnd_sel */
531 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
532 int *togetherp, natid actor)
534 struct emp_qelem *qp;
535 struct emp_qelem *next;
548 for (qp = list->q_back; qp != list; qp = next) {
550 llp = (struct llist *)qp;
551 getland(llp->land.lnd_uid, &land);
552 if (land.lnd_own != actor) {
553 mpr(actor, "%s was disbanded at %s\n",
554 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
555 emp_remque((struct emp_qelem *)llp);
559 if (land.lnd_ship >= 0) {
560 lnd_mess("is on a ship", llp);
563 if (land.lnd_land >= 0) {
564 lnd_mess("is on a unit", llp);
567 if (!getsect(land.lnd_x, land.lnd_y, §)) {
568 lnd_mess("was sucked into the sky by a strange looking spaceland", llp); /* heh -KHS */
571 if (!(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
572 !(lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) &&
573 llp->land.lnd_item[I_MILIT] == 0) {
574 lnd_mess("has no mil on it to guide it", llp);
577 rel = getrel(getnatp(sect.sct_own), player->cnum);
578 if (sect.sct_own != land.lnd_own && rel != ALLIED &&
579 !(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
581 sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
590 if (land.lnd_x != allx || land.lnd_y != ally)
592 if (land.lnd_mobil + 1 < (int)llp->mobil) {
593 llp->mobil = land.lnd_mobil;
595 if (llp->mobil < *minmobp)
596 *minmobp = llp->mobil;
597 if (llp->mobil > *maxmobp)
598 *maxmobp = llp->mobil;
604 lnd_put(struct emp_qelem *list, natid actor)
606 struct emp_qelem *qp;
607 struct emp_qelem *newqp;
612 llp = (struct llist *)qp;
614 mpr(actor, "%s stopped at %s\n", prland(&llp->land),
615 xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
616 if (llp->mobil < -127)
618 llp->land.lnd_mobil = llp->mobil;
620 putland(llp->land.lnd_uid, &llp->land);
629 lnd_sweep(struct emp_qelem *land_list, int verbose, int takemob,
632 struct emp_qelem *qp;
633 struct emp_qelem *next;
636 int mines, m, max, sshells, lshells;
638 for (qp = land_list->q_back; qp != land_list; qp = next) {
640 llp = (struct llist *)qp;
641 if (!(llp->lcp->l_flags & L_ENGINEER)) {
643 mpr(actor, "%s is not an engineer!\n", prland(&llp->land));
646 if (takemob && llp->mobil < 0.0) {
648 lnd_mess("is out of mobility", llp);
651 getsect(llp->land.lnd_x, llp->land.lnd_y, §);
652 if (sect.sct_oldown == llp->land.lnd_own) {
655 "%s is in a sector completely owned by you. Don't bother digging up mines there!\n",
659 if (sect.sct_type == SCT_BSPAN) {
661 mpr(actor, "%s is on a bridge. No mines there!\n",
666 llp->mobil -= lnd_pathcost(&llp->land, 0.2);
667 llp->land.lnd_mobil = (int)llp->mobil;
668 llp->land.lnd_harden = 0;
670 putland(llp->land.lnd_uid, &llp->land);
671 if (!(mines = sect.sct_mines))
673 max = llp->lcp->l_item[I_SHELL];
674 lshells = llp->land.lnd_item[I_SHELL];
675 sshells = sect.sct_item[I_SHELL];
676 for (m = 0; mines > 0 && m < max * 2; m++) {
677 if (chance(0.5 * llp->lcp->l_att)) {
678 mpr(actor, "Sweep...\n");
682 else if (sshells < ITEM_MAX)
686 sect.sct_mines = mines;
687 llp->land.lnd_item[I_SHELL] = lshells;
688 sect.sct_item[I_SHELL] = sshells;
689 putland(llp->land.lnd_uid, &llp->land);
695 contains_engineer(struct emp_qelem *list)
697 struct emp_qelem *qp;
698 struct emp_qelem *next;
701 for (qp = list->q_back; qp != list; qp = next) {
703 llp = (struct llist *)qp;
704 if (llp->lcp->l_flags & L_ENGINEER)
711 lnd_check_mines(struct emp_qelem *land_list)
713 struct emp_qelem *qp;
714 struct emp_qelem *next;
718 int with_eng = contains_engineer(land_list);
720 for (qp = land_list->q_back; qp != land_list; qp = next) {
722 llp = (struct llist *)qp;
723 getsect(llp->land.lnd_x, llp->land.lnd_y, §);
724 if (sect.sct_oldown == llp->land.lnd_own)
726 if (sect.sct_type == SCT_BSPAN)
730 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
731 lnd_hit_mine(&llp->land, llp->lcp);
734 putland(llp->land.lnd_uid, &llp->land);
735 if (!llp->land.lnd_own) {
746 lnd_list(struct emp_qelem *land_list)
748 struct emp_qelem *qp;
749 struct emp_qelem *next;
753 pr("lnd# land type x,y a eff sh gun xl mu tech retr fuel\n");
755 for (qp = land_list->q_back; qp != land_list; qp = next) {
757 llp = (struct llist *)qp;
759 pr("%4d ", lnd->lnd_uid);
760 pr("%-16.16s ", llp->lcp->l_name);
761 prxy("%4d,%-4d ", lnd->lnd_x, lnd->lnd_y, llp->land.lnd_own);
762 pr("%1.1s", &lnd->lnd_army);
763 pr("%4d%%", lnd->lnd_effic);
764 pr("%4d", lnd->lnd_item[I_SHELL]);
765 pr("%4d", lnd->lnd_item[I_GUN]);
766 count_land_planes(lnd);
767 pr("%3d", lnd->lnd_nxlight);
768 pr("%4d", lnd->lnd_mobil);
769 pr("%4d", lnd->lnd_tech);
770 pr("%4d%%", lnd->lnd_retreat);
771 pr("%5d\n", lnd->lnd_fuel);
776 lnd_mess(char *str, struct llist *llp)
778 mpr(llp->land.lnd_own, "%s %s & stays in %s\n",
780 str, xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
781 if (llp->mobil < -127)
783 llp->land.lnd_mobil = llp->mobil;
784 putland(llp->land.lnd_uid, &llp->land);
785 emp_remque((struct emp_qelem *)llp);
790 lnd_count(struct emp_qelem *list)
792 struct emp_qelem *qp;
793 struct emp_qelem *next;
797 for (qp = list->q_back; qp != list; qp = next) {
799 llp = (struct llist *)qp;
806 lnd_damage(struct emp_qelem *list, int totdam)
808 struct emp_qelem *qp;
809 struct emp_qelem *next;
814 if (!totdam || !(count = lnd_count(list)))
816 dam = ldround((double)totdam / count, 1);
817 for (qp = list->q_back; qp != list; qp = next) {
819 llp = (struct llist *)qp;
820 /* have to get it again because of collateral damage */
821 getland(llp->land.lnd_uid, &llp->land);
822 landdamage(&llp->land, dam);
823 putland(llp->land.lnd_uid, &llp->land);
824 if (!llp->land.lnd_own) {
833 lnd_easiest_target(struct emp_qelem *list)
835 struct emp_qelem *qp;
836 struct emp_qelem *next;
839 int easiest = 9876; /* things start great for victim */
842 for (qp = list->q_back; qp != list; qp = next) {
844 llp = (struct llist *)qp;
845 hard = lnd_hardtarget(&llp->land);
847 easiest = hard; /* things get worse for victim */
850 return easiest - count;
854 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
858 struct emp_qelem msl_list, *qp, *newqp;
860 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
862 dam = msl_launch_mindam(&msl_list, newx, newy,
863 lnd_easiest_target(list), EF_LAND,
864 lnd_count(list) * 20, "troops", victim,
867 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
868 collateral_damage(newx, newy, dam, 0);
870 qp = msl_list.q_forw;
871 while (qp != msl_list.q_forw) {
881 /* Steve M. - commented out for now until abuse is decided upon */
882 /* risner: allow forts to interdict land units. */
884 lnd_fort_interdiction(struct emp_qelem *list,
885 coord newx, coord newy, natid victim)
896 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
897 while (nxtsct(&ns, &fsect)) {
898 if (fsect.sct_own == 0)
900 if (fsect.sct_own == victim)
902 if (getrel(getnatp(fsect.sct_own), victim) >= NEUTRAL)
904 gun = fsect.sct_item[I_GUN];
907 range = roundrange(fortrange(&fsect));
908 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
911 if (fsect.sct_item[I_MILIT] < 5)
913 shell = fsect.sct_item[I_SHELL];
915 shell += supply_commod(fsect.sct_own, fsect.sct_x, fsect.sct_y,
920 fsect.sct_item[I_SHELL] = shell;
924 guneff = landgun((int)fsect.sct_effic, gun);
927 mpr(victim, "Incoming fire does %d damage!\n", dam);
929 "%s fires at %s land units in %s for %d!\n",
930 xyas(fsect.sct_x, fsect.sct_y,
932 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
933 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
936 return lnd_damage(list, totdam);
942 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
947 if (!opt_NO_FORT_FIRE)
948 /* Steve M. - commented out for now until abuse is decided upon */
949 stopping |= lnd_fort_interdiction(list, newx, newy, victim);
954 unit_interdict(newx, newy, victim, "land units",
955 lnd_easiest_target(list), MI_INTERDICT));
959 lnd_missile_interdiction(list, newx, newy, victim));
963 /* high value of hardtarget is harder to hit */
965 lnd_hardtarget(struct lndstr *lp)
969 getsect(lp->lnd_x, lp->lnd_y, §);
970 return (int)((lp->lnd_effic / 100.0) *
971 (10 + dchr[sect.sct_type].d_dstr * 2 + lp->lnd_spd / 2.0
976 lnd_hit_mine(struct lndstr *lp, struct lchrstr *lcp)
980 mpr(lp->lnd_own, "Blammo! Landmines detected in %s! ",
981 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
983 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
986 if (lcp->l_flags & L_ENGINEER)
989 landdamage(lp, ldround(m, 1));
995 lnd_pathcost(struct lndstr *lp, double pathcost)
999 effspd = lp->lnd_spd;
1000 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
1001 effspd *= lp->lnd_effic * 0.01;
1004 * The return value must be PATHCOST times a factor that depends
1005 * only on the land unit. Anything else breaks path finding. In
1006 * particular, you can't add or enforce a minimum cost here. Do
1007 * it in sector_mcost().
1009 return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
1013 lnd_mobtype(struct lndstr *lp)
1015 return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
1016 ? MOB_RAIL : MOB_MARCH;
1020 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
1022 return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
1026 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
1029 struct sctstr sect, osect;
1030 struct emp_qelem *qp;
1031 struct emp_qelem *qp2;
1032 struct emp_qelem *next;
1034 struct emp_qelem cur, done;
1046 if (dir <= DIR_STOP || dir >= DIR_VIEW) {
1047 lnd_put(list, actor);
1050 dx = diroff[dir][0];
1051 dy = diroff[dir][1];
1052 for (qp = list->q_back; qp != list; qp = next) {
1054 llp = (struct llist *)qp;
1055 getsect(llp->land.lnd_x, llp->land.lnd_y, &osect);
1056 oldown = osect.sct_own;
1057 newx = xnorm(llp->land.lnd_x + dx);
1058 newy = ynorm(llp->land.lnd_y + dy);
1059 getsect(newx, newy, §);
1060 rel = getrel(getnatp(sect.sct_own), player->cnum);
1061 if ((sect.sct_own != actor && rel != ALLIED &&
1062 !(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
1063 sect.sct_own) || (sect.sct_type == SCT_WATER ||
1064 sect.sct_type == SCT_SANCT ||
1065 sect.sct_type == SCT_WASTE)) {
1067 pr("can't go to %s\n", xyas(newx, newy, actor));
1070 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
1075 if (sect.sct_rail == 0 &&
1076 lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) {
1078 pr("no rail system in %s\n", xyas(newx, newy, actor));
1081 sprintf(dp, "has no rail system in %s",
1082 xyas(newx, newy, actor));
1087 /* Note we check would_abandon first because we don't want
1088 to always have to do these checks */
1089 if (would_abandon(&osect, I_CIVIL, 0, &llp->land)) {
1091 if (!want_to_abandon(&osect, I_CIVIL, 0, &llp->land)) {
1094 /* now check stuff */
1095 if (!check_sect_ok(§))
1097 if (!check_sect_ok(&osect))
1099 for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
1100 if (!check_land_ok(&((struct llist *)qp2)->land))
1104 lnd_mess("stops", llp);
1108 if (llp->mobil <= 0.0) {
1109 lnd_mess("is out of mobility", llp);
1112 llp->land.lnd_x = newx;
1113 llp->land.lnd_y = newy;
1114 llp->mobil -= lnd_mobcost(&llp->land, §);
1115 llp->land.lnd_mobil = (int)llp->mobil;
1116 llp->land.lnd_harden = 0;
1117 putland(llp->land.lnd_uid, &llp->land);
1119 getsect(osect.sct_x, osect.sct_y, &osect);
1120 if (osect.sct_own != oldown && oldown == player->cnum) {
1121 /* It was your sector, now it's not. Simple :) */
1122 pr("You no longer own %s\n",
1123 xyas(osect.sct_x, osect.sct_y, player->cnum));
1125 if (rel != ALLIED && sect.sct_own != actor && sect.sct_own) { /* must be a spy */
1126 /* Always a 10% chance of getting caught. */
1127 if (chance(LND_SPY_DETECT_CHANCE(llp->land.lnd_effic))) {
1128 if (rel == NEUTRAL || rel == FRIENDLY) {
1130 "%s unit spotted in %s\n", cname(player->cnum),
1131 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1132 setrel(sect.sct_own, llp->land.lnd_own, HOSTILE);
1133 } else if (rel == HOSTILE || rel == AT_WAR ||
1134 rel == SITZKRIEG || rel == MOBILIZATION) {
1136 "%s spy shot in %s\n", cname(player->cnum),
1137 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1138 pr("%s was shot and killed.\n", prland(&llp->land));
1139 llp->land.lnd_effic = 0;
1140 putland(llp->land.lnd_uid, &llp->land);
1148 lnd_sweep(list, 0, 1, actor);
1149 stopping |= lnd_check_mines(list);
1153 /* interdict land units sector by sector */
1156 while (!QEMPTY(list)) {
1157 llp = (struct llist *)list->q_back;
1158 newx = llp->land.lnd_x;
1159 newy = llp->land.lnd_y;
1160 /* move units in NEWX,NEWY to cur */
1162 for (qp = list->q_back; qp != list; qp = next) {
1164 llp = (struct llist *)qp;
1165 if (llp->land.lnd_x == newx && llp->land.lnd_y == newy) {
1167 emp_insque(qp, &cur);
1168 if (!(lchr[(int)llp->land.lnd_type].l_flags & L_SPY))
1172 /* interdict them */
1174 stopping |= lnd_interdict(&cur, newx, newy, actor);
1175 /* move survivors to done */
1176 for (qp = cur.q_back; qp != &cur; qp = next) {
1178 llp = (struct llist *)qp;
1180 emp_insque(qp, &done);
1183 /* assign surviving land units back to list */
1184 emp_insque(list, &done);
1191 * find all artillery units belonging
1192 * to the attacker or defender that can fire.
1193 * Each arty unit adds +1%/damage point
1196 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1198 struct nstr_item ni;
1207 snxtitem_all(&ni, EF_LAND);
1208 while (nxtitem(&ni, &land)) {
1209 if (land.lnd_frg == 0)
1211 if ((land.lnd_x == x) && (land.lnd_y == y))
1213 if (land.lnd_ship >= 0)
1215 if (land.lnd_land >= 0)
1217 if (land.lnd_effic < LAND_MINFIREEFF)
1219 /* Do we have mil? */
1220 if (land.lnd_item[I_MILIT] <= 0)
1222 rel = getrel(getnatp(land.lnd_own), attacker);
1223 rel2 = getrel(getnatp(land.lnd_own), victim);
1224 if ((land.lnd_own != attacker) &&
1225 ((rel != ALLIED) || (rel2 != AT_WAR)))
1228 /* do we have supplies? */
1229 if (!has_supply(&land))
1232 /* are we in range? */
1233 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1235 range = roundrange(effrange(land.lnd_frg, land.lnd_tech));
1239 shell = land.lnd_item[I_SHELL];
1240 gun = land.lnd_item[I_GUN];
1242 if (shell == 0 || gun == 0)
1247 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1249 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1250 if (roll(100) < land.lnd_acc) {
1251 dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1252 land.lnd_ammo, shell) / 2;
1254 dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1255 land.lnd_ammo, shell);
1257 if (land.lnd_own != attacker)
1259 "%s supported %s at %s\n",
1260 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1266 lnd_path(int together, struct lndstr *lp, char *buf)
1270 struct sctstr d_sect, sect;
1275 if (!sarg_xy(buf, &destx, &desty))
1278 pr("Cannot go to a destination sector if not all starting in the same sector\n");
1281 if (!getsect(destx, desty, &d_sect)) {
1282 pr("%d,%d is not a sector\n", destx, desty);
1285 getsect(lp->lnd_x, lp->lnd_y, §);
1286 mtype = lnd_mobtype(lp);
1287 cp = BestLandPath(buf, §, &d_sect, &dummy, mtype);
1289 pr("No owned %s from %s to %s!\n",
1290 mtype == MOB_RAIL ? "railway" : "path",
1291 xyas(lp->lnd_x, lp->lnd_y, player->cnum),
1292 xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
1295 pr("Using path '%s'\n", cp);
1300 lnd_can_attack(struct lndstr *lp)
1302 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1304 if (lcp->l_flags & L_SUPPLY)
1311 * Increase fortification value of LP.
1312 * Fortification costs mobility. Use up to HARD_AMT mobility.
1313 * Return actual fortification increase.
1316 lnd_fortify (struct lndstr *lp, int hard_amt)
1321 if ((lp->lnd_ship >= 0) || lp->lnd_land >= 0)
1324 hard_amt = MIN(lp->lnd_mobil, hard_amt);
1326 if ((lp->lnd_harden + hard_amt) > land_mob_max)
1327 hard_amt = land_mob_max - lp->lnd_harden;
1329 eng = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own);
1334 if ((lp->lnd_harden + hard_amt) > land_mob_max)
1335 hard_amt = land_mob_max - lp->lnd_harden;
1337 /* Ok, set the mobility used */
1338 mob_used = hard_amt;
1340 /* Now, if an engineer helped, it's really only 2/3rds of
1345 /* If we increased it, but not much, we gotta take at least 1
1347 if (mob_used <= 0 && hard_amt > 0)
1350 lp->lnd_mobil -= mob_used;
1351 if (lp->lnd_mobil < 0)
1354 lp->lnd_harden += hard_amt;
1355 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1361 * Set LP's tech to TLEV along with everything else that depends on it.
1364 lnd_set_tech(struct lndstr *lp, int tlev)
1366 struct lchrstr *lcp = lchr + lp->lnd_type;
1367 int tech_diff = tlev - lcp->l_tech;
1369 if (CANT_HAPPEN(tech_diff < 0)) {
1374 lp->lnd_tech = tlev;
1375 lp->lnd_att = (float)LND_ATTDEF(lcp->l_att, tech_diff);
1376 lp->lnd_def = (float)LND_ATTDEF(lcp->l_def, tech_diff);
1377 lp->lnd_vul = (int)LND_VUL(lcp->l_vul, tech_diff);
1378 lp->lnd_spd = (int)LND_SPD(lcp->l_spd, tech_diff);
1379 lp->lnd_vis = (int)LND_VIS(lcp->l_vis, tech_diff);
1380 lp->lnd_spy = (int)LND_SPY(lcp->l_spy, tech_diff);
1381 lp->lnd_rad = (int)LND_RAD(lcp->l_rad, tech_diff);
1382 lp->lnd_frg = (int)LND_FRG(lcp->l_frg, tech_diff);
1383 lp->lnd_acc = (int)LND_ACC(lcp->l_acc, tech_diff);
1384 lp->lnd_dam = (int)LND_DAM(lcp->l_dam, tech_diff);
1385 lp->lnd_ammo = (int)LND_AMM(lcp->l_ammo, lcp->l_dam, tech_diff);
1386 lp->lnd_aaf = (int)LND_AAF(lcp->l_aaf, tech_diff);
1387 lp->lnd_fuelc = (int)LND_FC(lcp->l_fuelc, tech_diff);
1388 lp->lnd_fuelu = (int)LND_FU(lcp->l_fuelu, tech_diff);
1389 lp->lnd_maxlight = (int)LND_XPL(lcp->l_nxlight, tech_diff);
1390 lp->lnd_maxland = (int)LND_MXL(lcp->l_nland, tech_diff);