2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * lndsub.c: Land unit subroutines
29 * Known contributors to this file:
31 * Steve McClure, 1998-2000
32 * Markus Armbruster, 2004-2015
51 #include "prototypes.h"
55 static void lnd_mar_put_one(struct ulist *);
56 static int lnd_check_one_mines(struct ulist *, int);
57 static int lnd_hit_mine(struct lndstr *);
58 static int has_helpful_engineer(coord, coord, natid);
61 lnd_find_capable(struct emp_qelem *list, int flags)
66 for (qp = list->q_back; qp != list; qp = qp->q_back) {
67 llp = (struct ulist *)qp;
68 if (lchr[llp->unit.land.lnd_type].l_flags & flags)
75 attack_val(int combat_mode, struct lndstr *lp)
81 if (lp->lnd_effic < LAND_MINEFF) {
82 putland(lp->lnd_uid, lp);
86 lcp = &lchr[(int)lp->lnd_type];
88 /* Spies always count as 1 during assaults. If they are the only ones
89 in the assault, they get to sneak on anyway. */
91 if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
94 men = lp->lnd_item[I_MILIT];
95 value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
97 switch (combat_mode) {
101 if (!(lcp->l_flags & L_MARINE))
102 return assault_penalty * value;
105 if (!(lcp->l_flags & L_MARINE))
106 return assault_penalty * men;
113 defense_val(struct lndstr *lp)
119 if (lp->lnd_effic < LAND_MINEFF) {
120 putland(lp->lnd_uid, lp);
124 lcp = &lchr[(int)lp->lnd_type];
126 men = lp->lnd_item[I_MILIT];
128 if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
129 !(lcp->l_flags & L_MARINE))
132 value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
133 value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
135 /* If there are military on the unit, you get at least a 1
136 man defensive unit, except for spies */
137 if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
144 lnd_reaction_range(struct lndstr *lp)
148 getsect(lp->lnd_x, lp->lnd_y, §);
149 if (sect.sct_type == SCT_HEADQ && sect.sct_effic >= 60)
150 return lchr[lp->lnd_type].l_rad + 1;
151 return lchr[lp->lnd_type].l_rad;
155 lnd_print(natid actor, struct ulist *llp, char *s)
157 if (actor == player->cnum)
158 pr("%s %s\n", prland(&llp->unit.land), s);
160 wu(0, actor, "%s %s\n", prland(&llp->unit.land), s);
164 lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
165 /* attacking or assaulting or paratrooping? */
166 /* number of casualties to take */
178 int nowhere_to_go = 0;
179 double mobcost, bmcost;
183 taken = llp->unit.land.lnd_item[I_MILIT];
184 /* Spies always die */
185 if (lchr[llp->unit.land.lnd_type].l_flags & L_SPY)
186 llp->unit.land.lnd_effic = 0;
188 eff_eq = ldround(cas * 100.0 /
189 lchr[llp->unit.land.lnd_type].l_item[I_MILIT], 1);
190 llp->unit.land.lnd_effic -= eff_eq;
191 lnd_submil(&llp->unit.land, cas);
194 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
195 sprintf(buf, "dies %s %s!",
196 combat_mode ? att_mode[combat_mode] : "defending",
197 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
198 llp->unit.land.lnd_own));
199 lnd_print(llp->unit.land.lnd_own, llp, buf);
201 /* Since we killed the unit, we killed all the mil on it */
204 /* Ok, now, how many did we take off? (sould be the diff) */
205 taken = taken - llp->unit.land.lnd_item[I_MILIT];
208 if (llp->unit.land.lnd_effic >= llp->unit.land.lnd_retreat)
211 /* we're being boarded */
212 if (llp->unit.land.lnd_ship >= 0 && combat_mode == A_DEFEND)
215 /* we're being boarded */
216 if (llp->unit.land.lnd_land >= 0 && combat_mode == A_DEFEND)
219 /* Have to make a retreat check */
221 ret_chance = llp->unit.land.lnd_retreat - llp->unit.land.lnd_effic;
222 if (pct_chance(ret_chance)) {
224 lnd_print(llp->unit.land.lnd_own, llp, "fails morale check!");
225 llp->unit.land.lnd_mission = 0;
226 llp->unit.land.lnd_harden = 0;
227 if (llp->unit.land.lnd_ship >= 0 || llp->unit.land.lnd_land >= 0)
229 else if (combat_mode == A_DEFEND) {
231 * defending unit.. find a place to send it
232 * strategy: look for the most-populated
233 * adjacent sector that is owned by the unit
234 * owner. Charge mob..
237 for (n = 1; n <= 6; ++n) {
238 ret_x = llp->unit.land.lnd_x + diroff[n][0];
239 ret_y = llp->unit.land.lnd_y + diroff[n][1];
240 getsect(ret_x, ret_y, §);
241 if (sect.sct_own != llp->unit.land.lnd_own)
243 if (sect.sct_type == SCT_MOUNT)
245 mobcost = lnd_mobcost(&llp->unit.land, §);
248 civs = sect.sct_item[I_CIVIL];
249 if (civs > biggest) {
259 /* retreat to bx,by */
260 llp->unit.land.lnd_x = bx;
261 llp->unit.land.lnd_y = by;
262 /* FIXME landmines */
263 mob = llp->unit.land.lnd_mobil - (int)bmcost;
266 orig = llp->unit.land.lnd_mobil;
267 llp->unit.land.lnd_mobil = (signed char)mob;
268 if (llp->unit.land.lnd_mobil > orig)
269 llp->unit.land.lnd_mobil = -127;
270 sprintf(buf, "retreats at %d%% efficiency to %s!",
271 llp->unit.land.lnd_effic,
272 xyas(bx, by, llp->unit.land.lnd_own));
273 lnd_print(llp->unit.land.lnd_own, llp, buf);
276 } else { /* attacking from a sector */
277 sprintf(buf, "leaves the battlefield at %d%% efficiency",
278 llp->unit.land.lnd_effic);
279 if ((llp->unit.land.lnd_mobil - (int)llp->mobil) < -127)
280 llp->unit.land.lnd_mobil = -127;
282 llp->unit.land.lnd_mobil -= (int)llp->mobil;
284 lnd_print(llp->unit.land.lnd_own, llp, buf);
289 /* nowhere to go.. take more casualties */
290 llp->unit.land.lnd_effic -= 10;
291 lnd_submil(&llp->unit.land,
292 lchr[llp->unit.land.lnd_type].l_item[I_MILIT] / 10);
293 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
294 lnd_print(llp->unit.land.lnd_own, llp,
295 "has nowhere to retreat, and dies!");
298 lnd_print(llp->unit.land.lnd_own, llp,
299 "has nowhere to retreat and takes extra losses!");
306 lnd_takemob(struct emp_qelem *list, double loss)
308 struct emp_qelem *qp, *next;
311 int mcost = ldround(combat_mob * loss, 1);
313 for (qp = list->q_forw; qp != list; qp = next) {
315 llp = (struct ulist *)qp;
318 use_supply(&llp->unit.land);
320 new = llp->unit.land.lnd_mobil - mcost;
323 llp->unit.land.lnd_mobil = (signed char)new;
328 lnd_submil(struct lndstr *lp, int num)
330 int new = lp->lnd_item[I_MILIT] - num;
331 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
335 lnd_spyval(struct lndstr *lp)
337 if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
338 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0) + 2;
340 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
344 intelligence_report(int destination, struct lndstr *lp, int spy,
347 int vis = lnd_vis(lp);
348 char buf1[80], buf2[80], buf3[80];
350 if (destination == 0)
353 if (lp->lnd_own == 0)
356 memset(buf1, 0, sizeof(buf1));
357 memset(buf2, 0, sizeof(buf2));
358 memset(buf3, 0, sizeof(buf3));
359 if (chance((spy + vis) / 10.0)) {
360 if (destination == player->cnum)
361 pr("%s %s", mess, prland(lp));
363 sprintf(buf1, "%s %s", mess, prland(lp));
365 if (chance((spy + vis) / 20.0)) {
366 if (destination == player->cnum)
367 pr(" (eff %d, mil %d",
368 roundintby(lp->lnd_effic, 5),
369 roundintby(lp->lnd_item[I_MILIT], 10));
371 sprintf(buf2, " (eff %d, mil %d",
372 roundintby(lp->lnd_effic, 5),
373 roundintby(lp->lnd_item[I_MILIT], 10));
375 if (chance((spy + vis) / 20.0)) {
377 t = lp->lnd_tech - 20 + roll(40);
379 if (destination == player->cnum)
380 pr(", tech %d)\n", t);
382 sprintf(buf3, ", tech %d)\n", t);
384 if (destination == player->cnum)
387 sprintf(buf3, ")\n");
390 if (destination == player->cnum)
397 if (destination != player->cnum) {
398 wu(0, destination, "%s%s%s", buf1, buf2, buf3);
403 lnd_may_mar(struct lndstr *lp, struct lndstr *ldr, char *suffix)
408 if (!lp->lnd_own || !getsect(lp->lnd_x, lp->lnd_y, §)) {
413 if (opt_MARKET && ontradingblock(EF_LAND, lp)) {
414 mpr(lp->lnd_own, "%s is on the trading block%s\n",
419 if (lp->lnd_ship >= 0) {
420 mpr(lp->lnd_own, "%s is on a ship%s\n", prland(lp), suffix);
423 if (lp->lnd_land >= 0) {
424 mpr(lp->lnd_own, "%s is on a unit%s\n", prland(lp), suffix);
428 if (!(lchr[lp->lnd_type].l_flags & L_SPY) &&
429 !(lchr[lp->lnd_type].l_flags & L_TRAIN) &&
430 lp->lnd_item[I_MILIT] == 0) {
431 mpr(lp->lnd_own, "%s has no mil on it to guide it%s\n",
436 switch (lnd_check_mar(lp, §)) {
439 case LND_STUCK_NO_RAIL:
440 mpr(lp->lnd_own, "%s is stuck off the rail system%s\n",
446 case LND_STUCK_IMPASSABLE:
447 mpr(lp->lnd_own, "%s is stuck%s\n", prland(lp), suffix);
451 if (relations_with(sect.sct_own, lp->lnd_own) != ALLIED &&
452 !(lchr[lp->lnd_type].l_flags & L_SPY) &&
454 mpr(lp->lnd_own, "%s has been kidnapped by %s%s\n",
455 prland(lp), cname(sect.sct_own), suffix);
459 if (ldr && (lp->lnd_x != ldr->lnd_x || lp->lnd_y != ldr->lnd_y)) {
460 mpr(lp->lnd_own, "%s is not with the leader%s\n",
466 * The marching code gets confused when trains and non-trains
467 * march together. Disallow for now.
469 mobtype = lnd_mobtype(lp);
470 if (!ldr || mobtype == lnd_mobtype(ldr))
472 else if (mobtype == MOB_RAIL) {
474 "%s is a train and can't march with the leader%s\n",
478 mpr(lp->lnd_own, "%s can't rail-march with the leading train%s\n",
487 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
489 struct lndstr land, *ldr = NULL;
493 while (nxtitem(ni, &land)) {
495 * It would be nice to let deities march foreign land units,
496 * but much of the code assumes that only the land unit's
497 * owner can march it.
499 if (!land.lnd_own || land.lnd_own != player->cnum)
501 if (!lnd_may_mar(&land, ldr, ""))
504 land.lnd_mission = 0;
506 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
507 putland(land.lnd_uid, &land);
508 llp = lnd_insque(&land, list);
510 ldr = &llp->unit.land;
516 * Return the new list link.
519 lnd_insque(struct lndstr *lp, struct emp_qelem *list)
521 struct ulist *mlp = malloc(sizeof(struct ulist));
523 mlp->unit.land = *lp;
524 mlp->mobil = lp->lnd_mobil;
525 emp_insque(&mlp->queue, list);
530 lnd_mar_stay_behind(struct emp_qelem *list, natid actor)
532 struct emp_qelem *qp;
533 struct emp_qelem *next;
535 struct lndstr *lp, *ldr = NULL;
538 for (qp = list->q_back; qp != list; qp = next) {
540 llp = (struct ulist *)qp;
541 lp = &llp->unit.land;
542 getland(lp->lnd_uid, lp);
544 if (lp->lnd_own != actor) {
545 mpr(actor, "%s was disbanded at %s\n",
546 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
547 emp_remque(&llp->queue);
552 snprintf(and_stays, sizeof(and_stays), " & stays in %s",
553 xyas(lp->lnd_x, lp->lnd_y, actor));
554 if (!lnd_may_mar(lp, ldr, and_stays)) {
555 lnd_mar_put_one(llp);
561 if (lp->lnd_mobil + 1 < (int)llp->mobil) {
562 llp->mobil = lp->lnd_mobil;
568 lnd_mar_put_one(struct ulist *llp)
570 if (llp->mobil < -127)
572 llp->unit.land.lnd_mobil = llp->mobil;
577 lnd_mar_put(struct emp_qelem *list, natid actor)
579 struct emp_qelem *qp, *next;
583 for (qp = list->q_back; qp != list; qp = next) {
585 llp = (struct ulist *)qp;
586 lp = &llp->unit.land;
587 mpr(actor, "%s stopped at %s\n",
588 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
589 lnd_mar_put_one(llp);
594 lnd_put(struct emp_qelem *list)
596 struct emp_qelem *qp, *next;
598 for (qp = list->q_back; qp != list; qp = next) {
600 lnd_put_one((struct ulist *)qp);
605 lnd_put_one(struct ulist *llp)
607 putland(llp->unit.land.lnd_uid, &llp->unit.land);
608 emp_remque(&llp->queue);
613 * Sweep landmines with engineers in LAND_LIST for ACTOR.
614 * All land units in LAND_LIST must be in the same sector.
615 * If EXPLICIT is non-zero, this is for an explicit sweep command from
616 * a player. Else it's an automatic "on the move" sweep.
617 * If TAKEMOB is non-zero, require and charge mobility.
618 * Return non-zero when the land units should stop.
621 lnd_sweep(struct emp_qelem *land_list, int explicit, int takemob,
624 struct emp_qelem *qp;
625 struct emp_qelem *next;
628 int mines, m, max, sshells, lshells;
629 int stopping = 0, first = 1;
631 llp = lnd_find_capable(land_list, L_ENGINEER);
634 mpr(actor, "No engineers!\n");
638 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
640 && relations_with(sect.sct_oldown, actor) == ALLIED)
642 if (SCT_MINES_ARE_SEAMINES(§)) {
644 mpr(actor, "%s is a %s. No landmines there!\n",
645 xyas(sect.sct_x, sect.sct_y, actor),
646 dchr[sect.sct_type].d_name);
650 for (qp = land_list->q_back; qp != land_list; qp = next) {
652 llp = (struct ulist *)qp;
653 if (!(lchr[llp->unit.land.lnd_type].l_flags & L_ENGINEER))
656 if (llp->mobil <= 0.0) {
658 mpr(actor, "%s is out of mobility!\n",
659 prland(&llp->unit.land));
662 llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
663 llp->unit.land.lnd_mobil = (int)llp->mobil;
664 llp->unit.land.lnd_harden = 0;
666 putland(llp->unit.land.lnd_uid, &llp->unit.land);
667 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
668 if (!(mines = sect.sct_mines))
670 max = lchr[llp->unit.land.lnd_type].l_item[I_SHELL];
671 lshells = llp->unit.land.lnd_item[I_SHELL];
672 sshells = sect.sct_item[I_SHELL];
673 for (m = 0; mines > 0 && m < max * 2; m++) {
674 if (chance(0.5 * lchr[llp->unit.land.lnd_type].l_att)) {
676 mpr(actor, "Approaching minefield at %s...\n",
677 xyas(sect.sct_x, sect.sct_y, actor));
680 mpr(actor, "Sweep...\n");
684 else if (sshells < ITEM_MAX)
688 sect.sct_mines = mines;
689 llp->unit.land.lnd_item[I_SHELL] = lshells;
690 sect.sct_item[I_SHELL] = sshells;
691 putland(llp->unit.land.lnd_uid, &llp->unit.land);
693 stopping |= lnd_check_one_mines(llp, 1);
699 lnd_check_one_mines(struct ulist *llp, int with_eng)
703 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
704 if (SCT_LANDMINES(§) == 0)
706 if (relations_with(sect.sct_oldown, llp->unit.land.lnd_own) == ALLIED)
708 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
709 lnd_hit_mine(&llp->unit.land);
712 putland(llp->unit.land.lnd_uid, &llp->unit.land);
713 if (!llp->unit.land.lnd_own) {
714 emp_remque(&llp->queue);
723 lnd_check_mines(struct emp_qelem *land_list)
725 struct emp_qelem *qp;
726 struct emp_qelem *next;
728 int with_eng = !!lnd_find_capable(land_list, L_ENGINEER);
730 for (qp = land_list->q_back; qp != land_list; qp = next) {
732 stopping |= lnd_check_one_mines((struct ulist *)qp, with_eng);
737 /* Return whether and why SP would be stuck in SECTP. */
739 lnd_check_mar(struct lndstr *lp, struct sctstr *sectp)
741 if (dchr[sectp->sct_type].d_mob0 < 0)
742 return LND_STUCK_IMPASSABLE;
743 if (lnd_mobtype(lp) == MOB_RAIL && !SCT_HAS_RAIL(sectp))
744 return LND_STUCK_NO_RAIL;
745 return LND_STUCK_NOT;
749 lnd_damage(struct emp_qelem *list, int totdam)
751 struct emp_qelem *qp;
752 struct emp_qelem *next;
757 if (!totdam || !(count = emp_quelen(list)))
759 dam = ldround((double)totdam / count, 1);
760 for (qp = list->q_back; qp != list; qp = next) {
762 llp = (struct ulist *)qp;
763 /* land unit might have changed (launched SAMs, collateral dmg) */
764 getland(llp->unit.land.lnd_uid, &llp->unit.land);
765 landdamage(&llp->unit.land, dam);
766 putland(llp->unit.land.lnd_uid, &llp->unit.land);
767 if (!llp->unit.land.lnd_own) {
776 lnd_easiest_target(struct emp_qelem *list)
778 struct emp_qelem *qp;
779 struct emp_qelem *next;
782 int easiest = 9876; /* things start great for victim */
785 for (qp = list->q_back; qp != list; qp = next) {
787 llp = (struct ulist *)qp;
788 hard = lnd_hardtarget(&llp->unit.land);
790 easiest = hard; /* things get worse for victim */
793 return easiest - count;
797 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
800 int mindam = emp_quelen(list) * 20;
801 int hardtarget = lnd_easiest_target(list);
802 int dam, newdam, sublaunch;
805 struct emp_qelem msl_list, *qp, *newqp;
807 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
810 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
812 plp = (struct plist *)qp;
814 if (dam < mindam && mission_pln_equip(plp, NULL, 'p') >= 0) {
815 if (msl_launch(&plp->plane, EF_LAND, "troops",
816 newx, newy, victim, &sublaunch) < 0)
819 if (msl_hit(&plp->plane, hardtarget, EF_LAND,
820 N_LND_MISS, N_LND_SMISS, sublaunch, victim)) {
821 newdam = pln_damage(&plp->plane, 'p', 1);
824 newdam = pln_damage(&plp->plane, 'p', 0);
825 collateral_damage(newx, newy, newdam);
828 plp->plane.pln_effic = 0;
829 putplane(plp->plane.pln_uid, &plp->plane);
836 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
837 collateral_damage(newx, newy, dam);
838 lnd_damage(list, dam);
844 /* Steve M. - commented out for now until abuse is decided upon */
845 /* risner: allow forts to interdict land units. */
847 lnd_fort_interdiction(struct emp_qelem *list,
848 coord newx, coord newy, natid victim)
857 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
858 while (nxtsct(&ns, &fsect)) {
859 if (fsect.sct_own == 0)
861 if (relations_with(fsect.sct_own, victim) >= NEUTRAL)
863 range = roundrange(fortrange(&fsect));
864 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
867 dam = fort_fire(&fsect);
873 mpr(victim, "Incoming fire does %d damage!\n", dam);
875 "%s fires at %s land units in %s for %d!\n",
876 xyas(fsect.sct_x, fsect.sct_y,
878 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
879 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
882 lnd_damage(list, totdam);
888 lnd_mission_interdiction(struct emp_qelem *list, coord x, coord y,
893 dam = unit_interdict(x, y, victim, "land units",
894 lnd_easiest_target(list),
897 lnd_damage(list, dam);
902 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
907 if (!opt_NO_FORT_FIRE)
908 /* Steve M. - commented out for now until abuse is decided upon */
909 stopping |= lnd_fort_interdiction(list, newx, newy, victim);
912 stopping |= lnd_mission_interdiction(list, newx, newy, victim);
913 stopping |= lnd_missile_interdiction(list, newx, newy, victim);
917 /* high value of hardtarget is harder to hit */
919 lnd_hardtarget(struct lndstr *lp)
923 getsect(lp->lnd_x, lp->lnd_y, §);
924 return (int)((lp->lnd_effic / 100.0) *
925 (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
930 lnd_hit_mine(struct lndstr *lp)
934 mpr(lp->lnd_own, "Blammo! Landmines detected in %s!\n",
935 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
937 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
940 if (lchr[lp->lnd_type].l_flags & L_ENGINEER)
948 lnd_pathcost(struct lndstr *lp, double pathcost)
952 effspd = lnd_spd(lp);
953 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
954 effspd *= lp->lnd_effic * 0.01;
957 * The return value must be PATHCOST times a factor that depends
958 * only on the land unit. Anything else breaks path finding. In
959 * particular, you can't add or enforce a minimum cost here. Do
960 * it in sector_mcost().
962 return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
966 lnd_mobtype(struct lndstr *lp)
968 return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
969 ? MOB_RAIL : MOB_MARCH;
973 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
975 return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
979 * Ask user to confirm sector abandonment, if any.
980 * All land units in LIST must be in the same sector.
981 * If removing the land units in LIST would abandon their sector, ask
982 * the user to confirm.
983 * Return zero when abandonment was declined, else non-zero.
985 int lnd_abandon_askyn(struct emp_qelem *list)
989 struct emp_qelem *qp;
993 llp = (struct ulist *)list->q_back;
994 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
995 if (!abandon_askyn(§, I_CIVIL, 0, llp))
997 if (!check_sect_ok(§))
999 for (qp = list->q_back; qp != list; qp = qp->q_back) {
1000 if (!check_land_ok(&((struct ulist *)qp)->unit.land))
1007 lnd_mar_dir(struct emp_qelem *list, int dir, natid actor)
1009 struct sctstr sect, osect;
1010 struct emp_qelem *qp;
1011 struct emp_qelem *next;
1021 if (CANT_HAPPEN(QEMPTY(list)))
1024 if (dir <= DIR_STOP || dir > DIR_LAST) {
1025 CANT_HAPPEN(dir != DIR_STOP);
1026 lnd_mar_put(list, actor);
1029 dx = diroff[dir][0];
1030 dy = diroff[dir][1];
1032 llp = (struct ulist *)list->q_back;
1033 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
1034 oldown = osect.sct_own;
1035 newx = xnorm(llp->unit.land.lnd_x + dx);
1036 newy = ynorm(llp->unit.land.lnd_y + dy);
1037 getsect(newx, newy, §);
1038 rel = sect.sct_own ? relations_with(sect.sct_own, actor) : ALLIED;
1041 for (qp = list->q_back; qp != list; qp = next) {
1043 llp = (struct ulist *)qp;
1044 switch (lnd_check_mar(&llp->unit.land, §)) {
1047 || (lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
1050 case LND_STUCK_NO_RAIL:
1052 mpr(actor, "no rail system in %s\n",
1053 xyas(newx, newy, actor));
1055 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1060 case LND_STUCK_IMPASSABLE:
1061 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1066 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1070 for (qp = list->q_back; qp != list; qp = next) {
1072 llp = (struct ulist *)qp;
1074 && !(lchr[llp->unit.land.lnd_type].l_flags & L_SPY)) {
1075 mpr(actor, "%s can't go to %s & stays in %s\n",
1076 prland(&llp->unit.land), xyas(newx, newy, actor),
1077 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
1078 lnd_mar_put_one(llp);
1081 if (llp->mobil <= 0.0) {
1082 mpr(actor, "%s is out of mobility & stays in %s\n",
1083 prland(&llp->unit.land),
1084 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
1085 lnd_mar_put_one(llp);
1088 llp->unit.land.lnd_x = newx;
1089 llp->unit.land.lnd_y = newy;
1090 llp->mobil -= lnd_mobcost(&llp->unit.land, §);
1091 llp->unit.land.lnd_mobil = (int)llp->mobil;
1092 llp->unit.land.lnd_harden = 0;
1093 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1095 getsect(osect.sct_x, osect.sct_y, &osect);
1096 if (osect.sct_own != oldown && oldown == actor) {
1097 /* It was your sector, now it's not. Simple :) */
1098 mpr(actor, "You no longer own %s\n",
1099 xyas(osect.sct_x, osect.sct_y, actor));
1101 if (rel != ALLIED) {
1103 /* Always a 10% chance of getting caught. */
1104 if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1105 if (rel == NEUTRAL || rel == FRIENDLY) {
1107 "%s unit spotted in %s\n", cname(actor),
1108 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1109 setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1110 } else if (rel <= HOSTILE) {
1112 "%s spy shot in %s\n", cname(actor),
1113 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1114 mpr(actor, "%s was shot and killed.\n",
1115 prland(&llp->unit.land));
1116 llp->unit.land.lnd_effic = 0;
1117 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1128 lnd_mar_gauntlet(struct emp_qelem *list, int interdict, natid actor)
1130 struct ulist *mlp = (struct ulist *)list->q_back;
1131 coord newx = mlp->unit.land.lnd_x;
1132 coord newy = mlp->unit.land.lnd_y;
1133 int stopping, visible;
1134 struct emp_qelem *qp, *next;
1137 stopping = lnd_sweep(list, 0, 1, actor);
1140 stopping |= lnd_check_mines(list);
1145 for (qp = list->q_back; qp != list; qp = next) {
1147 llp = (struct ulist *)qp;
1148 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1151 if (visible && interdict)
1152 stopping |= lnd_interdict(list, newx, newy, actor);
1158 * Fire land unit support against VICTIM for ATTACKER, at X,Y.
1159 * If DEFENDING, this is defensive support, else offensive support.
1160 * Return total damage.
1163 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1165 struct nstr_item ni;
1172 snxtitem_all(&ni, EF_LAND);
1173 while (nxtitem(&ni, &land)) {
1174 if ((land.lnd_x == x) && (land.lnd_y == y))
1176 if (!feels_like_helping(land.lnd_own, attacker, victim))
1179 /* are we in range? */
1180 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1182 range = roundrange(lnd_fire_range(&land));
1186 dam2 = lnd_fire(&land);
1187 putland(land.lnd_uid, &land);
1192 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1194 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1195 if (pct_chance(lnd_acc(&land) - 1))
1198 if (land.lnd_own != attacker)
1200 "%s supported %s at %s\n",
1201 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1207 lnd_can_attack(struct lndstr *lp)
1209 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1211 if (lcp->l_flags & L_SUPPLY)
1218 * Increase fortification value of LP.
1219 * Fortification costs mobility. Use up to MOB mobility.
1220 * Return actual fortification increase.
1223 lnd_fortify(struct lndstr *lp, int mob)
1226 double mob_used, mult;
1228 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1231 mob_used = MIN(lp->lnd_mobil, mob);
1235 mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1238 hard_amt = (int)(mob_used * mult);
1239 if (lp->lnd_harden + hard_amt > land_mob_max) {
1240 hard_amt = land_mob_max - lp->lnd_harden;
1241 mob_used = ceil(hard_amt / mult);
1244 lp->lnd_mobil -= (int)mob_used;
1245 lp->lnd_harden += hard_amt;
1246 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1252 * Is there a engineer unit at X,Y that can help nation CN?
1255 has_helpful_engineer(coord x, coord y, natid cn)
1257 struct nstr_item ni;
1260 snxtitem_xy(&ni, EF_LAND, x, y);
1261 while (nxtitem(&ni, &land)) {
1262 if (relations_with(land.lnd_own, cn) != ALLIED)
1264 if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1272 * Set LP's tech to TLEV along with everything else that depends on it.
1275 lnd_set_tech(struct lndstr *lp, int tlev)
1277 struct lchrstr *lcp = lchr + lp->lnd_type;
1279 if (CANT_HAPPEN(tlev < lcp->l_tech))
1282 lp->lnd_tech = tlev;