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"
57 attack_val(int combat_mode, struct lndstr *lp)
63 if (((int)lp->lnd_effic) < LAND_MINEFF) {
64 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
66 putland(lp->lnd_uid, lp);
70 lcp = &lchr[(int)lp->lnd_type];
72 /* Spies always count as 1 during assaults. If they are the only ones
73 in the assault, they get to sneak on anyway. */
75 if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
80 value = ldround(((double)men * (double)lp->lnd_att), 1);
82 value = (int)((double)value * ((double)lp->lnd_effic / 100.0));
84 switch (combat_mode) {
88 if (!(lcp->l_flags & L_MARINE))
89 return (int)(assault_penalty * value);
92 if (!(lcp->l_flags & L_MARINE))
93 return (int)(assault_penalty * men);
100 defense_val(struct lndstr *lp)
106 if (((int)lp->lnd_effic) < LAND_MINEFF) {
107 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
109 putland(lp->lnd_uid, lp);
113 lcp = &lchr[(int)lp->lnd_type];
119 if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
120 !(lcp->l_flags & L_MARINE))
123 value = men * lp->lnd_def;
126 ((double)land_mob_max + lp->lnd_harden) / (double)land_mob_max;
127 value = (int)((double)value * ((double)lp->lnd_effic / 100.0));
128 value = (int)ldround(value, 1);
130 /* If there are military on the unit, you get at least a 1
131 man defensive unit, except for spies */
132 if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
139 total_mil(struct lndstr *lp)
144 lcp = &lchr[(int)lp->lnd_type];
146 men = lnd_getmil(lp);
147 /* men *= ((double)lp->lnd_effic)/100.0;*/
149 return ldround(men, 1);
153 lnd_print(struct llist *llp, s_char *s)
155 if (llp->land.lnd_own == player->cnum)
156 pr("%s %s\n", prland(&llp->land), s);
158 wu(0, llp->land.lnd_own, "%s %s\n", prland(&llp->land), s);
162 lnd_delete(struct llist *llp, s_char *s)
166 putland(llp->land.lnd_uid, &llp->land);
167 emp_remque((struct emp_qelem *)llp);
172 lnd_take_casualty(int combat_mode, struct llist *llp, int cas)
173 /* attacking or assaulting or paratrooping? */
174 /* number of casualties to take */
187 int nowhere_to_go = 0;
193 taken = lnd_getmil(&llp->land);
194 /* Spies always die */
195 if (llp->lcp->l_flags & L_SPY) {
197 llp->land.lnd_effic = 0;
200 ldround((((double)cas * 100.0) / (double)llp->lcp->l_mil), 1);
201 llp->land.lnd_effic -= eff_eq;
202 lnd_submil(&llp->land, cas);
205 if (llp->land.lnd_effic < LAND_MINEFF) {
206 sprintf(buf, "dies %s %s!",
207 combat_mode ? att_mode[combat_mode] : "defending",
208 xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
209 lnd_delete(llp, buf);
210 /* Since we killed the unit, we killed all the mil on it */
213 /* Ok, now, how many did we take off? (sould be the diff) */
214 taken = taken - lnd_getmil(&llp->land);
217 if (llp->land.lnd_effic >= llp->land.lnd_retreat)
220 /* we're being boarded */
221 if (llp->land.lnd_ship >= 0 && combat_mode == A_DEFEND)
224 /* we're being boarded */
225 if (llp->land.lnd_land >= 0 && combat_mode == A_DEFEND)
228 /* Have to make a retreat check */
230 ret_chance = llp->land.lnd_retreat - llp->land.lnd_effic;
231 if (roll(100) < ret_chance) {
233 lnd_print(llp, "fails morale check!");
234 llp->land.lnd_mission = 0;
235 llp->land.lnd_harden = 0;
236 if (llp->land.lnd_ship >= 0 || llp->land.lnd_land >= 0)
238 else if (combat_mode == A_DEFEND) {
240 * defending unit.. find a place to send it
241 * strategy: look for the most-populated
242 * adjacent sector that is owned by the unit
243 * player->owner. Charge mob..
247 for (n = 1; n <= 6; ++n) {
248 ret_x = llp->land.lnd_x + diroff[n][0];
249 ret_y = llp->land.lnd_y + diroff[n][1];
250 getsect(ret_x, ret_y, §);
251 if (sect.sct_own != llp->land.lnd_own)
253 if (sect.sct_type == SCT_MOUNT)
256 civs = sect.sct_item[I_CIVIL];
257 if (civs > biggest) {
266 /* retreat to bx,by */
267 llp->land.lnd_x = bx;
268 llp->land.lnd_y = by;
269 getsect(bx, by, &rsect);
270 mobcost = lnd_mobcost(&llp->land, &rsect, MOB_ROAD);
271 mob = llp->land.lnd_mobil - (int)mobcost;
274 orig = llp->land.lnd_mobil;
275 llp->land.lnd_mobil = (s_char)mob;
276 if (llp->land.lnd_mobil > orig)
277 llp->land.lnd_mobil = -127;
278 sprintf(buf, "retreats at %d%% efficiency to %s!",
280 xyas(bx, by, llp->land.lnd_own));
281 lnd_delete(llp, buf);
283 } else { /* attacking from a sector */
284 sprintf(buf, "leaves the battlefield at %d%% efficiency",
285 llp->land.lnd_effic);
286 if ((llp->land.lnd_mobil - (int)llp->mobil) < -127)
287 llp->land.lnd_mobil = -127;
289 llp->land.lnd_mobil -= (int)llp->mobil;
291 lnd_delete(llp, buf);
295 /* nowhere to go.. take more casualties */
296 llp->land.lnd_effic -= 10;
297 lnd_submil(&llp->land, llp->lcp->l_mil / 10);
298 if (llp->land.lnd_effic < LAND_MINEFF)
299 lnd_delete(llp, "has nowhere to retreat, and dies!");
302 "has nowhere to retreat and takes extra losses!");
309 lnd_takemob(struct emp_qelem *list, double loss)
311 struct emp_qelem *qp, *next;
314 int mcost = ldround(combat_mob * loss, 1);
316 for (qp = list->q_forw; qp != list; qp = next) {
318 llp = (struct llist *)qp;
321 use_supply(&llp->land);
322 if (llp->land.lnd_mission == MI_RESERVE)
323 new = llp->land.lnd_mobil - mcost/2;
326 new = llp->land.lnd_mobil - mcost;
329 llp->land.lnd_mobil = (s_char)new;
333 lnd_getmil(struct lndstr *lp)
335 return lp->lnd_item[I_MILIT];
339 lnd_submil(struct lndstr *lp, int num)
341 int new = lp->lnd_item[I_MILIT] - num;
342 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
346 lnd_spyval(struct lndstr *lp)
348 if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
349 return lp->lnd_spy * (lp->lnd_effic / 100.0) + 2;
351 return lp->lnd_spy * (lp->lnd_effic / 100.0);
355 intelligence_report(int destination, struct lndstr *lp, int spy,
359 s_char buf1[80], buf2[80], buf3[80];
360 double estimate = 0.0; /* estimated defense value */
362 if (destination == 0)
365 if (lp->lnd_own == 0)
368 lcp = &lchr[(int)lp->lnd_type];
370 memset(buf1, 0, sizeof(buf1));
371 memset(buf2, 0, sizeof(buf2));
372 memset(buf3, 0, sizeof(buf3));
373 if (chance((double)(spy + lp->lnd_vis) / 10.0)) {
374 if (destination == player->cnum)
375 pr("%s %s", mess, prland(lp));
377 sprintf(buf1, "%s %s", mess, prland(lp));
379 estimate = lnd_getmil(lp);
381 if (chance((double)(spy + lp->lnd_vis) / 20.0)) {
383 if (destination == player->cnum)
384 pr(" (eff %d, mil %d", roundintby(lp->lnd_effic, 5),
385 roundintby(lnd_getmil(lp), 10));
387 sprintf(buf2, " (eff %d, mil %d",
388 roundintby(lp->lnd_effic, 5),
389 roundintby(lnd_getmil(lp), 10));
390 estimate = lnd_getmil(lp) * lp->lnd_effic / 100.0;
392 if (chance((double)(spy + lp->lnd_vis) / 20.0)) {
394 t = lp->lnd_tech - 20 + roll(40);
396 if (destination == player->cnum)
397 pr(", tech %d)\n", t);
399 sprintf(buf3, ", tech %d)\n", t);
401 if (destination == player->cnum)
404 sprintf(buf3, ")\n");
407 if (destination == player->cnum)
414 if (destination != player->cnum) {
415 wu(0, destination, "%s%s%s", buf1, buf2, buf3);
418 if (lp->lnd_ship < 0 || lcp->l_flags & L_MARINE)
419 estimate *= lp->lnd_def;
421 return (int)estimate;
424 /* Used by the spy command to count land units in a sector. If used
425 for anything else, you may want to reconsider, because this doesn't
426 always count spies. :) */
428 count_sect_units(struct sctstr *sp)
434 snxtitem_all(&ni, EF_LAND);
435 while (nxtitem(&ni, &land)) {
438 if (land.lnd_x != sp->sct_x || land.lnd_y != sp->sct_y)
440 /* Don't always see spies */
441 if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
442 if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
445 /* Got here, report it */
453 count_units(struct shpstr *sp)
459 if (sp->shp_effic < SHIP_MINEFF)
462 snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
463 while (nxtitem(&ni, &land)) {
464 if (land.lnd_own == 0)
466 if (land.lnd_ship == sp->shp_uid)
470 if (sp->shp_nland != nland) {
471 sp->shp_nland = nland;
472 putship(sp->shp_uid, sp);
477 lnd_count_units(struct lndstr *lp)
483 if (lp->lnd_effic < LAND_MINEFF)
486 snxtitem_xy(&ni, EF_LAND, lp->lnd_x, lp->lnd_y);
487 while (nxtitem(&ni, &land)) {
488 if (land.lnd_own == 0)
490 if (land.lnd_land == lp->lnd_uid)
494 if (lp->lnd_nland != nland) {
495 lp->lnd_nland = nland;
496 putland(lp->lnd_uid, lp);
501 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
513 while (nxtitem(ni, &land)) {
517 if (ontradingblock(EF_LAND, &land)) {
518 pr("unit #%d inelligible - it's for sale.\n",
523 lcp = &lchr[(int)land.lnd_type];
524 /* if (wantflags && (lcp->m_flags & wantflags) != wantflags)
526 if (nowantflags && lcp->m_flags & nowantflags)
529 /* This abuse is better fixed by building a unit with the normal negative
530 mobility that everything else is built with */
531 /* Just so that the player can't build a bunch of land units, and them
532 march them a few minutes later... */
534 if (opt_MOB_ACCESS) {
535 if (land.lnd_effic < 11 &&
536 land.lnd_mobil < etu_per_update) {
537 pr("Land unit #%d needs at least %d mob to march.\n",
538 land.lnd_uid, etu_per_update);
543 land.lnd_mission = 0;
546 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
547 putland(land.lnd_uid, &land);
548 llp = malloc(sizeof(struct llist));
551 llp->mobil = (double)land.lnd_mobil;
552 emp_insque(&llp->queue, list);
556 /* This function assumes that the list was created by lnd_sel */
558 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
559 int *togetherp, natid actor)
561 struct emp_qelem *qp;
562 struct emp_qelem *next;
575 for (qp = list->q_back; qp != list; qp = next) {
577 llp = (struct llist *)qp;
578 getland(llp->land.lnd_uid, &land);
579 if (land.lnd_own != actor) {
580 mpr(actor, "%s was disbanded at %s\n",
581 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
582 emp_remque((struct emp_qelem *)llp);
586 if (land.lnd_ship >= 0) {
587 lnd_mess("is on a ship", llp);
590 if (land.lnd_land >= 0) {
591 lnd_mess("is on a unit", llp);
594 if (!getsect(land.lnd_x, land.lnd_y, §)) {
595 lnd_mess("was sucked into the sky by a strange looking spaceland", llp); /* heh -KHS */
598 if (!(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
599 !(lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) &&
600 lnd_getmil(&llp->land) == 0) {
601 lnd_mess("has no mil on it to guide it", llp);
604 rel = getrel(getnatp(sect.sct_own), player->cnum);
605 if (sect.sct_own != land.lnd_own && rel != ALLIED &&
606 !(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
608 sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
617 if (land.lnd_x != allx || land.lnd_y != ally)
619 if (land.lnd_mobil + 1 < (int)llp->mobil) {
620 llp->mobil = (double)land.lnd_mobil;
622 if (llp->mobil < *minmobp)
623 *minmobp = llp->mobil;
624 if (llp->mobil > *maxmobp)
625 *maxmobp = llp->mobil;
631 lnd_put(struct emp_qelem *list, natid actor)
633 struct emp_qelem *qp;
634 struct emp_qelem *newqp;
639 llp = (struct llist *)qp;
641 mpr(actor, "%s stopped at %s\n", prland(&llp->land),
642 xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
643 if (llp->mobil < -127)
645 llp->land.lnd_mobil = llp->mobil;
647 putland(llp->land.lnd_uid, &llp->land);
656 lnd_sweep(struct emp_qelem *land_list, int verbose, int takemob,
659 struct emp_qelem *qp;
660 struct emp_qelem *next;
663 int mines, m, max, sshells, lshells;
666 for (qp = land_list->q_back; qp != land_list; qp = next) {
668 llp = (struct llist *)qp;
669 if (!(llp->lcp->l_flags & L_ENGINEER)) {
671 mpr(actor, "%s is not an engineer!\n", prland(&llp->land));
674 if (takemob && llp->mobil < 0.0) {
676 lnd_mess("is out of mobility", llp);
679 getsect(llp->land.lnd_x, llp->land.lnd_y, §);
680 if (sect.sct_oldown == llp->land.lnd_own) {
683 "%s is in a sector completely owned by you. Don't bother digging up mines there!\n",
687 if (sect.sct_type == SCT_BSPAN) {
689 mpr(actor, "%s is on a bridge. No mines there!\n",
694 /* mobcost = llp->land.lnd_effic * 0.01 * llp->lcp->l_spd;*/
695 mobcost = llp->land.lnd_spd;
696 mobcost = 480.0 / (mobcost +
697 techfact(llp->land.lnd_tech, mobcost));
698 llp->mobil -= mobcost;
699 llp->land.lnd_mobil = (int)llp->mobil;
700 llp->land.lnd_harden = 0;
702 putland(llp->land.lnd_uid, &llp->land);
703 if (!(mines = sect.sct_mines))
705 max = llp->lcp->l_item[I_SHELL];
706 lshells = llp->land.lnd_item[I_SHELL];
707 sshells = sect.sct_item[I_SHELL];
708 for (m = 0; mines > 0 && m < max * 2; m++) {
709 if (chance(0.5 * llp->lcp->l_att)) {
710 mpr(actor, "Sweep...\n");
714 else if (sshells < ITEM_MAX)
718 sect.sct_mines = mines;
719 llp->land.lnd_item[I_SHELL] = lshells;
720 sect.sct_item[I_SHELL] = sshells;
721 putland(llp->land.lnd_uid, &llp->land);
727 contains_engineer(struct emp_qelem *list)
729 struct emp_qelem *qp;
730 struct emp_qelem *next;
733 for (qp = list->q_back; qp != list; qp = next) {
735 llp = (struct llist *)qp;
736 if (llp->lcp->l_flags & L_ENGINEER)
743 lnd_check_mines(struct emp_qelem *land_list)
745 struct emp_qelem *qp;
746 struct emp_qelem *next;
750 int with_eng = contains_engineer(land_list);
752 for (qp = land_list->q_back; qp != land_list; qp = next) {
754 llp = (struct llist *)qp;
755 getsect(llp->land.lnd_x, llp->land.lnd_y, §);
756 if (sect.sct_oldown == llp->land.lnd_own)
758 if (sect.sct_type == SCT_BSPAN)
762 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
763 lnd_hit_mine(&llp->land, llp->lcp);
766 putland(llp->land.lnd_uid, &llp->land);
767 if (!llp->land.lnd_own) {
778 lnd_list(struct emp_qelem *land_list)
780 struct emp_qelem *qp;
781 struct emp_qelem *next;
785 pr("lnd# land type x,y a eff sh gun xl mu tech retr fuel\n");
787 for (qp = land_list->q_back; qp != land_list; qp = next) {
789 llp = (struct llist *)qp;
791 pr("%4d ", lnd->lnd_uid);
792 pr("%-16.16s ", llp->lcp->l_name);
793 prxy("%4d,%-4d ", lnd->lnd_x, lnd->lnd_y, llp->land.lnd_own);
794 pr("%c", lnd->lnd_army);
795 pr("%4d%%", lnd->lnd_effic);
796 pr("%4d", lnd->lnd_item[I_SHELL]);
797 pr("%4d", lnd->lnd_item[I_GUN]);
798 count_land_planes(lnd);
799 pr("%3d", lnd->lnd_nxlight);
800 pr("%4d", lnd->lnd_mobil);
801 pr("%4d", lnd->lnd_tech);
802 pr("%4d%%", lnd->lnd_retreat);
803 pr("%5d\n", lnd->lnd_fuel);
808 lnd_mess(s_char *str, struct llist *llp)
810 mpr(llp->land.lnd_own, "%s %s & stays in %s\n",
812 str, xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
813 if (llp->mobil < -127)
815 llp->land.lnd_mobil = llp->mobil;
816 putland(llp->land.lnd_uid, &llp->land);
817 emp_remque((struct emp_qelem *)llp);
822 lnd_count(struct emp_qelem *list)
824 struct emp_qelem *qp;
825 struct emp_qelem *next;
829 for (qp = list->q_back; qp != list; qp = next) {
831 llp = (struct llist *)qp;
838 lnd_damage(struct emp_qelem *list, int totdam)
840 struct emp_qelem *qp;
841 struct emp_qelem *next;
846 if (!totdam || !(count = lnd_count(list)))
848 dam = ldround(((double)totdam / (double)count), 1);
849 for (qp = list->q_back; qp != list; qp = next) {
851 llp = (struct llist *)qp;
852 /* have to get it again because of collateral damage */
853 getland(llp->land.lnd_uid, &llp->land);
854 landdamage(&llp->land, dam);
855 putland(llp->land.lnd_uid, &llp->land);
856 if (!llp->land.lnd_own) {
865 lnd_easiest_target(struct emp_qelem *list)
867 struct emp_qelem *qp;
868 struct emp_qelem *next;
871 int easiest = 9876; /* things start great for victim */
874 for (qp = list->q_back; qp != list; qp = next) {
876 llp = (struct llist *)qp;
877 hard = lnd_hardtarget(&llp->land);
879 easiest = hard; /* things get worse for victim */
882 return easiest - count;
886 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
890 struct emp_qelem msl_list, *qp, *newqp;
892 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
894 dam = msl_launch_mindam(&msl_list, newx, newy,
895 lnd_easiest_target(list), EF_LAND,
896 lnd_count(list) * 20, "troops", victim,
899 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
900 collateral_damage(newx, newy, dam, 0);
902 qp = msl_list.q_forw;
903 while (qp != msl_list.q_forw) {
913 /* Steve M. - commented out for now until abuse is decided upon */
914 /* risner: allow forts to interdict land units. */
916 lnd_fort_interdiction(struct emp_qelem *list,
917 coord newx, coord newy, natid victim)
922 double range, range2, guneff;
928 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
929 while (nxtsct(&ns, &fsect)) {
930 if (fsect.sct_own == 0)
932 if (fsect.sct_own == victim)
934 if (fsect.sct_type != SCT_FORTR)
936 if (getrel(getnatp(fsect.sct_own), victim) >= NEUTRAL)
938 gun = fsect.sct_item[I_GUN];
941 range = tfactfire(fsect.sct_own, (double)MIN(gun, 7));
942 if (fsect.sct_effic > 59)
944 range2 = roundrange(range);
945 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
948 if (fsect.sct_item[I_MILIT] < 5)
950 shell = fsect.sct_item[I_SHELL];
952 shell += supply_commod(fsect.sct_own,
953 fsect.sct_x, fsect.sct_y, I_SHELL, 1);
957 fsect.sct_item[I_SHELL] = shell;
961 guneff = landgun((int)fsect.sct_effic, gun);
964 mpr(victim, "Incoming fire does %d damage!\n", dam);
966 "%s fires at %s land units in %s for %d!\n",
967 xyas(fsect.sct_x, fsect.sct_y,
969 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
970 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
973 return lnd_damage(list, totdam);
979 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
984 if (!opt_NO_FORT_FIRE)
985 /* Steve M. - commented out for now until abuse is decided upon */
986 stopping |= lnd_fort_interdiction(list, newx, newy, victim);
991 unit_interdict(newx, newy, victim, "land units",
992 lnd_easiest_target(list), MI_INTERDICT));
996 lnd_missile_interdiction(list, newx, newy, victim));
1000 /* high value of hardtarget is harder to hit */
1002 lnd_hardtarget(struct lndstr *lp)
1006 getsect(lp->lnd_x, lp->lnd_y, §);
1007 return (int)(((double)lp->lnd_effic / 100.0) *
1008 (10 + dchr[sect.sct_type].d_dstr * 2 +
1009 (double)lp->lnd_spd / 2.0 - lp->lnd_vis));
1013 lnd_hit_mine(struct lndstr *lp, struct lchrstr *lcp)
1017 mpr(lp->lnd_own, "Blammo! Landmines detected in %s! ",
1018 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
1020 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
1023 if (lcp->l_flags & L_ENGINEER)
1026 landdamage(lp, ldround(m, 1));
1032 lnd_mobcost(struct lndstr *lp, struct sctstr *sp, int mobtype)
1037 /* supply unit's speed depends on their eff, since
1038 that is their purpose */
1039 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
1040 mobcost = lp->lnd_effic * 0.01 * lp->lnd_spd;
1042 mobcost = lp->lnd_spd;
1046 /* sector_mcost now takes 2 different arguments, a sector pointer, and
1047 whether or not to figure in the highway bonus, rail bonus or none.
1048 bridge heads, bridges and highways have built-in highways bonus
1049 because they are a 1, and this will discount that. */
1051 smobcost = (double)sector_mcost(sp, mobtype);
1052 if (smobcost < 0.01)
1055 /* marching through 0 mobility conquered sectors takes lots of mobility,
1056 unless you are a train. Capturing railways is a good thing. */
1058 if (sp->sct_own != sp->sct_oldown && sp->sct_mobil <= 0 &&
1059 smobcost < LND_MINMOBCOST && mobtype != MOB_RAIL)
1060 smobcost = LND_MINMOBCOST;
1062 mobcost = smobcost * 5.0 * 480.0 /
1063 (mobcost + techfact(lp->lnd_tech, mobcost));
1069 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
1072 struct sctstr sect, osect;
1073 struct emp_qelem *qp;
1074 struct emp_qelem *qp2;
1075 struct emp_qelem *next;
1077 struct emp_qelem cur, done;
1089 if (dir <= DIR_STOP || dir >= DIR_VIEW) {
1090 lnd_put(list, actor);
1093 dx = diroff[dir][0];
1094 dy = diroff[dir][1];
1095 for (qp = list->q_back; qp != list; qp = next) {
1097 llp = (struct llist *)qp;
1098 getsect(llp->land.lnd_x, llp->land.lnd_y, &osect);
1099 oldown = osect.sct_own;
1100 newx = xnorm(llp->land.lnd_x + dx);
1101 newy = ynorm(llp->land.lnd_y + dy);
1102 getsect(newx, newy, §);
1103 rel = getrel(getnatp(sect.sct_own), player->cnum);
1104 if ((sect.sct_own != actor && rel != ALLIED &&
1105 !(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
1106 sect.sct_own) || (sect.sct_type == SCT_WATER ||
1107 sect.sct_type == SCT_SANCT ||
1108 sect.sct_type == SCT_WASTE)) {
1110 pr("can't go to %s\n", xyas(newx, newy, actor));
1113 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
1118 if (sect.sct_rail == 0 &&
1119 lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) {
1121 pr("no rail system in %s\n", xyas(newx, newy, actor));
1124 sprintf(dp, "has no rail system in %s",
1125 xyas(newx, newy, actor));
1130 /* Note we check would_abandon first because we don't want
1131 to always have to do these checks */
1132 if (would_abandon(&osect, I_CIVIL, 0, &llp->land)) {
1134 if (!want_to_abandon(&osect, I_CIVIL, 0, &llp->land)) {
1137 /* now check stuff */
1138 if (!check_sect_ok(§))
1140 if (!check_sect_ok(&osect))
1142 for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
1143 if (!check_land_ok(&((struct llist *)qp2)->land))
1147 lnd_mess("stops", llp);
1151 if (llp->mobil <= 0.0) {
1152 lnd_mess("is out of mobility", llp);
1155 llp->land.lnd_x = newx;
1156 llp->land.lnd_y = newy;
1157 if (lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) {
1158 llp->mobil -= lnd_mobcost(&llp->land, §, MOB_RAIL);
1160 llp->mobil -= lnd_mobcost(&llp->land, §, MOB_ROAD);
1162 llp->land.lnd_mobil = (int)llp->mobil;
1163 llp->land.lnd_harden = 0;
1164 putland(llp->land.lnd_uid, &llp->land);
1166 getsect(osect.sct_x, osect.sct_y, &osect);
1167 if (osect.sct_own != oldown && oldown == player->cnum) {
1168 /* It was your sector, now it's not. Simple :) */
1169 pr("You no longer own %s\n",
1170 xyas(osect.sct_x, osect.sct_y, player->cnum));
1172 if (rel != ALLIED && sect.sct_own != actor && sect.sct_own) { /* must be a spy */
1173 /* Always a 10% chance of getting caught. */
1174 if (chance(LND_SPY_DETECT_CHANCE(llp->land.lnd_effic))) {
1175 if (rel == NEUTRAL || rel == FRIENDLY) {
1177 "%s unit spotted in %s\n", cname(player->cnum),
1178 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1179 setrel(sect.sct_own, llp->land.lnd_own, HOSTILE);
1180 } else if (rel == HOSTILE || rel == AT_WAR ||
1181 rel == SITZKRIEG || rel == MOBILIZATION) {
1183 "%s spy shot in %s\n", cname(player->cnum),
1184 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1185 pr("%s was shot and killed.\n", prland(&llp->land));
1186 llp->land.lnd_effic = 0;
1187 putland(llp->land.lnd_uid, &llp->land);
1195 lnd_sweep(list, 0, 1, actor);
1196 stopping |= lnd_check_mines(list);
1200 /* interdict land units sector by sector */
1203 while (!QEMPTY(list)) {
1204 llp = (struct llist *)list->q_back;
1205 newx = llp->land.lnd_x;
1206 newy = llp->land.lnd_y;
1207 /* move units in NEWX,NEWY to cur */
1209 for (qp = list->q_back; qp != list; qp = next) {
1211 llp = (struct llist *)qp;
1212 if (llp->land.lnd_x == newx && llp->land.lnd_y == newy) {
1214 emp_insque(qp, &cur);
1215 if (!(lchr[(int)llp->land.lnd_type].l_flags & L_SPY))
1219 /* interdict them */
1221 stopping |= lnd_interdict(&cur, newx, newy, actor);
1222 /* move survivors to done */
1223 for (qp = cur.q_back; qp != &cur; qp = next) {
1225 llp = (struct llist *)qp;
1227 emp_insque(qp, &done);
1230 /* assign surviving land units back to list */
1231 emp_insque(list, &done);
1238 * find all artillery units belonging
1239 * to the attacker or defender that can fire.
1240 * Each arty unit adds +1%/damage point
1244 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1246 struct nstr_item ni;
1253 double range, range2;
1255 snxtitem_all(&ni, EF_LAND);
1256 while (nxtitem(&ni, &land)) {
1257 if (land.lnd_frg == 0)
1259 if ((land.lnd_x == x) && (land.lnd_y == y))
1261 if (land.lnd_ship >= 0)
1263 if (land.lnd_land >= 0)
1265 if (land.lnd_mission > 0)
1267 if (land.lnd_effic < LAND_MINFIREEFF)
1269 /* Do we have mil? */
1270 if (land.lnd_item[I_MILIT] <= 0)
1272 rel = getrel(getnatp(land.lnd_own), attacker);
1273 rel2 = getrel(getnatp(land.lnd_own), victim);
1274 if ((land.lnd_own != attacker) &&
1275 ((rel != ALLIED) || (rel2 != AT_WAR)))
1278 /* do we have supplies? */
1279 if (!has_supply(&land))
1282 /* are we in range? */
1283 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1285 range = techfact((int)land.lnd_tech, (double)land.lnd_frg / 2.0);
1287 range2 = roundrange(range);
1291 shell = land.lnd_item[I_SHELL];
1292 gun = land.lnd_item[I_GUN];
1294 if (shell == 0 || gun == 0)
1299 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1301 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1302 if (roll(100) < land.lnd_acc) {
1303 dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1304 land.lnd_ammo, shell) / 2;
1306 dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1307 land.lnd_ammo, shell);
1309 if (land.lnd_own != attacker)
1311 "%s supported %s at %s\n",
1312 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1318 lnd_path(int together, struct lndstr *lp, s_char *buf)
1322 struct sctstr d_sect, sect;
1326 if (!sarg_xy(buf, &destx, &desty))
1329 pr("Cannot go to a destination sector if not all starting in the same sector\n");
1332 if (!getsect(destx, desty, &d_sect)) {
1333 pr("%d,%d is not a sector\n", destx, desty);
1336 getsect(lp->lnd_x, lp->lnd_y, §);
1337 if (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
1338 cp = BestLandPath(buf, §, &d_sect, &dummy, MOB_RAIL);
1340 cp = BestLandPath(buf, §, &d_sect, &dummy, MOB_ROAD);
1342 pr("No owned path from %s to %s!\n",
1343 xyas(lp->lnd_x, lp->lnd_y, player->cnum),
1344 xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
1347 pr("Using path '%s'\n", cp);
1352 lnd_can_attack(struct lndstr *lp)
1354 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1356 /* if (lcp->l_flags & L_SUPPLY ||
1357 lcp->l_flags & L_SECURITY ||
1358 lcp->l_flags & L_FLAK ||
1361 if (lcp->l_flags & L_SUPPLY)
1368 * Increase fortification value of LP.
1369 * Fortification costs mobility. Use up to HARD_AMT mobility.
1370 * Return actual fortification increase.
1373 lnd_fortify (struct lndstr *lp, int hard_amt)
1378 if ((lp->lnd_ship >= 0) || lp->lnd_land >= 0)
1381 hard_amt = MIN(lp->lnd_mobil, hard_amt);
1383 if ((lp->lnd_harden + hard_amt) > land_mob_max)
1384 hard_amt = land_mob_max - lp->lnd_harden;
1386 eng = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own);
1389 hard_amt = ((float)hard_amt * 1.5);
1391 if ((lp->lnd_harden + hard_amt) > land_mob_max)
1392 hard_amt = land_mob_max - lp->lnd_harden;
1394 /* Ok, set the mobility used */
1395 mob_used = hard_amt;
1397 /* Now, if an engineer helped, it's really only 2/3rds of
1400 mob_used = (int)((float)mob_used / 1.5);
1402 /* If we increased it, but not much, we gotta take at least 1
1404 if (mob_used <= 0 && hard_amt > 0)
1407 lp->lnd_mobil -= mob_used;
1408 if (lp->lnd_mobil < 0)
1411 lp->lnd_harden += hard_amt;
1412 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1418 * Set LP's tech to TLEV along with everything else that depends on it.
1421 lnd_set_tech(struct lndstr *lp, int tlev)
1423 struct lchrstr *lcp = lchr + lp->lnd_type;
1424 int tech_diff = tlev - lcp->l_tech;
1426 if (CANT_HAPPEN(tech_diff < 0)) {
1431 lp->lnd_tech = tlev;
1432 lp->lnd_att = (float)LND_ATTDEF(lcp->l_att, tech_diff);
1433 lp->lnd_def = (float)LND_ATTDEF(lcp->l_def, tech_diff);
1434 lp->lnd_vul = (int)LND_VUL(lcp->l_vul, tech_diff);
1435 lp->lnd_spd = (int)LND_SPD(lcp->l_spd, tech_diff);
1436 lp->lnd_vis = (int)LND_VIS(lcp->l_vis, tech_diff);
1437 lp->lnd_spy = (int)LND_SPY(lcp->l_spy, tech_diff);
1438 lp->lnd_rad = (int)LND_RAD(lcp->l_rad, tech_diff);
1439 lp->lnd_frg = (int)LND_FRG(lcp->l_frg, tech_diff);
1440 lp->lnd_acc = (int)LND_ACC(lcp->l_acc, tech_diff);
1441 lp->lnd_dam = (int)LND_DAM(lcp->l_dam, tech_diff);
1442 lp->lnd_ammo = (int)LND_AMM(lcp->l_ammo, lcp->l_dam, tech_diff);
1443 lp->lnd_aaf = (int)LND_AAF(lcp->l_aaf, tech_diff);
1444 lp->lnd_fuelc = (int)LND_FC(lcp->l_fuelc, tech_diff);
1445 lp->lnd_fuelu = (int)LND_FU(lcp->l_fuelu, tech_diff);
1446 lp->lnd_maxlight = (int)LND_XPL(lcp->l_nxlight, tech_diff);
1447 lp->lnd_maxland = (int)LND_MXL(lcp->l_nland, tech_diff);