2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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-2016
50 #include "prototypes.h"
54 static void lnd_mar_put_one(struct ulist *);
55 static int lnd_check_one_mines(struct ulist *, int);
56 static int lnd_hit_mine(struct lndstr *);
57 static int has_helpful_engineer(coord, coord, natid);
60 lnd_find_capable(struct emp_qelem *list, int flags)
65 for (qp = list->q_back; qp != list; qp = qp->q_back) {
66 llp = (struct ulist *)qp;
67 if (lchr[llp->unit.land.lnd_type].l_flags & flags)
74 attack_val(int combat_mode, struct lndstr *lp)
80 if (lp->lnd_effic < LAND_MINEFF) {
81 putland(lp->lnd_uid, lp);
85 lcp = &lchr[(int)lp->lnd_type];
87 men = lp->lnd_item[I_MILIT];
88 value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
90 switch (combat_mode) {
94 if (!(lcp->l_flags & L_MARINE))
95 return assault_penalty * value;
98 if (!(lcp->l_flags & L_MARINE))
99 return assault_penalty * men;
106 defense_val(struct lndstr *lp)
112 if (lp->lnd_effic < LAND_MINEFF) {
113 putland(lp->lnd_uid, lp);
117 lcp = &lchr[(int)lp->lnd_type];
119 men = lp->lnd_item[I_MILIT];
121 if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
122 !(lcp->l_flags & L_MARINE))
125 value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
126 value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
128 /* If there are military on the unit, you get at least a 1
129 man defensive unit, except for spies */
130 if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
137 lnd_reaction_range(struct lndstr *lp)
141 getsect(lp->lnd_x, lp->lnd_y, §);
142 if (sect.sct_type == SCT_HEADQ && sect.sct_effic >= 60)
143 return lchr[lp->lnd_type].l_rad + 1;
144 return lchr[lp->lnd_type].l_rad;
148 lnd_print(natid actor, struct ulist *llp, char *s)
150 if (actor == player->cnum)
151 pr("%s %s\n", prland(&llp->unit.land), s);
153 wu(0, actor, "%s %s\n", prland(&llp->unit.land), s);
157 lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
158 /* attacking or assaulting or paratrooping? */
159 /* number of casualties to take */
171 int nowhere_to_go = 0;
172 double mobcost, bmcost;
176 if (CANT_HAPPEN(lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
179 taken = llp->unit.land.lnd_item[I_MILIT];
180 eff_eq = ldround(cas * 100.0 /
181 lchr[llp->unit.land.lnd_type].l_item[I_MILIT], 1);
182 llp->unit.land.lnd_effic -= eff_eq;
183 lnd_submil(&llp->unit.land, cas);
185 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
186 sprintf(buf, "dies %s %s!",
187 combat_mode ? att_mode[combat_mode] : "defending",
188 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
189 llp->unit.land.lnd_own));
190 lnd_print(llp->unit.land.lnd_own, llp, buf);
192 /* Since we killed the unit, we killed all the mil on it */
195 /* Ok, now, how many did we take off? (sould be the diff) */
196 taken = taken - llp->unit.land.lnd_item[I_MILIT];
199 if (llp->unit.land.lnd_effic >= llp->unit.land.lnd_retreat)
202 /* we're being boarded */
203 if (llp->unit.land.lnd_ship >= 0 && combat_mode == A_DEFEND)
206 /* we're being boarded */
207 if (llp->unit.land.lnd_land >= 0 && combat_mode == A_DEFEND)
210 /* Have to make a retreat check */
212 ret_chance = llp->unit.land.lnd_retreat - llp->unit.land.lnd_effic;
213 if (pct_chance(ret_chance)) {
215 lnd_print(llp->unit.land.lnd_own, llp, "fails morale check!");
216 llp->unit.land.lnd_mission = 0;
217 llp->unit.land.lnd_harden = 0;
218 if (llp->unit.land.lnd_ship >= 0 || llp->unit.land.lnd_land >= 0)
220 else if (combat_mode == A_DEFEND) {
222 * defending unit.. find a place to send it
223 * strategy: look for the most-populated
224 * adjacent sector that is owned by the unit
225 * owner. Charge mob..
228 for (n = 1; n <= 6; ++n) {
229 ret_x = llp->unit.land.lnd_x + diroff[n][0];
230 ret_y = llp->unit.land.lnd_y + diroff[n][1];
231 getsect(ret_x, ret_y, §);
232 if (sect.sct_own != llp->unit.land.lnd_own)
234 if (sect.sct_type == SCT_MOUNT)
236 mobcost = lnd_mobcost(&llp->unit.land, §);
239 civs = sect.sct_item[I_CIVIL];
240 if (civs > biggest) {
250 /* retreat to bx,by */
251 llp->unit.land.lnd_x = bx;
252 llp->unit.land.lnd_y = by;
253 /* FIXME landmines */
254 mob = llp->unit.land.lnd_mobil - (int)bmcost;
257 orig = llp->unit.land.lnd_mobil;
258 llp->unit.land.lnd_mobil = (signed char)mob;
259 if (llp->unit.land.lnd_mobil > orig)
260 llp->unit.land.lnd_mobil = -127;
261 sprintf(buf, "retreats at %d%% efficiency to %s!",
262 llp->unit.land.lnd_effic,
263 xyas(bx, by, llp->unit.land.lnd_own));
264 lnd_print(llp->unit.land.lnd_own, llp, buf);
267 } else { /* attacking from a sector */
268 sprintf(buf, "leaves the battlefield at %d%% efficiency",
269 llp->unit.land.lnd_effic);
270 if ((llp->unit.land.lnd_mobil - (int)llp->mobil) < -127)
271 llp->unit.land.lnd_mobil = -127;
273 llp->unit.land.lnd_mobil -= (int)llp->mobil;
275 lnd_print(llp->unit.land.lnd_own, llp, buf);
280 /* nowhere to go.. take more casualties */
281 llp->unit.land.lnd_effic -= 10;
282 lnd_submil(&llp->unit.land,
283 lchr[llp->unit.land.lnd_type].l_item[I_MILIT] / 10);
284 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
285 lnd_print(llp->unit.land.lnd_own, llp,
286 "has nowhere to retreat, and dies!");
289 lnd_print(llp->unit.land.lnd_own, llp,
290 "has nowhere to retreat and takes extra losses!");
297 lnd_takemob(struct emp_qelem *list, double loss)
299 struct emp_qelem *qp, *next;
302 int mcost = ldround(combat_mob * loss, 1);
304 for (qp = list->q_forw; qp != list; qp = next) {
306 llp = (struct ulist *)qp;
309 use_supply(&llp->unit.land);
311 new = llp->unit.land.lnd_mobil - mcost;
314 llp->unit.land.lnd_mobil = (signed char)new;
319 lnd_submil(struct lndstr *lp, int num)
321 int new = lp->lnd_item[I_MILIT] - num;
322 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
326 lnd_spyval(struct lndstr *lp)
328 if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
329 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0) + 2;
331 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
335 intelligence_report(natid destination, struct lndstr *lp, int spy,
338 int vis = lnd_vis(lp);
339 char buf1[80], buf2[80], buf3[80];
341 if (!destination || !lp->lnd_own)
344 if (chance((spy + vis) / 10.0)) {
345 sprintf(buf1, "%s %s", mess, prland(lp));
347 if (chance((spy + vis) / 20.0)) {
348 sprintf(buf2, " (eff %d, mil %d",
349 roundintby(lp->lnd_effic, 5),
350 roundintby(lp->lnd_item[I_MILIT], 10));
352 if (chance((spy + vis) / 20.0)) {
354 t = lp->lnd_tech - 20 + roll(40);
356 sprintf(buf3, ", tech %d)", t);
361 buf2[0] = buf3[0] = 0;
363 if (destination == player->cnum)
364 pr("%s%s%s\n", buf1, buf2, buf3);
366 wu(0, destination, "%s%s%s\n", buf1, buf2, buf3);
371 lnd_may_mar(struct lndstr *lp, struct lndstr *ldr, char *suffix)
376 if (!lp->lnd_own || !getsect(lp->lnd_x, lp->lnd_y, §)) {
381 if (opt_MARKET && ontradingblock(EF_LAND, lp)) {
382 mpr(lp->lnd_own, "%s is on the trading block%s\n",
387 if (lp->lnd_ship >= 0) {
388 mpr(lp->lnd_own, "%s is on a ship%s\n", prland(lp), suffix);
391 if (lp->lnd_land >= 0) {
392 mpr(lp->lnd_own, "%s is on a unit%s\n", prland(lp), suffix);
396 if (!(lchr[lp->lnd_type].l_flags & L_SPY) &&
397 !(lchr[lp->lnd_type].l_flags & L_TRAIN) &&
398 lp->lnd_item[I_MILIT] == 0) {
399 mpr(lp->lnd_own, "%s has no mil on it to guide it%s\n",
404 switch (lnd_check_mar(lp, §)) {
407 case LND_STUCK_NO_RAIL:
408 mpr(lp->lnd_own, "%s is stuck off the rail system%s\n",
414 case LND_STUCK_IMPASSABLE:
415 mpr(lp->lnd_own, "%s is stuck%s\n", prland(lp), suffix);
419 if (relations_with(sect.sct_own, lp->lnd_own) != ALLIED &&
420 !(lchr[lp->lnd_type].l_flags & L_SPY) &&
422 mpr(lp->lnd_own, "%s has been kidnapped by %s%s\n",
423 prland(lp), cname(sect.sct_own), suffix);
427 if (ldr && (lp->lnd_x != ldr->lnd_x || lp->lnd_y != ldr->lnd_y)) {
428 mpr(lp->lnd_own, "%s is not with the leader%s\n",
434 * The marching code gets confused when trains and non-trains
435 * march together. Disallow for now.
437 mobtype = lnd_mobtype(lp);
438 if (!ldr || mobtype == lnd_mobtype(ldr))
440 else if (mobtype == MOB_RAIL) {
442 "%s is a train and can't march with the leader%s\n",
446 mpr(lp->lnd_own, "%s can't rail-march with the leading train%s\n",
455 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
457 struct lndstr land, *ldr = NULL;
461 while (nxtitem(ni, &land)) {
463 * It would be nice to let deities march foreign land units,
464 * but much of the code assumes that only the land unit's
465 * owner can march it.
467 if (!land.lnd_own || land.lnd_own != player->cnum)
469 if (!lnd_may_mar(&land, ldr, ""))
472 land.lnd_mission = 0;
474 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
475 putland(land.lnd_uid, &land);
476 llp = lnd_insque(&land, list);
478 ldr = &llp->unit.land;
483 * Append @lp to @list.
484 * Return the new list link.
487 lnd_insque(struct lndstr *lp, struct emp_qelem *list)
489 struct ulist *mlp = malloc(sizeof(struct ulist));
491 mlp->unit.land = *lp;
492 mlp->mobil = lp->lnd_mobil;
493 emp_insque(&mlp->queue, list);
498 lnd_mar_stay_behind(struct emp_qelem *list, natid actor)
500 struct emp_qelem *qp;
501 struct emp_qelem *next;
503 struct lndstr *lp, *ldr = NULL;
506 for (qp = list->q_back; qp != list; qp = next) {
508 llp = (struct ulist *)qp;
509 lp = &llp->unit.land;
510 getland(lp->lnd_uid, lp);
512 if (lp->lnd_own != actor) {
513 mpr(actor, "%s was disbanded at %s\n",
514 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
515 emp_remque(&llp->queue);
520 snprintf(and_stays, sizeof(and_stays), " & stays in %s",
521 xyas(lp->lnd_x, lp->lnd_y, actor));
522 if (!lnd_may_mar(lp, ldr, and_stays)) {
523 lnd_mar_put_one(llp);
529 if (lp->lnd_mobil + 1 < (int)llp->mobil) {
530 llp->mobil = lp->lnd_mobil;
536 lnd_mar_put_one(struct ulist *llp)
538 if (llp->mobil < -127)
540 llp->unit.land.lnd_mobil = llp->mobil;
545 lnd_mar_put(struct emp_qelem *list, natid actor)
547 struct emp_qelem *qp, *next;
551 for (qp = list->q_back; qp != list; qp = next) {
553 llp = (struct ulist *)qp;
554 lp = &llp->unit.land;
555 mpr(actor, "%s stopped at %s\n",
556 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
557 lnd_mar_put_one(llp);
562 lnd_put(struct emp_qelem *list)
564 struct emp_qelem *qp, *next;
566 for (qp = list->q_back; qp != list; qp = next) {
568 lnd_put_one((struct ulist *)qp);
573 lnd_put_one(struct ulist *llp)
575 putland(llp->unit.land.lnd_uid, &llp->unit.land);
576 emp_remque(&llp->queue);
581 * Sweep landmines with engineers in @land_list for @actor.
582 * All land units in @land_list must be in the same sector.
583 * If @explicit is non-zero, this is for an explicit sweep command from
584 * a player. Else it's an automatic "on the move" sweep.
585 * If @takemob is non-zero, require and charge mobility.
586 * Return non-zero when the land units should stop.
589 lnd_sweep(struct emp_qelem *land_list, int explicit, int takemob,
592 struct emp_qelem *qp;
593 struct emp_qelem *next;
596 int mines, m, max, sshells, lshells;
597 int stopping = 0, first = 1;
599 llp = lnd_find_capable(land_list, L_ENGINEER);
602 mpr(actor, "No engineers!\n");
606 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
608 && relations_with(sect.sct_oldown, actor) == ALLIED)
610 if (SCT_MINES_ARE_SEAMINES(§)) {
612 mpr(actor, "%s is a %s. No landmines there!\n",
613 xyas(sect.sct_x, sect.sct_y, actor),
614 dchr[sect.sct_type].d_name);
618 for (qp = land_list->q_back; qp != land_list; qp = next) {
620 llp = (struct ulist *)qp;
621 if (!(lchr[llp->unit.land.lnd_type].l_flags & L_ENGINEER))
624 if (llp->mobil <= 0.0) {
626 mpr(actor, "%s is out of mobility!\n",
627 prland(&llp->unit.land));
630 llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
631 llp->unit.land.lnd_mobil = (int)llp->mobil;
632 llp->unit.land.lnd_harden = 0;
634 putland(llp->unit.land.lnd_uid, &llp->unit.land);
635 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
636 if (!(mines = sect.sct_mines))
638 max = lchr[llp->unit.land.lnd_type].l_item[I_SHELL];
639 lshells = llp->unit.land.lnd_item[I_SHELL];
640 sshells = sect.sct_item[I_SHELL];
641 for (m = 0; mines > 0 && m < max * 2; m++) {
642 if (chance(0.5 * lchr[llp->unit.land.lnd_type].l_att)) {
644 mpr(actor, "Approaching minefield at %s...\n",
645 xyas(sect.sct_x, sect.sct_y, actor));
648 mpr(actor, "Sweep...\n");
652 else if (sshells < ITEM_MAX)
656 sect.sct_mines = mines;
657 llp->unit.land.lnd_item[I_SHELL] = lshells;
658 sect.sct_item[I_SHELL] = sshells;
659 putland(llp->unit.land.lnd_uid, &llp->unit.land);
661 stopping |= lnd_check_one_mines(llp, 1);
667 lnd_check_one_mines(struct ulist *llp, int with_eng)
671 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
672 if (SCT_LANDMINES(§) == 0)
674 if (relations_with(sect.sct_oldown, llp->unit.land.lnd_own) == ALLIED)
676 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
677 lnd_hit_mine(&llp->unit.land);
680 putland(llp->unit.land.lnd_uid, &llp->unit.land);
681 if (!llp->unit.land.lnd_own) {
682 emp_remque(&llp->queue);
691 lnd_check_mines(struct emp_qelem *land_list)
693 struct emp_qelem *qp;
694 struct emp_qelem *next;
696 int with_eng = !!lnd_find_capable(land_list, L_ENGINEER);
698 for (qp = land_list->q_back; qp != land_list; qp = next) {
700 stopping |= lnd_check_one_mines((struct ulist *)qp, with_eng);
705 /* Return whether and why SP would be stuck in SECTP. */
707 lnd_check_mar(struct lndstr *lp, struct sctstr *sectp)
709 if (dchr[sectp->sct_type].d_mob0 < 0)
710 return LND_STUCK_IMPASSABLE;
711 if (lnd_mobtype(lp) == MOB_RAIL && !SCT_HAS_RAIL(sectp))
712 return LND_STUCK_NO_RAIL;
713 return LND_STUCK_NOT;
717 lnd_damage(struct emp_qelem *list, int totdam)
719 struct emp_qelem *qp;
720 struct emp_qelem *next;
725 if (!totdam || !(count = emp_quelen(list)))
727 dam = ldround((double)totdam / count, 1);
728 for (qp = list->q_back; qp != list; qp = next) {
730 llp = (struct ulist *)qp;
731 /* land unit might have changed (launched SAMs, collateral dmg) */
732 getland(llp->unit.land.lnd_uid, &llp->unit.land);
733 landdamage(&llp->unit.land, dam);
734 putland(llp->unit.land.lnd_uid, &llp->unit.land);
735 if (!llp->unit.land.lnd_own) {
744 lnd_easiest_target(struct emp_qelem *list)
746 struct emp_qelem *qp;
747 struct emp_qelem *next;
750 int easiest = 9876; /* things start great for victim */
753 for (qp = list->q_back; qp != list; qp = next) {
755 llp = (struct ulist *)qp;
756 hard = lnd_hardtarget(&llp->unit.land);
758 easiest = hard; /* things get worse for victim */
761 return easiest - count;
765 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
768 int mindam = emp_quelen(list) * 20;
769 int hardtarget = lnd_easiest_target(list);
770 int dam, newdam, sublaunch;
773 struct emp_qelem msl_list, *qp, *newqp;
775 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
778 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
780 plp = (struct plist *)qp;
782 if (dam < mindam && mission_pln_equip(plp, NULL, 'p') >= 0) {
783 if (msl_launch(&plp->plane, EF_LAND, "troops",
784 newx, newy, victim, &sublaunch) < 0)
787 if (msl_hit(&plp->plane, hardtarget, EF_LAND,
788 N_LND_MISS, N_LND_SMISS, sublaunch, victim)) {
789 newdam = pln_damage(&plp->plane, 'p', "");
792 newdam = pln_damage(&plp->plane, 'p', NULL);
793 collateral_damage(newx, newy, newdam);
796 plp->plane.pln_effic = 0;
797 putplane(plp->plane.pln_uid, &plp->plane);
804 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
805 collateral_damage(newx, newy, dam);
806 lnd_damage(list, dam);
812 /* Steve M. - commented out for now until abuse is decided upon */
813 /* risner: allow forts to interdict land units. */
815 lnd_fort_interdiction(struct emp_qelem *list,
816 coord newx, coord newy, natid victim)
825 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
826 while (nxtsct(&ns, &fsect)) {
827 if (fsect.sct_own == 0)
829 if (relations_with(fsect.sct_own, victim) >= NEUTRAL)
831 range = roundrange(fortrange(&fsect));
832 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
835 dam = fort_fire(&fsect);
841 mpr(victim, "Incoming fire does %d damage!\n", dam);
843 "%s fires at %s land units in %s for %d!\n",
844 xyas(fsect.sct_x, fsect.sct_y,
846 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
847 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
850 lnd_damage(list, totdam);
856 lnd_mission_interdiction(struct emp_qelem *list, coord x, coord y,
861 dam = unit_interdict(x, y, victim, "land units",
862 lnd_easiest_target(list),
865 lnd_damage(list, dam);
870 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
875 if (!opt_NO_FORT_FIRE)
876 /* Steve M. - commented out for now until abuse is decided upon */
877 stopping |= lnd_fort_interdiction(list, newx, newy, victim);
880 stopping |= lnd_mission_interdiction(list, newx, newy, victim);
881 stopping |= lnd_missile_interdiction(list, newx, newy, victim);
885 /* high value of hardtarget is harder to hit */
887 lnd_hardtarget(struct lndstr *lp)
891 getsect(lp->lnd_x, lp->lnd_y, §);
892 return (int)((lp->lnd_effic / 100.0) *
893 (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
898 lnd_hit_mine(struct lndstr *lp)
902 mpr(lp->lnd_own, "Blammo! Landmines detected in %s!\n",
903 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
905 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
908 if (lchr[lp->lnd_type].l_flags & L_ENGINEER)
916 lnd_pathcost(struct lndstr *lp, double pathcost)
920 effspd = lnd_spd(lp);
921 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
922 effspd *= lp->lnd_effic * 0.01;
925 * The return value must be PATHCOST times a factor that depends
926 * only on the land unit. Anything else breaks path finding. In
927 * particular, you can't add or enforce a minimum cost here. Do
928 * it in sector_mcost().
930 return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
934 lnd_mobtype(struct lndstr *lp)
936 return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
937 ? MOB_RAIL : MOB_MARCH;
941 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
943 return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
947 * Ask user to confirm sector abandonment, if any.
948 * All land units in @list must be in the same sector, owned by the
949 * player, and not loaded onto anything.
950 * If removing the land units in @list would abandon their sector, ask
951 * the user to confirm.
952 * Return zero when abandonment was declined, else non-zero.
954 int lnd_abandon_askyn(struct emp_qelem *list)
959 struct emp_qelem *qp;
963 llp = (struct ulist *)list->q_back;
964 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
965 abandon = abandon_askyn(§, I_CIVIL, 0, llp);
966 if (!check_sect_ok(§))
968 for (qp = list->q_back; qp != list; qp = qp->q_back) {
969 if (!check_land_ok(&((struct ulist *)qp)->unit.land))
976 lnd_mar_dir(struct emp_qelem *list, int dir, natid actor)
978 struct sctstr sect, osect;
979 struct emp_qelem *qp;
980 struct emp_qelem *next;
990 if (CANT_HAPPEN(QEMPTY(list)))
993 if (dir <= DIR_STOP || dir > DIR_LAST) {
994 CANT_HAPPEN(dir != DIR_STOP);
995 lnd_mar_put(list, actor);
1001 llp = (struct ulist *)list->q_back;
1002 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
1003 oldown = osect.sct_own;
1004 newx = xnorm(llp->unit.land.lnd_x + dx);
1005 newy = ynorm(llp->unit.land.lnd_y + dy);
1006 getsect(newx, newy, §);
1007 rel = sect.sct_own ? relations_with(sect.sct_own, actor) : ALLIED;
1010 for (qp = list->q_back; qp != list; qp = next) {
1012 llp = (struct ulist *)qp;
1013 switch (lnd_check_mar(&llp->unit.land, §)) {
1016 || (lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
1019 case LND_STUCK_NO_RAIL:
1021 mpr(actor, "no rail system in %s\n",
1022 xyas(newx, newy, actor));
1024 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1029 case LND_STUCK_IMPASSABLE:
1030 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1035 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1039 for (qp = list->q_back; qp != list; qp = next) {
1041 llp = (struct ulist *)qp;
1043 && !(lchr[llp->unit.land.lnd_type].l_flags & L_SPY)) {
1044 mpr(actor, "%s can't go to %s & stays in %s\n",
1045 prland(&llp->unit.land), xyas(newx, newy, actor),
1046 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
1047 lnd_mar_put_one(llp);
1050 if (llp->mobil <= 0.0) {
1051 mpr(actor, "%s is out of mobility & stays in %s\n",
1052 prland(&llp->unit.land),
1053 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
1054 lnd_mar_put_one(llp);
1057 llp->unit.land.lnd_x = newx;
1058 llp->unit.land.lnd_y = newy;
1059 llp->mobil -= lnd_mobcost(&llp->unit.land, §);
1060 llp->unit.land.lnd_mobil = (int)llp->mobil;
1061 llp->unit.land.lnd_harden = 0;
1062 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1064 getsect(osect.sct_x, osect.sct_y, &osect);
1065 if (osect.sct_own != oldown && oldown == actor) {
1066 /* It was your sector, now it's not. Simple :) */
1067 mpr(actor, "You no longer own %s\n",
1068 xyas(osect.sct_x, osect.sct_y, actor));
1070 if (rel != ALLIED) {
1072 /* Always a 10% chance of getting caught. */
1073 if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1074 if (rel == NEUTRAL || rel == FRIENDLY) {
1076 "%s unit spotted in %s\n", cname(actor),
1077 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1078 setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1079 } else if (rel <= HOSTILE) {
1081 "%s spy shot in %s\n", cname(actor),
1082 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1083 mpr(actor, "%s was shot and killed.\n",
1084 prland(&llp->unit.land));
1085 llp->unit.land.lnd_effic = 0;
1086 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1093 return QEMPTY(list);
1097 lnd_mar_gauntlet(struct emp_qelem *list, int interdict, natid actor)
1099 struct ulist *mlp = (struct ulist *)list->q_back;
1101 int stopping, visible;
1102 struct emp_qelem *qp, *next;
1105 if (CANT_HAPPEN(QEMPTY(list)))
1107 newx = mlp->unit.land.lnd_x;
1108 newy = mlp->unit.land.lnd_y;
1109 stopping = lnd_sweep(list, 0, 1, actor);
1112 stopping |= lnd_check_mines(list);
1117 for (qp = list->q_back; qp != list; qp = next) {
1119 llp = (struct ulist *)qp;
1120 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1123 if (visible && interdict)
1124 stopping |= lnd_interdict(list, newx, newy, actor);
1130 * Fire land unit support against @victim for @attacker, at @x,@y.
1131 * If @defending, this is defensive support, else offensive support.
1132 * Return total damage.
1135 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1137 struct nstr_item ni;
1144 snxtitem_all(&ni, EF_LAND);
1145 while (nxtitem(&ni, &land)) {
1146 if ((land.lnd_x == x) && (land.lnd_y == y))
1148 if (!feels_like_helping(land.lnd_own, attacker, victim))
1151 /* are we in range? */
1152 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1154 range = roundrange(lnd_fire_range(&land));
1158 dam2 = lnd_fire(&land);
1159 putland(land.lnd_uid, &land);
1164 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1166 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1167 if (pct_chance(lnd_acc(&land) - 1))
1170 if (land.lnd_own != attacker)
1172 "%s supported %s at %s\n",
1173 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1179 lnd_can_attack(struct lndstr *lp)
1181 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1183 if (lcp->l_flags & L_SUPPLY)
1190 * Increase fortification value of @lp.
1191 * Fortification costs mobility. Use up to @mob mobility.
1192 * Return actual fortification increase.
1195 lnd_fortify(struct lndstr *lp, int mob)
1198 double mob_used, mult;
1200 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1203 mob_used = MIN(lp->lnd_mobil, mob);
1207 mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1210 hard_amt = (int)(mob_used * mult);
1211 if (lp->lnd_harden + hard_amt > land_mob_max) {
1212 hard_amt = land_mob_max - lp->lnd_harden;
1213 mob_used = ceil(hard_amt / mult);
1216 lp->lnd_mobil -= (int)mob_used;
1217 lp->lnd_harden += hard_amt;
1218 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1224 * Is there a engineer unit at @x,@y that can help nation @cn?
1227 has_helpful_engineer(coord x, coord y, natid cn)
1229 struct nstr_item ni;
1232 snxtitem_xy(&ni, EF_LAND, x, y);
1233 while (nxtitem(&ni, &land)) {
1234 if (relations_with(land.lnd_own, cn) != ALLIED)
1236 if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1244 * Set @lp's tech to @tlev along with everything else that depends on it.
1247 lnd_set_tech(struct lndstr *lp, int tlev)
1249 struct lchrstr *lcp = lchr + lp->lnd_type;
1251 if (CANT_HAPPEN(tlev < lcp->l_tech))
1254 lp->lnd_tech = tlev;