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_oland(int, struct ulist *);
87 static int get_dland(struct combat *, struct ulist *);
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;
1281 if (check && !get_oland(combat_mode, llp))
1283 if (combat_mode == A_ATTACK) {
1285 for (n = 0; n <= off->last; ++n) {
1286 if (off[n].type == EF_BAD)
1288 if ((off[n].x == llp->unit.land.lnd_x) &&
1289 (off[n].y == llp->unit.land.lnd_y))
1293 lnd_print(player->cnum, llp,
1294 "is in a sector not owned by you");
1298 ototal += attack_val(combat_mode, &llp->unit.land) *
1299 att_combat_eff(off + w);
1301 ototal += attack_val(combat_mode, &llp->unit.land);
1306 return ldround(ototal, 1);
1309 /* Calculate the total defensive strength */
1312 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1315 double dtotal = 0.0, eff = 1.0, d_unit;
1316 struct emp_qelem *qp, *next;
1319 if (check && att_get_combat(def, 1) < 0)
1321 eff = att_combat_eff(def);
1322 dtotal = def->troops * eff;
1325 * next, add in the defense_values of all
1326 * the defending non-retreating units
1329 for (qp = list->q_forw; qp != list; qp = next) {
1331 llp = (struct ulist *)qp;
1332 if (check && !get_dland(def, llp))
1334 d_unit = defense_val(&llp->unit.land);
1337 dtotal += d_unit * eff;
1342 return ldround(dtotal, 1);
1346 * This is the land unit integrity check.
1350 get_oland(int combat_mode, struct ulist *llp)
1352 struct lndstr *lp = &llp->unit.land;
1355 getland(llp->unit.land.lnd_uid, lp);
1357 if (lp->lnd_own != player->cnum) {
1358 sprintf(buf, "was destroyed and is no longer a part of the %s",
1359 att_mode[combat_mode]);
1360 lnd_print(player->cnum, llp, buf);
1364 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1366 "left to fight another battle and is no longer a part of the %s",
1367 att_mode[combat_mode]);
1368 lnd_print(player->cnum, llp, buf);
1372 if (lp->lnd_effic < llp->eff) {
1373 sprintf(buf, "damaged from %d%% to %d%%",
1374 llp->eff, lp->lnd_effic);
1375 lnd_print(player->cnum, llp, buf);
1378 llp->eff = llp->unit.land.lnd_effic;
1383 get_dland(struct combat *def, struct ulist *llp)
1385 struct lndstr *lp = &llp->unit.land;
1388 getland(llp->unit.land.lnd_uid, lp);
1390 if (lp->lnd_effic < LAND_MINEFF) {
1391 sprintf(buf, "was destroyed and is no longer a part of the defense");
1392 lnd_print(llp->unit.land.lnd_own, llp, buf);
1396 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1397 lnd_print(llp->unit.land.lnd_own, llp,
1398 "left to go fight another battle and is no longer a part of the defense");
1403 llp->eff = llp->unit.land.lnd_effic;
1408 kill_land(struct emp_qelem *list)
1410 struct emp_qelem *qp, *next;
1413 for (qp = list->q_forw; qp != list; qp = next) {
1415 llp = (struct ulist *)qp;
1416 if (llp->unit.land.lnd_ship >= 0) {
1417 llp->unit.land.lnd_effic = 0;
1418 lnd_print(player->cnum, llp,
1419 "cannot return to the ship, and dies!");
1426 att_infect_units(struct emp_qelem *list, int plague)
1428 struct emp_qelem *qp, *next;
1433 for (qp = list->q_forw; qp != list; qp = next) {
1435 llp = (struct ulist *)qp;
1436 if (llp->unit.land.lnd_pstage == PLG_HEALTHY)
1437 llp->unit.land.lnd_pstage = PLG_EXPOSED;
1442 * Put the land unit on the disk. If there was some mobility cost, then
1443 * subtract it from the units mobility. Note that this works the same way
1444 * as sectors & ships in that no mobility is actually taken until the attacker
1445 * has committed to attacking.
1449 put_oland(struct emp_qelem *list)
1451 struct emp_qelem *qp, *next;
1454 for (qp = list->q_forw; qp != list; qp = next) {
1456 llp = (struct ulist *)qp;
1457 llp->unit.land.lnd_mission = 0;
1458 llp->unit.land.lnd_harden = 0;
1459 llp->unit.land.lnd_mobil -= (int)llp->mobil;
1461 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1462 if (llp->unit.land.lnd_own != player->cnum) {
1463 emp_remque((struct emp_qelem *)llp);
1466 get_oland(A_ATTACK, llp);
1471 * Keep sending in reinforcements until it looks like we're going to win.
1472 * Note that the "strength" command also calls this routine.
1476 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1477 int *d_spyp, int ototal)
1479 struct nstr_item ni;
1481 struct sctstr sect, dsect;
1484 double new_land = 0;
1488 double eff = att_combat_eff(def);
1492 dtotal = get_dtotal(def, list, 1.0, 1);
1495 snxtitem_all(&ni, EF_LAND);
1496 while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1499 if (land.lnd_mission != MI_RESERVE)
1501 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1503 if (land.lnd_own != def->own)
1505 if (land.lnd_ship >= 0)
1507 if (land.lnd_land >= 0)
1509 if (defense_val(&land) < 1.0)
1511 if (!lnd_can_attack(&land))
1514 /* Only supplied units can react */
1515 if (list ? !lnd_supply_all(&land) : !lnd_could_be_supplied(&land))
1518 if (!in_oparea((struct empobj *)&land, def->x, def->y))
1521 getsect(land.lnd_x, land.lnd_y, §);
1522 getsect(def->x, def->y, &dsect);
1523 if (!BestLandPath(buf, §, &dsect, &pathcost,
1524 lnd_mobtype(&land)))
1527 mobcost = lnd_pathcost(&land, pathcost);
1528 if (land.lnd_mobil < mobcost)
1531 new_land += defense_val(&land);
1533 if (!list) /* we are in the "strength" command */
1536 /* move to defending sector */
1537 land.lnd_mobil -= ldround(mobcost, 1);
1540 land.lnd_x = def->x;
1541 land.lnd_y = def->y;
1542 putland(land.lnd_uid, &land);
1543 wu(0, land.lnd_own, "%s reacts to %s.\n",
1544 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1546 llp = malloc(sizeof(struct ulist));
1548 memset(llp, 0, sizeof(struct ulist));
1552 llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
1553 llp->unit.land = land;
1554 llp->eff = land.lnd_effic;
1555 emp_insque(&llp->queue, list);
1556 if (lnd_spyval(&land) > *d_spyp)
1557 *d_spyp = lnd_spyval(&land);
1559 intelligence_report(player->cnum, &land, a_spy,
1560 "Scouts sight reacting enemy unit:");
1565 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1568 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1569 int land_sup, int plane_sup)
1571 double osupport = 1.0;
1573 double af, as, au, ap;
1575 af = as = au = ap = 0.0;
1577 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1582 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1589 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1595 dam = off_support(def->x, def->y, def->own, player->cnum);
1599 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1604 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1607 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1608 int ototal, int dtotal)
1610 double dsupport = 1.0;
1612 double df, ds, du, dp;
1615 df = ds = du = dp = 0.0;
1616 if (dtotal < 0.1 * ototal) {
1618 } else if (dtotal >= 1.2 * ototal) {
1621 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1625 dtotal = get_dtotal(def, list, dsupport, 0);
1626 if (dtotal < 1.2 * ototal) {
1627 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1630 dtotal = get_dtotal(def, list, dsupport, 0);
1632 if (dtotal < 1.2 * ototal) {
1633 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1636 dtotal = get_dtotal(def, list, dsupport, 1);
1638 if (dtotal < 1.2 * ototal) {
1639 dam = def_support(def->x, def->y, player->cnum, def->own);
1647 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1652 "\nOdds are bad for us...support cancelled.\n\n");
1655 "\nOdds are good for us...support cancelled.\n\n");
1661 * Land mines add to the defense multiplier. If the attacker has engineers
1662 * then this multiplier is cut in half.
1666 get_mine_dsupport(struct combat *def, int a_engineer)
1671 getsect(def->x, def->y, §);
1673 if (sect.sct_oldown != player->cnum) {
1674 mines = SCT_LANDMINES(§);
1675 mines = MIN(mines, 20);
1677 mines = ldround(mines / 2.0, 1);
1680 wu(0, def->own, "Defending mines add %1.2f\n",
1682 pr("Defending mines add %1.2f\n", mines * 0.02);
1683 return mines * 0.02;
1689 /* Get the offensive and defensive support */
1691 att_get_support(int combat_mode, int ofort, int oship, int oland,
1692 int oplane, struct emp_qelem *olist, struct combat *off,
1693 struct emp_qelem *dlist, struct combat *def,
1694 double *osupportp, double *dsupportp, int a_engineer)
1697 char osupports[512];
1698 char dsupports[512];
1700 if (combat_mode == A_PARA)
1703 *osupportp = get_osupport(osupports, def,
1704 ofort, oship, oland, oplane);
1707 * I need to put a 1 at the end of the next four total_stren calls
1708 * because units & mil may have been damaged by collateral damage or
1709 * nuclear warheads from the offensive & defensive support.
1712 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1713 if (att_empty_attack(combat_mode, ototal, def))
1714 return abort_attack();
1715 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1718 * Calculate defensive support. If odds are too good or too bad
1719 * then don't call in support.
1722 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1723 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1724 if (att_empty_attack(combat_mode, ototal, def))
1725 return abort_attack();
1727 if ((*osupports || *dsupports) &&
1728 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1729 pr("\n\t\tsupport values\n");
1730 pr("\t\tforts\tships\tunits\tplanes\n");
1731 if (*osupportp != 1.0)
1732 pr("%s", osupports);
1733 if (*dsupportp != 1.0)
1734 pr("%s", dsupports);
1736 wu(0, def->own, "\n\t\tsupport values\n");
1737 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1738 if (*osupportp != 1.0)
1739 wu(0, def->own, "%s", osupports);
1740 if (*dsupportp != 1.0)
1741 wu(0, def->own, "%s", dsupports);
1745 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1746 if (dtotal && def->type == EF_SECTOR)
1747 *dsupportp += get_mine_dsupport(def, a_engineer);
1751 /* How many two-legged bipeds are in this combat force? */
1754 count_bodies(struct combat *off, struct emp_qelem *list)
1758 struct emp_qelem *qp;
1761 for (n = 0; n <= off->last; ++n)
1762 bodies += off[n].troops;
1763 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1764 llp = (struct ulist *)qp;
1765 bodies += llp->unit.land.lnd_item[I_MILIT];
1770 /* This is where the fighting actually occurs. */
1773 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1774 double osupport, struct combat *def, struct emp_qelem *dlist,
1778 int a_cas = 0; /* Casualty counts */
1780 int ototal; /* total attacking strength */
1781 int dtotal; /* total defending strength */
1782 int a_bodies; /* total attacking mil (incl. mil in units) */
1783 int d_bodies; /* total defending mil (incl. mil in units) */
1793 ototal = get_ototal(combat_mode, off, olist, osupport,
1794 combat_mode != A_PARA);
1795 dtotal = get_dtotal(def, dlist, dsupport, 0);
1799 a_bodies = count_bodies(off, olist);
1800 d_bodies = count_bodies(def, dlist);
1801 d_mil = def->troops;
1802 for (n = 0; n <= off->last; ++n)
1803 if (off[n].type == EF_BAD)
1806 a_troops[n] = off[n].troops;
1808 /* This switch is required to get the spacing right */
1809 switch (combat_mode) {
1811 pr(" Final attack strength: %8d\n", ototal);
1814 pr(" Final assault strength: %8d\n", ototal);
1817 if (def->sct_type == SCT_MOUNT ||
1818 def->sct_type == SCT_WATER ||
1819 def->sct_type == SCT_CAPIT ||
1820 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1821 pr("You can't air-assault a %s sector!\n",
1822 def->sct_dcp->d_name);
1825 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1827 pr(" Final air-assault strength: %8d\n", ototal);
1831 pr(" Final board strength: %8d\n", ototal);
1835 pr(" Final defense strength: %8d\n", dtotal);
1836 odds = att_calcodds(ototal, dtotal);
1837 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1839 /* spread the plague */
1840 if (combat_mode != A_PARA) {
1842 for (n = 0; n <= off->last; ++n)
1843 if (off[n].type != EF_BAD)
1844 def->plague |= off[n].plague;
1845 for (n = 0; n <= off->last; ++n)
1846 if (off[n].type != EF_BAD)
1847 off[n].plague |= def->plague;
1849 att_infect_units(olist, off->plague);
1850 att_infect_units(dlist, def->plague);
1853 * Fighting is slightly random. There is always that last little
1854 * effort you see people put in. Or the stray bullet that takes out
1855 * an officer and the rest go into chaos. Things like that.
1856 * Thus, we have added a very slight random factor that will sometimes
1857 * allow the little guy to win. We modify the odds a little
1858 * (either +- 5%) to account for this randomness. We also only
1859 * recalculate the odds every 8-50 casualties, not every cacsualty,
1860 * since a single dead guy normally wouldn't cause a commander to
1861 * rethink his strategies, but 50 dead guys might.
1863 odds += (random() % 11 - 5) / 100.0;
1868 recalctime = 8 + (random() % 43);
1869 while (!success && ototal) {
1872 d_cas += take_casualty(A_DEFEND, def, dlist);
1873 dtotal = get_dtotal(def, dlist, dsupport, 0);
1878 a_cas += take_casualty(combat_mode, off, olist);
1879 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1881 if (((a_cas + d_cas) % 70) == 69)
1883 if (recalctime-- <= 0) {
1884 recalctime = 8 + (random() % 43);
1885 odds = att_calcodds(ototal, dtotal);
1886 odds += (random() % 11 - 5) / 100.0;
1894 /* update defense mobility & mil */
1898 if (def->type == EF_SECTOR && d_mil && d_cas) {
1902 newmob = damage(def->mob, 100 * d_cas / d_mil);
1903 def->mobcost = MIN(20, def->mob - newmob);
1906 def->mil = def->troops;
1909 /* update attack mobility & mil */
1910 for (n = 0; n <= off->last; ++n)
1911 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1912 if (off[n].type == EF_SECTOR && off[n].mil) {
1913 if (!CANT_HAPPEN(off[n].mob < 0)) {
1914 newmob = damage(off[n].mob,
1915 100 * (a_troops[n] - off[n].troops)
1917 off[n].mobcost += MIN(20, off[n].mob - newmob);
1920 off[n].mil -= a_troops[n] - off[n].troops;
1923 /* update land unit mobility */
1924 if (d_bodies && d_cas)
1925 lnd_takemob(dlist, (double)d_cas / d_bodies);
1926 if (a_bodies && a_cas)
1927 lnd_takemob(olist, (double)a_cas / a_bodies);
1929 /* damage attacked sector */
1930 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1932 pr("- Casualties -\n Yours: %d\n", a_cas);
1933 pr(" Theirs: %d\n", d_cas);
1934 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1935 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1938 switch (combat_mode) {
1940 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1941 pr("We have captured %s, sir!\n", prcom(0, def));
1945 news_item = def->own ? N_AWON_SECT : N_START_COL;
1946 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1947 action = "assaulting and taking";
1950 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1951 pr("We have captured %s, sir!\n", prcom(0, def));
1952 action = "air-assaulting and taking";
1955 news_item = N_BOARD_SHIP;
1956 pr("We have boarded %s, sir!\n", prcom(0, def));
1957 action = "boarding";
1960 news_item = N_BOARD_LAND;
1961 pr("We have boarded %s, sir!\n", prcom(0, def));
1962 action = "boarding";
1967 action = "defeating";
1970 switch (combat_mode) {
1972 news_item = N_SCT_LOSE;
1973 pr("You have been defeated!\n");
1974 action = "attacking";
1977 news_item = N_ALOSE_SCT;
1978 pr("You have been defeated!\n");
1980 action = "trying to assault";
1983 news_item = N_PLOSE_SCT;
1984 pr("All of your troops were destroyed\n");
1985 action = "trying to air-assault";
1988 news_item = N_SHP_LOSE;
1989 pr("You have been repelled\n");
1991 action = "trying to board";
1994 news_item = N_LND_LOSE;
1995 pr("You have been repelled\n");
1997 action = "trying to board";
2002 action = "fighting";
2005 nreport(player->cnum, news_item, def->own, 1);
2008 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2009 cname(player->cnum), player->cnum, a_cas,
2010 action, pr_com(0, def, def->own), d_cas);
2013 send_reacting_units_home(dlist);
2015 /* putland the defending land */
2018 /* putland the attacking land */
2021 /* put the victim sector/ship/land */
2022 if (!success || !take_def(combat_mode, olist, off, def))
2025 /* put the attacking sectors/ship */
2026 for (n = 0; n <= off->last; ++n)
2027 if (off[n].type != EF_BAD)
2028 put_combat(&off[n]);
2033 switch (combat_mode) {
2035 ask_move_in(off, olist, def);
2037 /* put sectors again to get abandon warnings */
2038 for (n = 0; n <= off->last; ++n)
2039 if (off[n].type != EF_BAD)
2040 put_combat(&off[n]);
2043 att_move_in_off(combat_mode, off, olist, def);
2046 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2050 /* What percentage of the combat forces going head-to-head are we? */
2053 att_calcodds(int ototal, int dtotal)
2057 /* calculate odds */
2060 else if (dtotal <= 0)
2063 odds = (double)ototal / (dtotal + ototal);
2068 /* Here's where the dead soldiers get dragged off the battlefield */
2071 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2073 int to_take = CASUALTY_LUMP;
2074 int biggest_troops = 0, index = -1;
2075 int n, tot_troops = 0, biggest_mil, cas;
2076 struct emp_qelem *qp, *biggest;
2079 for (n = 0; n <= off->last; ++n) {
2080 if (off[n].type != EF_BAD) {
2081 tot_troops += off[n].troops;
2082 if (off[n].troops > biggest_troops) {
2083 biggest_troops = off[n].troops;
2090 to_take -= tot_troops;
2093 for (n = 0; n <= off->last; ++n)
2094 if (off[n].type != EF_BAD)
2098 * They can all come off mil. We rotate the casualties,
2099 * starting with the sector containing the most mil.
2101 to_take = CASUALTY_LUMP;
2103 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2106 while (to_take > 0) {
2107 for (n = index; n <= off->last && to_take; ++n) {
2108 if (off[n].type != EF_BAD && off[n].troops > 0) {
2113 for (n = 0; n < index && to_take; ++n) {
2114 if (off[n].type != EF_BAD && off[n].troops > 0) {
2120 return CASUALTY_LUMP;
2124 return CASUALTY_LUMP - to_take;
2127 * Need to take some casualties from attacking units
2128 * Procedure: find the biggest unit remaining (in
2129 * terms of mil) and give it the casualties.
2133 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2134 llp = (struct ulist *)qp;
2136 if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2137 biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2141 if (biggest == NULL)
2142 return CASUALTY_LUMP - to_take;
2144 llp = (struct ulist *)biggest;
2145 cas = lnd_take_casualty(combat_mode, llp, to_take);
2146 return CASUALTY_LUMP - (to_take - cas);
2149 /* Send reacting defense units back to where they came from (at no mob cost) */
2152 send_reacting_units_home(struct emp_qelem *list)
2154 struct emp_qelem *qp, *next;
2158 for (qp = list->q_forw; qp != list; qp = next) {
2160 llp = (struct ulist *)qp;
2161 if ((llp->unit.land.lnd_x != llp->x) ||
2162 (llp->unit.land.lnd_y != llp->y)) {
2163 sprintf(buf, "returns to %s",
2164 xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2165 llp->unit.land.lnd_x = llp->x;
2166 llp->unit.land.lnd_y = llp->y;
2167 lnd_print(llp->unit.land.lnd_own, llp, buf);
2173 /* Check for 0 offense strength. This call will always preceed an abort */
2176 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2179 if (def->own && player->cnum != def->own) {
2181 "%s (#%d) considered %sing you @%s\n",
2182 cname(player->cnum), player->cnum,
2183 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2185 pr("No troops for %s...\n", att_mode[combat_mode]);
2192 * Take the defending sector or ship from the defender and give it to the
2197 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2202 struct ulist *llp, *delete_me = NULL;
2208 for (n = 0; n <= off->last && !occuppied; ++n) {
2209 if (off[n].type != EF_BAD &&
2210 off[n].troops > 0 &&
2211 (off[n].type != EF_SECTOR || off[n].mob)) {
2213 if (def->type == EF_LAND) {
2214 if (def->lnd_lcp->l_flags & L_SPY) {
2221 pr("1 mil from %s moves %s\n",
2222 prcom(0, off + n), prcom(2, def));
2227 pr("%s left unoccupied\n", prcom(0, def));
2230 "No enemy troops moved %s so you still own it!\n",
2231 pr_com(2, def, def->own));
2234 llp = (struct ulist *)list->q_forw;
2235 llp->unit.land.lnd_x = def->x;
2236 llp->unit.land.lnd_y = def->y;
2237 take_move_in_mob(combat_mode, llp, off, def);
2238 if (def->type == EF_SHIP) {
2239 llp->unit.land.lnd_ship = def->shp_uid;
2240 sprintf(buf, "boards %s", prcom(0, def));
2241 lnd_print(player->cnum, llp, buf);
2244 llp->unit.land.lnd_ship = -1;
2245 sprintf(buf, "moves in to occupy %s",
2246 xyas(def->x, def->y, player->cnum));
2247 lnd_print(player->cnum, llp, buf);
2253 if (def->type == EF_SECTOR) {
2254 getsect(def->x, def->y, §);
2255 takeover(§, player->cnum);
2256 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2257 caploss(§, def->own,
2258 "* We have captured %s's capital, sir! *\n");
2260 } else if (def->type == EF_SHIP) {
2261 getship(def->shp_uid, &ship);
2262 takeover_ship(&ship, player->cnum);
2263 putship(ship.shp_uid, &ship);
2264 } else if (def->type == EF_LAND) {
2265 getland(def->lnd_uid, &land);
2266 takeover_land(&land, player->cnum);
2267 putland(land.lnd_uid, &land);
2270 lnd_delete(delete_me);
2271 att_get_combat(def, 0);
2276 * Ask the attacker which mil & land units they'd like to move into the
2281 ask_move_in(struct combat *off, struct emp_qelem *olist,
2285 struct emp_qelem *qp, *next;
2289 char land_answer[256];
2292 for (n = 0; n <= off->last; ++n)
2293 if (off[n].type != EF_BAD && off[n].troops > 0)
2295 ask_move_in_off(&off[n], def);
2296 if (player->aborted)
2302 memset(land_answer, 0, sizeof(land_answer));
2303 for (qp = olist->q_forw; qp != olist; qp = next) {
2305 llp = (struct ulist *)qp;
2306 answerp = &land_answer[(int)llp->unit.land.lnd_army];
2307 if (player->aborted || att_get_combat(def, 0) < 0)
2309 if (*answerp == 'Y')
2311 if (!get_oland(A_ATTACK, llp))
2313 if (*answerp != 'N') {
2314 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2315 prland(&llp->unit.land),
2316 llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2317 llp->unit.land.lnd_effic);
2318 *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2319 if (player->aborted || att_get_combat(def, 0) < 0)
2321 if (!get_oland(A_ATTACK, llp))
2324 if (*answerp == 'y' || *answerp == 'Y')
2326 sprintf(buf, "stays in %s",
2327 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2329 lnd_print(player->cnum, llp, buf);
2334 if (att_get_combat(def, 0) < 0) {
2335 for (qp = olist->q_forw; qp != olist; qp = next) {
2337 llp = (struct ulist *)qp;
2338 if (!get_oland(A_ATTACK, llp))
2340 sprintf(buf, "stays in %s",
2341 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2343 lnd_print(player->cnum, llp, buf);
2348 if (opt_INTERDICT_ATT)
2349 lnd_interdict(olist, def->x, def->y, player->cnum);
2350 move_in_land(A_ATTACK, off, olist, def);
2353 /* Move offensive land units to the conquered sector or ship */
2356 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2359 struct emp_qelem *qp, *next;
2365 for (qp = olist->q_forw; qp != olist; qp = next) {
2367 llp = (struct ulist *)qp;
2368 if (!get_oland(combat_mode, llp))
2370 take_move_in_mob(combat_mode, llp, off, def);
2371 llp->unit.land.lnd_x = def->x;
2372 llp->unit.land.lnd_y = def->y;
2373 if (def->type == EF_SHIP)
2374 llp->unit.land.lnd_ship = def->shp_uid;
2376 llp->unit.land.lnd_ship = -1;
2380 if (def->type == EF_SECTOR) {
2381 if (opt_INTERDICT_ATT) {
2382 lnd_sweep(olist, 0, 0, player->cnum);
2383 lnd_check_mines(olist);
2385 sprintf(buf, "now occupies %s", prcom(0, def));
2387 sprintf(buf, "boards %s", prcom(0, def));
2391 for (qp = olist->q_forw; qp != olist; qp = next) {
2393 llp = (struct ulist *)qp;
2394 lnd_print(player->cnum, llp, buf);
2402 * Move assaulting, paradropping, or boarding mil & units into def
2403 * If the mil are coming from a ship, then pack a lunch.
2407 att_move_in_off(int combat_mode, struct combat *off,
2408 struct emp_qelem *olist, struct combat *def)
2415 move_in_land(combat_mode, off, olist, def);
2417 for (n = 0; n <= off->last; ++n) {
2418 if (off[n].type == EF_BAD || !off[n].troops)
2420 troops = off[n].troops;
2422 off[n].mil -= troops;
2424 put_combat(off + n);
2425 if (combat_mode == A_ASSAULT) {
2426 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2428 getship(off[n].shp_uid, &ship);
2429 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2430 / (ship.shp_item[I_MILIT] + troops
2431 + ship.shp_item[I_CIVIL] + 0.5));
2433 ship.shp_item[I_FOOD] -= lunchbox;
2434 putship(ship.shp_uid, &ship);
2440 if (combat_mode == A_ASSAULT) {
2441 if (CANT_HAPPEN(def->type != EF_SECTOR))
2443 getsect(def->x, def->y, §);
2444 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2445 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2446 sect.sct_item[I_FOOD] += lunchbox;
2452 /* Ask how many mil to move in from each sector */
2455 ask_move_in_off(struct combat *off, struct combat *def)
2458 int num_mil, dam = 0, left;
2464 if (att_get_combat(off, 0) <= 0)
2466 if (att_get_combat(def, 0) < 0)
2468 if (off->own != player->cnum)
2470 d = att_mobcost(off->own, def, MOB_MOVE);
2471 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2473 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2474 xyas(off->x, off->y, player->cnum), mob_support);
2475 p = getstring(prompt, buf);
2476 if (!p || !*p || (num_mil = atoi(p)) <= 0)
2478 /* Make sure we don't move in more than we can support mobility-wise */
2479 if (num_mil > mob_support)
2480 num_mil = mob_support;
2481 if (att_get_combat(off, 0) <= 0)
2483 if (att_get_combat(def, 0) < 0)
2485 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2486 pr("No mil moved in from %s\n",
2487 xyas(off->x, off->y, player->cnum));
2490 mob_support = MAX(1, (int)(num_mil * d));
2491 off->mob -= MIN(off->mob, mob_support);
2492 off->mil -= num_mil;
2493 off->troops -= num_mil;
2496 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2497 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2498 if (chance(weight / 100.0))
2500 ground_interdict(def->x, def->y, player->cnum, "military");
2501 dam += check_lmines(def->x, def->y, weight);
2505 left = commdamage(num_mil, dam, I_MILIT);
2506 if (left < num_mil) {
2508 pr("%d of the mil you were moving were destroyed!\n"
2509 "Only %d mil made it to %s\n",
2510 num_mil - left, left,
2511 xyas(def->x, def->y, player->cnum));
2513 pr("All of the mil you were moving were destroyed!\n");
2516 /* maybe got nuked */
2517 if (att_get_combat(def, 0) < 0)
2525 /* Charge land units for moving into a sector or onto a ship */
2528 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2531 double mob = llp->unit.land.lnd_mobil;
2532 double gain = etu_per_update * land_mob_scale;
2533 double mobcost, moblim;
2535 switch (combat_mode) {
2537 mobcost = lnd_pathcost(&llp->unit.land,
2538 att_mobcost(off->own, def,
2539 lnd_mobtype(&llp->unit.land)));
2543 * Set mobcost to basic assault cost, moblim to maximum
2544 * mobility to keep when assaulting from non-landing ship
2546 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE) {
2547 mobcost = gain / 2.0;
2553 if (!(off->shp_mcp->m_flags & M_LAND))
2554 /* Not a landing ship, ensure we go to or below moblim */
2555 mobcost = MAX(mobcost, mob - moblim);
2558 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
2571 llp->unit.land.lnd_mobil = (signed char)mob;
2572 llp->unit.land.lnd_harden = 0;
2576 free_list(struct emp_qelem *list)
2578 struct emp_qelem *qp, *next;
2580 if (!list || QEMPTY(list))
2584 while (qp != list) {
2593 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2601 * sector_strength - Everyone starts at 1. You can get up to a max
2602 * of d_dstr, depending on how much you build up the
2603 * defenses of the sector.
2607 sector_strength(struct sctstr *sp)
2609 double def = SCT_DEFENSE(sp) / 100.0;
2610 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2611 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2613 if (d > dchr[sp->sct_type].d_dstr)
2614 d = dchr[sp->sct_type].d_dstr;