2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, 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-2014
51 #include "prototypes.h"
55 static void lnd_stays(natid, char *, struct ulist *);
56 static int lnd_hit_mine(struct lndstr *);
57 static int has_helpful_engineer(coord, coord, natid);
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 * lnd_att(lp) * lp->lnd_effic / 100.0;
82 switch (combat_mode) {
86 if (!(lcp->l_flags & L_MARINE))
87 return assault_penalty * value;
90 if (!(lcp->l_flags & L_MARINE))
91 return 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 * lnd_def(lp) * 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_reaction_range(struct lndstr *lp)
133 getsect(lp->lnd_x, lp->lnd_y, §);
134 if (sect.sct_type == SCT_HEADQ && sect.sct_effic >= 60)
135 return lchr[lp->lnd_type].l_rad + 1;
136 return lchr[lp->lnd_type].l_rad;
140 lnd_print(natid actor, struct ulist *llp, char *s)
142 if (actor == player->cnum)
143 pr("%s %s\n", prland(&llp->unit.land), s);
145 wu(0, actor, "%s %s\n", prland(&llp->unit.land), s);
149 lnd_delete(struct ulist *llp)
151 putland(llp->unit.land.lnd_uid, &llp->unit.land);
152 emp_remque(&llp->queue);
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 taken = llp->unit.land.lnd_item[I_MILIT];
177 /* Spies always die */
178 if (((struct lchrstr *)llp->chrp)->l_flags & L_SPY)
179 llp->unit.land.lnd_effic = 0;
181 eff_eq = ldround(cas * 100.0 /
182 ((struct lchrstr *)llp->chrp)->l_item[I_MILIT], 1);
183 llp->unit.land.lnd_effic -= eff_eq;
184 lnd_submil(&llp->unit.land, cas);
187 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
188 sprintf(buf, "dies %s %s!",
189 combat_mode ? att_mode[combat_mode] : "defending",
190 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
191 llp->unit.land.lnd_own));
192 lnd_print(llp->unit.land.lnd_own, llp, buf);
194 /* Since we killed the unit, we killed all the mil on it */
197 /* Ok, now, how many did we take off? (sould be the diff) */
198 taken = taken - llp->unit.land.lnd_item[I_MILIT];
201 if (llp->unit.land.lnd_effic >= llp->unit.land.lnd_retreat)
204 /* we're being boarded */
205 if (llp->unit.land.lnd_ship >= 0 && combat_mode == A_DEFEND)
208 /* we're being boarded */
209 if (llp->unit.land.lnd_land >= 0 && combat_mode == A_DEFEND)
212 /* Have to make a retreat check */
214 ret_chance = llp->unit.land.lnd_retreat - llp->unit.land.lnd_effic;
215 if (pct_chance(ret_chance)) {
217 lnd_print(llp->unit.land.lnd_own, llp, "fails morale check!");
218 llp->unit.land.lnd_mission = 0;
219 llp->unit.land.lnd_harden = 0;
220 if (llp->unit.land.lnd_ship >= 0 || llp->unit.land.lnd_land >= 0)
222 else if (combat_mode == A_DEFEND) {
224 * defending unit.. find a place to send it
225 * strategy: look for the most-populated
226 * adjacent sector that is owned by the unit
227 * owner. Charge mob..
230 for (n = 1; n <= 6; ++n) {
231 ret_x = llp->unit.land.lnd_x + diroff[n][0];
232 ret_y = llp->unit.land.lnd_y + diroff[n][1];
233 getsect(ret_x, ret_y, §);
234 if (sect.sct_own != llp->unit.land.lnd_own)
236 if (sect.sct_type == SCT_MOUNT)
238 mobcost = lnd_mobcost(&llp->unit.land, §);
241 civs = sect.sct_item[I_CIVIL];
242 if (civs > biggest) {
252 /* retreat to bx,by */
253 llp->unit.land.lnd_x = bx;
254 llp->unit.land.lnd_y = by;
255 /* FIXME landmines */
256 mob = llp->unit.land.lnd_mobil - (int)bmcost;
259 orig = llp->unit.land.lnd_mobil;
260 llp->unit.land.lnd_mobil = (signed char)mob;
261 if (llp->unit.land.lnd_mobil > orig)
262 llp->unit.land.lnd_mobil = -127;
263 sprintf(buf, "retreats at %d%% efficiency to %s!",
264 llp->unit.land.lnd_effic,
265 xyas(bx, by, llp->unit.land.lnd_own));
266 lnd_print(llp->unit.land.lnd_own, llp, buf);
269 } else { /* attacking from a sector */
270 sprintf(buf, "leaves the battlefield at %d%% efficiency",
271 llp->unit.land.lnd_effic);
272 if ((llp->unit.land.lnd_mobil - (int)llp->mobil) < -127)
273 llp->unit.land.lnd_mobil = -127;
275 llp->unit.land.lnd_mobil -= (int)llp->mobil;
277 lnd_print(llp->unit.land.lnd_own, llp, buf);
282 /* nowhere to go.. take more casualties */
283 llp->unit.land.lnd_effic -= 10;
284 lnd_submil(&llp->unit.land,
285 ((struct lchrstr *)llp->chrp)->l_item[I_MILIT] / 10);
286 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
287 lnd_print(llp->unit.land.lnd_own, llp,
288 "has nowhere to retreat, and dies!");
291 lnd_print(llp->unit.land.lnd_own, llp,
292 "has nowhere to retreat and takes extra losses!");
299 lnd_takemob(struct emp_qelem *list, double loss)
301 struct emp_qelem *qp, *next;
304 int mcost = ldround(combat_mob * loss, 1);
306 for (qp = list->q_forw; qp != list; qp = next) {
308 llp = (struct ulist *)qp;
311 use_supply(&llp->unit.land);
313 new = llp->unit.land.lnd_mobil - mcost;
316 llp->unit.land.lnd_mobil = (signed char)new;
321 lnd_submil(struct lndstr *lp, int num)
323 int new = lp->lnd_item[I_MILIT] - num;
324 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
328 lnd_spyval(struct lndstr *lp)
330 if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
331 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0) + 2;
333 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
337 intelligence_report(int destination, struct lndstr *lp, int spy,
340 int vis = lnd_vis(lp);
341 char buf1[80], buf2[80], buf3[80];
343 if (destination == 0)
346 if (lp->lnd_own == 0)
349 memset(buf1, 0, sizeof(buf1));
350 memset(buf2, 0, sizeof(buf2));
351 memset(buf3, 0, sizeof(buf3));
352 if (chance((spy + vis) / 10.0)) {
353 if (destination == player->cnum)
354 pr("%s %s", mess, prland(lp));
356 sprintf(buf1, "%s %s", mess, prland(lp));
358 if (chance((spy + vis) / 20.0)) {
359 if (destination == player->cnum)
360 pr(" (eff %d, mil %d",
361 roundintby(lp->lnd_effic, 5),
362 roundintby(lp->lnd_item[I_MILIT], 10));
364 sprintf(buf2, " (eff %d, mil %d",
365 roundintby(lp->lnd_effic, 5),
366 roundintby(lp->lnd_item[I_MILIT], 10));
368 if (chance((spy + vis) / 20.0)) {
370 t = lp->lnd_tech - 20 + roll(40);
372 if (destination == player->cnum)
373 pr(", tech %d)\n", t);
375 sprintf(buf3, ", tech %d)\n", t);
377 if (destination == player->cnum)
380 sprintf(buf3, ")\n");
383 if (destination == player->cnum)
390 if (destination != player->cnum) {
391 wu(0, destination, "%s%s%s", buf1, buf2, buf3);
396 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
400 int mobtype = MOB_MOVE; /* indeterminate */
403 while (nxtitem(ni, &land)) {
405 * It would be nice to let deities march foreign land units,
406 * but much of the code assumes that only the land unit's
407 * owner can march it.
409 if (!land.lnd_own || land.lnd_own != player->cnum)
412 if (ontradingblock(EF_LAND, &land)) {
413 pr("unit #%d inelligible - it's for sale.\n",
419 * The marching code gets confused when trains and non-trains
420 * march together. Disallow for now.
422 this_mot = lnd_mobtype(&land);
423 if (this_mot != mobtype) {
424 if (mobtype == MOB_MOVE)
426 else if (mobtype == MOB_MARCH) {
427 pr("%s is a train and can't march with the leader.\n",
431 pr("%s can't rail-march with the leading train.\n",
437 land.lnd_mission = 0;
440 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
441 putland(land.lnd_uid, &land);
442 lnd_insque(&land, list);
448 * Return the new list link.
451 lnd_insque(struct lndstr *lp, struct emp_qelem *list)
453 struct ulist *mlp = malloc(sizeof(struct ulist));
455 mlp->chrp = (struct empobj_chr *)&lchr[lp->lnd_type];
456 mlp->unit.land = *lp;
457 mlp->mobil = lp->lnd_mobil;
458 emp_insque(&mlp->queue, list);
462 /* This function assumes that the list was created by lnd_sel */
464 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
465 int *togetherp, natid actor)
467 struct emp_qelem *qp;
468 struct emp_qelem *next;
480 for (qp = list->q_back; qp != list; qp = next) {
482 llp = (struct ulist *)qp;
483 lp = &llp->unit.land;
484 getland(lp->lnd_uid, lp);
485 if (lp->lnd_own != actor) {
486 mpr(actor, "%s was disbanded at %s\n",
487 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
488 emp_remque(&llp->queue);
492 if (lp->lnd_ship >= 0) {
493 lnd_stays(actor, "is on a ship", llp);
496 if (lp->lnd_land >= 0) {
497 lnd_stays(actor, "is on a unit", llp);
500 if (!getsect(lp->lnd_x, lp->lnd_y, §)) {
501 lnd_stays(actor, "was sucked into the sky by a strange looking spaceland", llp); /* heh -KHS */
504 if (!(lchr[lp->lnd_type].l_flags & L_SPY) &&
505 !(lchr[lp->lnd_type].l_flags & L_TRAIN) &&
506 lp->lnd_item[I_MILIT] == 0) {
507 lnd_stays(actor, "has no mil on it to guide it", llp);
510 if (relations_with(sect.sct_own, actor) != ALLIED &&
511 !(lchr[lp->lnd_type].l_flags & L_SPY) &&
513 sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
514 lnd_stays(actor, mess, llp);
522 if (lp->lnd_x != allx || lp->lnd_y != ally)
524 if (lp->lnd_mobil + 1 < (int)llp->mobil) {
525 llp->mobil = lp->lnd_mobil;
527 if (llp->mobil < *minmobp)
528 *minmobp = llp->mobil;
529 if (llp->mobil > *maxmobp)
530 *maxmobp = llp->mobil;
535 lnd_put(struct emp_qelem *list, natid actor)
537 struct emp_qelem *qp, *next;
541 for (qp = list->q_back; qp != list; qp = next) {
543 llp = (struct ulist *)qp;
544 lp = &llp->unit.land;
546 mpr(actor, "%s stopped at %s\n",
547 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
548 if (llp->mobil < -127)
550 lp->lnd_mobil = llp->mobil;
552 putland(lp->lnd_uid, lp);
559 * Sweep landmines with engineers in LAND_LIST for ACTOR.
560 * If EXPLICIT is non-zero, this is for an explicit sweep command from
561 * a player. Else it's an automatic "on the move" sweep.
562 * If TAKEMOB is non-zero, require and charge mobility.
565 lnd_sweep(struct emp_qelem *land_list, int explicit, int takemob,
568 struct emp_qelem *qp;
569 struct emp_qelem *next;
572 int mines, m, max, sshells, lshells;
574 for (qp = land_list->q_back; qp != land_list; qp = next) {
576 llp = (struct ulist *)qp;
577 if (!(((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)) {
579 mpr(actor, "%s is not an engineer!\n",
580 prland(&llp->unit.land));
583 if (takemob && llp->mobil < 0.0) {
585 lnd_stays(actor, "is out of mobility", llp);
588 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
589 if (!explicit && relations_with(sect.sct_oldown, actor) == ALLIED)
591 if (SCT_MINES_ARE_SEAMINES(§)) {
593 mpr(actor, "%s is in a %s sector. No landmines there!\n",
594 prland(&llp->unit.land), dchr[sect.sct_type].d_name);
598 llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
599 llp->unit.land.lnd_mobil = (int)llp->mobil;
600 llp->unit.land.lnd_harden = 0;
602 putland(llp->unit.land.lnd_uid, &llp->unit.land);
603 if (!(mines = sect.sct_mines))
605 max = ((struct lchrstr *)llp->chrp)->l_item[I_SHELL];
606 lshells = llp->unit.land.lnd_item[I_SHELL];
607 sshells = sect.sct_item[I_SHELL];
608 for (m = 0; mines > 0 && m < max * 2; m++) {
609 if (chance(0.5 * ((struct lchrstr *)llp->chrp)->l_att)) {
610 mpr(actor, "Sweep...\n");
614 else if (sshells < ITEM_MAX)
618 sect.sct_mines = mines;
619 llp->unit.land.lnd_item[I_SHELL] = lshells;
620 sect.sct_item[I_SHELL] = sshells;
621 putland(llp->unit.land.lnd_uid, &llp->unit.land);
627 contains_engineer(struct emp_qelem *list)
629 struct emp_qelem *qp;
630 struct emp_qelem *next;
633 for (qp = list->q_back; qp != list; qp = next) {
635 llp = (struct ulist *)qp;
636 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
643 lnd_check_mines(struct emp_qelem *land_list)
645 struct emp_qelem *qp;
646 struct emp_qelem *next;
650 int with_eng = contains_engineer(land_list);
652 for (qp = land_list->q_back; qp != land_list; qp = next) {
654 llp = (struct ulist *)qp;
655 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
656 if (SCT_LANDMINES(§) == 0)
658 if (relations_with(sect.sct_oldown, llp->unit.land.lnd_own)
661 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
662 lnd_hit_mine(&llp->unit.land);
665 putland(llp->unit.land.lnd_uid, &llp->unit.land);
666 if (!llp->unit.land.lnd_own) {
677 lnd_stays(natid actor, char *str, struct ulist *llp)
679 mpr(actor, "%s %s & stays in %s\n",
680 prland(&llp->unit.land), str,
681 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
682 if (llp->mobil < -127)
684 llp->unit.land.lnd_mobil = llp->mobil;
689 lnd_count(struct emp_qelem *list)
691 struct emp_qelem *qp;
692 struct emp_qelem *next;
695 for (qp = list->q_back; qp != list; qp = next) {
703 lnd_damage(struct emp_qelem *list, int totdam)
705 struct emp_qelem *qp;
706 struct emp_qelem *next;
711 if (!totdam || !(count = lnd_count(list)))
713 dam = ldround((double)totdam / count, 1);
714 for (qp = list->q_back; qp != list; qp = next) {
716 llp = (struct ulist *)qp;
717 /* land unit might have changed (launched SAMs, collateral dmg) */
718 getland(llp->unit.land.lnd_uid, &llp->unit.land);
719 landdamage(&llp->unit.land, dam);
720 putland(llp->unit.land.lnd_uid, &llp->unit.land);
721 if (!llp->unit.land.lnd_own) {
730 lnd_easiest_target(struct emp_qelem *list)
732 struct emp_qelem *qp;
733 struct emp_qelem *next;
736 int easiest = 9876; /* things start great for victim */
739 for (qp = list->q_back; qp != list; qp = next) {
741 llp = (struct ulist *)qp;
742 hard = lnd_hardtarget(&llp->unit.land);
744 easiest = hard; /* things get worse for victim */
747 return easiest - count;
751 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
754 int mindam = lnd_count(list) * 20;
755 int hardtarget = lnd_easiest_target(list);
756 int dam, newdam, sublaunch;
759 struct emp_qelem msl_list, *qp, *newqp;
761 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
764 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
766 plp = (struct plist *)qp;
768 if (dam < mindam && mission_pln_equip(plp, NULL, 'p') >= 0) {
769 if (msl_launch(&plp->plane, EF_LAND, "troops",
770 newx, newy, victim, &sublaunch) < 0)
773 if (msl_hit(&plp->plane, hardtarget, EF_LAND,
774 N_LND_MISS, N_LND_SMISS, sublaunch, victim)) {
775 newdam = pln_damage(&plp->plane, 'p', 1);
778 newdam = pln_damage(&plp->plane, 'p', 0);
779 collateral_damage(newx, newy, newdam);
782 plp->plane.pln_effic = 0;
783 putplane(plp->plane.pln_uid, &plp->plane);
790 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
791 collateral_damage(newx, newy, dam);
792 lnd_damage(list, dam);
798 /* Steve M. - commented out for now until abuse is decided upon */
799 /* risner: allow forts to interdict land units. */
801 lnd_fort_interdiction(struct emp_qelem *list,
802 coord newx, coord newy, natid victim)
811 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
812 while (nxtsct(&ns, &fsect)) {
813 if (fsect.sct_own == 0)
815 if (relations_with(fsect.sct_own, victim) >= NEUTRAL)
817 range = roundrange(fortrange(&fsect));
818 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
821 dam = fort_fire(&fsect);
827 mpr(victim, "Incoming fire does %d damage!\n", dam);
829 "%s fires at %s land units in %s for %d!\n",
830 xyas(fsect.sct_x, fsect.sct_y,
832 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
833 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
836 lnd_damage(list, totdam);
842 lnd_mission_interdiction(struct emp_qelem *list, coord x, coord y,
847 dam = unit_interdict(x, y, victim, "land units",
848 lnd_easiest_target(list),
851 lnd_damage(list, dam);
856 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
861 if (!opt_NO_FORT_FIRE)
862 /* Steve M. - commented out for now until abuse is decided upon */
863 stopping |= lnd_fort_interdiction(list, newx, newy, victim);
866 stopping |= lnd_mission_interdiction(list, newx, newy, victim);
867 stopping |= lnd_missile_interdiction(list, newx, newy, victim);
871 /* high value of hardtarget is harder to hit */
873 lnd_hardtarget(struct lndstr *lp)
877 getsect(lp->lnd_x, lp->lnd_y, §);
878 return (int)((lp->lnd_effic / 100.0) *
879 (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
884 lnd_hit_mine(struct lndstr *lp)
888 mpr(lp->lnd_own, "Blammo! Landmines detected in %s!\n",
889 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
891 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
894 if (lchr[lp->lnd_type].l_flags & L_ENGINEER)
902 lnd_pathcost(struct lndstr *lp, double pathcost)
906 effspd = lnd_spd(lp);
907 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
908 effspd *= lp->lnd_effic * 0.01;
911 * The return value must be PATHCOST times a factor that depends
912 * only on the land unit. Anything else breaks path finding. In
913 * particular, you can't add or enforce a minimum cost here. Do
914 * it in sector_mcost().
916 return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
920 lnd_mobtype(struct lndstr *lp)
922 return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
923 ? MOB_RAIL : MOB_MARCH;
927 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
929 return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
933 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
936 struct sctstr sect, osect;
937 struct emp_qelem *qp;
938 struct emp_qelem *qp2;
939 struct emp_qelem *next;
941 struct emp_qelem cur, done;
953 if (dir <= DIR_STOP || dir >= DIR_VIEW) {
954 lnd_put(list, actor);
959 for (qp = list->q_back; qp != list; qp = next) {
961 llp = (struct ulist *)qp;
962 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
963 oldown = osect.sct_own;
964 newx = xnorm(llp->unit.land.lnd_x + dx);
965 newy = ynorm(llp->unit.land.lnd_y + dy);
966 getsect(newx, newy, §);
967 rel = relations_with(sect.sct_own, actor);
968 if ((rel != ALLIED &&
969 !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
970 sect.sct_own) || (sect.sct_type == SCT_WATER ||
971 sect.sct_type == SCT_SANCT ||
972 sect.sct_type == SCT_WASTE)) {
974 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
977 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
978 lnd_stays(actor, dp, llp);
982 if (!SCT_HAS_RAIL(§)
983 && lnd_mobtype(&llp->unit.land) == MOB_RAIL) {
985 mpr(actor, "no rail system in %s\n",
986 xyas(newx, newy, actor));
989 sprintf(dp, "has no rail system in %s",
990 xyas(newx, newy, actor));
991 lnd_stays(actor, dp, llp);
995 /* Note we check would_abandon first because we don't want
996 to always have to do these checks */
997 if (would_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
999 if (!want_to_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
1002 /* now check stuff */
1003 if (!check_sect_ok(§))
1005 if (!check_sect_ok(&osect))
1007 for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
1008 if (!check_land_ok(&((struct ulist *)qp2)->unit.land))
1012 lnd_stays(actor, "stops", llp);
1016 if (llp->mobil <= 0.0) {
1017 lnd_stays(actor, "is out of mobility", llp);
1020 llp->unit.land.lnd_x = newx;
1021 llp->unit.land.lnd_y = newy;
1022 llp->mobil -= lnd_mobcost(&llp->unit.land, §);
1023 llp->unit.land.lnd_mobil = (int)llp->mobil;
1024 llp->unit.land.lnd_harden = 0;
1025 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1027 getsect(osect.sct_x, osect.sct_y, &osect);
1028 if (osect.sct_own != oldown && oldown == actor) {
1029 /* It was your sector, now it's not. Simple :) */
1030 mpr(actor, "You no longer own %s\n",
1031 xyas(osect.sct_x, osect.sct_y, actor));
1033 if (rel != ALLIED && sect.sct_own) { /* must be a spy */
1034 /* Always a 10% chance of getting caught. */
1035 if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1036 if (rel == NEUTRAL || rel == FRIENDLY) {
1038 "%s unit spotted in %s\n", cname(actor),
1039 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1040 setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1041 } else if (rel <= HOSTILE) {
1043 "%s spy shot in %s\n", cname(actor),
1044 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1045 mpr(actor, "%s was shot and killed.\n",
1046 prland(&llp->unit.land));
1047 llp->unit.land.lnd_effic = 0;
1048 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1056 lnd_sweep(list, 0, 1, actor);
1057 stopping |= lnd_check_mines(list);
1061 /* interdict land units sector by sector */
1064 while (!QEMPTY(list)) {
1065 llp = (struct ulist *)list->q_back;
1066 newx = llp->unit.land.lnd_x;
1067 newy = llp->unit.land.lnd_y;
1068 /* move units in NEWX,NEWY to cur */
1070 for (qp = list->q_back; qp != list; qp = next) {
1072 llp = (struct ulist *)qp;
1073 if (llp->unit.land.lnd_x == newx && llp->unit.land.lnd_y == newy) {
1075 emp_insque(qp, &cur);
1076 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1080 /* interdict them */
1082 stopping |= lnd_interdict(&cur, newx, newy, actor);
1083 /* move survivors to done */
1084 for (qp = cur.q_back; qp != &cur; qp = next) {
1087 emp_insque(qp, &done);
1090 /* assign surviving land units back to list */
1091 emp_insque(list, &done);
1098 * Fire land unit support against VICTIM for ATTACKER, at X,Y.
1099 * If DEFENDING, this is defensive support, else offensive support.
1100 * Return total damage.
1103 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1105 struct nstr_item ni;
1112 snxtitem_all(&ni, EF_LAND);
1113 while (nxtitem(&ni, &land)) {
1114 if ((land.lnd_x == x) && (land.lnd_y == y))
1116 if (!feels_like_helping(land.lnd_own, attacker, victim))
1119 /* are we in range? */
1120 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1122 range = roundrange(lnd_fire_range(&land));
1126 dam2 = lnd_fire(&land);
1127 putland(land.lnd_uid, &land);
1132 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1134 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1135 if (pct_chance(lnd_acc(&land) - 1))
1138 if (land.lnd_own != attacker)
1140 "%s supported %s at %s\n",
1141 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1147 lnd_can_attack(struct lndstr *lp)
1149 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1151 if (lcp->l_flags & L_SUPPLY)
1158 * Increase fortification value of LP.
1159 * Fortification costs mobility. Use up to MOB mobility.
1160 * Return actual fortification increase.
1163 lnd_fortify(struct lndstr *lp, int mob)
1166 double mob_used, mult;
1168 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1171 mob_used = MIN(lp->lnd_mobil, mob);
1175 mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1178 hard_amt = (int)(mob_used * mult);
1179 if (lp->lnd_harden + hard_amt > land_mob_max) {
1180 hard_amt = land_mob_max - lp->lnd_harden;
1181 mob_used = ceil(hard_amt / mult);
1184 lp->lnd_mobil -= (int)mob_used;
1185 lp->lnd_harden += hard_amt;
1186 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1192 * Is there a engineer unit at X,Y that can help nation CN?
1195 has_helpful_engineer(coord x, coord y, natid cn)
1197 struct nstr_item ni;
1200 snxtitem_xy(&ni, EF_LAND, x, y);
1201 while (nxtitem(&ni, &land)) {
1202 if (relations_with(land.lnd_own, cn) != ALLIED)
1204 if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1212 * Set LP's tech to TLEV along with everything else that depends on it.
1215 lnd_set_tech(struct lndstr *lp, int tlev)
1217 struct lchrstr *lcp = lchr + lp->lnd_type;
1219 if (CANT_HAPPEN(tlev < lcp->l_tech))
1222 lp->lnd_tech = tlev;