2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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 * attsub.c: Attack subroutines
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
33 * Markus Armbruster, 2006-2009
50 #include "prototypes.h"
55 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
57 static void ask_olist(int combat_mode, struct combat *off,
58 struct combat *def, struct emp_qelem *olist,
59 char *land_answer, int *a_spyp, int *a_engineerp);
60 static void take_move_in_mob(int combat_mode, struct ulist *llp,
61 struct combat *off, struct combat *def);
62 static void move_in_land(int combat_mode, struct combat *off,
63 struct emp_qelem *olist, struct combat *def);
64 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
66 static void ask_move_in_off(struct combat *off, struct combat *def);
68 static int board_abort(struct combat *off, struct combat *def);
69 static int land_board_abort(struct combat *off, struct combat *def);
70 static int ask_off(int combat_mode, struct combat *off,
72 static void get_dlist(struct combat *def, struct emp_qelem *, int a_spy,
74 static int get_ototal(int combat_mode, struct combat *off,
75 struct emp_qelem *olist, double osupport, int check);
76 static int get_dtotal(struct combat *def, struct emp_qelem *list,
77 double dsupport, int check);
78 static double att_calcodds(int, int);
79 static int take_casualty(int combat_mode, struct combat *off,
80 struct emp_qelem *olist);
82 static void send_reacting_units_home(struct emp_qelem *list);
83 static int take_def(int combat_mode, struct emp_qelem *list,
84 struct combat *off, struct combat *def);
86 static int get_land(int combat_mode, struct combat *def,
87 struct ulist *llp, int victim_land);
90 /* must match combat types in combat.h */
91 "defend", "attack", "assault", "paradrop", "board", "lboard"
96 * The principal object in this code is the "combat" object. A combat object
97 * is either a sector or ship. There are
98 * usually two instances of this, the "def" or defense combat object, and
99 * the array of "off" or offense objects. The number of offense objects is
100 * determined by the value of off->last (e.g. more than one attacking sector).
101 * the type of the object is determined by combat->type which can take the
102 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
103 * which is often passed to these functions is combat_mode. This can take
104 * the value A_DEFEND, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
105 * As these six modes of being in combat affect things like mobcost and combat
106 * value, there are often switches made on combat_mode. Note that in all cases
107 * no mobility is taken from sectors, ships, or land units until the player
108 * has committed to a fight. Instead, the cost is temporarily placed in
109 * combat->mobcost, or llp->mobil as the case may be, and then when the object
110 * is "put" back onto disk, then the amounts in these variables are subtracted
111 * from the object's mobility. It needs to be done this way as the objects
112 * are constantly being re-read from disk, and we don't want to take any mob
113 * unless a fight actually occurrs.
117 /* initialize combat object */
120 att_combat_init(struct combat *com, int type)
122 memset(com, 0, sizeof(*com));
127 /* print a combat object with optional preposition */
130 pr_com(int inon, struct combat *com, natid who)
132 if (com->type == EF_SECTOR) {
134 inon ? inon == 1 ? "in " : "into " : "",
135 xyas(com->x, com->y, who));
136 } else if (com->type == EF_SHIP) {
137 return prbuf("%s%s %s(#%d)",
138 inon ? inon == 1 ? "on " : "onto " : "",
139 com->shp_mcp->m_name, com->shp_name, com->shp_uid);
140 } else if (com->type == EF_LAND) {
141 return prbuf("%s%s #%d",
142 inon ? inon == 1 ? "on " : "onto " : "",
143 com->lnd_lcp->l_name, com->lnd_uid);
145 return "your forces";
150 prcom(int inon, struct combat *com)
152 return pr_com(inon, com, player->cnum);
156 * This is the combat object "type" based integrity check. It basically
157 * splits along three divisions: ship/sector, attacker/defender,
158 * first time/not first time.
162 att_get_combat(struct combat *com, int isdef)
176 if (!getsect(com->x, com->y, §)) {
177 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
178 return att_combat_init(com, EF_BAD);
180 com->sct_type = sect.sct_type;
181 com->sct_dcp = &dchr[sect.sct_type];
182 mil = sect.sct_item[I_MILIT];
183 pstage = sect.sct_pstage;
184 owner = sect.sct_own;
185 eff = sect.sct_effic;
186 mob = sect.sct_mobil;
191 if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
193 pr("Land unit #%d is not in the same sector!\n",
195 return att_combat_init(com, EF_BAD);
197 if (isdef && player->owner) {
198 pr("Boarding yourself? Try using the 'load' command.\n");
199 return att_combat_init(com, EF_BAD);
201 com->lnd_lcp = &lchr[(int)land.lnd_type];
202 mil = land.lnd_item[I_MILIT];
203 pstage = land.lnd_pstage;
204 owner = land.lnd_own;
205 eff = land.lnd_effic;
206 mob = land.lnd_mobil;
211 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
213 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
215 pr("Ship #%d is not your ship!\n", com->shp_uid);
216 return att_combat_init(com, EF_BAD);
219 if (isdef && player->owner && ontradingblock(EF_SHIP, &ship)) {
220 pr("%s is on the trading block.\n", prcom(0, com));
221 return att_combat_init(com, EF_BAD);
224 if (isdef && player->owner) {
225 pr("Boarding yourself? Try using the 'tend' command.\n");
226 return att_combat_init(com, EF_BAD);
228 com->shp_mcp = &mchr[(int)ship.shp_type];
229 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
230 if (!isdef && !player->owner) {
232 pr("%s was just sunk!\n", prcom(0, com));
234 pr("Ship #%d is not your ship!\n", com->shp_uid);
235 return att_combat_init(com, EF_BAD);
237 mil = ship.shp_item[I_MILIT];
238 pstage = ship.shp_pstage;
239 owner = ship.shp_own;
240 eff = ship.shp_effic;
241 mob = ship.shp_mobil;
250 return att_combat_init(com, EF_BAD);
253 if (!com->set) { /* first time */
254 if (isdef) { /* defender */
256 } else { /* attacker */
258 pr("No mil %s\n", prcom(1, com));
260 pr("Only 1 mil %s\n", prcom(1, com));
261 /* don't abandon attacking sectors or ships */
262 com->troops = MAX(0, mil - 1);
264 com->plague = pstage == PLG_INFECT;
265 } else { /* not first time */
266 if (isdef) { /* defender */
267 if (com->x != x || com->y != y) {
268 pr("%s has moved!\n", prcom(0, com));
269 return att_combat_init(com, EF_BAD);
271 if (owner != com->own) {
273 pr("WARNING: The ownership of %s just changed from %s to %s!\n",
274 prcom(0, com), cname(com->own), cname(owner));
275 } else if (com->type == EF_SECTOR) {
276 pr("WARNING: %s just abandoned sector %s!\n",
278 xyas(com->x, com->y, player->cnum));
282 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
284 com->mil < mil ? "increased" : "decreased", com->mil,
287 } else { /* attacker */
288 if (owner != player->cnum
289 && getrel(getnatp(owner), player->cnum) != ALLIED) {
290 /* must be EF_SECTOR */
292 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
293 com->mil, xyas(com->x, com->y, player->cnum),
296 pr("You no longer own %s\n",
297 xyas(com->x, com->y, player->cnum));
298 return att_combat_init(com, EF_BAD);
300 if (com->troops && com->troops + 1 > mil) {
301 if (com->own == owner && player->cnum == owner)
303 pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
304 prcom(1, com), com->troops, MAX(0, mil - 1));
305 com->troops = MAX(0, mil - 1);
320 * In the course of the fight, the combat object may have lost mil, eff, or
321 * mobility. This is the place where the data in the object gets flushed to
322 * disk to make it "real".
326 put_combat(struct combat *com)
335 getsect(com->x, com->y, §);
336 sect.sct_type = com->sct_type;
337 deff = sect.sct_effic - com->eff;
339 sect.sct_road -= sect.sct_road * deff / 100.0;
340 sect.sct_rail -= sect.sct_rail * deff / 100.0;
341 sect.sct_defense -= sect.sct_defense * deff / 100.0;
342 if (sect.sct_road <= 0)
344 if (sect.sct_rail <= 0)
346 if (sect.sct_defense <= 0)
347 sect.sct_defense = 0;
349 sect.sct_effic = com->eff;
351 if (opt_MOB_ACCESS) {
352 if ((com->mob - com->mobcost) < -127)
353 sect.sct_mobil = -127;
355 sect.sct_mobil = com->mob - com->mobcost;
357 if ((com->mob - com->mobcost) < 0)
360 sect.sct_mobil = com->mob - com->mobcost;
363 sect.sct_own = com->own;
365 if (sect.sct_pstage == PLG_HEALTHY)
366 sect.sct_pstage = PLG_EXPOSED;
368 sect.sct_item[I_MILIT] = com->mil;
370 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
373 getland(com->lnd_uid, &land);
374 land.lnd_effic = com->eff;
376 if (com->mob - com->mobcost < -127)
377 land.lnd_mobil = -127;
379 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
381 land.lnd_own = com->own;
383 if (land.lnd_pstage == PLG_HEALTHY)
384 land.lnd_pstage = PLG_EXPOSED;
386 if (!(com->lnd_lcp->l_flags & L_SPY))
387 land.lnd_item[I_MILIT] = com->mil;
388 if (com->own == player->cnum) {
389 land.lnd_mission = 0;
391 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
393 putland(com->lnd_uid, &land);
396 getship(com->shp_uid, &ship);
397 ship.shp_effic = com->eff;
399 if (com->mob - com->mobcost < -127)
400 ship.shp_mobil = -127;
402 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
404 ship.shp_own = com->own;
406 if (ship.shp_pstage == PLG_HEALTHY)
407 ship.shp_pstage = PLG_EXPOSED;
409 ship.shp_item[I_MILIT] = com->mil;
410 if (com->own == player->cnum) {
411 ship.shp_mission = 0;
413 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
415 putship(com->shp_uid, &ship);
418 att_get_combat(com, com->own != player->cnum);
421 /* If pre-attack, abort fight. If post-attack, don't move anything in */
426 return player->aborted = 1;
430 * This is the combat_mode based integrity check. It splits among two main
431 * divisions: first time/not first time, and attack/assault/para/board.
435 att_abort(int combat_mode, struct combat *off, struct combat *def)
444 if (att_get_combat(def, 1) < 0)
445 return abort_attack();
447 if (off && combat_mode != A_ATTACK) {
448 if (att_get_combat(off, 0) < 0)
449 return abort_attack();
450 if (off->type == EF_SHIP &&
451 !(off->x == def->x && off->y == def->y) &&
452 (!getsect(off->x, off->y, §) ||
453 sect.sct_type != SCT_WATER)) {
454 pr("%s can not %s from that far inland!\n",
455 prcom(0, off), att_mode[combat_mode]);
456 return abort_attack();
459 switch (combat_mode) {
461 if (!neigh(def->x, def->y, player->cnum) &&
462 !adj_units(def->x, def->y, player->cnum)) {
463 pr("You are not adjacent to %s\n",
464 xyas(def->x, def->y, player->cnum));
465 return abort_attack();
467 if (def->own == player->cnum) {
468 pr("You can't attack your own sector.\n");
469 return abort_attack();
473 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
474 pr("You'll have to get there first...\n");
475 return abort_attack();
477 if (off && def->sct_type == SCT_MOUNT) {
478 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
479 return abort_attack();
483 if (def->own == player->cnum) {
484 pr("You can't air-assault your own sector.\n");
485 return abort_attack();
487 if (off && (def->sct_type == SCT_MOUNT ||
488 def->sct_type == SCT_WATER ||
489 def->sct_type == SCT_CAPIT ||
490 def->sct_type == SCT_FORTR ||
491 def->sct_type == SCT_WASTE)) {
492 pr("You can't air-assault a %s sector!\n",
493 def->sct_dcp->d_name);
494 return abort_attack();
498 return board_abort(off, def);
500 return land_board_abort(off, def);
503 if (off && def->sct_dcp->d_mob0 < 0) {
504 pr("You can't %s a %s sector!\n",
505 att_mode[combat_mode], def->sct_dcp->d_name);
506 return abort_attack();
508 if (!off || off->relations_checked)
510 off->relations_checked = 1;
513 setcont(player->cnum, def->own, FOUND_SPY);
514 setcont(def->own, player->cnum, FOUND_SPY);
516 if (opt_SLOW_WAR && def->own != player->cnum) {
517 natp = getnatp(player->cnum);
518 rel = getrel(natp, def->own);
521 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
522 cname(def->own), att_mode[combat_mode]);
523 if (!confirm(y_or_n))
524 return abort_attack();
527 if ((rel != AT_WAR) && (def->own) &&
528 (sect.sct_oldown != player->cnum)) {
529 pr("You're not at war with them!\n");
530 return abort_attack();
537 * Lots of special things need to be checked for boarding, so I put it in
542 board_abort(struct combat *off, struct combat *def)
544 struct shpstr aship, dship; /* for tech levels */
547 if (att_get_combat(def, 1) < 0)
548 return abort_attack();
553 if (att_get_combat(off, 0) < 0)
554 return abort_attack();
556 if (off->x != def->x || off->y != def->y) {
557 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
558 return abort_attack();
560 if (off->type == EF_SHIP) {
562 pr("%s has no mobility!\n", prcom(0, off));
563 return abort_attack();
565 getship(off->shp_uid, &aship);
566 getship(def->shp_uid, &dship);
567 if (techfact(aship.shp_tech, shp_speed(&aship)) * off->eff
568 <= techfact(dship.shp_tech, shp_speed(&dship)) * def->eff) {
569 pr("Victim ship moves faster than you do!\n");
572 "%s (#%d) %s failed to catch %s\n",
573 cname(aship.shp_own), aship.shp_own,
574 pr_com(0, off, def->own), pr_com(0, def, def->own));
575 return abort_attack();
577 } else if (off->type != EF_SECTOR) {
578 pr("Please tell the deity that you got the 'banana boat' error\n");
579 return abort_attack();
581 if (def->shp_mcp->m_flags & M_SUB) {
582 getsect(def->x, def->y, §);
583 if (sect.sct_type == SCT_WATER) {
584 pr("You can't board a submarine!\n");
585 return abort_attack();
592 * Lots of special things need to be checked for boarding, so I put it in
594 * STM - I copied it for land unit boarding. :)
598 land_board_abort(struct combat *off, struct combat *def)
600 if (att_get_combat(def, 1) < 0)
601 return abort_attack();
606 if (att_get_combat(off, 0) < 0)
607 return abort_attack();
609 if (off->x != def->x || off->y != def->y) {
610 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
611 return abort_attack();
617 /* If we are boarding, then the defending ship gets a chance to fire back */
619 att_approach(struct combat *off, struct combat *def)
625 pr("Approaching %s...\n", prcom(0, def));
628 "%s is being approached by %s...\n",
629 pr_com(0, def, def->own), pr_com(0, off, def->own));
630 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
633 pr("They're firing at us sir!\n");
636 "Your fleet at %s does %d damage to %s\n",
637 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
639 if (off->type == EF_SECTOR) {
640 getsect(off->x, off->y, §);
641 sectdamage(§, dam);
643 pr("Enemy fleet at %s does %d damage to %s\n",
644 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
645 } else if (off->type == EF_SHIP) {
646 getship(off->shp_uid, &ship);
647 shipdamage(&ship, dam);
648 putship(off->shp_uid, &ship);
649 if (def->own && ship.shp_effic < SHIP_MINEFF) {
650 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
651 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
654 if (att_get_combat(off, 0) < 0)
655 return abort_attack();
659 /* The attack is valid. Tell the attacker about what they're going to hit */
662 att_show(struct combat *def)
664 /* Note that we tell the player about the treaty BEFORE we tell them
665 about the item. If we didn't, then they gain free information */
666 if (def->type == EF_SECTOR) {
667 if (!trechk(player->cnum, def->own, LANATT))
668 return abort_attack();
669 pr("%s is a %d%% %s %s with approximately %d military.\n",
670 xyas(def->x, def->y, player->cnum),
671 roundintby((int)def->eff, 10),
672 cname(def->own), def->sct_dcp->d_name,
673 roundintby(def->troops, 10));
674 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
675 writemap(player->cnum);
676 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
677 if (def->type == EF_SHIP) {
678 if (!trechk(player->cnum, def->own, SEAATT))
679 return abort_attack();
681 if (!trechk(player->cnum, def->own, LNDATT))
682 return abort_attack();
684 pr("%s is about %d%% efficient and has approximately %d mil on board.\n",
685 prcom(0, def), roundintby((int)def->eff, 10),
686 roundintby(def->troops, 10));
688 /* Ok, everything is fine */
692 /* Attack and assault ask the user which kind of support they want */
695 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
700 *fortp = *shipp = *landp = *planep = 1;
702 if (player->argp[offset] != NULL) {
703 if ((player->argp[offset + 1] == NULL) ||
704 (player->argp[offset + 2] == NULL) ||
705 (player->argp[offset + 3] == NULL)) {
706 pr("If any support arguments are used, all must be!\n");
711 *landp = *planep = 0;
713 p = getstarg(player->argp[offset], "Use fort support? ", buf);
717 if ((*p == 'y') || (*p == 'Y'))
720 p = getstarg(player->argp[offset + 1], "Use ship support? ", buf);
724 if ((*p == 'y') || (*p == 'Y'))
727 p = getstarg(player->argp[offset + 2], "Use land support? ", buf);
731 if ((*p == 'y') || (*p == 'Y'))
734 p = getstarg(player->argp[offset + 3], "Use plane support? ", buf);
738 if ((*p == 'y') || (*p == 'Y'))
745 * Attack, assault, and board ask the attacker what they'd like to attack
746 * with. This includes mil and land units from each "off" object. Note that
747 * after each sub-prompt, we check to make sure that the attack is still
748 * valid, and if it's not, then we abort the attack.
752 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
753 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
756 char land_answer[256];
759 if (att_abort(combat_mode, off, def))
761 memset(land_answer, 0, sizeof(land_answer));
762 for (n = 0; n <= off->last; ++n) {
763 off[n].troops = ask_off(combat_mode, off + n, def);
764 if (att_abort(combat_mode, off, def))
766 ask_olist(combat_mode, off + n, def, olist, land_answer,
767 a_spyp, a_engineerp);
768 if (att_abort(combat_mode, off, def))
775 * Return path cost for ATTACKER to enter sector given by DEF.
776 * MOBTYPE is a mobility type accepted by sector_mcost().
779 att_mobcost(natid attacker, struct combat *def, int mobtype)
784 if (CANT_HAPPEN(def->type != EF_SECTOR))
786 ok = getsect(def->x, def->y, §);
787 if (CANT_HAPPEN(!ok))
791 * We want the cost to move/march into the sector. If we just
792 * called sector_mcost(), we'd get the defender's cost. The
793 * attacker's cost is higher unless he's the old-owner. Note: if
794 * there are no civilians, a victorious attacker will become the
795 * old-owner. But he isn't now.
797 sect.sct_own = attacker;
799 return sector_mcost(§, mobtype);
802 /* How many mil is off allowed to attack with when it attacks def? */
805 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
810 switch (combat_mode) {
812 mobcost = att_mobcost(off->own, def, MOB_MOVE);
813 if (mobcost < 0 || off->mob <= 0)
815 mob_support = off->mob / mobcost;
816 if (mob_support < off->troops)
817 pr("Sector %s has %d mobility which can only support %d mil,\n",
818 xyas(off->x, off->y, player->cnum), off->mob, mob_support);
820 mob_support = off->troops;
823 if (def->own != player->cnum && def->mil) {
824 if (off->shp_mcp->m_flags & M_SEMILAND)
825 return off->troops / 4;
826 else if (!(off->shp_mcp->m_flags & M_LAND))
827 return off->troops / 10;
831 if (off->type == EF_SECTOR && off->mob <= 0)
833 mob_support = def->shp_mcp->m_item[I_MILIT];
834 if (mob_support < off->troops)
835 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
837 mob_support = off->troops;
842 if (def->lnd_lcp->l_flags & L_SPY)
844 mob_support = def->lnd_lcp->l_item[I_MILIT];
845 if (mob_support < off->troops)
846 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
848 mob_support = off->troops;
855 * If the attacker decides to go through with the attack, then the
856 * sectors/ships they are attacking with may be charged some mobility.
857 * This is where that amount of mobility is calculated. It is actually
858 * subtracted "for real" from the object's mobility in put_combat().
862 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
869 switch (combat_mode) {
871 off->mobcost += MAX(1,
873 * att_mobcost(off->own, def, MOB_MOVE)));
876 off->mobcost += MAX(1, attacking_mil / 5);
881 off->mobcost += MAX(1, attacking_mil / 5);
884 /* the 2 in the formula below is a fudge factor */
885 getship(def->shp_uid, &ship);
886 off->mobcost += (def->eff / 100) * (shp_speed(&ship) / 2);
891 /* How many mil to we want to attack from off against def? */
894 ask_off(int combat_mode, struct combat *off, struct combat *def)
900 if (att_get_combat(off, 0) <= 0)
902 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
904 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
906 if (off->type == EF_SECTOR) {
907 if (off->own != player->cnum)
909 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
910 off->sct_dcp->d_name,
911 xyas(off->x, off->y, player->cnum), mob_support);
913 sprintf(prompt, "Number of mil from %s (max %d) : ",
914 prcom(0, off), mob_support);
916 if ((attacking_mil = onearg(NULL, prompt)) < 0)
918 if (att_abort(combat_mode, off, def))
920 if (att_get_combat(off, 0) <= 0)
923 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
926 calc_mobcost(combat_mode, off, def, attacking_mil);
927 return attacking_mil;
931 * Which units would you like to attack with or move in with [ynYNq?]
935 att_prompt(char *prompt, char army)
943 p = getstring(prompt, buf);
944 if (!p || *p == 'q') {
950 if (tolower(*p) == 'y' || tolower(*p) == 'n')
952 pr("y - yes this unit\n"
954 "Y - yes to all units in army '%c'\n"
955 "N - no to all units in army '%c'\n"
956 "q - quit\n? - this help message\n\n",
961 /* Ask the attacker which units they want to attack/assault/board with */
964 ask_olist(int combat_mode, struct combat *off, struct combat *def,
965 struct emp_qelem *olist, char *land_answer, int *a_spyp,
970 double pathcost, mobcost;
980 if (def->type == EF_LAND)
982 if (def->type == EF_SHIP)
983 maxland = def->shp_mcp->m_nland;
985 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
986 while (nxtitem(&ni, &land)) {
987 if (land.lnd_own != player->cnum)
989 if (land.lnd_effic < LAND_MINEFF)
991 if (land_answer[(int)land.lnd_army] == 'N')
993 if (!lnd_can_attack(&land))
995 lcp = &lchr[(int)land.lnd_type];
997 if (def->type == EF_SHIP && !maxland) {
998 pr("Land units are not able to board this kind of ship\n");
1001 if (def->type == EF_SHIP
1002 && (def->shp_mcp->m_flags & (M_SUPPLY | M_SUB)) != M_SUPPLY
1003 && !(lcp->l_flags & L_LIGHT)) {
1004 pr("Only light land units can board this kind of ship\n");
1007 if (land.lnd_mobil <= 0) {
1008 pr("%s is out of mobility, and cannot %s\n",
1009 prland(&land), att_mode[combat_mode]);
1014 if (ontradingblock(EF_LAND, &land)) {
1015 pr("%s is on the trading block, and cannot %s\n",
1016 prland(&land), att_mode[combat_mode]);
1021 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1022 pr("%s is on ship #%d, and cannot %s\n",
1023 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1025 } else if (off->type == EF_SHIP) {
1026 if (land.lnd_ship != off->shp_uid)
1028 } else if (land.lnd_land >= 0) {
1029 pr("%s is on unit #%d, and cannot %s\n",
1030 prland(&land), land.lnd_land, att_mode[combat_mode]);
1033 switch (combat_mode) {
1036 * We used to let land units attack only if they have the
1037 * mobility consumed by the attack, not counting combat
1038 * and moving in to occupy. Making sure your land units
1039 * reach attack positions with enough mobility left is a
1040 * pain in the neck. We now require positive mobility,
1041 * just like for marching. Except we don't allow rushing
1042 * of high-mobility sectors (mountains): for those we
1043 * still require attack mobility.
1045 pathcost = att_mobcost(off->own, def, lnd_mobtype(&land));
1046 mobcost = lnd_pathcost(&land, pathcost);
1047 if (pathcost < 1.0) {
1048 if (land.lnd_mobil <= 0) {
1049 pr("%s is out of mobility\n", prland(&land));
1053 reqmob = MIN(land_mob_max, (int)ceil(mobcost));
1054 if (land.lnd_mobil < reqmob) {
1055 pr("%s does not have enough mobility (%d needed)\n",
1056 prland(&land), reqmob);
1064 if (!(lcp->l_flags & L_ASSAULT))
1071 att_val = attack_val(combat_mode, &land);
1072 if (att_val < 1.0) {
1073 pr("%s has no offensive strength\n", prland(&land));
1076 if (!lnd_supply_all(&land)) {
1077 pr("%s is out of supply, and cannot %s\n",
1078 prland(&land), att_mode[combat_mode]);
1081 if (def->type == EF_SHIP && first_time) {
1083 pr("You may board with a maximum of %d land units\n", maxland);
1085 pr("%s has a base %s value of %.0f\n",
1086 prland(&land), att_mode[combat_mode], att_val);
1087 if (land_answer[(int)land.lnd_army] != 'Y') {
1089 "%s with %s %s (%c %d%%) [ynYNq?] ",
1090 att_mode[combat_mode],
1093 land.lnd_army ? land.lnd_army : '~',
1095 land_answer[(int)land.lnd_army] =
1096 att_prompt(prompt, land.lnd_army);
1097 if (att_abort(combat_mode, off, def))
1099 if (land_answer[(int)land.lnd_army] != 'y' &&
1100 land_answer[(int)land.lnd_army] != 'Y')
1103 if (!(llp = malloc(sizeof(struct ulist)))) {
1104 logerror("Malloc failed in attack!\n");
1108 memset(llp, 0, sizeof(struct ulist));
1109 emp_insque(&llp->queue, olist);
1110 llp->mobil = mobcost;
1111 llp->unit.land = land;
1112 llp->x = llp->unit.land.lnd_x;
1113 llp->y = llp->unit.land.lnd_y;
1114 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1115 llp->eff = llp->unit.land.lnd_effic;
1116 if (lnd_spyval(&land) > *a_spyp)
1117 *a_spyp = lnd_spyval(&land);
1118 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
1120 if (def->type == EF_SHIP && ++count >= maxland)
1125 /* What's the offense or defense multiplier? */
1128 att_combat_eff(struct combat *com)
1134 if (com->type == EF_SECTOR) {
1135 eff = com->eff / 100.0;
1136 if (com->own == player->cnum) {
1137 str = com->sct_dcp->d_ostr;
1138 eff = 1.0 + (str - 1.0) * eff;
1140 eff = sector_strength(getsectp(com->x, com->y));
1141 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1142 getship(com->shp_uid, &ship);
1143 eff = 1.0 + shp_armor(&ship) / 100.0;
1149 att_get_offense(int combat_mode, struct combat *off,
1150 struct emp_qelem *olist, struct combat *def)
1155 * Get the attacker units & mil again in case they changed while the
1156 * attacker was answering sub-prompts.
1159 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1160 if (att_empty_attack(combat_mode, ototal, def))
1161 return abort_attack();
1162 if (combat_mode == A_PARA)
1164 pr("\n Initial attack strength: %8d\n", ototal);
1168 /* Get the defensive units and reacting units */
1170 att_get_defense(struct emp_qelem *olist, struct combat *def,
1171 struct emp_qelem *dlist, int a_spy, int ototal)
1174 struct emp_qelem *qp;
1180 get_dlist(def, dlist, 0, &d_spy);
1181 dtotal = get_dtotal(def, dlist, 1.0, 0);
1184 * Call in reacting units
1187 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1188 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1190 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1191 llp = (struct ulist *)qp;
1192 intelligence_report(def->own, &llp->unit.land, d_spy,
1193 "Scouts report attacking unit:");
1196 old_dtotal = dtotal;
1197 dtotal = get_dtotal(def, dlist, 1.0, 0);
1198 if (dtotal != old_dtotal)
1199 pr("Defense strength with reacting units: %8d\n", dtotal);
1204 /* Get the defensive land units in the sector or on the ship */
1207 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1210 struct nstr_item ni;
1214 /* In here is where you need to take out spies and trains from the defending
1215 lists. Spies try to hide, trains get trapped and can be boarded. */
1217 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1218 while (nxtitem(&ni, &land)) {
1221 if (land.lnd_own != def->own)
1223 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1225 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1227 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1229 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1231 intelligence_report(player->cnum, &land, a_spy,
1232 "Scouts report defending unit:");
1233 if (!(llp = malloc(sizeof(struct ulist)))) {
1234 logerror("Malloc failed in attack!\n");
1238 memset(llp, 0, sizeof(struct ulist));
1239 emp_insque(&llp->queue, list);
1240 llp->supplied = lnd_supply_all(&land);
1241 llp->unit.land = land;
1242 llp->x = llp->unit.land.lnd_x;
1243 llp->y = llp->unit.land.lnd_y;
1244 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1245 llp->eff = llp->unit.land.lnd_effic;
1246 if (lnd_spyval(&land) > *d_spyp)
1247 *d_spyp = lnd_spyval(&land);
1251 /* Calculate the total offensive strength */
1254 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1255 double osupport, int check)
1257 double ototal = 0.0;
1258 struct emp_qelem *qp, *next;
1263 * first, total the attacking mil
1266 for (n = 0; n <= off->last; ++n) {
1267 if (off[n].type == EF_BAD || (check &&
1268 att_get_combat(&off[n], 0) <= 0))
1270 ototal += off[n].troops * att_combat_eff(off + n);
1274 * next, add in the attack_values of all
1275 * the attacking units
1278 for (qp = olist->q_forw; qp != olist; qp = next) {
1280 llp = (struct ulist *)qp;
1282 !get_land(combat_mode, NULL, llp, 0))
1284 if (combat_mode == A_ATTACK) {
1286 for (n = 0; n <= off->last; ++n) {
1287 if (off[n].type == EF_BAD)
1289 if ((off[n].x == llp->unit.land.lnd_x) &&
1290 (off[n].y == llp->unit.land.lnd_y))
1294 lnd_delete(llp, "is in a sector not owned by you");
1297 ototal += attack_val(combat_mode, &llp->unit.land) *
1298 att_combat_eff(off + w);
1300 ototal += attack_val(combat_mode, &llp->unit.land);
1305 return ldround(ototal, 1);
1308 /* Calculate the total defensive strength */
1311 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1314 double dtotal = 0.0, eff = 1.0, d_unit;
1315 struct emp_qelem *qp, *next;
1318 if (check && att_get_combat(def, 1) < 0)
1320 eff = att_combat_eff(def);
1321 dtotal = def->troops * eff;
1324 * next, add in the defense_values of all
1325 * the defending non-retreating units
1328 for (qp = list->q_forw; qp != list; qp = next) {
1330 llp = (struct ulist *)qp;
1331 if (check && !get_land(A_DEFEND, def, llp, 1))
1333 d_unit = defense_val(&llp->unit.land);
1336 dtotal += d_unit * eff;
1341 return ldround(dtotal, 1);
1345 * This is the land unit integrity check. Note that we don't print
1346 * warnings about victim land units because the attacker may not have seen them
1350 get_land(int combat_mode, struct combat *def, struct ulist *llp,
1353 struct lndstr *lp = &llp->unit.land;
1356 getland(llp->unit.land.lnd_uid, lp);
1358 if (lp->lnd_effic < LAND_MINEFF) {
1359 sprintf(buf, "was destroyed and is no longer a part of the %s",
1360 att_mode[combat_mode]);
1361 lnd_delete(llp, buf);
1365 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1367 "left to go fight another battle and is no longer a part of the defense");
1371 if (lp->lnd_own != player->cnum) {
1373 "was destroyed and is no longer a part of the %s",
1374 att_mode[combat_mode]);
1375 lnd_delete(llp, buf);
1378 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1380 "left to fight another battle and is no longer a part of the %s",
1381 att_mode[combat_mode]);
1382 lnd_delete(llp, buf);
1385 if (lp->lnd_effic < llp->eff) {
1386 sprintf(buf, "damaged from %d%% to %d%%",
1387 llp->eff, lp->lnd_effic);
1388 lnd_print(llp, buf);
1391 llp->eff = llp->unit.land.lnd_effic;
1397 * Put the land unit on the disk. If there was some mobility cost, then
1398 * subtract it from the units mobility. Note that this works the same way
1399 * as sectors & ships in that no mobility is actually taken until the attacker
1400 * has committed to attacking.
1404 kill_land(struct emp_qelem *list)
1406 struct emp_qelem *qp, *next;
1409 for (qp = list->q_forw; qp != list; qp = next) {
1411 llp = (struct ulist *)qp;
1412 if (llp->unit.land.lnd_ship >= 0) {
1413 llp->unit.land.lnd_effic = 0;
1414 lnd_delete(llp, "cannot return to the ship, and dies!");
1420 att_infect_units(struct emp_qelem *list, int plague)
1422 struct emp_qelem *qp, *next;
1427 for (qp = list->q_forw; qp != list; qp = next) {
1429 llp = (struct ulist *)qp;
1430 if (llp->unit.land.lnd_pstage == PLG_HEALTHY)
1431 llp->unit.land.lnd_pstage = PLG_EXPOSED;
1436 put_land(struct emp_qelem *list)
1438 struct emp_qelem *qp, *next;
1441 for (qp = list->q_forw; qp != list; qp = next) {
1443 llp = (struct ulist *)qp;
1444 llp->unit.land.lnd_mission = 0;
1445 llp->unit.land.lnd_harden = 0;
1446 llp->unit.land.lnd_mobil -= (int)llp->mobil;
1448 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1449 if (llp->unit.land.lnd_own != player->cnum) {
1450 emp_remque((struct emp_qelem *)llp);
1453 get_land(A_ATTACK, NULL, llp, 0);
1458 * Keep sending in reinforcements until it looks like we're going to win.
1459 * Note that the "strength" command also calls this routine.
1463 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1464 int *d_spyp, int ototal)
1466 struct nstr_item ni;
1468 struct sctstr sect, dsect;
1471 double new_land = 0;
1475 double eff = att_combat_eff(def);
1479 dtotal = get_dtotal(def, list, 1.0, 1);
1482 snxtitem_all(&ni, EF_LAND);
1483 while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1486 if (land.lnd_mission != MI_RESERVE)
1488 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1490 if (land.lnd_own != def->own)
1492 if (land.lnd_ship >= 0)
1494 if (land.lnd_land >= 0)
1496 if (defense_val(&land) < 1.0)
1498 if (!lnd_can_attack(&land))
1501 /* Only supplied units can react */
1502 if (list ? !lnd_supply_all(&land) : !lnd_could_be_supplied(&land))
1505 if (!in_oparea((struct empobj *)&land, def->x, def->y))
1508 getsect(land.lnd_x, land.lnd_y, §);
1509 getsect(def->x, def->y, &dsect);
1510 if (!BestLandPath(buf, §, &dsect, &pathcost,
1511 lnd_mobtype(&land)))
1514 mobcost = lnd_pathcost(&land, pathcost);
1515 if (land.lnd_mobil < mobcost)
1518 new_land += defense_val(&land);
1520 if (!list) /* we are in the "strength" command */
1523 /* move to defending sector */
1524 land.lnd_mobil -= ldround(mobcost, 1);
1527 land.lnd_x = def->x;
1528 land.lnd_y = def->y;
1529 putland(land.lnd_uid, &land);
1530 wu(0, land.lnd_own, "%s reacts to %s.\n",
1531 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1533 llp = malloc(sizeof(struct ulist));
1535 memset(llp, 0, sizeof(struct ulist));
1539 llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
1540 llp->unit.land = land;
1541 llp->eff = land.lnd_effic;
1542 emp_insque(&llp->queue, list);
1543 if (lnd_spyval(&land) > *d_spyp)
1544 *d_spyp = lnd_spyval(&land);
1546 intelligence_report(player->cnum, &land, a_spy,
1547 "Scouts sight reacting enemy unit:");
1552 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1555 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1556 int land_sup, int plane_sup)
1558 double osupport = 1.0;
1560 double af, as, au, ap;
1562 af = as = au = ap = 0.0;
1564 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1569 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1576 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1582 dam = off_support(def->x, def->y, def->own, player->cnum);
1586 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1591 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1594 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1595 int ototal, int dtotal)
1597 double dsupport = 1.0;
1599 double df, ds, du, dp;
1602 df = ds = du = dp = 0.0;
1603 if (dtotal < 0.1 * ototal) {
1605 } else if (dtotal >= 1.2 * ototal) {
1608 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1612 dtotal = get_dtotal(def, list, dsupport, 0);
1613 if (dtotal < 1.2 * ototal) {
1614 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1617 dtotal = get_dtotal(def, list, dsupport, 0);
1619 if (dtotal < 1.2 * ototal) {
1620 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1623 dtotal = get_dtotal(def, list, dsupport, 1);
1625 if (dtotal < 1.2 * ototal) {
1626 dam = def_support(def->x, def->y, player->cnum, def->own);
1634 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1639 "\nOdds are bad for us...support cancelled.\n\n");
1642 "\nOdds are good for us...support cancelled.\n\n");
1648 * Land mines add to the defense multiplier. If the attacker has engineers
1649 * then this multiplier is cut in half.
1653 get_mine_dsupport(struct combat *def, int a_engineer)
1658 getsect(def->x, def->y, §);
1660 if (sect.sct_oldown != player->cnum) {
1661 mines = SCT_LANDMINES(§);
1662 mines = MIN(mines, 20);
1664 mines = ldround(mines / 2.0, 1);
1667 wu(0, def->own, "Defending mines add %1.2f\n",
1669 pr("Defending mines add %1.2f\n", mines * 0.02);
1670 return mines * 0.02;
1676 /* Get the offensive and defensive support */
1678 att_get_support(int combat_mode, int ofort, int oship, int oland,
1679 int oplane, struct emp_qelem *olist, struct combat *off,
1680 struct emp_qelem *dlist, struct combat *def,
1681 double *osupportp, double *dsupportp, int a_engineer)
1684 char osupports[512];
1685 char dsupports[512];
1687 if (combat_mode == A_PARA)
1690 *osupportp = get_osupport(osupports, def,
1691 ofort, oship, oland, oplane);
1694 * I need to put a 1 at the end of the next four total_stren calls
1695 * because units & mil may have been damaged by collateral damage or
1696 * nuclear warheads from the offensive & defensive support.
1699 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1700 if (att_empty_attack(combat_mode, ototal, def))
1701 return abort_attack();
1702 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1705 * Calculate defensive support. If odds are too good or too bad
1706 * then don't call in support.
1709 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1710 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1711 if (att_empty_attack(combat_mode, ototal, def))
1712 return abort_attack();
1714 if ((*osupports || *dsupports) &&
1715 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1716 pr("\n\t\tsupport values\n");
1717 pr("\t\tforts\tships\tunits\tplanes\n");
1718 if (*osupportp != 1.0)
1719 pr("%s", osupports);
1720 if (*dsupportp != 1.0)
1721 pr("%s", dsupports);
1723 wu(0, def->own, "\n\t\tsupport values\n");
1724 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1725 if (*osupportp != 1.0)
1726 wu(0, def->own, "%s", osupports);
1727 if (*dsupportp != 1.0)
1728 wu(0, def->own, "%s", dsupports);
1732 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1733 if (dtotal && def->type == EF_SECTOR)
1734 *dsupportp += get_mine_dsupport(def, a_engineer);
1738 /* How many two-legged bipeds are in this combat force? */
1741 count_bodies(struct combat *off, struct emp_qelem *list)
1745 struct emp_qelem *qp;
1748 for (n = 0; n <= off->last; ++n)
1749 bodies += off[n].troops;
1750 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1751 llp = (struct ulist *)qp;
1752 bodies += llp->unit.land.lnd_item[I_MILIT];
1757 /* This is where the fighting actually occurs. */
1760 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1761 double osupport, struct combat *def, struct emp_qelem *dlist,
1765 int a_cas = 0; /* Casualty counts */
1767 int ototal; /* total attacking strength */
1768 int dtotal; /* total defending strength */
1769 int a_bodies; /* total attacking mil (incl. mil in units) */
1770 int d_bodies; /* total defending mil (incl. mil in units) */
1780 ototal = get_ototal(combat_mode, off, olist, osupport,
1781 combat_mode != A_PARA);
1782 dtotal = get_dtotal(def, dlist, dsupport, 0);
1786 a_bodies = count_bodies(off, olist);
1787 d_bodies = count_bodies(def, dlist);
1788 d_mil = def->troops;
1789 for (n = 0; n <= off->last; ++n)
1790 if (off[n].type == EF_BAD)
1793 a_troops[n] = off[n].troops;
1795 /* This switch is required to get the spacing right */
1796 switch (combat_mode) {
1798 pr(" Final attack strength: %8d\n", ototal);
1801 pr(" Final assault strength: %8d\n", ototal);
1804 if (def->sct_type == SCT_MOUNT ||
1805 def->sct_type == SCT_WATER ||
1806 def->sct_type == SCT_CAPIT ||
1807 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1808 pr("You can't air-assault a %s sector!\n",
1809 def->sct_dcp->d_name);
1812 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1814 pr(" Final air-assault strength: %8d\n", ototal);
1818 pr(" Final board strength: %8d\n", ototal);
1822 pr(" Final defense strength: %8d\n", dtotal);
1823 odds = att_calcodds(ototal, dtotal);
1824 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1826 /* spread the plague */
1827 if (combat_mode != A_PARA) {
1829 for (n = 0; n <= off->last; ++n)
1830 if (off[n].type != EF_BAD)
1831 def->plague |= off[n].plague;
1832 for (n = 0; n <= off->last; ++n)
1833 if (off[n].type != EF_BAD)
1834 off[n].plague |= def->plague;
1836 att_infect_units(olist, off->plague);
1837 att_infect_units(dlist, def->plague);
1840 * Fighting is slightly random. There is always that last little
1841 * effort you see people put in. Or the stray bullet that takes out
1842 * an officer and the rest go into chaos. Things like that.
1843 * Thus, we have added a very slight random factor that will sometimes
1844 * allow the little guy to win. We modify the odds a little
1845 * (either +- 5%) to account for this randomness. We also only
1846 * recalculate the odds every 8-50 casualties, not every cacsualty,
1847 * since a single dead guy normally wouldn't cause a commander to
1848 * rethink his strategies, but 50 dead guys might.
1850 odds += (random() % 11 - 5) / 100.0;
1855 recalctime = 8 + (random() % 43);
1856 while (!success && ototal) {
1859 d_cas += take_casualty(A_DEFEND, def, dlist);
1860 dtotal = get_dtotal(def, dlist, dsupport, 0);
1865 a_cas += take_casualty(combat_mode, off, olist);
1866 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1868 if (((a_cas + d_cas) % 70) == 69)
1870 if (recalctime-- <= 0) {
1871 recalctime = 8 + (random() % 43);
1872 odds = att_calcodds(ototal, dtotal);
1873 odds += (random() % 11 - 5) / 100.0;
1881 /* update defense mobility & mil */
1885 if (def->type == EF_SECTOR && d_mil && d_cas) {
1889 newmob = damage(def->mob, 100 * d_cas / d_mil);
1890 def->mobcost = MIN(20, def->mob - newmob);
1893 def->mil = def->troops;
1896 /* update attack mobility & mil */
1897 for (n = 0; n <= off->last; ++n)
1898 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1899 if (off[n].type == EF_SECTOR && off[n].mil) {
1900 if (!CANT_HAPPEN(off[n].mob < 0)) {
1901 newmob = damage(off[n].mob,
1902 100 * (a_troops[n] - off[n].troops)
1904 off[n].mobcost += MIN(20, off[n].mob - newmob);
1907 off[n].mil -= a_troops[n] - off[n].troops;
1910 /* update land unit mobility */
1911 if (d_bodies && d_cas)
1912 lnd_takemob(dlist, (double)d_cas / d_bodies);
1913 if (a_bodies && a_cas)
1914 lnd_takemob(olist, (double)a_cas / a_bodies);
1916 /* damage attacked sector */
1917 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1919 pr("- Casualties -\n Yours: %d\n", a_cas);
1920 pr(" Theirs: %d\n", d_cas);
1921 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1922 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1925 switch (combat_mode) {
1927 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1928 pr("We have captured %s, sir!\n", prcom(0, def));
1932 news_item = def->own ? N_AWON_SECT : N_START_COL;
1933 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1934 action = "assaulting and taking";
1937 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1938 pr("We have captured %s, sir!\n", prcom(0, def));
1939 action = "air-assaulting and taking";
1942 news_item = N_BOARD_SHIP;
1943 pr("We have boarded %s, sir!\n", prcom(0, def));
1944 action = "boarding";
1947 news_item = N_BOARD_LAND;
1948 pr("We have boarded %s, sir!\n", prcom(0, def));
1949 action = "boarding";
1954 action = "defeating";
1957 switch (combat_mode) {
1959 news_item = N_SCT_LOSE;
1960 pr("You have been defeated!\n");
1961 action = "attacking";
1964 news_item = N_ALOSE_SCT;
1965 pr("You have been defeated!\n");
1967 action = "trying to assault";
1970 news_item = N_PLOSE_SCT;
1971 pr("All of your troops were destroyed\n");
1972 action = "trying to air-assault";
1975 news_item = N_SHP_LOSE;
1976 pr("You have been repelled\n");
1978 action = "trying to board";
1981 news_item = N_LND_LOSE;
1982 pr("You have been repelled\n");
1984 action = "trying to board";
1989 action = "fighting";
1992 nreport(player->cnum, news_item, def->own, 1);
1995 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
1996 cname(player->cnum), player->cnum, a_cas,
1997 action, pr_com(0, def, def->own), d_cas);
2000 send_reacting_units_home(dlist);
2002 /* putland the defending land */
2005 /* putland the attacking land */
2008 /* put the victim sector/ship/land */
2009 if (!success || !take_def(combat_mode, olist, off, def))
2012 /* put the attacking sectors/ship */
2013 for (n = 0; n <= off->last; ++n)
2014 if (off[n].type != EF_BAD)
2015 put_combat(&off[n]);
2020 switch (combat_mode) {
2022 ask_move_in(off, olist, def);
2024 /* put sectors again to get abandon warnings */
2025 for (n = 0; n <= off->last; ++n)
2026 if (off[n].type != EF_BAD)
2027 put_combat(&off[n]);
2030 att_move_in_off(combat_mode, off, olist, def);
2033 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2037 /* What percentage of the combat forces going head-to-head are we? */
2040 att_calcodds(int ototal, int dtotal)
2044 /* calculate odds */
2047 else if (dtotal <= 0)
2050 odds = (double)ototal / (dtotal + ototal);
2055 /* Here's where the dead soldiers get dragged off the battlefield */
2058 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2060 int to_take = CASUALTY_LUMP;
2061 int biggest_troops = 0, index = -1;
2062 int n, tot_troops = 0, biggest_mil, cas;
2063 struct emp_qelem *qp, *biggest;
2066 for (n = 0; n <= off->last; ++n) {
2067 if (off[n].type != EF_BAD) {
2068 tot_troops += off[n].troops;
2069 if (off[n].troops > biggest_troops) {
2070 biggest_troops = off[n].troops;
2077 to_take -= tot_troops;
2080 for (n = 0; n <= off->last; ++n)
2081 if (off[n].type != EF_BAD)
2085 * They can all come off mil. We rotate the casualties,
2086 * starting with the sector containing the most mil.
2088 to_take = CASUALTY_LUMP;
2090 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2093 while (to_take > 0) {
2094 for (n = index; n <= off->last && to_take; ++n) {
2095 if (off[n].type != EF_BAD && off[n].troops > 0) {
2100 for (n = 0; n < index && to_take; ++n) {
2101 if (off[n].type != EF_BAD && off[n].troops > 0) {
2107 return CASUALTY_LUMP;
2111 return CASUALTY_LUMP - to_take;
2114 * Need to take some casualties from attacking units
2115 * Procedure: find the biggest unit remaining (in
2116 * terms of mil) and give it the casualties.
2120 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2121 llp = (struct ulist *)qp;
2123 if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2124 biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2128 if (biggest == NULL)
2129 return CASUALTY_LUMP - to_take;
2131 llp = (struct ulist *)biggest;
2132 cas = lnd_take_casualty(combat_mode, llp, to_take);
2133 return CASUALTY_LUMP - (to_take - cas);
2136 /* Send reacting defense units back to where they came from (at no mob cost) */
2139 send_reacting_units_home(struct emp_qelem *list)
2141 struct emp_qelem *qp, *next;
2145 for (qp = list->q_forw; qp != list; qp = next) {
2147 llp = (struct ulist *)qp;
2148 if ((llp->unit.land.lnd_x != llp->x) ||
2149 (llp->unit.land.lnd_y != llp->y)) {
2150 sprintf(buf, "returns to %s",
2151 xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2152 llp->unit.land.lnd_x = llp->x;
2153 llp->unit.land.lnd_y = llp->y;
2154 lnd_delete(llp, buf);
2159 /* Check for 0 offense strength. This call will always preceed an abort */
2162 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2165 if (def->own && player->cnum != def->own) {
2167 "%s (#%d) considered %sing you @%s\n",
2168 cname(player->cnum), player->cnum,
2169 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2171 pr("No troops for %s...\n", att_mode[combat_mode]);
2178 * Take the defending sector or ship from the defender and give it to the
2183 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2188 struct ulist *llp, *delete_me = NULL;
2194 for (n = 0; n <= off->last && !occuppied; ++n) {
2195 if (off[n].type != EF_BAD &&
2196 off[n].troops > 0 &&
2197 (off[n].type != EF_SECTOR || off[n].mob)) {
2199 if (def->type == EF_LAND) {
2200 if (def->lnd_lcp->l_flags & L_SPY) {
2207 pr("1 mil from %s moves %s\n",
2208 prcom(0, off + n), prcom(2, def));
2213 pr("%s left unoccupied\n", prcom(0, def));
2216 "No enemy troops moved %s so you still own it!\n",
2217 pr_com(2, def, def->own));
2220 llp = (struct ulist *)list->q_forw;
2221 llp->unit.land.lnd_x = def->x;
2222 llp->unit.land.lnd_y = def->y;
2223 take_move_in_mob(combat_mode, llp, off, def);
2224 if (def->type == EF_SHIP) {
2225 llp->unit.land.lnd_ship = def->shp_uid;
2226 sprintf(buf, "boards %s", prcom(0, def));
2229 llp->unit.land.lnd_ship = -1;
2230 sprintf(buf, "moves in to occupy %s",
2231 xyas(def->x, def->y, player->cnum));
2232 lnd_delete(llp, buf);
2237 if (def->type == EF_SECTOR) {
2238 getsect(def->x, def->y, §);
2239 takeover(§, player->cnum);
2240 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2241 caploss(§, def->own,
2242 "* We have captured %s's capital, sir! *\n");
2244 } else if (def->type == EF_SHIP) {
2245 getship(def->shp_uid, &ship);
2246 takeover_ship(&ship, player->cnum);
2247 putship(ship.shp_uid, &ship);
2248 } else if (def->type == EF_LAND) {
2249 getland(def->lnd_uid, &land);
2250 takeover_land(&land, player->cnum);
2251 putland(land.lnd_uid, &land);
2254 lnd_delete(delete_me, buf);
2255 att_get_combat(def, 0);
2260 * Ask the attacker which mil & land units they'd like to move into the
2265 ask_move_in(struct combat *off, struct emp_qelem *olist,
2269 struct emp_qelem *qp, *next;
2273 char land_answer[256];
2276 for (n = 0; n <= off->last; ++n)
2277 if (off[n].type != EF_BAD && off[n].troops > 0)
2279 ask_move_in_off(&off[n], def);
2280 if (player->aborted)
2286 memset(land_answer, 0, sizeof(land_answer));
2287 for (qp = olist->q_forw; qp != olist; qp = next) {
2289 llp = (struct ulist *)qp;
2290 answerp = &land_answer[(int)llp->unit.land.lnd_army];
2291 if (player->aborted || att_get_combat(def, 0) < 0)
2293 if (*answerp == 'Y')
2295 if (!get_land(A_ATTACK, def, llp, 0))
2297 if (*answerp != 'N') {
2298 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2299 prland(&llp->unit.land),
2300 llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2301 llp->unit.land.lnd_effic);
2302 *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2303 if (player->aborted || att_get_combat(def, 0) < 0)
2305 if (!get_land(A_ATTACK, def, llp, 0))
2308 if (*answerp == 'y' || *answerp == 'Y')
2310 sprintf(buf, "stays in %s",
2311 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2313 lnd_delete(llp, buf);
2317 if (att_get_combat(def, 0) < 0) {
2318 for (qp = olist->q_forw; qp != olist; qp = next) {
2320 llp = (struct ulist *)qp;
2321 if (!get_land(A_ATTACK, def, llp, 0))
2323 sprintf(buf, "stays in %s",
2324 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2326 lnd_delete(llp, buf);
2330 if (opt_INTERDICT_ATT)
2331 lnd_interdict(olist, def->x, def->y, player->cnum);
2332 move_in_land(A_ATTACK, off, olist, def);
2335 /* Move offensive land units to the conquered sector or ship */
2338 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2341 struct emp_qelem *qp, *next;
2347 for (qp = olist->q_forw; qp != olist; qp = next) {
2349 llp = (struct ulist *)qp;
2350 if (!get_land(combat_mode, def, llp, 0))
2352 take_move_in_mob(combat_mode, llp, off, def);
2353 llp->unit.land.lnd_x = def->x;
2354 llp->unit.land.lnd_y = def->y;
2355 if (def->type == EF_SHIP)
2356 llp->unit.land.lnd_ship = def->shp_uid;
2358 llp->unit.land.lnd_ship = -1;
2362 if (def->type == EF_SECTOR) {
2363 if (opt_INTERDICT_ATT) {
2364 lnd_sweep(olist, 0, 0, def->own);
2365 lnd_check_mines(olist);
2367 sprintf(buf, "now occupies %s", prcom(0, def));
2369 sprintf(buf, "boards %s", prcom(0, def));
2373 for (qp = olist->q_forw; qp != olist; qp = next) {
2375 llp = (struct ulist *)qp;
2376 lnd_print(llp, buf);
2384 * Move assaulting, paradropping, or boarding mil & units into def
2385 * If the mil are coming from a ship, then pack a lunch.
2389 att_move_in_off(int combat_mode, struct combat *off,
2390 struct emp_qelem *olist, struct combat *def)
2397 move_in_land(combat_mode, off, olist, def);
2399 for (n = 0; n <= off->last; ++n) {
2400 if (off[n].type == EF_BAD || !off[n].troops)
2402 troops = off[n].troops;
2404 off[n].mil -= troops;
2406 put_combat(off + n);
2407 if (combat_mode == A_ASSAULT) {
2408 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2410 getship(off[n].shp_uid, &ship);
2411 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2412 / (ship.shp_item[I_MILIT] + troops
2413 + ship.shp_item[I_CIVIL] + 0.5));
2415 ship.shp_item[I_FOOD] -= lunchbox;
2416 putship(ship.shp_uid, &ship);
2422 if (combat_mode == A_ASSAULT) {
2423 if (CANT_HAPPEN(def->type != EF_SECTOR))
2425 getsect(def->x, def->y, §);
2426 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2427 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2428 sect.sct_item[I_FOOD] += lunchbox;
2434 /* Ask how many mil to move in from each sector */
2437 ask_move_in_off(struct combat *off, struct combat *def)
2440 int num_mil, dam = 0, left;
2446 if (att_get_combat(off, 0) <= 0)
2448 if (att_get_combat(def, 0) < 0)
2450 if (off->own != player->cnum)
2452 d = att_mobcost(off->own, def, MOB_MOVE);
2453 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2455 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2456 xyas(off->x, off->y, player->cnum), mob_support);
2457 p = getstring(prompt, buf);
2458 if (!p || !*p || (num_mil = atoi(p)) <= 0)
2460 /* Make sure we don't move in more than we can support mobility-wise */
2461 if (num_mil > mob_support)
2462 num_mil = mob_support;
2463 if (att_get_combat(off, 0) <= 0)
2465 if (att_get_combat(def, 0) < 0)
2467 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2468 pr("No mil moved in from %s\n",
2469 xyas(off->x, off->y, player->cnum));
2472 mob_support = MAX(1, (int)(num_mil * d));
2473 off->mob -= MIN(off->mob, mob_support);
2474 off->mil -= num_mil;
2475 off->troops -= num_mil;
2478 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2479 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2480 if (chance(weight / 100.0))
2482 ground_interdict(def->x, def->y, player->cnum, "military");
2483 dam += check_lmines(def->x, def->y, weight);
2487 left = commdamage(num_mil, dam, I_MILIT);
2488 if (left < num_mil) {
2490 pr("%d of the mil you were moving were destroyed!\n"
2491 "Only %d mil made it to %s\n",
2492 num_mil - left, left,
2493 xyas(def->x, def->y, player->cnum));
2495 pr("All of the mil you were moving were destroyed!\n");
2498 /* maybe got nuked */
2499 if (att_get_combat(def, 0) < 0)
2507 /* Charge land units for moving into a sector or onto a ship */
2510 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2513 double mob = llp->unit.land.lnd_mobil;
2514 double gain = etu_per_update * land_mob_scale;
2515 double mobcost, moblim;
2517 switch (combat_mode) {
2519 mobcost = lnd_pathcost(&llp->unit.land,
2520 att_mobcost(off->own, def,
2521 lnd_mobtype(&llp->unit.land)));
2525 * Set mobcost to basic assault cost, moblim to maximum
2526 * mobility to keep when assaulting from non-landing ship
2528 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE) {
2529 mobcost = gain / 2.0;
2535 if (!(off->shp_mcp->m_flags & M_LAND))
2536 /* Not a landing ship, ensure we go to or below moblim */
2537 mobcost = MAX(mobcost, mob - moblim);
2540 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
2553 llp->unit.land.lnd_mobil = (signed char)mob;
2554 llp->unit.land.lnd_harden = 0;
2558 free_list(struct emp_qelem *list)
2560 struct emp_qelem *qp, *next;
2562 if (!list || QEMPTY(list))
2566 while (qp != list) {
2575 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2583 * sector_strength - Everyone starts at 1. You can get up to a max
2584 * of d_dstr, depending on how much you build up the
2585 * defenses of the sector.
2589 sector_strength(struct sctstr *sp)
2591 double def = SCT_DEFENSE(sp) / 100.0;
2592 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2593 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2595 if (d > dchr[sp->sct_type].d_dstr)
2596 d = dchr[sp->sct_type].d_dstr;