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 *list, 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,
141 } else if (com->type == EF_LAND) {
142 return prbuf("%s%s #%d",
143 inon ? inon == 1 ? "on " : "onto " : "",
144 com->lnd_lcp->l_name, com->lnd_uid);
146 return "your forces";
151 prcom(int inon, struct combat *com)
153 return pr_com(inon, com, player->cnum);
157 * This is the combat object "type" based integrity check. It basically
158 * splits along three divisions: ship/sector, attacker/defender,
159 * first time/not first time.
163 att_get_combat(struct combat *com, int isdef)
177 if (!getsect(com->x, com->y, §)) {
178 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
179 return att_combat_init(com, EF_BAD);
181 com->sct_type = sect.sct_type;
182 com->sct_dcp = &dchr[sect.sct_type];
183 mil = sect.sct_item[I_MILIT];
184 pstage = sect.sct_pstage;
185 owner = sect.sct_own;
186 eff = sect.sct_effic;
187 mob = sect.sct_mobil;
192 if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
194 pr("Land unit #%d is not in the same sector!\n",
196 return att_combat_init(com, EF_BAD);
198 if (isdef && player->owner) {
199 pr("Boarding yourself? Try using the 'load' command.\n");
200 return att_combat_init(com, EF_BAD);
202 com->lnd_lcp = &lchr[(int)land.lnd_type];
203 mil = land.lnd_item[I_MILIT];
204 pstage = land.lnd_pstage;
205 owner = land.lnd_own;
206 eff = land.lnd_effic;
207 mob = land.lnd_mobil;
212 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
214 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
216 pr("Ship #%d is not your ship!\n", com->shp_uid);
217 return att_combat_init(com, EF_BAD);
220 if (isdef && player->owner &&
221 ontradingblock(EF_SHIP, &ship)) {
222 pr("%s is on the trading block.\n", prcom(0, com));
223 return att_combat_init(com, EF_BAD);
226 if (isdef && player->owner) {
227 pr("Boarding yourself? Try using the 'tend' command.\n");
228 return att_combat_init(com, EF_BAD);
230 com->shp_mcp = &mchr[(int)ship.shp_type];
231 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
232 if (!isdef && !player->owner) {
234 pr("%s was just sunk!\n", prcom(0, com));
236 pr("Ship #%d is not your ship!\n", com->shp_uid);
237 return att_combat_init(com, EF_BAD);
239 mil = ship.shp_item[I_MILIT];
240 pstage = ship.shp_pstage;
241 owner = ship.shp_own;
242 eff = ship.shp_effic;
243 mob = ship.shp_mobil;
252 return att_combat_init(com, EF_BAD);
255 if (!com->set) { /* first time */
256 if (isdef) { /* defender */
258 } else { /* attacker */
260 pr("No mil %s\n", prcom(1, com));
262 pr("Only 1 mil %s\n", prcom(1, com));
263 /* don't abandon attacking sectors or ships */
264 com->troops = MAX(0, mil - 1);
266 com->plague = pstage == PLG_INFECT;
267 } else { /* not first time */
268 if (isdef) { /* defender */
269 if (com->x != x || com->y != y) {
270 pr("%s has moved!\n", prcom(0, com));
271 return att_combat_init(com, EF_BAD);
273 if (owner != com->own) {
275 pr("WARNING: The ownership of %s just changed from %s to %s!\n",
276 prcom(0, com), cname(com->own), cname(owner));
277 } else if (com->type == EF_SECTOR) {
278 pr("WARNING: %s just abandoned sector %s!\n",
280 xyas(com->x, com->y, player->cnum));
284 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
286 com->mil < mil ? "increased" : "decreased", com->mil,
289 } else { /* attacker */
290 if (owner != player->cnum && getrel(getnatp(owner), player->cnum) != ALLIED) {
291 /* must be EF_SECTOR */
293 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
294 com->mil, xyas(com->x, com->y, player->cnum),
297 pr("You no longer own %s\n",
298 xyas(com->x, com->y, player->cnum));
299 return att_combat_init(com, EF_BAD);
301 if (com->troops && com->troops + 1 > mil) {
302 if (com->own == owner && player->cnum == owner)
304 pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
305 prcom(1, com), com->troops, MAX(0, mil - 1));
306 com->troops = MAX(0, mil - 1);
321 * In the course of the fight, the combat object may have lost mil, eff, or
322 * mobility. This is the place where the data in the object gets flushed to
323 * disk to make it "real".
327 put_combat(struct combat *com)
336 getsect(com->x, com->y, §);
337 sect.sct_type = com->sct_type;
338 deff = sect.sct_effic - com->eff;
340 sect.sct_road -= sect.sct_road * deff / 100.0;
341 sect.sct_rail -= sect.sct_rail * deff / 100.0;
342 sect.sct_defense -= sect.sct_defense * deff / 100.0;
343 if (sect.sct_road <= 0)
345 if (sect.sct_rail <= 0)
347 if (sect.sct_defense <= 0)
348 sect.sct_defense = 0;
350 sect.sct_effic = com->eff;
352 if (opt_MOB_ACCESS) {
353 if ((com->mob - com->mobcost) < -127)
354 sect.sct_mobil = -127;
356 sect.sct_mobil = com->mob - com->mobcost;
358 if ((com->mob - com->mobcost) < 0)
361 sect.sct_mobil = com->mob - com->mobcost;
364 sect.sct_own = com->own;
366 if (sect.sct_pstage == PLG_HEALTHY)
367 sect.sct_pstage = PLG_EXPOSED;
369 sect.sct_item[I_MILIT] = com->mil;
371 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
374 getland(com->lnd_uid, &land);
375 land.lnd_effic = com->eff;
377 if (com->mob - com->mobcost < -127)
378 land.lnd_mobil = -127;
380 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
382 land.lnd_own = com->own;
384 if (land.lnd_pstage == PLG_HEALTHY)
385 land.lnd_pstage = PLG_EXPOSED;
387 if (!(com->lnd_lcp->l_flags & L_SPY))
388 land.lnd_item[I_MILIT] = com->mil;
389 if (com->own == player->cnum) {
390 land.lnd_mission = 0;
392 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
394 putland(com->lnd_uid, &land);
397 getship(com->shp_uid, &ship);
398 ship.shp_effic = com->eff;
400 if (com->mob - com->mobcost < -127)
401 ship.shp_mobil = -127;
403 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
405 ship.shp_own = com->own;
407 if (ship.shp_pstage == PLG_HEALTHY)
408 ship.shp_pstage = PLG_EXPOSED;
410 ship.shp_item[I_MILIT] = com->mil;
411 if (com->own == player->cnum) {
412 ship.shp_mission = 0;
414 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
416 putship(com->shp_uid, &ship);
419 att_get_combat(com, com->own != player->cnum);
422 /* If pre-attack, abort fight. If post-attack, don't move anything in */
427 return player->aborted = 1;
431 * This is the combat_mode based integrity check. It splits among two main
432 * divisions: first time/not first time, and attack/assault/para/board.
436 att_abort(int combat_mode, struct combat *off, struct combat *def)
445 if (att_get_combat(def, 1) < 0)
446 return abort_attack();
448 if (off && combat_mode != A_ATTACK) {
449 if (att_get_combat(off, 0) < 0)
450 return abort_attack();
451 if (off->type == EF_SHIP &&
452 !(off->x == def->x && off->y == def->y) &&
453 (!getsect(off->x, off->y, §) ||
454 sect.sct_type != SCT_WATER)) {
455 pr("%s can not %s from that far inland!\n",
456 prcom(0, off), att_mode[combat_mode]);
457 return abort_attack();
460 switch (combat_mode) {
462 if (!neigh(def->x, def->y, player->cnum) &&
463 !adj_units(def->x, def->y, player->cnum)) {
464 pr("You are not adjacent to %s\n",
465 xyas(def->x, def->y, player->cnum));
466 return abort_attack();
468 if (def->own == player->cnum) {
469 pr("You can't attack your own sector.\n");
470 return abort_attack();
474 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
475 pr("You'll have to get there first...\n");
476 return abort_attack();
478 if (off && def->sct_type == SCT_MOUNT) {
479 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
480 return abort_attack();
484 if (def->own == player->cnum) {
485 pr("You can't air-assault your own sector.\n");
486 return abort_attack();
488 if (off && (def->sct_type == SCT_MOUNT ||
489 def->sct_type == SCT_WATER ||
490 def->sct_type == SCT_CAPIT ||
491 def->sct_type == SCT_FORTR ||
492 def->sct_type == SCT_WASTE)) {
493 pr("You can't air-assault a %s sector!\n",
494 def->sct_dcp->d_name);
495 return abort_attack();
499 return board_abort(off, def);
501 return land_board_abort(off, def);
504 if (off && def->sct_dcp->d_mob0 < 0) {
505 pr("You can't %s a %s sector!\n",
506 att_mode[combat_mode], def->sct_dcp->d_name);
507 return abort_attack();
509 if (!off || off->relations_checked)
511 off->relations_checked = 1;
514 setcont(player->cnum, def->own, FOUND_SPY);
515 setcont(def->own, player->cnum, FOUND_SPY);
517 if (opt_SLOW_WAR && def->own != player->cnum) {
518 natp = getnatp(player->cnum);
519 rel = getrel(natp, def->own);
522 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
523 cname(def->own), att_mode[combat_mode]);
524 if (!confirm(y_or_n))
525 return abort_attack();
528 if ((rel != AT_WAR) && (def->own) &&
529 (sect.sct_oldown != player->cnum)) {
530 pr("You're not at war with them!\n");
531 return abort_attack();
538 * Lots of special things need to be checked for boarding, so I put it in
543 board_abort(struct combat *off, struct combat *def)
545 struct shpstr aship, dship; /* for tech levels */
548 if (att_get_combat(def, 1) < 0)
549 return abort_attack();
554 if (att_get_combat(off, 0) < 0)
555 return abort_attack();
557 if (off->x != def->x || off->y != def->y) {
558 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
559 return abort_attack();
561 if (off->type == EF_SHIP) {
563 pr("%s has no mobility!\n", prcom(0, off));
564 return abort_attack();
566 getship(off->shp_uid, &aship);
567 getship(def->shp_uid, &dship);
568 if (techfact(aship.shp_tech, shp_speed(&aship)) * off->eff
569 <= techfact(dship.shp_tech, shp_speed(&dship)) * def->eff) {
570 pr("Victim ship moves faster than you do!\n");
573 "%s (#%d) %s failed to catch %s\n",
574 cname(aship.shp_own), aship.shp_own,
575 pr_com(0, off, def->own), pr_com(0, def, def->own));
576 return abort_attack();
578 } else if (off->type != EF_SECTOR) {
579 pr("Please tell the deity that you got the 'banana boat' error\n");
580 return abort_attack();
582 if (def->shp_mcp->m_flags & M_SUB) {
583 getsect(def->x, def->y, §);
584 if (sect.sct_type == SCT_WATER) {
585 pr("You can't board a submarine!\n");
586 return abort_attack();
593 * Lots of special things need to be checked for boarding, so I put it in
595 * STM - I copied it for land unit boarding. :)
599 land_board_abort(struct combat *off, struct combat *def)
601 if (att_get_combat(def, 1) < 0)
602 return abort_attack();
607 if (att_get_combat(off, 0) < 0)
608 return abort_attack();
610 if (off->x != def->x || off->y != def->y) {
611 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
612 return abort_attack();
618 /* If we are boarding, then the defending ship gets a chance to fire back */
620 att_approach(struct combat *off, struct combat *def)
626 pr("Approaching %s...\n", prcom(0, def));
629 "%s is being approached by %s...\n",
630 pr_com(0, def, def->own), pr_com(0, off, def->own));
631 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
634 pr("They're firing at us sir!\n");
637 "Your fleet at %s does %d damage to %s\n",
638 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
640 if (off->type == EF_SECTOR) {
641 getsect(off->x, off->y, §);
642 sectdamage(§, dam);
644 pr("Enemy fleet at %s does %d damage to %s\n",
645 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
646 } else if (off->type == EF_SHIP) {
647 getship(off->shp_uid, &ship);
648 shipdamage(&ship, dam);
649 putship(off->shp_uid, &ship);
650 if (def->own && ship.shp_effic < SHIP_MINEFF) {
651 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
652 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
655 if (att_get_combat(off, 0) < 0)
656 return abort_attack();
660 /* The attack is valid. Tell the attacker about what they're going to hit */
663 att_show(struct combat *def)
665 /* Note that we tell the player about the treaty BEFORE we tell them
666 about the item. If we didn't, then they gain free information */
667 if (def->type == EF_SECTOR) {
668 if (!trechk(player->cnum, def->own, LANATT))
669 return abort_attack();
670 pr("%s is a %d%% %s %s with approximately %d military.\n",
671 xyas(def->x, def->y, player->cnum),
672 roundintby((int)def->eff, 10),
673 cname(def->own), def->sct_dcp->d_name,
674 roundintby(def->troops, 10));
675 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
676 writemap(player->cnum);
677 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
678 if (def->type == EF_SHIP) {
679 if (!trechk(player->cnum, def->own, SEAATT))
680 return abort_attack();
682 if (!trechk(player->cnum, def->own, LNDATT))
683 return abort_attack();
685 pr("%s is about %d%% efficient and has approximately %d mil on board.\n", prcom(0, def), roundintby((int)def->eff, 10), roundintby(def->troops, 10));
687 /* Ok, everything is fine */
691 /* Attack and assault ask the user which kind of support they want */
694 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
699 *fortp = *shipp = *landp = *planep = 1;
701 if (player->argp[offset] != NULL) {
702 if ((player->argp[offset + 1] == NULL) ||
703 (player->argp[offset + 2] == NULL) ||
704 (player->argp[offset + 3] == NULL)) {
705 pr("If any support arguments are used, all must be!\n");
710 *landp = *planep = 0;
712 p = getstarg(player->argp[offset], "Use fort support? ", buf);
716 if ((*p == 'y') || (*p == 'Y'))
719 p = getstarg(player->argp[offset + 1], "Use ship support? ", buf);
723 if ((*p == 'y') || (*p == 'Y'))
726 p = getstarg(player->argp[offset + 2], "Use land support? ", buf);
730 if ((*p == 'y') || (*p == 'Y'))
733 p = getstarg(player->argp[offset + 3], "Use plane support? ", buf);
737 if ((*p == 'y') || (*p == 'Y'))
744 * Attack, assault, and board ask the attacker what they'd like to attack
745 * with. This includes mil and land units from each "off" object. Note that
746 * after each sub-prompt, we check to make sure that the attack is still
747 * valid, and if it's not, then we abort the attack.
751 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
752 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
755 char land_answer[256];
758 if (att_abort(combat_mode, off, def))
760 memset(land_answer, 0, sizeof(land_answer));
761 for (n = 0; n <= off->last; ++n) {
762 off[n].troops = ask_off(combat_mode, off + n, def);
763 if (att_abort(combat_mode, off, def))
765 ask_olist(combat_mode, off + n, def, olist, land_answer,
766 a_spyp, a_engineerp);
767 if (att_abort(combat_mode, off, def))
774 * Return path cost for ATTACKER to enter sector given by DEF.
775 * MOBTYPE is a mobility type accepted by sector_mcost().
778 att_mobcost(natid attacker, struct combat *def, int mobtype)
783 if (CANT_HAPPEN(def->type != EF_SECTOR))
785 ok = getsect(def->x, def->y, §);
786 if (CANT_HAPPEN(!ok))
790 * We want the cost to move/march into the sector. If we just
791 * called sector_mcost(), we'd get the defender's cost. The
792 * attacker's cost is higher unless he's the old-owner. Note: if
793 * there are no civilians, a victorious attacker will become the
794 * old-owner. But he isn't now.
796 sect.sct_own = attacker;
798 return sector_mcost(§, mobtype);
801 /* How many mil is off allowed to attack with when it attacks def? */
804 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
809 switch (combat_mode) {
811 mobcost = att_mobcost(off->own, def, MOB_MOVE);
812 if (mobcost < 0 || off->mob <= 0)
814 mob_support = off->mob / mobcost;
815 if (mob_support < off->troops)
816 pr("Sector %s has %d mobility which can only support %d mil,\n",
817 xyas(off->x, off->y, player->cnum), off->mob, mob_support);
819 mob_support = off->troops;
822 if (def->own != player->cnum && def->mil) {
823 if (off->shp_mcp->m_flags & M_SEMILAND)
824 return off->troops / 4;
825 else if (!(off->shp_mcp->m_flags & M_LAND))
826 return off->troops / 10;
830 if (off->type == EF_SECTOR && off->mob <= 0)
832 mob_support = def->shp_mcp->m_item[I_MILIT];
833 if (mob_support < off->troops)
834 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
836 mob_support = off->troops;
841 if (def->lnd_lcp->l_flags & L_SPY)
843 mob_support = def->lnd_lcp->l_item[I_MILIT];
844 if (mob_support < off->troops)
845 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
847 mob_support = off->troops;
854 * If the attacker decides to go through with the attack, then the
855 * sectors/ships they are attacking with may be charged some mobility.
856 * This is where that amount of mobility is calculated. It is actually
857 * subtracted "for real" from the object's mobility in put_combat().
861 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
868 switch (combat_mode) {
870 off->mobcost += MAX(1,
872 * att_mobcost(off->own, def, MOB_MOVE)));
875 off->mobcost += MAX(1, attacking_mil / 5);
880 off->mobcost += MAX(1, attacking_mil / 5);
883 /* the 2 in the formula below is a fudge factor */
884 getship(def->shp_uid, &ship);
885 off->mobcost += (def->eff / 100) * (shp_speed(&ship) / 2);
890 /* How many mil to we want to attack from off against def? */
893 ask_off(int combat_mode, struct combat *off, struct combat *def)
899 if (att_get_combat(off, 0) <= 0)
901 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
903 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
905 if (off->type == EF_SECTOR) {
906 if (off->own != player->cnum)
908 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
909 off->sct_dcp->d_name,
910 xyas(off->x, off->y, player->cnum), mob_support);
912 sprintf(prompt, "Number of mil from %s (max %d) : ",
913 prcom(0, off), mob_support);
915 if ((attacking_mil = onearg(NULL, prompt)) < 0)
917 if (att_abort(combat_mode, off, def))
919 if (att_get_combat(off, 0) <= 0)
922 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
925 calc_mobcost(combat_mode, off, def, attacking_mil);
926 return attacking_mil;
930 * Which units would you like to attack with or move in with [ynYNq?]
934 att_prompt(char *prompt, char army)
942 p = getstring(prompt, buf);
943 if (!p || *p == 'q') {
949 if (tolower(*p) == 'y' || tolower(*p) == 'n')
951 pr("y - yes this unit\n"
953 "Y - yes to all units in army '%c'\n"
954 "N - no to all units in army '%c'\n"
955 "q - quit\n? - this help message\n\n",
960 /* Ask the attacker which units they want to attack/assault/board with */
963 ask_olist(int combat_mode, struct combat *off, struct combat *def,
964 struct emp_qelem *olist, char *land_answer, int *a_spyp,
969 double pathcost, mobcost;
979 if (def->type == EF_LAND)
981 if (def->type == EF_SHIP)
982 maxland = def->shp_mcp->m_nland;
984 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
985 while (nxtitem(&ni, &land)) {
986 if (land.lnd_own != player->cnum)
988 if (land.lnd_effic < LAND_MINEFF)
990 if (land_answer[(int)land.lnd_army] == 'N')
992 if (!lnd_can_attack(&land))
994 lcp = &lchr[(int)land.lnd_type];
996 if (def->type == EF_SHIP && !maxland) {
997 pr("Land units are not able to board this kind of ship\n");
1000 if (def->type == EF_SHIP
1001 && (def->shp_mcp->m_flags & (M_SUPPLY | M_SUB)) != M_SUPPLY
1002 && !(lcp->l_flags & L_LIGHT)) {
1003 pr("Only light land units can board this kind of ship\n");
1006 if (land.lnd_mobil <= 0) {
1007 pr("%s is out of mobility, and cannot %s\n",
1008 prland(&land), att_mode[combat_mode]);
1013 if (ontradingblock(EF_LAND, &land)) {
1014 pr("%s is on the trading block, and cannot %s\n",
1015 prland(&land), att_mode[combat_mode]);
1020 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1021 pr("%s is on ship #%d, and cannot %s\n",
1022 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1024 } else if (off->type == EF_SHIP) {
1025 if (land.lnd_ship != off->shp_uid)
1027 } else if (land.lnd_land >= 0) {
1028 pr("%s is on unit #%d, and cannot %s\n",
1029 prland(&land), land.lnd_land, att_mode[combat_mode]);
1032 switch (combat_mode) {
1035 * We used to let land units attack only if they have the
1036 * mobility consumed by the attack, not counting combat
1037 * and moving in to occupy. Making sure your land units
1038 * reach attack positions with enough mobility left is a
1039 * pain in the neck. We now require positive mobility,
1040 * just like for marching. Except we don't allow rushing
1041 * of high-mobility sectors (mountains): for those we
1042 * still require attack mobility.
1044 pathcost = att_mobcost(off->own, def, lnd_mobtype(&land));
1045 mobcost = lnd_pathcost(&land, pathcost);
1046 if (pathcost < 1.0) {
1047 if (land.lnd_mobil <= 0) {
1048 pr("%s is out of mobility\n", prland(&land));
1052 reqmob = MIN(land_mob_max, (int)ceil(mobcost));
1053 if (land.lnd_mobil < reqmob) {
1054 pr("%s does not have enough mobility (%d needed)\n",
1055 prland(&land), reqmob);
1063 if (!(lcp->l_flags & L_ASSAULT))
1070 att_val = attack_val(combat_mode, &land);
1071 if (att_val < 1.0) {
1072 pr("%s has no offensive strength\n", prland(&land));
1075 if (!lnd_supply_all(&land)) {
1076 pr("%s is out of supply, and cannot %s\n",
1077 prland(&land), att_mode[combat_mode]);
1080 if (def->type == EF_SHIP && first_time) {
1082 pr("You may board with a maximum of %d land units\n", maxland);
1084 pr("%s has a base %s value of %.0f\n",
1085 prland(&land), att_mode[combat_mode], att_val);
1086 if (land_answer[(int)land.lnd_army] != 'Y') {
1088 "%s with %s %s (%c %d%%) [ynYNq?] ",
1089 att_mode[combat_mode],
1092 land.lnd_army ? land.lnd_army : '~',
1094 land_answer[(int)land.lnd_army] =
1095 att_prompt(prompt, land.lnd_army);
1096 if (att_abort(combat_mode, off, def))
1098 if (land_answer[(int)land.lnd_army] != 'y' &&
1099 land_answer[(int)land.lnd_army] != 'Y')
1102 if (!(llp = malloc(sizeof(struct ulist)))) {
1103 logerror("Malloc failed in attack!\n");
1107 memset(llp, 0, sizeof(struct ulist));
1108 emp_insque(&llp->queue, olist);
1109 llp->mobil = mobcost;
1110 llp->unit.land = land;
1111 llp->x = llp->unit.land.lnd_x;
1112 llp->y = llp->unit.land.lnd_y;
1113 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1114 llp->eff = llp->unit.land.lnd_effic;
1115 if (lnd_spyval(&land) > *a_spyp)
1116 *a_spyp = lnd_spyval(&land);
1117 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
1119 if (def->type == EF_SHIP && ++count >= maxland)
1124 /* What's the offense or defense multiplier? */
1127 att_combat_eff(struct combat *com)
1133 if (com->type == EF_SECTOR) {
1134 eff = com->eff / 100.0;
1135 if (com->own == player->cnum) {
1136 str = com->sct_dcp->d_ostr;
1137 eff = 1.0 + (str - 1.0) * eff;
1139 eff = sector_strength(getsectp(com->x, com->y));
1140 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1141 getship(com->shp_uid, &ship);
1142 eff = 1.0 + shp_armor(&ship) / 100.0;
1148 att_get_offense(int combat_mode, struct combat *off,
1149 struct emp_qelem *olist, struct combat *def)
1154 * Get the attacker units & mil again in case they changed while the
1155 * attacker was answering sub-prompts.
1158 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1159 if (att_empty_attack(combat_mode, ototal, def))
1160 return abort_attack();
1161 if (combat_mode == A_PARA)
1163 pr("\n Initial attack strength: %8d\n", ototal);
1167 /* Get the defensive units and reacting units */
1169 att_get_defense(struct emp_qelem *olist, struct combat *def,
1170 struct emp_qelem *dlist, int a_spy, int ototal)
1173 struct emp_qelem *qp;
1179 get_dlist(def, dlist, 0, &d_spy);
1180 dtotal = get_dtotal(def, dlist, 1.0, 0);
1183 * Call in reacting units
1186 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1187 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1189 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1190 llp = (struct ulist *)qp;
1191 intelligence_report(def->own, &llp->unit.land, d_spy,
1192 "Scouts report attacking unit:");
1195 old_dtotal = dtotal;
1196 dtotal = get_dtotal(def, dlist, 1.0, 0);
1197 if (dtotal != old_dtotal)
1198 pr("Defense strength with reacting units: %8d\n", dtotal);
1203 /* Get the defensive land units in the sector or on the ship */
1206 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1209 struct nstr_item ni;
1213 /* In here is where you need to take out spies and trains from the defending
1214 lists. Spies try to hide, trains get trapped and can be boarded. */
1216 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1217 while (nxtitem(&ni, &land)) {
1220 if (land.lnd_own != def->own)
1222 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1224 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1226 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1228 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1230 intelligence_report(player->cnum, &land, a_spy,
1231 "Scouts report defending unit:");
1232 if (!(llp = malloc(sizeof(struct ulist)))) {
1233 logerror("Malloc failed in attack!\n");
1237 memset(llp, 0, sizeof(struct ulist));
1238 emp_insque(&llp->queue, list);
1239 llp->supplied = lnd_supply_all(&land);
1240 llp->unit.land = land;
1241 llp->x = llp->unit.land.lnd_x;
1242 llp->y = llp->unit.land.lnd_y;
1243 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1244 llp->eff = llp->unit.land.lnd_effic;
1245 if (lnd_spyval(&land) > *d_spyp)
1246 *d_spyp = lnd_spyval(&land);
1250 /* Calculate the total offensive strength */
1253 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1254 double osupport, int check)
1256 double ototal = 0.0;
1257 struct emp_qelem *qp, *next;
1262 * first, total the attacking mil
1265 for (n = 0; n <= off->last; ++n) {
1266 if (off[n].type == EF_BAD || (check &&
1267 att_get_combat(&off[n], 0) <= 0))
1269 ototal += off[n].troops * att_combat_eff(off + n);
1273 * next, add in the attack_values of all
1274 * the attacking units
1277 for (qp = olist->q_forw; qp != olist; qp = next) {
1279 llp = (struct ulist *)qp;
1281 !get_land(combat_mode, NULL, llp, 0))
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_delete(llp, "is in a sector not owned by you");
1296 ototal += attack_val(combat_mode, &llp->unit.land) *
1297 att_combat_eff(off + w);
1299 ototal += attack_val(combat_mode, &llp->unit.land);
1304 return ldround(ototal, 1);
1307 /* Calculate the total defensive strength */
1310 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1313 double dtotal = 0.0, eff = 1.0, d_unit;
1314 struct emp_qelem *qp, *next;
1317 if (check && att_get_combat(def, 1) < 0)
1319 eff = att_combat_eff(def);
1320 dtotal = def->troops * eff;
1323 * next, add in the defense_values of all
1324 * the defending non-retreating units
1327 for (qp = list->q_forw; qp != list; qp = next) {
1329 llp = (struct ulist *)qp;
1330 if (check && !get_land(A_DEFEND, def, llp, 1))
1332 d_unit = defense_val(&llp->unit.land);
1335 dtotal += d_unit * eff;
1340 return ldround(dtotal, 1);
1344 * This is the land unit integrity check. Note that we don't print
1345 * warnings about victim land units because the attacker may not have seen them
1349 get_land(int combat_mode, struct combat *def, struct ulist *llp,
1352 struct lndstr *lp = &llp->unit.land;
1355 getland(llp->unit.land.lnd_uid, lp);
1357 if (lp->lnd_effic < LAND_MINEFF) {
1358 sprintf(buf, "was destroyed and is no longer a part of the %s",
1359 att_mode[combat_mode]);
1360 lnd_delete(llp, buf);
1364 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1366 "left to go fight another battle and is no longer a part of the defense");
1370 if (lp->lnd_own != player->cnum) {
1372 "was destroyed and is no longer a part of the %s",
1373 att_mode[combat_mode]);
1374 lnd_delete(llp, buf);
1377 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1379 "left to fight another battle and is no longer a part of the %s",
1380 att_mode[combat_mode]);
1381 lnd_delete(llp, buf);
1384 if (lp->lnd_effic < llp->eff) {
1385 sprintf(buf, "damaged from %d%% to %d%%",
1386 llp->eff, lp->lnd_effic);
1387 lnd_print(llp, buf);
1390 llp->eff = llp->unit.land.lnd_effic;
1396 * Put the land unit on the disk. If there was some mobility cost, then
1397 * subtract it from the units mobility. Note that this works the same way
1398 * as sectors & ships in that no mobility is actually taken until the attacker
1399 * has committed to attacking.
1403 kill_land(struct emp_qelem *list)
1405 struct emp_qelem *qp, *next;
1408 for (qp = list->q_forw; qp != list; qp = next) {
1410 llp = (struct ulist *)qp;
1411 if (llp->unit.land.lnd_ship >= 0) {
1412 llp->unit.land.lnd_effic = 0;
1413 lnd_delete(llp, "cannot return to the ship, and dies!");
1419 att_infect_units(struct emp_qelem *list, int plague)
1421 struct emp_qelem *qp, *next;
1426 for (qp = list->q_forw; qp != list; qp = next) {
1428 llp = (struct ulist *)qp;
1429 if (llp->unit.land.lnd_pstage == PLG_HEALTHY)
1430 llp->unit.land.lnd_pstage = PLG_EXPOSED;
1435 put_land(struct emp_qelem *list)
1437 struct emp_qelem *qp, *next;
1440 for (qp = list->q_forw; qp != list; qp = next) {
1442 llp = (struct ulist *)qp;
1443 llp->unit.land.lnd_mission = 0;
1444 llp->unit.land.lnd_harden = 0;
1445 llp->unit.land.lnd_mobil -= (int)llp->mobil;
1447 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1448 if (llp->unit.land.lnd_own != player->cnum) {
1449 emp_remque((struct emp_qelem *)llp);
1452 get_land(A_ATTACK, NULL, llp, 0);
1457 * Keep sending in reinforcements until it looks like we're going to win.
1458 * Note that the "strength" command also calls this routine.
1462 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1463 int *d_spyp, int ototal)
1465 struct nstr_item ni;
1467 struct sctstr sect, dsect;
1470 double new_land = 0;
1474 double eff = att_combat_eff(def);
1478 dtotal = get_dtotal(def, list, 1.0, 1);
1481 snxtitem_all(&ni, EF_LAND);
1482 while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1485 if (land.lnd_mission != MI_RESERVE)
1487 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1489 if (land.lnd_own != def->own)
1491 if (land.lnd_ship >= 0)
1493 if (land.lnd_land >= 0)
1495 if (defense_val(&land) < 1.0)
1497 if (!lnd_can_attack(&land))
1500 /* Only supplied units can react */
1501 if (list ? !lnd_supply_all(&land) : !lnd_could_be_supplied(&land))
1504 if (!in_oparea((struct empobj *)&land, def->x, def->y))
1507 getsect(land.lnd_x, land.lnd_y, §);
1508 getsect(def->x, def->y, &dsect);
1509 if (!BestLandPath(buf, §, &dsect, &pathcost,
1510 lnd_mobtype(&land)))
1513 mobcost = lnd_pathcost(&land, pathcost);
1514 if (land.lnd_mobil < mobcost)
1517 new_land += defense_val(&land);
1519 if (!list) /* we are in the "strength" command */
1522 /* move to defending sector */
1523 land.lnd_mobil -= ldround(mobcost, 1);
1526 land.lnd_x = def->x;
1527 land.lnd_y = def->y;
1528 putland(land.lnd_uid, &land);
1529 wu(0, land.lnd_own, "%s reacts to %s.\n",
1530 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1532 llp = malloc(sizeof(struct ulist));
1534 memset(llp, 0, sizeof(struct ulist));
1538 llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
1539 llp->unit.land = land;
1540 llp->eff = land.lnd_effic;
1541 emp_insque(&llp->queue, list);
1542 if (lnd_spyval(&land) > *d_spyp)
1543 *d_spyp = lnd_spyval(&land);
1545 intelligence_report(player->cnum, &land, a_spy,
1546 "Scouts sight reacting enemy unit:");
1551 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1554 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1555 int land_sup, int plane_sup)
1557 double osupport = 1.0;
1559 double af, as, au, ap;
1561 af = as = au = ap = 0.0;
1563 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1568 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1575 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1581 dam = off_support(def->x, def->y, def->own, player->cnum);
1585 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1590 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1593 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1594 int ototal, int dtotal)
1596 double dsupport = 1.0;
1598 double df, ds, du, dp;
1601 df = ds = du = dp = 0.0;
1602 if (dtotal < 0.1 * ototal) {
1604 } else if (dtotal >= 1.2 * ototal) {
1607 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1611 dtotal = get_dtotal(def, list, dsupport, 0);
1612 if (dtotal < 1.2 * ototal) {
1613 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1616 dtotal = get_dtotal(def, list, dsupport, 0);
1618 if (dtotal < 1.2 * ototal) {
1619 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1622 dtotal = get_dtotal(def, list, dsupport, 1);
1624 if (dtotal < 1.2 * ototal) {
1625 dam = def_support(def->x, def->y, player->cnum, def->own);
1633 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1638 "\nOdds are bad for us...support cancelled.\n\n");
1641 "\nOdds are good for us...support cancelled.\n\n");
1647 * Land mines add to the defense multiplier. If the attacker has engineers
1648 * then this multiplier is cut in half.
1652 get_mine_dsupport(struct combat *def, int a_engineer)
1657 getsect(def->x, def->y, §);
1659 if (sect.sct_oldown != player->cnum) {
1660 mines = SCT_LANDMINES(§);
1661 mines = MIN(mines, 20);
1663 mines = ldround(mines / 2.0, 1);
1666 wu(0, def->own, "Defending mines add %1.2f\n",
1668 pr("Defending mines add %1.2f\n", mines * 0.02);
1669 return mines * 0.02;
1675 /* Get the offensive and defensive support */
1677 att_get_support(int combat_mode, int ofort, int oship, int oland,
1678 int oplane, struct emp_qelem *olist, struct combat *off,
1679 struct emp_qelem *dlist, struct combat *def,
1680 double *osupportp, double *dsupportp, int a_engineer)
1683 char osupports[512];
1684 char dsupports[512];
1686 if (combat_mode == A_PARA)
1689 *osupportp = get_osupport(osupports, def,
1690 ofort, oship, oland, oplane);
1693 * I need to put a 1 at the end of the next four total_stren calls
1694 * because units & mil may have been damaged by collateral damage or
1695 * nuclear warheads from the offensive & defensive support.
1698 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1699 if (att_empty_attack(combat_mode, ototal, def))
1700 return abort_attack();
1701 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1704 * Calculate defensive support. If odds are too good or too bad
1705 * then don't call in support.
1708 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1709 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1710 if (att_empty_attack(combat_mode, ototal, def))
1711 return abort_attack();
1713 if ((*osupports || *dsupports) &&
1714 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1715 pr("\n\t\tsupport values\n");
1716 pr("\t\tforts\tships\tunits\tplanes\n");
1717 if (*osupportp != 1.0)
1718 pr("%s", osupports);
1719 if (*dsupportp != 1.0)
1720 pr("%s", dsupports);
1722 wu(0, def->own, "\n\t\tsupport values\n");
1723 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1724 if (*osupportp != 1.0)
1725 wu(0, def->own, "%s", osupports);
1726 if (*dsupportp != 1.0)
1727 wu(0, def->own, "%s", dsupports);
1731 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1732 if (dtotal && def->type == EF_SECTOR)
1733 *dsupportp += get_mine_dsupport(def, a_engineer);
1737 /* How many two-legged bipeds are in this combat force? */
1740 count_bodies(struct combat *off, struct emp_qelem *list)
1744 struct emp_qelem *qp;
1747 for (n = 0; n <= off->last; ++n)
1748 bodies += off[n].troops;
1749 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1750 llp = (struct ulist *)qp;
1751 bodies += llp->unit.land.lnd_item[I_MILIT];
1756 /* This is where the fighting actually occurs. */
1759 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1760 double osupport, struct combat *def, struct emp_qelem *dlist,
1764 int a_cas = 0; /* Casualty counts */
1766 int ototal; /* total attacking strength */
1767 int dtotal; /* total defending strength */
1768 int a_bodies; /* total attacking mil (incl. mil in units) */
1769 int d_bodies; /* total defending mil (incl. mil in units) */
1779 ototal = get_ototal(combat_mode, off, olist, osupport,
1780 combat_mode != A_PARA);
1781 dtotal = get_dtotal(def, dlist, dsupport, 0);
1785 a_bodies = count_bodies(off, olist);
1786 d_bodies = count_bodies(def, dlist);
1787 d_mil = def->troops;
1788 for (n = 0; n <= off->last; ++n)
1789 if (off[n].type == EF_BAD)
1792 a_troops[n] = off[n].troops;
1794 /* This switch is required to get the spacing right */
1795 switch (combat_mode) {
1797 pr(" Final attack strength: %8d\n", ototal);
1800 pr(" Final assault strength: %8d\n", ototal);
1803 if (def->sct_type == SCT_MOUNT ||
1804 def->sct_type == SCT_WATER ||
1805 def->sct_type == SCT_CAPIT ||
1806 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1807 pr("You can't air-assault a %s sector!\n",
1808 def->sct_dcp->d_name);
1811 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1813 pr(" Final air-assault strength: %8d\n", ototal);
1817 pr(" Final board strength: %8d\n", ototal);
1821 pr(" Final defense strength: %8d\n", dtotal);
1822 odds = att_calcodds(ototal, dtotal);
1823 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1825 /* spread the plague */
1826 if (combat_mode != A_PARA) {
1828 for (n = 0; n <= off->last; ++n)
1829 if (off[n].type != EF_BAD)
1830 def->plague |= off[n].plague;
1831 for (n = 0; n <= off->last; ++n)
1832 if (off[n].type != EF_BAD)
1833 off[n].plague |= def->plague;
1835 att_infect_units(olist, off->plague);
1836 att_infect_units(dlist, def->plague);
1839 * Fighting is slightly random. There is always that last little
1840 * effort you see people put in. Or the stray bullet that takes out
1841 * an officer and the rest go into chaos. Things like that.
1842 * Thus, we have added a very slight random factor that will sometimes
1843 * allow the little guy to win. We modify the odds a little
1844 * (either +- 5%) to account for this randomness. We also only
1845 * recalculate the odds every 8-50 casualties, not every cacsualty,
1846 * since a single dead guy normally wouldn't cause a commander to
1847 * rethink his strategies, but 50 dead guys might.
1849 odds += (random() % 11 - 5) / 100.0;
1854 recalctime = 8 + (random() % 43);
1855 while (!success && ototal) {
1858 d_cas += take_casualty(A_DEFEND, def, dlist);
1859 dtotal = get_dtotal(def, dlist, dsupport, 0);
1864 a_cas += take_casualty(combat_mode, off, olist);
1865 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1867 if (((a_cas + d_cas) % 70) == 69)
1869 if (recalctime-- <= 0) {
1870 recalctime = 8 + (random() % 43);
1871 odds = att_calcodds(ototal, dtotal);
1872 odds += (random() % 11 - 5) / 100.0;
1880 /* update defense mobility & mil */
1884 if (def->type == EF_SECTOR && d_mil && d_cas) {
1888 newmob = damage(def->mob, 100 * d_cas / d_mil);
1889 def->mobcost = MIN(20, def->mob - newmob);
1892 def->mil = def->troops;
1895 /* update attack mobility & mil */
1896 for (n = 0; n <= off->last; ++n)
1897 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1898 if (off[n].type == EF_SECTOR && off[n].mil) {
1899 if (!CANT_HAPPEN(off[n].mob < 0)) {
1900 newmob = damage(off[n].mob,
1901 100 * (a_troops[n] - off[n].troops)
1903 off[n].mobcost += MIN(20, off[n].mob - newmob);
1906 off[n].mil -= a_troops[n] - off[n].troops;
1909 /* update land unit mobility */
1910 if (d_bodies && d_cas)
1911 lnd_takemob(dlist, (double)d_cas / d_bodies);
1912 if (a_bodies && a_cas)
1913 lnd_takemob(olist, (double)a_cas / a_bodies);
1915 /* damage attacked sector */
1916 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1918 pr("- Casualties -\n Yours: %d\n", a_cas);
1919 pr(" Theirs: %d\n", d_cas);
1920 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1921 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1924 switch (combat_mode) {
1926 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1927 pr("We have captured %s, sir!\n", prcom(0, def));
1931 news_item = def->own ? N_AWON_SECT : N_START_COL;
1932 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1933 action = "assaulting and taking";
1936 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1937 pr("We have captured %s, sir!\n", prcom(0, def));
1938 action = "air-assaulting and taking";
1941 news_item = N_BOARD_SHIP;
1942 pr("We have boarded %s, sir!\n", prcom(0, def));
1943 action = "boarding";
1946 news_item = N_BOARD_LAND;
1947 pr("We have boarded %s, sir!\n", prcom(0, def));
1948 action = "boarding";
1953 action = "defeating";
1956 switch (combat_mode) {
1958 news_item = N_SCT_LOSE;
1959 pr("You have been defeated!\n");
1960 action = "attacking";
1963 news_item = N_ALOSE_SCT;
1964 pr("You have been defeated!\n");
1966 action = "trying to assault";
1969 news_item = N_PLOSE_SCT;
1970 pr("All of your troops were destroyed\n");
1971 action = "trying to air-assault";
1974 news_item = N_SHP_LOSE;
1975 pr("You have been repelled\n");
1977 action = "trying to board";
1980 news_item = N_LND_LOSE;
1981 pr("You have been repelled\n");
1983 action = "trying to board";
1988 action = "fighting";
1991 nreport(player->cnum, news_item, def->own, 1);
1994 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
1995 cname(player->cnum), player->cnum, a_cas,
1996 action, pr_com(0, def, def->own), d_cas);
1999 send_reacting_units_home(dlist);
2001 /* putland the defending land */
2004 /* putland the attacking land */
2007 /* put the victim sector/ship/land */
2008 if (!success || !take_def(combat_mode, olist, off, def))
2011 /* put the attacking sectors/ship */
2012 for (n = 0; n <= off->last; ++n)
2013 if (off[n].type != EF_BAD)
2014 put_combat(&off[n]);
2019 switch (combat_mode) {
2021 ask_move_in(off, olist, def);
2023 /* put sectors again to get abandon warnings */
2024 for (n = 0; n <= off->last; ++n)
2025 if (off[n].type != EF_BAD)
2026 put_combat(&off[n]);
2029 att_move_in_off(combat_mode, off, olist, def);
2032 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2036 /* What percentage of the combat forces going head-to-head are we? */
2039 att_calcodds(int ototal, int dtotal)
2043 /* calculate odds */
2046 else if (dtotal <= 0)
2049 odds = (double)ototal / (dtotal + ototal);
2054 /* Here's where the dead soldiers get dragged off the battlefield */
2057 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2059 int to_take = CASUALTY_LUMP;
2060 int biggest_troops = 0, index = -1;
2061 int n, tot_troops = 0, biggest_mil, cas;
2062 struct emp_qelem *qp, *biggest;
2065 for (n = 0; n <= off->last; ++n) {
2066 if (off[n].type != EF_BAD) {
2067 tot_troops += off[n].troops;
2068 if (off[n].troops > biggest_troops) {
2069 biggest_troops = off[n].troops;
2076 to_take -= tot_troops;
2079 for (n = 0; n <= off->last; ++n)
2080 if (off[n].type != EF_BAD)
2084 * They can all come off mil. We rotate the casualties,
2085 * starting with the sector containing the most mil.
2087 to_take = CASUALTY_LUMP;
2089 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2092 while (to_take > 0) {
2093 for (n = index; n <= off->last && to_take; ++n) {
2094 if (off[n].type != EF_BAD && off[n].troops > 0) {
2099 for (n = 0; n < index && to_take; ++n) {
2100 if (off[n].type != EF_BAD && off[n].troops > 0) {
2106 return CASUALTY_LUMP;
2110 return CASUALTY_LUMP - to_take;
2113 * Need to take some casualties from attacking units
2114 * Procedure: find the biggest unit remaining (in
2115 * terms of mil) and give it the casualties.
2119 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2120 llp = (struct ulist *)qp;
2122 if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2123 biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2127 if (biggest == NULL)
2128 return CASUALTY_LUMP - to_take;
2130 llp = (struct ulist *)biggest;
2131 cas = lnd_take_casualty(combat_mode, llp, to_take);
2132 return CASUALTY_LUMP - (to_take - cas);
2135 /* Send reacting defense units back to where they came from (at no mob cost) */
2138 send_reacting_units_home(struct emp_qelem *list)
2140 struct emp_qelem *qp, *next;
2144 for (qp = list->q_forw; qp != list; qp = next) {
2146 llp = (struct ulist *)qp;
2147 if ((llp->unit.land.lnd_x != llp->x) ||
2148 (llp->unit.land.lnd_y != llp->y)) {
2149 sprintf(buf, "returns to %s",
2150 xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2151 llp->unit.land.lnd_x = llp->x;
2152 llp->unit.land.lnd_y = llp->y;
2153 lnd_delete(llp, buf);
2158 /* Check for 0 offense strength. This call will always preceed an abort */
2161 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2164 if (def->own && player->cnum != def->own) {
2166 "%s (#%d) considered %sing you @%s\n",
2167 cname(player->cnum), player->cnum,
2168 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2170 pr("No troops for %s...\n", att_mode[combat_mode]);
2177 * Take the defending sector or ship from the defender and give it to the
2182 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2187 struct ulist *llp, *delete_me = NULL;
2193 for (n = 0; n <= off->last && !occuppied; ++n) {
2194 if (off[n].type != EF_BAD &&
2195 off[n].troops > 0 &&
2196 (off[n].type != EF_SECTOR || off[n].mob)) {
2198 if (def->type == EF_LAND) {
2199 if (def->lnd_lcp->l_flags & L_SPY) {
2206 pr("1 mil from %s moves %s\n",
2207 prcom(0, off + n), prcom(2, def));
2212 pr("%s left unoccupied\n", prcom(0, def));
2215 "No enemy troops moved %s so you still own it!\n",
2216 pr_com(2, def, def->own));
2219 llp = (struct ulist *)list->q_forw;
2220 llp->unit.land.lnd_x = def->x;
2221 llp->unit.land.lnd_y = def->y;
2222 take_move_in_mob(combat_mode, llp, off, def);
2223 if (def->type == EF_SHIP) {
2224 llp->unit.land.lnd_ship = def->shp_uid;
2225 sprintf(buf, "boards %s", prcom(0, def));
2228 llp->unit.land.lnd_ship = -1;
2229 sprintf(buf, "moves in to occupy %s",
2230 xyas(def->x, def->y, player->cnum));
2231 lnd_delete(llp, buf);
2236 if (def->type == EF_SECTOR) {
2237 getsect(def->x, def->y, §);
2238 takeover(§, player->cnum);
2239 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2240 caploss(§, def->own,
2241 "* We have captured %s's capital, sir! *\n");
2243 } else if (def->type == EF_SHIP) {
2244 getship(def->shp_uid, &ship);
2245 takeover_ship(&ship, player->cnum);
2246 putship(ship.shp_uid, &ship);
2247 } else if (def->type == EF_LAND) {
2248 getland(def->lnd_uid, &land);
2249 takeover_land(&land, player->cnum);
2250 putland(land.lnd_uid, &land);
2253 lnd_delete(delete_me, buf);
2254 att_get_combat(def, 0);
2259 * Ask the attacker which mil & land units they'd like to move into the
2264 ask_move_in(struct combat *off, struct emp_qelem *olist,
2268 struct emp_qelem *qp, *next;
2272 char land_answer[256];
2275 for (n = 0; n <= off->last; ++n)
2276 if (off[n].type != EF_BAD && off[n].troops > 0)
2278 ask_move_in_off(&off[n], def);
2279 if (player->aborted)
2285 memset(land_answer, 0, sizeof(land_answer));
2286 for (qp = olist->q_forw; qp != olist; qp = next) {
2288 llp = (struct ulist *)qp;
2289 answerp = &land_answer[(int)llp->unit.land.lnd_army];
2290 if (player->aborted || att_get_combat(def, 0) < 0)
2292 if (*answerp == 'Y')
2294 if (!get_land(A_ATTACK, def, llp, 0))
2296 if (*answerp != 'N') {
2297 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2298 prland(&llp->unit.land),
2299 llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2300 llp->unit.land.lnd_effic);
2301 *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2302 if (player->aborted || att_get_combat(def, 0) < 0)
2304 if (!get_land(A_ATTACK, def, llp, 0))
2307 if (*answerp == 'y' || *answerp == 'Y')
2309 sprintf(buf, "stays in %s",
2310 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2312 lnd_delete(llp, buf);
2316 if (att_get_combat(def, 0) < 0) {
2317 for (qp = olist->q_forw; qp != olist; qp = next) {
2319 llp = (struct ulist *)qp;
2320 if (!get_land(A_ATTACK, def, llp, 0))
2322 sprintf(buf, "stays in %s",
2323 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2325 lnd_delete(llp, buf);
2329 if (opt_INTERDICT_ATT)
2330 lnd_interdict(olist, def->x, def->y, player->cnum);
2331 move_in_land(A_ATTACK, off, olist, def);
2334 /* Move offensive land units to the conquered sector or ship */
2337 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2340 struct emp_qelem *qp, *next;
2346 for (qp = olist->q_forw; qp != olist; qp = next) {
2348 llp = (struct ulist *)qp;
2349 if (!get_land(combat_mode, def, llp, 0))
2351 take_move_in_mob(combat_mode, llp, off, def);
2352 llp->unit.land.lnd_x = def->x;
2353 llp->unit.land.lnd_y = def->y;
2354 if (def->type == EF_SHIP)
2355 llp->unit.land.lnd_ship = def->shp_uid;
2357 llp->unit.land.lnd_ship = -1;
2361 if (def->type == EF_SECTOR) {
2362 if (opt_INTERDICT_ATT) {
2363 lnd_sweep(olist, 0, 0, def->own);
2364 lnd_check_mines(olist);
2366 sprintf(buf, "now occupies %s", prcom(0, def));
2368 sprintf(buf, "boards %s", prcom(0, def));
2372 for (qp = olist->q_forw; qp != olist; qp = next) {
2374 llp = (struct ulist *)qp;
2375 lnd_print(llp, buf);
2383 * Move assaulting, paradropping, or boarding mil & units into def
2384 * If the mil are coming from a ship, then pack a lunch.
2388 att_move_in_off(int combat_mode, struct combat *off,
2389 struct emp_qelem *olist, struct combat *def)
2396 move_in_land(combat_mode, off, olist, def);
2398 for (n = 0; n <= off->last; ++n) {
2399 if (off[n].type == EF_BAD || !off[n].troops)
2401 troops = off[n].troops;
2403 off[n].mil -= troops;
2405 put_combat(off + n);
2406 if (combat_mode == A_ASSAULT) {
2407 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2409 getship(off[n].shp_uid, &ship);
2410 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2411 / (ship.shp_item[I_MILIT] + troops
2412 + ship.shp_item[I_CIVIL] + 0.5));
2414 ship.shp_item[I_FOOD] -= lunchbox;
2415 putship(ship.shp_uid, &ship);
2421 if (combat_mode == A_ASSAULT) {
2422 if (CANT_HAPPEN(def->type != EF_SECTOR))
2424 getsect(def->x, def->y, §);
2425 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2426 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2427 sect.sct_item[I_FOOD] += lunchbox;
2433 /* Ask how many mil to move in from each sector */
2436 ask_move_in_off(struct combat *off, struct combat *def)
2439 int num_mil, dam = 0, left;
2445 if (att_get_combat(off, 0) <= 0)
2447 if (att_get_combat(def, 0) < 0)
2449 if (off->own != player->cnum)
2451 d = att_mobcost(off->own, def, MOB_MOVE);
2452 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2454 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2455 xyas(off->x, off->y, player->cnum), mob_support);
2456 p = getstring(prompt, buf);
2457 if (!p || !*p || (num_mil = atoi(p)) <= 0)
2459 /* Make sure we don't move in more than we can support mobility-wise */
2460 if (num_mil > mob_support)
2461 num_mil = mob_support;
2462 if (att_get_combat(off, 0) <= 0)
2464 if (att_get_combat(def, 0) < 0)
2466 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2467 pr("No mil moved in from %s\n",
2468 xyas(off->x, off->y, player->cnum));
2471 mob_support = MAX(1, (int)(num_mil * d));
2472 off->mob -= MIN(off->mob, mob_support);
2473 off->mil -= num_mil;
2474 off->troops -= num_mil;
2477 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2478 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2479 if (chance(weight / 100.0))
2481 ground_interdict(def->x, def->y, player->cnum, "military");
2482 dam += check_lmines(def->x, def->y, weight);
2486 left = commdamage(num_mil, dam, I_MILIT);
2487 if (left < num_mil) {
2489 pr("%d of the mil you were moving were destroyed!\nOnly %d mil made it to %s\n", num_mil - left, left, xyas(def->x, def->y, player->cnum));
2491 pr("All of the mil you were moving were destroyed!\n");
2494 /* maybe got nuked */
2495 if (att_get_combat(def, 0) < 0)
2503 /* Charge land units for moving into a sector or onto a ship */
2506 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2509 double mob = llp->unit.land.lnd_mobil;
2510 double gain = etu_per_update * land_mob_scale;
2511 double mobcost, moblim;
2513 switch (combat_mode) {
2515 mobcost = lnd_pathcost(&llp->unit.land,
2516 att_mobcost(off->own, def,
2517 lnd_mobtype(&llp->unit.land)));
2521 * Set mobcost to basic assault cost, moblim to maximum
2522 * mobility to keep when assaulting from non-landing ship
2524 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE) {
2525 mobcost = gain / 2.0;
2531 if (!(off->shp_mcp->m_flags & M_LAND))
2532 /* Not a landing ship, ensure we go to or below moblim */
2533 mobcost = MAX(mobcost, mob - moblim);
2536 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
2549 llp->unit.land.lnd_mobil = (signed char)mob;
2550 llp->unit.land.lnd_harden = 0;
2554 free_list(struct emp_qelem *list)
2556 struct emp_qelem *qp, *next;
2558 if (!list || QEMPTY(list))
2562 while (qp != list) {
2571 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2579 * sector_strength - Everyone starts at 1. You can get up to a max
2580 * of d_dstr, depending on how much you build up the
2581 * defenses of the sector.
2585 sector_strength(struct sctstr *sp)
2587 double def = SCT_DEFENSE(sp) / 100.0;
2588 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2589 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2591 if (d > dchr[sp->sct_type].d_dstr)
2592 d = dchr[sp->sct_type].d_dstr;