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 != com->own && owner != player->cnum) {
289 /* must be EF_SECTOR */
291 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
292 com->mil, xyas(com->x, com->y, player->cnum),
295 pr("%s just took %s!\n",
296 cname(owner), xyas(com->x, com->y, player->cnum));
297 return att_combat_init(com, EF_BAD);
299 if (com->troops && com->troops + 1 > mil) {
300 if (com->own == owner && player->cnum == owner)
302 pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
303 prcom(1, com), com->troops, MAX(0, mil - 1));
304 com->troops = MAX(0, mil - 1);
319 * In the course of the fight, the combat object may have lost mil, eff, or
320 * mobility. This is the place where the data in the object gets flushed to
321 * disk to make it "real".
325 put_combat(struct combat *com)
334 getsect(com->x, com->y, §);
335 sect.sct_type = com->sct_type;
336 deff = sect.sct_effic - com->eff;
338 sect.sct_road -= sect.sct_road * deff / 100.0;
339 sect.sct_rail -= sect.sct_rail * deff / 100.0;
340 sect.sct_defense -= sect.sct_defense * deff / 100.0;
341 if (sect.sct_road <= 0)
343 if (sect.sct_rail <= 0)
345 if (sect.sct_defense <= 0)
346 sect.sct_defense = 0;
348 sect.sct_effic = com->eff;
350 if (opt_MOB_ACCESS) {
351 if ((com->mob - com->mobcost) < -127)
352 sect.sct_mobil = -127;
354 sect.sct_mobil = com->mob - com->mobcost;
356 if ((com->mob - com->mobcost) < 0)
359 sect.sct_mobil = com->mob - com->mobcost;
362 sect.sct_own = com->own;
364 if (sect.sct_pstage == PLG_HEALTHY)
365 sect.sct_pstage = PLG_EXPOSED;
367 sect.sct_item[I_MILIT] = com->mil;
369 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
372 getland(com->lnd_uid, &land);
373 land.lnd_effic = com->eff;
375 if (com->mob - com->mobcost < -127)
376 land.lnd_mobil = -127;
378 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
380 land.lnd_own = com->own;
382 if (land.lnd_pstage == PLG_HEALTHY)
383 land.lnd_pstage = PLG_EXPOSED;
385 if (!(com->lnd_lcp->l_flags & L_SPY))
386 land.lnd_item[I_MILIT] = com->mil;
387 if (com->own == player->cnum) {
388 land.lnd_mission = 0;
390 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
392 putland(com->lnd_uid, &land);
395 getship(com->shp_uid, &ship);
396 ship.shp_effic = com->eff;
398 if (com->mob - com->mobcost < -127)
399 ship.shp_mobil = -127;
401 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
403 ship.shp_own = com->own;
405 if (ship.shp_pstage == PLG_HEALTHY)
406 ship.shp_pstage = PLG_EXPOSED;
408 ship.shp_item[I_MILIT] = com->mil;
409 if (com->own == player->cnum) {
410 ship.shp_mission = 0;
412 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
414 putship(com->shp_uid, &ship);
417 att_get_combat(com, com->own != player->cnum);
420 /* If pre-attack, abort fight. If post-attack, don't move anything in */
425 return player->aborted = 1;
429 * This is the combat_mode based integrity check. It splits among two main
430 * divisions: first time/not first time, and attack/assault/para/board.
434 att_abort(int combat_mode, struct combat *off, struct combat *def)
440 if (att_get_combat(def, 1) < 0)
441 return abort_attack();
443 if (off && combat_mode != A_ATTACK) {
444 if (att_get_combat(off, 0) < 0)
445 return abort_attack();
446 if (off->type == EF_SHIP &&
447 !(off->x == def->x && off->y == def->y) &&
448 (!getsect(off->x, off->y, §) ||
449 sect.sct_type != SCT_WATER)) {
450 pr("%s can not %s from that far inland!\n",
451 prcom(0, off), att_mode[combat_mode]);
452 return abort_attack();
455 switch (combat_mode) {
457 if (!neigh(def->x, def->y, player->cnum) &&
458 !adj_units(def->x, def->y, player->cnum)) {
459 pr("You are not adjacent to %s\n",
460 xyas(def->x, def->y, player->cnum));
461 return abort_attack();
463 if (def->own == player->cnum) {
464 pr("You can't attack your own sector.\n");
465 return abort_attack();
469 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
470 pr("You'll have to get there first...\n");
471 return abort_attack();
473 if (off && def->sct_type == SCT_MOUNT) {
474 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
475 return abort_attack();
479 if (def->own == player->cnum) {
480 pr("You can't air-assault your own sector.\n");
481 return abort_attack();
483 if (off && (def->sct_type == SCT_MOUNT ||
484 def->sct_type == SCT_WATER ||
485 def->sct_type == SCT_CAPIT ||
486 def->sct_type == SCT_FORTR ||
487 def->sct_type == SCT_WASTE)) {
488 pr("You can't air-assault a %s sector!\n",
489 def->sct_dcp->d_name);
490 return abort_attack();
494 return board_abort(off, def);
496 return land_board_abort(off, def);
499 if (off && def->sct_dcp->d_mob0 < 0) {
500 pr("You can't %s a %s sector!\n",
501 att_mode[combat_mode], def->sct_dcp->d_name);
502 return abort_attack();
504 if (!off || off->relations_checked)
506 off->relations_checked = 1;
509 setcont(player->cnum, def->own, FOUND_SPY);
510 setcont(def->own, player->cnum, FOUND_SPY);
517 * Lots of special things need to be checked for boarding, so I put it in
522 board_abort(struct combat *off, struct combat *def)
524 struct shpstr aship, dship; /* for tech levels */
527 if (att_get_combat(def, 1) < 0)
528 return abort_attack();
533 if (att_get_combat(off, 0) < 0)
534 return abort_attack();
536 if (off->x != def->x || off->y != def->y) {
537 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
538 return abort_attack();
540 if (off->type == EF_SHIP) {
542 pr("%s has no mobility!\n", prcom(0, off));
543 return abort_attack();
545 getship(off->shp_uid, &aship);
546 getship(def->shp_uid, &dship);
547 if (techfact(aship.shp_tech, shp_speed(&aship)) * off->eff
548 <= techfact(dship.shp_tech, shp_speed(&dship)) * def->eff) {
549 pr("Victim ship moves faster than you do!\n");
552 "%s (#%d) %s failed to catch %s\n",
553 cname(aship.shp_own), aship.shp_own,
554 pr_com(0, off, def->own), pr_com(0, def, def->own));
555 return abort_attack();
557 } else if (off->type != EF_SECTOR) {
558 pr("Please tell the deity that you got the 'banana boat' error\n");
559 return abort_attack();
561 if (def->shp_mcp->m_flags & M_SUB) {
562 getsect(def->x, def->y, §);
563 if (sect.sct_type == SCT_WATER) {
564 pr("You can't board a submarine!\n");
565 return abort_attack();
572 * Lots of special things need to be checked for boarding, so I put it in
574 * STM - I copied it for land unit boarding. :)
578 land_board_abort(struct combat *off, struct combat *def)
580 if (att_get_combat(def, 1) < 0)
581 return abort_attack();
586 if (att_get_combat(off, 0) < 0)
587 return abort_attack();
589 if (off->x != def->x || off->y != def->y) {
590 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
591 return abort_attack();
597 /* If we are boarding, then the defending ship gets a chance to fire back */
599 att_approach(struct combat *off, struct combat *def)
605 pr("Approaching %s...\n", prcom(0, def));
608 "%s is being approached by %s...\n",
609 pr_com(0, def, def->own), pr_com(0, off, def->own));
610 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
613 pr("They're firing at us sir!\n");
616 "Your fleet at %s does %d damage to %s\n",
617 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
619 if (off->type == EF_SECTOR) {
620 getsect(off->x, off->y, §);
621 sectdamage(§, dam);
623 pr("Enemy fleet at %s does %d damage to %s\n",
624 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
625 } else if (off->type == EF_SHIP) {
626 getship(off->shp_uid, &ship);
627 shipdamage(&ship, dam);
628 putship(off->shp_uid, &ship);
629 if (def->own && ship.shp_effic < SHIP_MINEFF) {
630 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
631 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
634 if (att_get_combat(off, 0) < 0)
635 return abort_attack();
639 /* The attack is valid. Tell the attacker about what they're going to hit */
642 att_show(struct combat *def)
644 /* Note that we tell the player about the treaty BEFORE we tell them
645 about the item. If we didn't, then they gain free information */
646 if (def->type == EF_SECTOR) {
647 if (!trechk(player->cnum, def->own, LANATT))
648 return abort_attack();
649 pr("%s is a %d%% %s %s with approximately %d military.\n",
650 xyas(def->x, def->y, player->cnum),
651 roundintby((int)def->eff, 10),
652 cname(def->own), def->sct_dcp->d_name,
653 roundintby(def->troops, 10));
654 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
655 writemap(player->cnum);
656 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
657 if (def->type == EF_SHIP) {
658 if (!trechk(player->cnum, def->own, SEAATT))
659 return abort_attack();
661 if (!trechk(player->cnum, def->own, LNDATT))
662 return abort_attack();
664 pr("%s is about %d%% efficient and has approximately %d mil on board.\n",
665 prcom(0, def), roundintby((int)def->eff, 10),
666 roundintby(def->troops, 10));
668 /* Ok, everything is fine */
672 /* Attack and assault ask the user which kind of support they want */
675 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
680 *fortp = *shipp = *landp = *planep = 1;
682 if (player->argp[offset] != NULL) {
683 if ((player->argp[offset + 1] == NULL) ||
684 (player->argp[offset + 2] == NULL) ||
685 (player->argp[offset + 3] == NULL)) {
686 pr("If any support arguments are used, all must be!\n");
691 *landp = *planep = 0;
693 p = getstarg(player->argp[offset], "Use fort support? ", buf);
697 if ((*p == 'y') || (*p == 'Y'))
700 p = getstarg(player->argp[offset + 1], "Use ship support? ", buf);
704 if ((*p == 'y') || (*p == 'Y'))
707 p = getstarg(player->argp[offset + 2], "Use land support? ", buf);
711 if ((*p == 'y') || (*p == 'Y'))
714 p = getstarg(player->argp[offset + 3], "Use plane support? ", buf);
718 if ((*p == 'y') || (*p == 'Y'))
725 * Attack, assault, and board ask the attacker what they'd like to attack
726 * with. This includes mil and land units from each "off" object. Note that
727 * after each sub-prompt, we check to make sure that the attack is still
728 * valid, and if it's not, then we abort the attack.
732 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
733 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
736 char land_answer[256];
739 if (att_abort(combat_mode, off, def))
741 memset(land_answer, 0, sizeof(land_answer));
742 for (n = 0; n <= off->last; ++n) {
743 off[n].troops = ask_off(combat_mode, off + n, def);
744 if (att_abort(combat_mode, off, def))
746 ask_olist(combat_mode, off + n, def, olist, land_answer,
747 a_spyp, a_engineerp);
748 if (att_abort(combat_mode, off, def))
755 * Return path cost for ATTACKER to enter sector given by DEF.
756 * MOBTYPE is a mobility type accepted by sector_mcost().
759 att_mobcost(natid attacker, struct combat *def, int mobtype)
764 if (CANT_HAPPEN(def->type != EF_SECTOR))
766 ok = getsect(def->x, def->y, §);
767 if (CANT_HAPPEN(!ok))
771 * We want the cost to move/march into the sector. If we just
772 * called sector_mcost(), we'd get the defender's cost. The
773 * attacker's cost is higher unless he's the old-owner. Note: if
774 * there are no civilians, a victorious attacker will become the
775 * old-owner. But he isn't now.
777 sect.sct_own = attacker;
779 return sector_mcost(§, mobtype);
782 /* How many mil is off allowed to attack with when it attacks def? */
785 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
790 switch (combat_mode) {
792 mobcost = att_mobcost(off->own, def, MOB_MOVE);
793 if (mobcost < 0 || off->mob <= 0)
795 mob_support = off->mob / mobcost;
796 if (mob_support < off->troops)
797 pr("Sector %s has %d mobility which can only support %d mil,\n",
798 xyas(off->x, off->y, player->cnum), off->mob, mob_support);
800 mob_support = off->troops;
803 if (def->own != player->cnum && def->mil) {
804 if (off->shp_mcp->m_flags & M_SEMILAND)
805 return off->troops / 4;
806 else if (!(off->shp_mcp->m_flags & M_LAND))
807 return off->troops / 10;
811 if (off->type == EF_SECTOR && off->mob <= 0)
813 mob_support = def->shp_mcp->m_item[I_MILIT];
814 if (mob_support < off->troops)
815 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
817 mob_support = off->troops;
822 if (def->lnd_lcp->l_flags & L_SPY)
824 mob_support = def->lnd_lcp->l_item[I_MILIT];
825 if (mob_support < off->troops)
826 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
828 mob_support = off->troops;
835 * If the attacker decides to go through with the attack, then the
836 * sectors/ships they are attacking with may be charged some mobility.
837 * This is where that amount of mobility is calculated. It is actually
838 * subtracted "for real" from the object's mobility in put_combat().
842 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
849 switch (combat_mode) {
851 off->mobcost += MAX(1,
853 * att_mobcost(off->own, def, MOB_MOVE)));
856 off->mobcost += MAX(1, attacking_mil / 5);
861 off->mobcost += MAX(1, attacking_mil / 5);
864 /* the 2 in the formula below is a fudge factor */
865 getship(def->shp_uid, &ship);
866 off->mobcost += (def->eff / 100) * (shp_speed(&ship) / 2);
871 /* How many mil to we want to attack from off against def? */
874 ask_off(int combat_mode, struct combat *off, struct combat *def)
880 if (att_get_combat(off, 0) <= 0)
882 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
884 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
886 if (off->type == EF_SECTOR) {
887 if (off->own != player->cnum)
889 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
890 off->sct_dcp->d_name,
891 xyas(off->x, off->y, player->cnum), mob_support);
893 sprintf(prompt, "Number of mil from %s (max %d) : ",
894 prcom(0, off), mob_support);
896 if ((attacking_mil = onearg(NULL, prompt)) < 0)
898 if (att_abort(combat_mode, off, def))
900 if (att_get_combat(off, 0) <= 0)
903 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
906 calc_mobcost(combat_mode, off, def, attacking_mil);
907 return attacking_mil;
911 * Which units would you like to attack with or move in with [ynYNq?]
915 att_prompt(char *prompt, char army)
923 p = getstring(prompt, buf);
924 if (!p || *p == 'q') {
930 if (tolower(*p) == 'y' || tolower(*p) == 'n')
932 pr("y - yes this unit\n"
934 "Y - yes to all units in army '%c'\n"
935 "N - no to all units in army '%c'\n"
936 "q - quit\n? - this help message\n\n",
941 /* Ask the attacker which units they want to attack/assault/board with */
944 ask_olist(int combat_mode, struct combat *off, struct combat *def,
945 struct emp_qelem *olist, char *land_answer, int *a_spyp,
950 double pathcost, mobcost;
960 if (def->type == EF_LAND)
962 if (def->type == EF_SHIP)
963 maxland = def->shp_mcp->m_nland;
965 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
966 while (nxtitem(&ni, &land)) {
967 if (land.lnd_own != player->cnum)
969 if (land.lnd_effic < LAND_MINEFF)
971 if (land_answer[(int)land.lnd_army] == 'N')
973 if (!lnd_can_attack(&land))
975 lcp = &lchr[(int)land.lnd_type];
977 if (def->type == EF_SHIP && !maxland) {
978 pr("Land units are not able to board this kind of ship\n");
981 if (def->type == EF_SHIP
982 && (def->shp_mcp->m_flags & (M_SUPPLY | M_SUB)) != M_SUPPLY
983 && !(lcp->l_flags & L_LIGHT)) {
984 pr("Only light land units can board this kind of ship\n");
987 if (land.lnd_mobil <= 0) {
988 pr("%s is out of mobility, and cannot %s\n",
989 prland(&land), att_mode[combat_mode]);
994 if (ontradingblock(EF_LAND, &land)) {
995 pr("%s is on the trading block, and cannot %s\n",
996 prland(&land), att_mode[combat_mode]);
1001 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1002 pr("%s is on ship #%d, and cannot %s\n",
1003 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1005 } else if (off->type == EF_SHIP) {
1006 if (land.lnd_ship != off->shp_uid)
1008 } else if (land.lnd_land >= 0) {
1009 pr("%s is on unit #%d, and cannot %s\n",
1010 prland(&land), land.lnd_land, att_mode[combat_mode]);
1013 switch (combat_mode) {
1016 * We used to let land units attack only if they have the
1017 * mobility consumed by the attack, not counting combat
1018 * and moving in to occupy. Making sure your land units
1019 * reach attack positions with enough mobility left is a
1020 * pain in the neck. We now require positive mobility,
1021 * just like for marching. Except we don't allow rushing
1022 * of high-mobility sectors (mountains): for those we
1023 * still require attack mobility.
1025 pathcost = att_mobcost(off->own, def, lnd_mobtype(&land));
1026 mobcost = lnd_pathcost(&land, pathcost);
1027 if (pathcost < 1.0) {
1028 if (land.lnd_mobil <= 0) {
1029 pr("%s is out of mobility\n", prland(&land));
1033 reqmob = MIN(land_mob_max, (int)ceil(mobcost));
1034 if (land.lnd_mobil < reqmob) {
1035 pr("%s does not have enough mobility (%d needed)\n",
1036 prland(&land), reqmob);
1044 if (!(lcp->l_flags & L_ASSAULT))
1051 att_val = attack_val(combat_mode, &land);
1052 if (att_val < 1.0) {
1053 pr("%s has no offensive strength\n", prland(&land));
1056 if (!lnd_supply_all(&land)) {
1057 pr("%s is out of supply, and cannot %s\n",
1058 prland(&land), att_mode[combat_mode]);
1061 if (def->type == EF_SHIP && first_time) {
1063 pr("You may board with a maximum of %d land units\n", maxland);
1065 pr("%s has a base %s value of %.0f\n",
1066 prland(&land), att_mode[combat_mode], att_val);
1067 if (land_answer[(int)land.lnd_army] != 'Y') {
1069 "%s with %s %s (%c %d%%) [ynYNq?] ",
1070 att_mode[combat_mode],
1073 land.lnd_army ? land.lnd_army : '~',
1075 land_answer[(int)land.lnd_army] =
1076 att_prompt(prompt, land.lnd_army);
1077 if (att_abort(combat_mode, off, def))
1079 if (land_answer[(int)land.lnd_army] != 'y' &&
1080 land_answer[(int)land.lnd_army] != 'Y')
1083 if (!(llp = malloc(sizeof(struct ulist)))) {
1084 logerror("Malloc failed in attack!\n");
1088 memset(llp, 0, sizeof(struct ulist));
1089 emp_insque(&llp->queue, olist);
1090 llp->mobil = mobcost;
1091 llp->unit.land = land;
1092 llp->x = llp->unit.land.lnd_x;
1093 llp->y = llp->unit.land.lnd_y;
1094 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1095 llp->eff = llp->unit.land.lnd_effic;
1096 if (lnd_spyval(&land) > *a_spyp)
1097 *a_spyp = lnd_spyval(&land);
1098 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
1100 if (def->type == EF_SHIP && ++count >= maxland)
1105 /* What's the offense or defense multiplier? */
1108 att_combat_eff(struct combat *com)
1114 if (com->type == EF_SECTOR) {
1115 eff = com->eff / 100.0;
1116 if (com->own == player->cnum) {
1117 str = com->sct_dcp->d_ostr;
1118 eff = 1.0 + (str - 1.0) * eff;
1120 eff = sector_strength(getsectp(com->x, com->y));
1121 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1122 getship(com->shp_uid, &ship);
1123 eff = 1.0 + shp_armor(&ship) / 100.0;
1129 att_get_offense(int combat_mode, struct combat *off,
1130 struct emp_qelem *olist, struct combat *def)
1135 * Get the attacker units & mil again in case they changed while the
1136 * attacker was answering sub-prompts.
1139 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1140 if (att_empty_attack(combat_mode, ototal, def))
1141 return abort_attack();
1142 if (combat_mode == A_PARA)
1144 pr("\n Initial attack strength: %8d\n", ototal);
1148 /* Get the defensive units and reacting units */
1150 att_get_defense(struct emp_qelem *olist, struct combat *def,
1151 struct emp_qelem *dlist, int a_spy, int ototal)
1154 struct emp_qelem *qp;
1160 get_dlist(def, dlist, 0, &d_spy);
1161 dtotal = get_dtotal(def, dlist, 1.0, 0);
1164 * Call in reacting units
1167 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1168 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1170 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1171 llp = (struct ulist *)qp;
1172 intelligence_report(def->own, &llp->unit.land, d_spy,
1173 "Scouts report attacking unit:");
1176 old_dtotal = dtotal;
1177 dtotal = get_dtotal(def, dlist, 1.0, 0);
1178 if (dtotal != old_dtotal)
1179 pr("Defense strength with reacting units: %8d\n", dtotal);
1184 /* Get the defensive land units in the sector or on the ship */
1187 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1190 struct nstr_item ni;
1194 /* In here is where you need to take out spies and trains from the defending
1195 lists. Spies try to hide, trains get trapped and can be boarded. */
1197 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1198 while (nxtitem(&ni, &land)) {
1201 if (land.lnd_own != def->own)
1203 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1205 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1207 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1209 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1211 intelligence_report(player->cnum, &land, a_spy,
1212 "Scouts report defending unit:");
1213 if (!(llp = malloc(sizeof(struct ulist)))) {
1214 logerror("Malloc failed in attack!\n");
1218 memset(llp, 0, sizeof(struct ulist));
1219 emp_insque(&llp->queue, list);
1220 llp->supplied = lnd_supply_all(&land);
1221 llp->unit.land = land;
1222 llp->x = llp->unit.land.lnd_x;
1223 llp->y = llp->unit.land.lnd_y;
1224 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1225 llp->eff = llp->unit.land.lnd_effic;
1226 if (lnd_spyval(&land) > *d_spyp)
1227 *d_spyp = lnd_spyval(&land);
1231 /* Calculate the total offensive strength */
1234 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1235 double osupport, int check)
1237 double ototal = 0.0;
1238 struct emp_qelem *qp, *next;
1243 * first, total the attacking mil
1246 for (n = 0; n <= off->last; ++n) {
1247 if (off[n].type == EF_BAD || (check &&
1248 att_get_combat(&off[n], 0) <= 0))
1250 ototal += off[n].troops * att_combat_eff(off + n);
1254 * next, add in the attack_values of all
1255 * the attacking units
1258 for (qp = olist->q_forw; qp != olist; qp = next) {
1260 llp = (struct ulist *)qp;
1261 if (check && !get_oland(combat_mode, llp))
1263 if (combat_mode == A_ATTACK) {
1265 for (n = 0; n <= off->last; ++n) {
1266 if (off[n].type == EF_BAD)
1268 if ((off[n].x == llp->unit.land.lnd_x) &&
1269 (off[n].y == llp->unit.land.lnd_y))
1273 lnd_print(player->cnum, llp,
1274 "can't attack from this sector now");
1278 ototal += attack_val(combat_mode, &llp->unit.land) *
1279 att_combat_eff(off + w);
1281 ototal += attack_val(combat_mode, &llp->unit.land);
1286 return ldround(ototal, 1);
1289 /* Calculate the total defensive strength */
1292 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1295 double dtotal = 0.0, eff = 1.0, d_unit;
1296 struct emp_qelem *qp, *next;
1299 if (check && att_get_combat(def, 1) < 0)
1301 eff = att_combat_eff(def);
1302 dtotal = def->troops * eff;
1305 * next, add in the defense_values of all
1306 * the defending non-retreating units
1309 for (qp = list->q_forw; qp != list; qp = next) {
1311 llp = (struct ulist *)qp;
1312 if (check && !get_dland(def, llp))
1314 d_unit = defense_val(&llp->unit.land);
1317 dtotal += d_unit * eff;
1322 return ldround(dtotal, 1);
1326 * This is the land unit integrity check.
1330 get_oland(int combat_mode, struct ulist *llp)
1332 struct lndstr *lp = &llp->unit.land;
1335 getland(llp->unit.land.lnd_uid, lp);
1337 if (lp->lnd_own != player->cnum) {
1338 sprintf(buf, "was destroyed and is no longer a part of the %s",
1339 att_mode[combat_mode]);
1340 lnd_print(player->cnum, llp, buf);
1344 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1346 "left to fight another battle and is no longer a part of the %s",
1347 att_mode[combat_mode]);
1348 lnd_print(player->cnum, llp, buf);
1352 if (lp->lnd_effic < llp->eff) {
1353 sprintf(buf, "damaged from %d%% to %d%%",
1354 llp->eff, lp->lnd_effic);
1355 lnd_print(player->cnum, llp, buf);
1358 llp->eff = llp->unit.land.lnd_effic;
1363 get_dland(struct combat *def, struct ulist *llp)
1365 struct lndstr *lp = &llp->unit.land;
1368 getland(llp->unit.land.lnd_uid, lp);
1370 if (lp->lnd_effic < LAND_MINEFF) {
1371 sprintf(buf, "was destroyed and is no longer a part of the defense");
1372 lnd_print(llp->unit.land.lnd_own, llp, buf);
1376 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1377 lnd_print(llp->unit.land.lnd_own, llp,
1378 "left to go fight another battle and is no longer a part of the defense");
1383 llp->eff = llp->unit.land.lnd_effic;
1388 kill_land(struct emp_qelem *list)
1390 struct emp_qelem *qp, *next;
1393 for (qp = list->q_forw; qp != list; qp = next) {
1395 llp = (struct ulist *)qp;
1396 if (llp->unit.land.lnd_ship >= 0) {
1397 llp->unit.land.lnd_effic = 0;
1398 lnd_print(player->cnum, llp,
1399 "cannot return to the ship, and dies!");
1406 att_infect_units(struct emp_qelem *list, int plague)
1408 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_pstage == PLG_HEALTHY)
1417 llp->unit.land.lnd_pstage = PLG_EXPOSED;
1422 * Put the land unit on the disk. If there was some mobility cost, then
1423 * subtract it from the units mobility. Note that this works the same way
1424 * as sectors & ships in that no mobility is actually taken until the attacker
1425 * has committed to attacking.
1429 put_oland(struct emp_qelem *list)
1431 struct emp_qelem *qp, *next;
1434 for (qp = list->q_forw; qp != list; qp = next) {
1436 llp = (struct ulist *)qp;
1437 llp->unit.land.lnd_mission = 0;
1438 llp->unit.land.lnd_harden = 0;
1439 llp->unit.land.lnd_mobil -= (int)llp->mobil;
1441 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1442 if (llp->unit.land.lnd_own != player->cnum) {
1443 emp_remque((struct emp_qelem *)llp);
1446 get_oland(A_ATTACK, llp);
1451 * Keep sending in reinforcements until it looks like we're going to win.
1452 * Note that the "strength" command also calls this routine.
1456 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1457 int *d_spyp, int ototal)
1459 struct nstr_item ni;
1461 struct sctstr sect, dsect;
1464 double new_land = 0;
1468 double eff = att_combat_eff(def);
1472 dtotal = get_dtotal(def, list, 1.0, 1);
1475 snxtitem_all(&ni, EF_LAND);
1476 while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1479 if (land.lnd_mission != MI_RESERVE)
1481 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1483 if (land.lnd_own != def->own)
1485 if (land.lnd_ship >= 0)
1487 if (land.lnd_land >= 0)
1489 if (defense_val(&land) < 1.0)
1491 if (!lnd_can_attack(&land))
1494 /* Only supplied units can react */
1495 if (list ? !lnd_supply_all(&land) : !lnd_could_be_supplied(&land))
1498 if (!in_oparea((struct empobj *)&land, def->x, def->y))
1501 getsect(land.lnd_x, land.lnd_y, §);
1502 getsect(def->x, def->y, &dsect);
1503 if (!BestLandPath(buf, §, &dsect, &pathcost,
1504 lnd_mobtype(&land)))
1507 mobcost = lnd_pathcost(&land, pathcost);
1508 if (land.lnd_mobil < mobcost)
1511 new_land += defense_val(&land);
1513 if (!list) /* we are in the "strength" command */
1516 /* move to defending sector */
1517 land.lnd_mobil -= ldround(mobcost, 1);
1520 land.lnd_x = def->x;
1521 land.lnd_y = def->y;
1522 putland(land.lnd_uid, &land);
1523 wu(0, land.lnd_own, "%s reacts to %s.\n",
1524 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1526 llp = malloc(sizeof(struct ulist));
1528 memset(llp, 0, sizeof(struct ulist));
1532 llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
1533 llp->unit.land = land;
1534 llp->eff = land.lnd_effic;
1535 emp_insque(&llp->queue, list);
1536 if (lnd_spyval(&land) > *d_spyp)
1537 *d_spyp = lnd_spyval(&land);
1539 intelligence_report(player->cnum, &land, a_spy,
1540 "Scouts sight reacting enemy unit:");
1545 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1548 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1549 int land_sup, int plane_sup)
1551 double osupport = 1.0;
1553 double af, as, au, ap;
1555 af = as = au = ap = 0.0;
1557 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1562 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1569 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1575 dam = off_support(def->x, def->y, def->own, player->cnum);
1579 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1584 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1587 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1588 int ototal, int dtotal)
1590 double dsupport = 1.0;
1592 double df, ds, du, dp;
1595 df = ds = du = dp = 0.0;
1596 if (dtotal < 0.1 * ototal) {
1598 } else if (dtotal >= 1.2 * ototal) {
1601 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1605 dtotal = get_dtotal(def, list, dsupport, 0);
1606 if (dtotal < 1.2 * ototal) {
1607 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1610 dtotal = get_dtotal(def, list, dsupport, 0);
1612 if (dtotal < 1.2 * ototal) {
1613 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1616 dtotal = get_dtotal(def, list, dsupport, 1);
1618 if (dtotal < 1.2 * ototal) {
1619 dam = def_support(def->x, def->y, player->cnum, def->own);
1627 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1632 "\nOdds are bad for us...support cancelled.\n\n");
1635 "\nOdds are good for us...support cancelled.\n\n");
1641 * Land mines add to the defense multiplier. If the attacker has engineers
1642 * then this multiplier is cut in half.
1646 get_mine_dsupport(struct combat *def, int a_engineer)
1651 getsect(def->x, def->y, §);
1653 if (sect.sct_oldown != player->cnum) {
1654 mines = SCT_LANDMINES(§);
1655 mines = MIN(mines, 20);
1657 mines = ldround(mines / 2.0, 1);
1660 wu(0, def->own, "Defending mines add %1.2f\n",
1662 pr("Defending mines add %1.2f\n", mines * 0.02);
1663 return mines * 0.02;
1669 /* Get the offensive and defensive support */
1671 att_get_support(int combat_mode, int ofort, int oship, int oland,
1672 int oplane, struct emp_qelem *olist, struct combat *off,
1673 struct emp_qelem *dlist, struct combat *def,
1674 double *osupportp, double *dsupportp, int a_engineer)
1677 char osupports[512];
1678 char dsupports[512];
1680 if (combat_mode == A_PARA)
1683 *osupportp = get_osupport(osupports, def,
1684 ofort, oship, oland, oplane);
1687 * I need to put a 1 at the end of the next four total_stren calls
1688 * because units & mil may have been damaged by collateral damage or
1689 * nuclear warheads from the offensive & defensive support.
1692 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1693 if (att_empty_attack(combat_mode, ototal, def))
1694 return abort_attack();
1695 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1698 * Calculate defensive support. If odds are too good or too bad
1699 * then don't call in support.
1702 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1703 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1704 if (att_empty_attack(combat_mode, ototal, def))
1705 return abort_attack();
1707 if ((*osupports || *dsupports) &&
1708 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1709 pr("\n\t\tsupport values\n");
1710 pr("\t\tforts\tships\tunits\tplanes\n");
1711 if (*osupportp != 1.0)
1712 pr("%s", osupports);
1713 if (*dsupportp != 1.0)
1714 pr("%s", dsupports);
1716 wu(0, def->own, "\n\t\tsupport values\n");
1717 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1718 if (*osupportp != 1.0)
1719 wu(0, def->own, "%s", osupports);
1720 if (*dsupportp != 1.0)
1721 wu(0, def->own, "%s", dsupports);
1725 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1726 if (dtotal && def->type == EF_SECTOR)
1727 *dsupportp += get_mine_dsupport(def, a_engineer);
1731 /* How many two-legged bipeds are in this combat force? */
1734 count_bodies(struct combat *off, struct emp_qelem *list)
1738 struct emp_qelem *qp;
1741 for (n = 0; n <= off->last; ++n)
1742 bodies += off[n].troops;
1743 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1744 llp = (struct ulist *)qp;
1745 bodies += llp->unit.land.lnd_item[I_MILIT];
1750 /* This is where the fighting actually occurs. */
1753 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1754 double osupport, struct combat *def, struct emp_qelem *dlist,
1758 int a_cas = 0; /* Casualty counts */
1760 int ototal; /* total attacking strength */
1761 int dtotal; /* total defending strength */
1762 int a_bodies; /* total attacking mil (incl. mil in units) */
1763 int d_bodies; /* total defending mil (incl. mil in units) */
1773 ototal = get_ototal(combat_mode, off, olist, osupport,
1774 combat_mode != A_PARA);
1775 dtotal = get_dtotal(def, dlist, dsupport, 0);
1779 a_bodies = count_bodies(off, olist);
1780 d_bodies = count_bodies(def, dlist);
1781 d_mil = def->troops;
1782 for (n = 0; n <= off->last; ++n)
1783 if (off[n].type == EF_BAD)
1786 a_troops[n] = off[n].troops;
1788 /* This switch is required to get the spacing right */
1789 switch (combat_mode) {
1791 pr(" Final attack strength: %8d\n", ototal);
1794 pr(" Final assault strength: %8d\n", ototal);
1797 if (def->sct_type == SCT_MOUNT ||
1798 def->sct_type == SCT_WATER ||
1799 def->sct_type == SCT_CAPIT ||
1800 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1801 pr("You can't air-assault a %s sector!\n",
1802 def->sct_dcp->d_name);
1805 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1807 pr(" Final air-assault strength: %8d\n", ototal);
1811 pr(" Final board strength: %8d\n", ototal);
1815 pr(" Final defense strength: %8d\n", dtotal);
1816 odds = att_calcodds(ototal, dtotal);
1817 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1819 /* spread the plague */
1820 if (combat_mode != A_PARA) {
1822 for (n = 0; n <= off->last; ++n)
1823 if (off[n].type != EF_BAD)
1824 def->plague |= off[n].plague;
1825 for (n = 0; n <= off->last; ++n)
1826 if (off[n].type != EF_BAD)
1827 off[n].plague |= def->plague;
1829 att_infect_units(olist, off->plague);
1830 att_infect_units(dlist, def->plague);
1833 * Fighting is slightly random. There is always that last little
1834 * effort you see people put in. Or the stray bullet that takes out
1835 * an officer and the rest go into chaos. Things like that.
1836 * Thus, we have added a very slight random factor that will sometimes
1837 * allow the little guy to win. We modify the odds a little
1838 * (either +- 5%) to account for this randomness. We also only
1839 * recalculate the odds every 8-50 casualties, not every cacsualty,
1840 * since a single dead guy normally wouldn't cause a commander to
1841 * rethink his strategies, but 50 dead guys might.
1843 odds += (random() % 11 - 5) / 100.0;
1848 recalctime = 8 + (random() % 43);
1849 while (!success && ototal) {
1852 d_cas += take_casualty(A_DEFEND, def, dlist);
1853 dtotal = get_dtotal(def, dlist, dsupport, 0);
1858 a_cas += take_casualty(combat_mode, off, olist);
1859 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1861 if (((a_cas + d_cas) % 70) == 69)
1863 if (recalctime-- <= 0) {
1864 recalctime = 8 + (random() % 43);
1865 odds = att_calcodds(ototal, dtotal);
1866 odds += (random() % 11 - 5) / 100.0;
1874 /* update defense mobility & mil */
1878 if (def->type == EF_SECTOR && d_mil && d_cas) {
1882 newmob = damage(def->mob, 100 * d_cas / d_mil);
1883 def->mobcost = MIN(20, def->mob - newmob);
1886 def->mil = def->troops;
1889 /* update attack mobility & mil */
1890 for (n = 0; n <= off->last; ++n)
1891 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1892 if (off[n].type == EF_SECTOR && off[n].mil) {
1893 if (!CANT_HAPPEN(off[n].mob < 0)) {
1894 newmob = damage(off[n].mob,
1895 100 * (a_troops[n] - off[n].troops)
1897 off[n].mobcost += MIN(20, off[n].mob - newmob);
1900 off[n].mil -= a_troops[n] - off[n].troops;
1903 /* update land unit mobility */
1904 if (d_bodies && d_cas)
1905 lnd_takemob(dlist, (double)d_cas / d_bodies);
1906 if (a_bodies && a_cas)
1907 lnd_takemob(olist, (double)a_cas / a_bodies);
1909 /* damage attacked sector */
1910 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1912 pr("- Casualties -\n Yours: %d\n", a_cas);
1913 pr(" Theirs: %d\n", d_cas);
1914 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1915 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1918 switch (combat_mode) {
1920 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1921 pr("We have captured %s, sir!\n", prcom(0, def));
1925 news_item = def->own ? N_AWON_SECT : N_START_COL;
1926 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1927 action = "assaulting and taking";
1930 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1931 pr("We have captured %s, sir!\n", prcom(0, def));
1932 action = "air-assaulting and taking";
1935 news_item = N_BOARD_SHIP;
1936 pr("We have boarded %s, sir!\n", prcom(0, def));
1937 action = "boarding";
1940 news_item = N_BOARD_LAND;
1941 pr("We have boarded %s, sir!\n", prcom(0, def));
1942 action = "boarding";
1947 action = "defeating";
1950 switch (combat_mode) {
1952 news_item = N_SCT_LOSE;
1953 pr("You have been defeated!\n");
1954 action = "attacking";
1957 news_item = N_ALOSE_SCT;
1958 pr("You have been defeated!\n");
1960 action = "trying to assault";
1963 news_item = N_PLOSE_SCT;
1964 pr("All of your troops were destroyed\n");
1965 action = "trying to air-assault";
1968 news_item = N_SHP_LOSE;
1969 pr("You have been repelled\n");
1971 action = "trying to board";
1974 news_item = N_LND_LOSE;
1975 pr("You have been repelled\n");
1977 action = "trying to board";
1982 action = "fighting";
1985 nreport(player->cnum, news_item, def->own, 1);
1988 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
1989 cname(player->cnum), player->cnum, a_cas,
1990 action, pr_com(0, def, def->own), d_cas);
1993 send_reacting_units_home(dlist);
1995 /* putland the defending land */
1998 /* putland the attacking land */
2001 /* put the victim sector/ship/land */
2002 if (!success || !take_def(combat_mode, olist, off, def))
2005 /* put the attacking sectors/ship */
2006 for (n = 0; n <= off->last; ++n)
2007 if (off[n].type != EF_BAD)
2008 put_combat(&off[n]);
2013 switch (combat_mode) {
2015 ask_move_in(off, olist, def);
2017 /* put sectors again to get abandon warnings */
2018 for (n = 0; n <= off->last; ++n)
2019 if (off[n].type != EF_BAD)
2020 put_combat(&off[n]);
2023 att_move_in_off(combat_mode, off, olist, def);
2026 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2030 /* What percentage of the combat forces going head-to-head are we? */
2033 att_calcodds(int ototal, int dtotal)
2037 /* calculate odds */
2040 else if (dtotal <= 0)
2043 odds = (double)ototal / (dtotal + ototal);
2048 /* Here's where the dead soldiers get dragged off the battlefield */
2051 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2053 int to_take = CASUALTY_LUMP;
2054 int biggest_troops = 0, index = -1;
2055 int n, tot_troops = 0, biggest_mil, cas;
2056 struct emp_qelem *qp, *biggest;
2059 for (n = 0; n <= off->last; ++n) {
2060 if (off[n].type != EF_BAD) {
2061 tot_troops += off[n].troops;
2062 if (off[n].troops > biggest_troops) {
2063 biggest_troops = off[n].troops;
2070 to_take -= tot_troops;
2073 for (n = 0; n <= off->last; ++n)
2074 if (off[n].type != EF_BAD)
2078 * They can all come off mil. We rotate the casualties,
2079 * starting with the sector containing the most mil.
2081 to_take = CASUALTY_LUMP;
2083 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2086 while (to_take > 0) {
2087 for (n = index; n <= off->last && to_take; ++n) {
2088 if (off[n].type != EF_BAD && off[n].troops > 0) {
2093 for (n = 0; n < index && to_take; ++n) {
2094 if (off[n].type != EF_BAD && off[n].troops > 0) {
2100 return CASUALTY_LUMP;
2104 return CASUALTY_LUMP - to_take;
2107 * Need to take some casualties from attacking units
2108 * Procedure: find the biggest unit remaining (in
2109 * terms of mil) and give it the casualties.
2113 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2114 llp = (struct ulist *)qp;
2116 if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2117 biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2121 if (biggest == NULL)
2122 return CASUALTY_LUMP - to_take;
2124 llp = (struct ulist *)biggest;
2125 cas = lnd_take_casualty(combat_mode, llp, to_take);
2126 return CASUALTY_LUMP - (to_take - cas);
2129 /* Send reacting defense units back to where they came from (at no mob cost) */
2132 send_reacting_units_home(struct emp_qelem *list)
2134 struct emp_qelem *qp, *next;
2138 for (qp = list->q_forw; qp != list; qp = next) {
2140 llp = (struct ulist *)qp;
2141 if ((llp->unit.land.lnd_x != llp->x) ||
2142 (llp->unit.land.lnd_y != llp->y)) {
2143 sprintf(buf, "returns to %s",
2144 xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2145 llp->unit.land.lnd_x = llp->x;
2146 llp->unit.land.lnd_y = llp->y;
2147 lnd_print(llp->unit.land.lnd_own, llp, buf);
2153 /* Check for 0 offense strength. This call will always preceed an abort */
2156 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2159 if (def->own && player->cnum != def->own) {
2161 "%s (#%d) considered %sing you @%s\n",
2162 cname(player->cnum), player->cnum,
2163 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2165 pr("No troops for %s...\n", att_mode[combat_mode]);
2172 * Take the defending sector or ship from the defender and give it to the
2177 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2182 struct ulist *llp, *delete_me = NULL;
2188 for (n = 0; n <= off->last && !occuppied; ++n) {
2189 if (off[n].type != EF_BAD &&
2190 off[n].troops > 0 &&
2191 (off[n].type != EF_SECTOR || off[n].mob)) {
2193 if (def->type == EF_LAND) {
2194 if (def->lnd_lcp->l_flags & L_SPY) {
2201 pr("1 mil from %s moves %s\n",
2202 prcom(0, off + n), prcom(2, def));
2207 pr("%s left unoccupied\n", prcom(0, def));
2210 "No enemy troops moved %s so you still own it!\n",
2211 pr_com(2, def, def->own));
2214 llp = (struct ulist *)list->q_forw;
2215 llp->unit.land.lnd_x = def->x;
2216 llp->unit.land.lnd_y = def->y;
2217 take_move_in_mob(combat_mode, llp, off, def);
2218 if (def->type == EF_SHIP) {
2219 llp->unit.land.lnd_ship = def->shp_uid;
2220 sprintf(buf, "boards %s", prcom(0, def));
2221 lnd_print(player->cnum, llp, buf);
2224 llp->unit.land.lnd_ship = -1;
2225 sprintf(buf, "moves in to occupy %s",
2226 xyas(def->x, def->y, player->cnum));
2227 lnd_print(player->cnum, llp, buf);
2233 if (def->type == EF_SECTOR) {
2234 getsect(def->x, def->y, §);
2235 takeover(§, player->cnum);
2236 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2237 caploss(§, def->own,
2238 "* We have captured %s's capital, sir! *\n");
2240 } else if (def->type == EF_SHIP) {
2241 getship(def->shp_uid, &ship);
2242 takeover_ship(&ship, player->cnum);
2243 putship(ship.shp_uid, &ship);
2244 } else if (def->type == EF_LAND) {
2245 getland(def->lnd_uid, &land);
2246 takeover_land(&land, player->cnum);
2247 putland(land.lnd_uid, &land);
2250 lnd_delete(delete_me);
2251 att_get_combat(def, 0);
2256 * Ask the attacker which mil & land units they'd like to move into the
2261 ask_move_in(struct combat *off, struct emp_qelem *olist,
2265 struct emp_qelem *qp, *next;
2269 char land_answer[256];
2272 for (n = 0; n <= off->last; ++n)
2273 if (off[n].type != EF_BAD && off[n].troops > 0)
2275 ask_move_in_off(&off[n], def);
2276 if (player->aborted)
2282 memset(land_answer, 0, sizeof(land_answer));
2283 for (qp = olist->q_forw; qp != olist; qp = next) {
2285 llp = (struct ulist *)qp;
2286 answerp = &land_answer[(int)llp->unit.land.lnd_army];
2287 if (player->aborted || att_get_combat(def, 0) < 0)
2289 if (*answerp == 'Y')
2291 if (!get_oland(A_ATTACK, llp))
2293 if (*answerp != 'N') {
2294 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2295 prland(&llp->unit.land),
2296 llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2297 llp->unit.land.lnd_effic);
2298 *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2299 if (player->aborted || att_get_combat(def, 0) < 0)
2301 if (!get_oland(A_ATTACK, llp))
2304 if (*answerp == 'y' || *answerp == 'Y')
2306 sprintf(buf, "stays in %s",
2307 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2309 lnd_print(player->cnum, llp, buf);
2314 if (att_get_combat(def, 0) < 0) {
2315 for (qp = olist->q_forw; qp != olist; qp = next) {
2317 llp = (struct ulist *)qp;
2318 if (!get_oland(A_ATTACK, llp))
2320 sprintf(buf, "stays in %s",
2321 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2323 lnd_print(player->cnum, llp, buf);
2328 if (opt_INTERDICT_ATT)
2329 lnd_interdict(olist, def->x, def->y, player->cnum);
2330 move_in_land(A_ATTACK, off, olist, def);
2333 /* Move offensive land units to the conquered sector or ship */
2336 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2339 struct emp_qelem *qp, *next;
2345 for (qp = olist->q_forw; qp != olist; qp = next) {
2347 llp = (struct ulist *)qp;
2348 if (!get_oland(combat_mode, llp))
2350 take_move_in_mob(combat_mode, llp, off, def);
2351 llp->unit.land.lnd_x = def->x;
2352 llp->unit.land.lnd_y = def->y;
2353 if (def->type == EF_SHIP)
2354 llp->unit.land.lnd_ship = def->shp_uid;
2356 llp->unit.land.lnd_ship = -1;
2360 if (def->type == EF_SECTOR) {
2361 if (opt_INTERDICT_ATT) {
2362 lnd_sweep(olist, 0, 0, player->cnum);
2363 lnd_check_mines(olist);
2365 sprintf(buf, "now occupies %s", prcom(0, def));
2367 sprintf(buf, "boards %s", prcom(0, def));
2371 for (qp = olist->q_forw; qp != olist; qp = next) {
2373 llp = (struct ulist *)qp;
2374 lnd_print(player->cnum, llp, buf);
2382 * Move assaulting, paradropping, or boarding mil & units into def
2383 * If the mil are coming from a ship, then pack a lunch.
2387 att_move_in_off(int combat_mode, struct combat *off,
2388 struct emp_qelem *olist, struct combat *def)
2395 move_in_land(combat_mode, off, olist, def);
2397 for (n = 0; n <= off->last; ++n) {
2398 if (off[n].type == EF_BAD || !off[n].troops)
2400 troops = off[n].troops;
2402 off[n].mil -= troops;
2404 put_combat(off + n);
2405 if (combat_mode == A_ASSAULT) {
2406 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2408 getship(off[n].shp_uid, &ship);
2409 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2410 / (ship.shp_item[I_MILIT] + troops
2411 + ship.shp_item[I_CIVIL] + 0.5));
2413 ship.shp_item[I_FOOD] -= lunchbox;
2414 putship(ship.shp_uid, &ship);
2420 if (combat_mode == A_ASSAULT) {
2421 if (CANT_HAPPEN(def->type != EF_SECTOR))
2423 getsect(def->x, def->y, §);
2424 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2425 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2426 sect.sct_item[I_FOOD] += lunchbox;
2432 /* Ask how many mil to move in from each sector */
2435 ask_move_in_off(struct combat *off, struct combat *def)
2438 int num_mil, dam = 0, left;
2444 if (att_get_combat(off, 0) <= 0)
2446 if (att_get_combat(def, 0) < 0)
2448 if (off->own != player->cnum)
2450 d = att_mobcost(off->own, def, MOB_MOVE);
2451 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2453 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2454 xyas(off->x, off->y, player->cnum), mob_support);
2455 p = getstring(prompt, buf);
2456 if (!p || !*p || (num_mil = atoi(p)) <= 0)
2458 /* Make sure we don't move in more than we can support mobility-wise */
2459 if (num_mil > mob_support)
2460 num_mil = mob_support;
2461 if (att_get_combat(off, 0) <= 0)
2463 if (att_get_combat(def, 0) < 0)
2465 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2466 pr("No mil moved in from %s\n",
2467 xyas(off->x, off->y, player->cnum));
2470 mob_support = MAX(1, (int)(num_mil * d));
2471 off->mob -= MIN(off->mob, mob_support);
2472 off->mil -= num_mil;
2473 off->troops -= num_mil;
2476 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2477 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2478 if (chance(weight / 100.0))
2480 ground_interdict(def->x, def->y, player->cnum, "military");
2481 dam += check_lmines(def->x, def->y, weight);
2485 left = commdamage(num_mil, dam, I_MILIT);
2486 if (left < num_mil) {
2488 pr("%d of the mil you were moving were destroyed!\n"
2489 "Only %d mil made it to %s\n",
2490 num_mil - left, left,
2491 xyas(def->x, def->y, player->cnum));
2493 pr("All of the mil you were moving were destroyed!\n");
2496 /* maybe got nuked */
2497 if (att_get_combat(def, 0) < 0)
2505 /* Charge land units for moving into a sector or onto a ship */
2508 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2511 double mob = llp->unit.land.lnd_mobil;
2512 double gain = etu_per_update * land_mob_scale;
2513 double mobcost, moblim;
2515 switch (combat_mode) {
2517 mobcost = lnd_pathcost(&llp->unit.land,
2518 att_mobcost(off->own, def,
2519 lnd_mobtype(&llp->unit.land)));
2523 * Set mobcost to basic assault cost, moblim to maximum
2524 * mobility to keep when assaulting from non-landing ship
2526 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE) {
2527 mobcost = gain / 2.0;
2533 if (!(off->shp_mcp->m_flags & M_LAND))
2534 /* Not a landing ship, ensure we go to or below moblim */
2535 mobcost = MAX(mobcost, mob - moblim);
2538 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
2551 llp->unit.land.lnd_mobil = (signed char)mob;
2552 llp->unit.land.lnd_harden = 0;
2556 free_list(struct emp_qelem *list)
2558 struct emp_qelem *qp, *next;
2560 if (!list || QEMPTY(list))
2564 while (qp != list) {
2573 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2581 * sector_strength - Everyone starts at 1. You can get up to a max
2582 * of d_dstr, depending on how much you build up the
2583 * defenses of the sector.
2587 sector_strength(struct sctstr *sp)
2589 double def = SCT_DEFENSE(sp) / 100.0;
2590 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2591 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2593 if (d > dchr[sp->sct_type].d_dstr)
2594 d = dchr[sp->sct_type].d_dstr;