2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * attsub.c: Attack subroutines
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
55 #include "prototypes.h"
57 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
59 static void ask_olist(int combat_mode, struct combat *off,
60 struct combat *def, struct emp_qelem *olist,
61 char *land_answer, int *a_spyp, int *a_engineerp);
62 static void take_move_in_mob(int combat_mode, struct llist *llp,
63 struct combat *off, struct combat *def);
64 static void move_in_land(int combat_mode, struct combat *off,
65 struct emp_qelem *olist, struct combat *def);
66 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
68 static void ask_move_in_off(struct combat *off, struct combat *def);
70 static int board_abort(struct combat *off, struct combat *def);
71 static int land_board_abort(struct combat *off, struct combat *def);
72 static int ask_off(int combat_mode, struct combat *off,
74 static int get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
76 static int get_ototal(int combat_mode, struct combat *off,
77 struct emp_qelem *olist, double osupport, int check);
78 static int get_dtotal(struct combat *def, struct emp_qelem *list,
79 double dsupport, int check);
80 static double att_calcodds(int, int);
81 static int take_casualty(int combat_mode, struct combat *off,
82 struct emp_qelem *olist);
84 static void send_reacting_units_home(struct emp_qelem *list);
85 static int take_def(int combat_mode, struct emp_qelem *list,
86 struct combat *off, struct combat *def);
88 static int get_land(int combat_mode, struct combat *def, int uid,
89 struct llist *llp, int victim_land);
92 /* must match combat types in combat.h */
93 "defend", "attack", "assault", "paradrop", "board", "lboard"
98 * The principal object in this code is the "combat" object. A combat object
99 * is either a sector or ship. There are
100 * usually two instances of this, the "def" or defense combat object, and
101 * the array of "off" or offense objects. The number of offense objects is
102 * determined by the value of off->last (e.g. more than one attacking sector).
103 * the type of the object is determined by combat->type which can take the
104 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
105 * which is often passed to these functions is combat_mode. This can take
106 * the value A_DEFEND, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
107 * As these six modes of being in combat affect things like mobcost and combat
108 * value, there are often switches made on combat_mode. Note that in all cases
109 * no mobility is taken from sectors, ships, or land units until the player
110 * has committed to a fight. Instead, the cost is temporarily placed in
111 * combat->mobcost, or llp->mobil as the case may be, and then when the object
112 * is "put" back onto disk, then the amounts in these variables are subtracted
113 * from the object's mobility. It needs to be done this way as the objects
114 * are constantly being re-read from disk, and we don't want to take any mob
115 * unless a fight actually occurrs.
119 /* initialize combat object */
122 att_combat_init(struct combat *com, int type)
124 memset(com, 0, sizeof(*com));
129 /* print a combat object with optional preposition */
132 pr_com(int inon, struct combat *com, natid who)
134 if (com->type == EF_SECTOR) {
136 inon ? inon == 1 ? "in " : "into " : "",
137 xyas(com->x, com->y, who));
138 } else if (com->type == EF_SHIP) {
139 return prbuf("%s%s %s(#%d)",
140 inon ? inon == 1 ? "on " : "onto " : "",
141 com->shp_mcp->m_name, com->shp_name,
143 } else if (com->type == EF_LAND) {
144 return prbuf("%s%s #%d",
145 inon ? inon == 1 ? "on " : "onto " : "",
146 com->lnd_lcp->l_name, com->lnd_uid);
148 return "your forces";
153 prcom(int inon, struct combat *com)
155 return pr_com(inon, com, player->cnum);
159 * This is the combat object "type" based integrity check. It basically
160 * splits along three divisions: ship/sector, attacker/defender,
161 * first time/not first time.
165 att_get_combat(struct combat *com, int isdef)
179 if (!getsect(com->x, com->y, §)) {
180 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
181 return att_combat_init(com, EF_BAD);
183 com->sct_type = sect.sct_type;
184 com->sct_dcp = &dchr[sect.sct_type];
185 mil = sect.sct_item[I_MILIT];
186 pstage = sect.sct_pstage;
187 owner = sect.sct_own;
188 eff = sect.sct_effic;
189 mob = sect.sct_mobil;
194 if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
196 pr("Land unit #%d is not in the same sector!\n",
198 return att_combat_init(com, EF_BAD);
200 if (isdef && player->owner) {
201 pr("Boarding yourself? Try using the 'load' command.\n");
202 return att_combat_init(com, EF_BAD);
204 com->lnd_lcp = &lchr[(int)land.lnd_type];
205 mil = land.lnd_item[I_MILIT];
206 pstage = land.lnd_pstage;
207 owner = land.lnd_own;
208 eff = land.lnd_effic;
209 mob = land.lnd_mobil;
214 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
216 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
218 pr("Ship #%d is not your ship!\n", com->shp_uid);
219 return att_combat_init(com, EF_BAD);
222 if (isdef && player->owner &&
223 ontradingblock(EF_SHIP, &ship)) {
224 pr("%s is on the trading block.\n", prcom(0, com));
225 return att_combat_init(com, EF_BAD);
228 if (isdef && player->owner) {
229 pr("Boarding yourself? Try using the 'tend' command.\n");
230 return att_combat_init(com, EF_BAD);
232 com->shp_mcp = &mchr[(int)ship.shp_type];
233 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
234 if (!isdef && !player->owner) {
236 pr("%s was just sunk!\n", prcom(0, com));
238 pr("Ship #%d is not your ship!\n", com->shp_uid);
239 return att_combat_init(com, EF_BAD);
241 mil = ship.shp_item[I_MILIT];
242 pstage = ship.shp_pstage;
243 owner = ship.shp_own;
244 eff = ship.shp_effic;
245 mob = ship.shp_mobil;
254 return att_combat_init(com, EF_BAD);
257 if (!com->set) { /* first time */
258 if (isdef) { /* defender */
260 } else { /* attacker */
262 pr("No mil %s\n", prcom(1, com));
264 pr("Only 1 mil %s\n", prcom(1, com));
265 /* don't abandon attacking sectors or ships */
266 com->troops = MAX(0, mil - 1);
268 com->plague = pstage == PLG_INFECT;
269 } else { /* not first time */
270 if (isdef) { /* defender */
271 if (com->x != x || com->y != y) {
272 pr("%s has moved!\n", prcom(0, com));
273 return att_combat_init(com, EF_BAD);
275 if (owner != com->own) {
277 pr("WARNING: The ownership of %s just changed from %s to %s!\n",
278 prcom(0, com), cname(com->own), cname(owner));
279 } else if (com->type == EF_SECTOR) {
280 pr("WARNING: %s just abandoned sector %s!\n",
282 xyas(com->x, com->y, player->cnum));
286 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
288 com->mil < mil ? "increased" : "decreased", com->mil,
291 } else { /* attacker */
292 if (owner != player->cnum && getrel(getnatp(owner), player->cnum) != ALLIED) {
293 /* must be EF_SECTOR */
295 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
296 com->mil, xyas(com->x, com->y, player->cnum),
299 pr("You no longer own %s\n",
300 xyas(com->x, com->y, player->cnum));
301 return att_combat_init(com, EF_BAD);
303 if (com->troops && com->troops + 1 > mil) {
304 if (com->own == owner && player->cnum == owner)
306 pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
307 prcom(1, com), com->troops, MAX(0, mil - 1));
308 com->troops = MAX(0, mil - 1);
323 * In the course of the fight, the combat object may have lost mil, eff, or
324 * mobility. This is the place where the data in the object gets flushed to
325 * disk to make it "real".
329 put_combat(struct combat *com)
338 getsect(com->x, com->y, §);
339 sect.sct_type = com->sct_type;
340 deff = sect.sct_effic - com->eff;
342 sect.sct_road -= sect.sct_road * deff / 100.0;
343 sect.sct_rail -= sect.sct_rail * deff / 100.0;
344 sect.sct_defense -= sect.sct_defense * deff / 100.0;
345 if (sect.sct_road <= 0)
347 if (sect.sct_rail <= 0)
349 if (sect.sct_defense <= 0)
350 sect.sct_defense = 0;
352 sect.sct_effic = com->eff;
354 if (opt_MOB_ACCESS) {
355 if ((com->mob - com->mobcost) < -127)
356 sect.sct_mobil = -127;
358 sect.sct_mobil = (short)(com->mob - com->mobcost);
360 if ((com->mob - com->mobcost) < 0)
363 sect.sct_mobil = (short)(com->mob - com->mobcost);
366 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
367 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
368 sect.sct_own = com->own;
370 if (sect.sct_pstage == PLG_HEALTHY)
371 sect.sct_pstage = PLG_EXPOSED;
373 sect.sct_item[I_MILIT] = com->mil;
375 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
378 getland(com->lnd_uid, &land);
379 land.lnd_effic = com->eff;
381 if (com->mob - com->mobcost < -127)
382 land.lnd_mobil = -127;
384 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
386 makelost(EF_LAND, land.lnd_own, land.lnd_uid,
387 land.lnd_x, land.lnd_y);
388 land.lnd_own = com->own;
389 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid,
390 land.lnd_x, land.lnd_y);
392 if (land.lnd_pstage == PLG_HEALTHY)
393 land.lnd_pstage = PLG_EXPOSED;
395 if (!(com->lnd_lcp->l_flags & L_SPY))
396 land.lnd_item[I_MILIT] = com->mil;
397 lnd_count_units(&land);
398 if (com->own == player->cnum) {
399 land.lnd_mission = 0;
401 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
403 putland(com->lnd_uid, &land);
406 getship(com->shp_uid, &ship);
407 ship.shp_effic = com->eff;
409 if (com->mob - com->mobcost < -127)
410 ship.shp_mobil = -127;
412 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
414 makelost(EF_SHIP, ship.shp_own, ship.shp_uid,
415 ship.shp_x, ship.shp_y);
416 ship.shp_own = com->own;
417 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid,
418 ship.shp_x, ship.shp_y);
420 if (ship.shp_pstage == PLG_HEALTHY)
421 ship.shp_pstage = PLG_EXPOSED;
423 ship.shp_item[I_MILIT] = com->mil;
425 if (com->own == player->cnum) {
426 ship.shp_mission = 0;
428 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
430 putship(com->shp_uid, &ship);
433 att_get_combat(com, com->own != player->cnum);
436 /* If pre-attack, abort fight. If post-attack, don't move anything in */
441 return player->aborted = 1;
445 * This is the combat_mode based integrity check. It splits among two main
446 * divisions: first time/not first time, and attack/assault/para/board.
450 att_abort(int combat_mode, struct combat *off, struct combat *def)
459 if (att_get_combat(def, 1) < 0)
460 return abort_attack();
462 if (off && combat_mode != A_ATTACK) {
463 if (att_get_combat(off, 0) < 0)
464 return abort_attack();
465 if (off->type == EF_SHIP &&
466 (!getsect(off->x, off->y, §) ||
467 sect.sct_type != SCT_WATER)) {
468 pr("%s can not %s from that far inland!\n",
469 prcom(0, off), att_mode[combat_mode]);
470 return abort_attack();
473 switch (combat_mode) {
475 if (!neigh(def->x, def->y, player->cnum) &&
476 !adj_units(def->x, def->y, player->cnum)) {
477 pr("You are not adjacent to %s\n",
478 xyas(def->x, def->y, player->cnum));
479 return abort_attack();
481 if (def->own == player->cnum) {
482 pr("You can't attack your own sector.\n");
483 return abort_attack();
487 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
488 pr("You'll have to get there first...\n");
489 return abort_attack();
491 if (off && def->sct_type == SCT_MOUNT) {
492 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
493 return abort_attack();
497 if (def->own == player->cnum) {
498 pr("You can't air-assault your own sector.\n");
499 return abort_attack();
501 if (off && (def->sct_type == SCT_MOUNT ||
502 def->sct_type == SCT_WATER ||
503 def->sct_type == SCT_CAPIT ||
504 def->sct_type == SCT_FORTR ||
505 def->sct_type == SCT_WASTE)) {
506 pr("You can't air-assault a %s sector!\n",
507 def->sct_dcp->d_name);
508 return abort_attack();
512 return board_abort(off, def);
514 return land_board_abort(off, def);
517 if (off && def->sct_dcp->d_mcst <= 0) {
518 pr("You can't %s a %s sector!\n",
519 att_mode[combat_mode], def->sct_dcp->d_name);
520 return abort_attack();
522 if (!off || off->relations_checked)
524 off->relations_checked = 1;
527 setcont(player->cnum, def->own, FOUND_SPY);
528 setcont(def->own, player->cnum, FOUND_SPY);
530 if (opt_SLOW_WAR && def->own != player->cnum) {
531 natp = getnatp(player->cnum);
532 rel = getrel(natp, def->own);
535 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
536 cname(def->own), att_mode[combat_mode]);
537 if (!confirm(y_or_n))
538 return abort_attack();
541 if ((rel != AT_WAR) && (def->own) &&
542 (sect.sct_oldown != player->cnum)) {
543 pr("You're not at war with them!\n");
544 return abort_attack();
551 * Lots of special things need to be checked for boarding, so I put it in
556 board_abort(struct combat *off, struct combat *def)
558 struct shpstr aship, dship; /* for tech levels */
561 if (att_get_combat(def, 1) < 0)
562 return abort_attack();
567 if (att_get_combat(off, 0) < 0)
568 return abort_attack();
570 if (off->x != def->x || off->y != def->y) {
571 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
572 return abort_attack();
574 if (off->type == EF_SHIP) {
576 pr("%s has no mobility!\n", prcom(0, off));
577 return abort_attack();
579 getship(off->shp_uid, &aship);
580 getship(def->shp_uid, &dship);
581 if (techfact(aship.shp_tech, 1.0) * aship.shp_speed * off->eff
582 <= techfact(dship.shp_tech, 1.0) * dship.shp_speed * def->eff) {
583 pr("Victim ship moves faster than you do!\n");
586 "%s (#%d) %s failed to catch %s\n",
587 cname(aship.shp_own), aship.shp_own,
588 pr_com(0, off, def->own), pr_com(0, def, def->own));
589 return abort_attack();
591 } else if (off->type != EF_SECTOR) {
592 pr("Please tell the deity that you got the 'banana boat' error\n");
593 return abort_attack();
595 if (def->shp_mcp->m_flags & M_SUB) {
596 getsect(def->x, def->y, §);
597 if (sect.sct_type == SCT_WATER) {
598 pr("You can't board a submarine!\n");
599 return abort_attack();
606 * Lots of special things need to be checked for boarding, so I put it in
608 * STM - I copied it for land unit boarding. :)
612 land_board_abort(struct combat *off, struct combat *def)
614 if (att_get_combat(def, 1) < 0)
615 return abort_attack();
620 if (att_get_combat(off, 0) < 0)
621 return abort_attack();
623 if (off->x != def->x || off->y != def->y) {
624 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
625 return abort_attack();
631 /* If we are boarding, then the defending ship gets a chance to fire back */
633 att_approach(struct combat *off, struct combat *def)
639 pr("Approaching %s...\n", prcom(0, def));
642 "%s is being approached by %s...\n",
643 pr_com(0, def, def->own), pr_com(0, off, def->own));
644 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
647 pr("They're firing at us sir!\n");
650 "Your fleet at %s does %d damage to %s\n",
651 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
653 if (off->type == EF_SECTOR) {
654 getsect(off->x, off->y, §);
655 sectdamage(§, dam, 0);
657 pr("Enemy fleet at %s does %d damage to %s\n",
658 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
659 } else if (off->type == EF_SHIP) {
660 getship(off->shp_uid, &ship);
661 shipdamage(&ship, dam);
662 putship(off->shp_uid, &ship);
663 if (def->own && ship.shp_effic < SHIP_MINEFF) {
664 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
665 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
668 if (att_get_combat(off, 0) < 0)
669 return abort_attack();
673 /* The attack is valid. Tell the attacker about what they're going to hit */
676 att_show(struct combat *def)
678 /* Note that we tell the player about the treaty BEFORE we tell them
679 about the item. If we didn't, then they gain free information */
680 if (def->type == EF_SECTOR) {
681 if (!trechk(player->cnum, def->own, LANATT))
682 return abort_attack();
683 pr("%s is a %d%% %s %s with approximately %d military.\n",
684 xyas(def->x, def->y, player->cnum),
685 roundintby((int)def->eff, 10),
686 cname(def->own), def->sct_dcp->d_name,
687 roundintby(def->troops, 10));
688 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
689 writemap(player->cnum);
690 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
691 if (def->type == EF_SHIP) {
692 if (!trechk(player->cnum, def->own, SEAATT))
693 return abort_attack();
695 if (!trechk(player->cnum, def->own, LNDATT))
696 return abort_attack();
698 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));
700 /* Ok, everything is fine */
704 /* Attack and assault ask the user which kind of support they want */
707 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
712 *fortp = *shipp = *landp = *planep = 1;
714 if (player->argp[offset] != NULL) {
715 if ((player->argp[offset + 1] == NULL) ||
716 (player->argp[offset + 2] == NULL) ||
717 (player->argp[offset + 3] == NULL)) {
718 pr("If any support arguments are used, all must be!\n");
723 *landp = *planep = 0;
725 if (!(p = getstarg(player->argp[offset], "Use fort support? ",
729 if ((*p == 'y') || (*p == 'Y'))
732 if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
736 if ((*p == 'y') || (*p == 'Y'))
739 if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
743 if ((*p == 'y') || (*p == 'Y'))
746 if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
750 if ((*p == 'y') || (*p == 'Y'))
757 * Attack, assault, and board ask the attacker what they'd like to attack
758 * with. This includes mil and land units from each "off" object. Note that
759 * after each sub-prompt, we check to make sure that the attack is still
760 * valid, and if it's not, then we abort the attack.
764 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
765 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
768 char land_answer[256];
771 if (att_abort(combat_mode, off, def))
773 memset(land_answer, 0, sizeof(land_answer));
774 for (n = 0; n <= off->last; ++n) {
775 off[n].troops = ask_off(combat_mode, off + n, def);
776 if (att_abort(combat_mode, off, def))
778 ask_olist(combat_mode, off + n, def, olist, land_answer,
779 a_spyp, a_engineerp);
780 if (att_abort(combat_mode, off, def))
786 /* How many mil is off allowed to attack with when it attacks def? */
789 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
793 switch (combat_mode) {
795 mob_support = off->mob / sector_mcost(getsectp(def->x, def->y),
799 if (mob_support < off->troops)
800 pr("Sector %s has %d mobility which can only support %d mil,\n", xyas(off->x, off->y, player->cnum), off->mob, mob_support);
802 mob_support = off->troops;
805 if (def->own != player->cnum && def->mil) {
806 if (off->shp_mcp->m_flags & M_SEMILAND)
807 return off->troops / 4;
808 else if (!(off->shp_mcp->m_flags & M_LAND))
809 return off->troops / 10;
813 if (off->type == EF_SECTOR && off->mob <= 0)
815 mob_support = def->shp_mcp->m_item[I_MILIT];
816 if (mob_support < off->troops)
817 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
819 mob_support = off->troops;
824 if (def->lnd_lcp->l_flags & L_SPY)
826 mob_support = def->lnd_lcp->l_item[I_MILIT];
827 if (mob_support < off->troops)
828 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
830 mob_support = off->troops;
837 * If the attacker decides to go through with the attack, then the
838 * sectors/ships they are attacking with may be charged some mobility.
839 * This is where that amount of mobility is calculated. It is actually
840 * subtracted "for real" from the object's mobility in put_combat().
844 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
851 switch (combat_mode) {
855 (int)(attacking_mil *
856 sector_mcost(getsectp(def->x, def->y), MOB_MOVE)));
859 off->mobcost += MAX(1, attacking_mil / 5);
864 off->mobcost += MAX(1, attacking_mil / 5);
867 /* the 2 in the formula below is a fudge factor */
868 getship(def->shp_uid, &ship);
869 off->mobcost += (def->eff / 100) * (ship.shp_speed / 2);
874 /* How many mil to we want to attack from off against def? */
877 ask_off(int combat_mode, struct combat *off, struct combat *def)
883 if (att_get_combat(off, 0) <= 0)
885 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
887 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
889 if (off->type == EF_SECTOR) {
890 if (off->own != player->cnum)
892 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
893 off->sct_dcp->d_name,
894 xyas(off->x, off->y, player->cnum), mob_support);
896 sprintf(prompt, "Number of mil from %s (max %d) : ",
897 prcom(0, off), mob_support);
899 if ((attacking_mil = onearg(0, prompt)) < 0)
901 if (att_abort(combat_mode, off, def))
903 if (att_get_combat(off, 0) <= 0)
906 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
909 calc_mobcost(combat_mode, off, def, attacking_mil);
910 return attacking_mil;
914 * Which units would you like to attack with or move in with [ynYNq?]
918 att_prompt(char *prompt, char army)
926 p = getstring(prompt, buf);
927 if (player->aborted || (p && *p == 'q')) {
933 if (tolower(*p) == 'y' || tolower(*p) == 'n')
935 pr("y - yes this unit\n"
937 "Y - yes to all units in army '%c'\n"
938 "N - no to all units in army '%c'\n"
939 "q - quit\n? - this help message\n\n",
944 /* Ask the attacker which units they want to attack/assault/board with */
947 ask_olist(int combat_mode, struct combat *off, struct combat *def,
948 struct emp_qelem *olist, char *land_answer, int *a_spyp,
962 if (def->type == EF_LAND)
964 if (def->type == EF_SHIP)
965 maxland = def->shp_mcp->m_nland;
967 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
968 while (nxtitem(&ni, &land)) {
969 if (land.lnd_own != player->cnum)
971 if (land.lnd_effic < LAND_MINEFF)
973 if (land_answer[(int)land.lnd_army] == 'N')
975 if (!lnd_can_attack(&land))
977 lcp = &lchr[(int)land.lnd_type];
979 if (def->type == EF_SHIP && !maxland) {
980 pr("Land units are not able to board this kind of ship\n");
983 if (land.lnd_mobil <= 0) {
984 pr("%s is out of mobility, and cannot %s\n",
985 prland(&land), att_mode[combat_mode]);
990 if (ontradingblock(EF_LAND, &land)) {
991 pr("%s is on the trading block, and cannot %s\n",
992 prland(&land), att_mode[combat_mode]);
997 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
998 pr("%s is on ship #%d, and cannot %s\n",
999 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1001 } else if (off->type == EF_SHIP) {
1002 if (land.lnd_ship != off->shp_uid)
1004 } else if (land.lnd_land >= 0) {
1005 pr("%s is on unit #%d, and cannot %s\n",
1006 prland(&land), land.lnd_land, att_mode[combat_mode]);
1009 switch (combat_mode) {
1012 lnd_mobcost(&land, getsectp(def->x, def->y), MOB_NONE);
1013 if (land.lnd_mobil < mobcost) {
1014 pr("%s does not have enough mobility (%d needed)\n",
1015 prland(&land), (int)ceil(mobcost));
1022 if (!(lcp->l_flags & L_ASSAULT))
1029 att_val = attack_val(combat_mode, &land);
1031 pr("%s has no offensive strength\n", prland(&land));
1034 resupply_all(&land);
1035 putland(land.lnd_uid, &land);
1036 if (!has_supply(&land)) {
1037 pr("%s is out of supply, and cannot %s\n",
1038 prland(&land), att_mode[combat_mode]);
1041 if (def->type == EF_SHIP && first_time) {
1043 pr("You may board with a maximum of %d land units\n", maxland);
1045 pr("%s has a base %s value of %d\n",
1046 prland(&land), att_mode[combat_mode], att_val);
1047 if (land_answer[(int)land.lnd_army] != 'Y') {
1049 "%s with %s %s (%c %d%%) [ynYNq?] ",
1050 att_mode[combat_mode],
1053 land.lnd_army ? land.lnd_army : '~',
1055 land_answer[(int)land.lnd_army] =
1056 att_prompt(prompt, land.lnd_army);
1057 if (att_abort(combat_mode, off, def))
1059 if (land_answer[(int)land.lnd_army] != 'y' &&
1060 land_answer[(int)land.lnd_army] != 'Y')
1063 if (!(llp = malloc(sizeof(struct llist)))) {
1064 logerror("Malloc failed in attack!\n");
1068 memset(llp, 0, sizeof(struct llist));
1069 emp_insque(&llp->queue, olist);
1070 llp->mobil = mobcost;
1071 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1073 if (lnd_spyval(&land) > *a_spyp)
1074 *a_spyp = lnd_spyval(&land);
1075 if (llp->lcp->l_flags & L_ENGINEER)
1077 if (def->type == EF_SHIP && ++count >= maxland)
1082 /* What's the offense or defense multiplier? */
1085 att_combat_eff(struct combat *com)
1091 if (com->type == EF_SECTOR) {
1092 eff = com->eff / 100.0;
1093 if (com->own == player->cnum) {
1094 str = com->sct_dcp->d_ostr;
1095 eff = 1.0 + ((str - 1.0) * eff);
1097 eff = sector_strength(getsectp(com->x, com->y));
1098 /* str = com->sct_dcp->d_dstr;*/
1099 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1100 getship(com->shp_uid, &ship);
1101 eff = 1.0 + ship.shp_armor / 100.0;
1107 * Estimate the defense strength and give the attacker a chance to abort
1108 * if the odds are less than 50%
1111 att_get_offense(int combat_mode, struct combat *off,
1112 struct emp_qelem *olist, struct combat *def)
1117 * Get the attacker units & mil again in case they changed while the
1118 * attacker was answering sub-prompts.
1121 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1122 if (att_empty_attack(combat_mode, ototal, def))
1123 return abort_attack();
1124 if (combat_mode == A_PARA)
1126 pr("\n Initial attack strength: %8d\n", ototal);
1130 /* Get the defensive units and reacting units */
1132 att_get_defense(struct emp_qelem *olist, struct combat *def,
1133 struct emp_qelem *dlist, int a_spy, int ototal)
1136 struct emp_qelem *qp;
1142 get_dlist(def, dlist, 0, &d_spy);
1143 dtotal = get_dtotal(def, dlist, 1.0, 0);
1146 * Call in reacting units
1149 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1150 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1152 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1153 llp = (struct llist *)qp;
1154 intelligence_report(def->own, &llp->land, d_spy,
1155 "Scouts report attacking unit:");
1158 old_dtotal = dtotal;
1159 dtotal = get_dtotal(def, dlist, 1.0, 0);
1160 if (dtotal != old_dtotal)
1161 pr("Defense strength with reacting units: %8d\n", dtotal);
1166 /* Get the defensive land units in the sector or on the ship */
1169 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1172 struct nstr_item ni;
1177 /* In here is where you need to take out spies and trains from the defending
1178 lists. Spies try to hide, trains get trapped and can be boarded. */
1180 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1181 while (nxtitem(&ni, &land)) {
1184 if (land.lnd_own != def->own)
1186 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1188 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1190 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1192 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1194 if (!list) { /* Just estimating the enemy strength */
1195 estimate += intelligence_report(player->cnum,
1197 "Scouts report defending unit:");
1200 if (!(llp = malloc(sizeof(struct llist)))) {
1201 logerror("Malloc failed in attack!\n");
1205 memset(llp, 0, sizeof(struct llist));
1206 emp_insque(&llp->queue, list);
1207 llp->supplied = has_supply(&land);
1208 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1210 if (lnd_spyval(&land) > *d_spyp)
1211 *d_spyp = lnd_spyval(&land);
1216 /* Calculate the total offensive strength */
1219 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1220 double osupport, int check)
1222 double ototal = 0.0;
1223 struct emp_qelem *qp, *next;
1228 * first, total the attacking mil
1231 for (n = 0; n <= off->last; ++n) {
1232 if (off[n].type == EF_BAD || (check &&
1233 att_get_combat(&off[n], 0) <= 0))
1235 ototal += off[n].troops * att_combat_eff(off + n);
1239 * next, add in the attack_values of all
1240 * the attacking units
1243 for (qp = olist->q_forw; qp != olist; qp = next) {
1245 llp = (struct llist *)qp;
1246 if (check && !get_land(combat_mode, 0, llp->land.lnd_uid, llp, 0))
1248 if (combat_mode == A_ATTACK) {
1250 for (n = 0; n <= off->last; ++n) {
1251 if (off[n].type == EF_BAD)
1253 if ((off[n].x == llp->land.lnd_x) &&
1254 (off[n].y == llp->land.lnd_y))
1258 lnd_delete(llp, "is in a sector not owned by you");
1261 ototal += attack_val(combat_mode, &llp->land) *
1262 att_combat_eff(off + w);
1264 ototal += attack_val(combat_mode, &llp->land);
1269 return ldround(ototal, 1);
1272 /* Calculate the total defensive strength */
1275 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1278 double dtotal = 0.0, eff = 1.0, d_unit;
1279 struct emp_qelem *qp, *next;
1282 if (check && att_get_combat(def, 1) < 0)
1284 eff = att_combat_eff(def);
1285 dtotal = def->troops * eff;
1288 * next, add in the defense_values of all
1289 * the defending non-retreating units
1292 for (qp = list->q_forw; qp != list; qp = next) {
1294 llp = (struct llist *)qp;
1296 if (check && !get_land(A_DEFEND, def, llp->land.lnd_uid, llp, 1))
1298 d_unit = defense_val(&llp->land);
1301 dtotal += d_unit * eff;
1306 return ldround(dtotal, 1);
1310 * This is the land unit integrity check. Note that we don't print
1311 * warnings about victim land units because the attacker may not have seen them
1315 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp,
1318 struct lndstr *lp = &llp->land;
1323 if (!llp->lcp) { /* first time */
1324 llp->x = llp->land.lnd_x;
1325 llp->y = llp->land.lnd_y;
1326 llp->lcp = &lchr[(int)llp->land.lnd_type];
1327 } else { /* not first time */
1328 if (lp->lnd_effic < LAND_MINEFF) {
1329 sprintf(buf, "was destroyed and is no longer a part of the %s",
1330 att_mode[combat_mode]);
1331 lnd_delete(llp, buf);
1335 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1337 "left to go fight another battle and is no longer a part of the defense");
1341 if (lp->lnd_own != player->cnum) {
1343 "was destroyed and is no longer a part of the %s",
1344 att_mode[combat_mode]);
1345 lnd_delete(llp, buf);
1348 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1350 "left to fight another battle and is no longer a part of the %s",
1351 att_mode[combat_mode]);
1352 lnd_delete(llp, buf);
1355 if (lp->lnd_effic < llp->eff) {
1356 sprintf(buf, "damaged from %d%% to %d%%",
1357 llp->eff, lp->lnd_effic);
1358 lnd_print(llp, buf);
1362 llp->eff = llp->land.lnd_effic;
1368 * Put the land unit on the disk. If there was some mobility cost, then
1369 * subtract it from the units mobility. Note that this works the same way
1370 * as sectors & ships in that no mobility is actually taken until the attacker
1371 * has committed to attacking.
1375 kill_land(struct emp_qelem *list)
1377 struct emp_qelem *qp, *next;
1380 for (qp = list->q_forw; qp != list; qp = next) {
1382 llp = (struct llist *)qp;
1383 if (llp->land.lnd_ship >= 0) {
1384 llp->land.lnd_effic = 0;
1385 lnd_delete(llp, "cannot return to the ship, and dies!");
1391 att_infect_units(struct emp_qelem *list, int plague)
1393 struct emp_qelem *qp, *next;
1398 for (qp = list->q_forw; qp != list; qp = next) {
1400 llp = (struct llist *)qp;
1401 if (llp->land.lnd_pstage == PLG_HEALTHY)
1402 llp->land.lnd_pstage = PLG_EXPOSED;
1407 put_land(struct emp_qelem *list)
1409 struct emp_qelem *qp, *next;
1412 for (qp = list->q_forw; qp != list; qp = next) {
1414 llp = (struct llist *)qp;
1415 llp->land.lnd_mission = 0;
1416 llp->land.lnd_harden = 0;
1417 llp->land.lnd_mobil -= (int)llp->mobil;
1419 putland(llp->land.lnd_uid, &llp->land);
1420 if (llp->land.lnd_own != player->cnum) {
1421 emp_remque((struct emp_qelem *)llp);
1424 get_land(A_ATTACK, 0, llp->land.lnd_uid, llp, 0);
1429 * Keep sending in reinforcements until it looks like we're going to win.
1430 * Note that the "strength" command also calls this routine.
1434 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1435 int *d_spyp, int ototal)
1437 struct nstr_item ni;
1439 struct sctstr sect, dsect;
1448 double eff = att_combat_eff(def);
1453 * All units that are within their reaction radius and not damaged
1454 * below their morale value now get to react to the threatened sect.
1455 * Once we've sent enough to counter the threat, stop sending them.
1457 * Not anymore. All units get to react. :)
1461 dtotal = get_dtotal(def, list, 1.0, 1);
1464 snxtitem_all(&ni, EF_LAND);
1465 while (nxtitem(&ni, &land) &&
1466 (dtotal + new_land * eff < (int)(1.2 * ototal))) {
1469 if (!land.lnd_rad_max)
1471 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1473 if (land.lnd_own != def->own)
1475 if (land.lnd_ship >= 0)
1477 if (land.lnd_land >= 0)
1479 if (!defense_val(&land))
1482 if (land.lnd_effic <= land.lnd_retreat)
1485 if (!lnd_can_attack(&land))
1488 /* Only supplied units can react */
1489 if (!has_supply(&land))
1492 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1494 getsect(land.lnd_x, land.lnd_y, §);
1495 /* Units on efficient headquarters can react 1 farther */
1496 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1497 radius = land.lnd_rad_max + 1;
1499 radius = land.lnd_rad_max;
1501 if (land.lnd_mission == MI_RESERVE)
1507 getsect(def->x, def->y, &dsect);
1508 if (!BestLandPath(buf, §, &dsect, &pathcost, MOB_MARCH))
1511 mobcost = lnd_pathcost(&land, pathcost);
1512 if (land.lnd_mobil < mobcost)
1515 new_land += defense_val(&land);
1517 if (!list) /* we are in the "strength" command */
1520 /* move to defending sector */
1521 land.lnd_mobil -= ldround(mobcost, 1);
1524 land.lnd_x = def->x;
1525 land.lnd_y = def->y;
1526 putland(land.lnd_uid, &land);
1527 wu(0, land.lnd_own, "%s reacts to %s.\n",
1528 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1530 llp = malloc(sizeof(struct llist));
1532 memset(llp, 0, sizeof(struct llist));
1536 llp->lcp = &lchr[(int)land.lnd_type];
1538 emp_insque(&llp->queue, list);
1539 if (lnd_spyval(&land) > *d_spyp)
1540 *d_spyp = lnd_spyval(&land);
1542 intelligence_report(player->cnum, &land, a_spy,
1543 "Scouts sight reacting enemy unit:");
1548 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1551 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1552 int land_sup, int plane_sup)
1554 double osupport = 1.0;
1556 double af, as, au, ap;
1558 af = as = au = ap = 0.0;
1560 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1565 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1572 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1578 dam = off_support(def->x, def->y, def->own, player->cnum);
1582 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1587 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1590 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1591 int ototal, int dtotal)
1593 double dsupport = 1.0;
1595 double df, ds, du, dp;
1598 df = ds = du = dp = 0.0;
1599 if (dtotal < 0.1 * ototal) {
1601 } else if (dtotal >= 1.2 * ototal) {
1604 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1608 dtotal = get_dtotal(def, list, dsupport, 0);
1609 if (dtotal < 1.2 * ototal) {
1610 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1613 dtotal = get_dtotal(def, list, dsupport, 0);
1615 if (dtotal < 1.2 * ototal) {
1616 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1619 dtotal = get_dtotal(def, list, dsupport, 1);
1621 if (dtotal < 1.2 * ototal) {
1622 dam = def_support(def->x, def->y, player->cnum, def->own);
1630 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1635 "\nOdds are bad for us...support cancelled.\n\n");
1638 "\nOdds are good for us...support cancelled.\n\n");
1644 * Land mines add to the defense multiplier. If the attacker has engineers
1645 * then this multiplier is cut in half.
1649 get_mine_dsupport(struct combat *def, int a_engineer)
1654 getsect(def->x, def->y, §);
1656 if (sect.sct_oldown != player->cnum) {
1657 mines = MIN(sect.sct_mines, 20);
1659 mines = ldround(mines / 2.0, 1);
1662 wu(0, def->own, "Defending mines add %1.2f\n",
1664 pr("Defending mines add %1.2f\n", mines * 0.02);
1665 return mines * 0.02;
1671 /* Get the offensive and defensive support */
1673 att_get_support(int combat_mode, int ofort, int oship, int oland,
1674 int oplane, struct emp_qelem *olist, struct combat *off,
1675 struct emp_qelem *dlist, struct combat *def,
1676 double *osupportp, double *dsupportp, int a_engineer)
1679 char osupports[512];
1680 char dsupports[512];
1682 if (combat_mode == A_PARA)
1685 *osupportp = get_osupport(osupports, def,
1686 ofort, oship, oland, oplane);
1689 * I need to put a 1 at the end of the next four total_stren calls
1690 * becauase units & mil may have been damaged by collateral damage or
1691 * neclear warheads from the offensive & defensive support.
1694 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1695 if (att_empty_attack(combat_mode, ototal, def))
1696 return abort_attack();
1697 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1700 * Calculate defensive support. If odds are too good or too bad
1701 * then don't call in support.
1704 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1705 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1706 if (att_empty_attack(combat_mode, ototal, def))
1707 return abort_attack();
1709 if ((*osupports || *dsupports) &&
1710 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1711 pr("\n\t\tsupport values\n");
1712 pr("\t\tforts\tships\tunits\tplanes\n");
1713 if (*osupportp != 1.0)
1714 pr("%s", osupports);
1715 if (*dsupportp != 1.0)
1716 pr("%s", dsupports);
1718 wu(0, def->own, "\n\t\tsupport values\n");
1719 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1720 if (*osupportp != 1.0)
1721 wu(0, def->own, "%s", osupports);
1722 if (*dsupportp != 1.0)
1723 wu(0, def->own, "%s", dsupports);
1727 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1728 if (dtotal && def->type == EF_SECTOR)
1729 *dsupportp += get_mine_dsupport(def, a_engineer);
1733 /* How many two-legged bipeds are in this combat force? */
1736 count_bodies(struct combat *off, struct emp_qelem *list)
1740 struct emp_qelem *qp;
1743 for (n = 0; n <= off->last; ++n)
1744 bodies += off[n].troops;
1745 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1746 llp = (struct llist *)qp;
1747 bodies += llp->land.lnd_item[I_MILIT];
1752 /* This is where the fighting actually occurs. */
1755 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1756 double osupport, struct combat *def, struct emp_qelem *dlist,
1760 int a_cas = 0; /* Casualty counts */
1762 int ototal; /* total attacking strength */
1763 int dtotal; /* total defending strength */
1764 int a_bodies; /* total attacking mil (incl. mil in units) */
1765 int d_bodies; /* total defending mil (incl. mil in units) */
1775 ototal = get_ototal(combat_mode, off, olist, osupport,
1776 combat_mode != A_PARA);
1777 dtotal = get_dtotal(def, dlist, dsupport, 0);
1781 a_bodies = count_bodies(off, olist);
1782 d_bodies = count_bodies(def, dlist);
1783 d_mil = def->troops;
1784 for (n = 0; n <= off->last; ++n)
1785 if (off[n].type == EF_BAD)
1788 a_troops[n] = off[n].troops;
1790 /* This switch is required to get the spacing right */
1791 switch (combat_mode) {
1793 pr(" Final attack strength: %8d\n", ototal);
1796 pr(" Final assault strength: %8d\n", ototal);
1799 if (def->sct_type == SCT_MOUNT ||
1800 def->sct_type == SCT_WATER ||
1801 def->sct_type == SCT_CAPIT ||
1802 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1803 pr("You can't air-assault a %s sector!\n",
1804 def->sct_dcp->d_name);
1807 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1809 pr(" Final air-assault strength: %8d\n", ototal);
1813 pr(" Final board strength: %8d\n", ototal);
1817 pr(" Final defense strength: %8d\n", dtotal);
1818 odds = att_calcodds(ototal, dtotal);
1819 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1821 /* spread the plague */
1822 if (combat_mode != A_PARA) {
1824 for (n = 0; n <= off->last; ++n)
1825 if (off[n].type != EF_BAD)
1826 def->plague |= off[n].plague;
1827 for (n = 0; n <= off->last; ++n)
1828 if (off[n].type != EF_BAD)
1829 off[n].plague |= def->plague;
1831 att_infect_units(olist, off->plague);
1832 att_infect_units(dlist, def->plague);
1835 * Fighting is slightly random. There is always that last little
1836 * effort you see people put in. Or the stray bullet that takes out
1837 * an officer and the rest go into chaos. Things like that.
1838 * Thus, we have added a very slight random factor that will sometimes
1839 * allow the little guy to win. We modify the odds a little
1840 * (either +- 5%) to account for this randomness. We also only
1841 * recalculate the odds every 8-50 casualties, not every cacsualty,
1842 * since a single dead guy normally wouldn't cause a commander to
1843 * rethink his strategies, but 50 dead guys might.
1845 odds += (random() % 11 - 5) / 100.0;
1850 recalctime = 8 + (random() % 43);
1851 while (!success && ototal) {
1854 d_cas += take_casualty(A_DEFEND, def, dlist);
1855 dtotal = get_dtotal(def, dlist, dsupport, 0);
1860 a_cas += take_casualty(combat_mode, off, olist);
1861 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1863 if (((a_cas + d_cas) % 70) == 69)
1865 if (recalctime-- <= 0) {
1866 recalctime = 8 + (random() % 43);
1867 odds = att_calcodds(ototal, dtotal);
1868 odds += (random() % 11 - 5) / 100.0;
1876 /* update defense mobility & mil */
1880 if (def->type == EF_SECTOR && d_mil && d_cas) {
1884 newmob = damage(def->mob, 100 * d_cas / d_mil);
1885 def->mobcost = MIN(20, def->mob - newmob);
1888 def->mil = def->troops;
1891 /* update attack mobility & mil */
1892 for (n = 0; n <= off->last; ++n)
1893 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1894 if (off[n].type == EF_SECTOR && off[n].mil) {
1895 if (!CANT_HAPPEN(off[n].mob < 0)) {
1896 newmob = damage(off[n].mob,
1897 100 * (a_troops[n] - off[n].troops)
1899 off[n].mobcost += MIN(20, off[n].mob - newmob);
1902 off[n].mil -= a_troops[n] - off[n].troops;
1905 /* update land unit mobility */
1906 if (d_bodies && d_cas)
1907 lnd_takemob(dlist, (double)d_cas / d_bodies);
1908 if (a_bodies && a_cas)
1909 lnd_takemob(olist, (double)a_cas / a_bodies);
1911 /* damage attacked sector */
1912 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1914 pr("- Casualties -\n Yours: %d\n", a_cas);
1915 pr(" Theirs: %d\n", d_cas);
1916 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1917 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1920 switch (combat_mode) {
1922 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1923 pr("We have captured %s, sir!\n", prcom(0, def));
1927 news_item = def->own ? N_AWON_SECT : N_START_COL;
1928 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1929 action = "assaulting and taking";
1932 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1933 pr("We have captured %s, sir!\n", prcom(0, def));
1934 action = "air-assaulting and taking";
1937 news_item = N_BOARD_SHIP;
1938 pr("We have boarded %s, sir!\n", prcom(0, def));
1939 action = "boarding";
1942 news_item = N_BOARD_LAND;
1943 pr("We have boarded %s, sir!\n", prcom(0, def));
1944 action = "boarding";
1949 action = "defeating";
1952 switch (combat_mode) {
1954 news_item = N_SCT_LOSE;
1955 pr("You have been defeated!\n");
1956 action = "attacking";
1959 news_item = N_ALOSE_SCT;
1960 pr("You have been defeated!\n");
1962 action = "trying to assault";
1965 news_item = N_PLOSE_SCT;
1966 pr("All of your troops were destroyed\n");
1967 action = "trying to air-assault";
1970 news_item = N_SHP_LOSE;
1971 pr("You have been repelled\n");
1973 action = "trying to board";
1976 news_item = N_LND_LOSE;
1977 pr("You have been repelled\n");
1979 action = "trying to board";
1984 action = "fighting";
1987 nreport(player->cnum, news_item, def->own, 1);
1990 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
1991 cname(player->cnum), player->cnum, a_cas,
1992 action, pr_com(0, def, def->own), d_cas);
1995 send_reacting_units_home(dlist);
1997 /* putland the defending land */
2000 /* putland the attacking land */
2003 /* put the victim sector/ship/land */
2004 if (!success || !take_def(combat_mode, olist, off, def))
2007 /* put the attacking sectors/ship */
2008 for (n = 0; n <= off->last; ++n)
2009 if (off[n].type != EF_BAD)
2010 put_combat(&off[n]);
2015 switch (combat_mode) {
2017 ask_move_in(off, olist, def);
2019 /* put sectors again to get abandon warnings */
2020 for (n = 0; n <= off->last; ++n)
2021 if (off[n].type != EF_BAD)
2022 put_combat(&off[n]);
2025 att_move_in_off(combat_mode, off, olist, def);
2028 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2032 /* What percentage of the combat forces going head-to-head are we? */
2035 att_calcodds(int ototal, int dtotal)
2039 /* calculate odds */
2042 else if (dtotal <= 0)
2045 odds = (double)ototal / (dtotal + ototal);
2050 /* Here's where the dead soldiers get dragged off the battlefield */
2053 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2055 int to_take = CASUALTY_LUMP;
2056 int biggest_troops = 0, index = -1;
2057 int n, tot_troops = 0, biggest_mil, cas;
2058 struct emp_qelem *qp, *biggest;
2061 for (n = 0; n <= off->last; ++n) {
2062 if (off[n].type != EF_BAD) {
2063 tot_troops += off[n].troops;
2064 if (off[n].troops > biggest_troops) {
2065 biggest_troops = off[n].troops;
2072 to_take -= tot_troops;
2075 for (n = 0; n <= off->last; ++n)
2076 if (off[n].type != EF_BAD)
2080 * They can all come off mil. We rotate the casualties,
2081 * starting with the sector containing the most mil.
2083 to_take = CASUALTY_LUMP;
2085 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2088 while (to_take > 0) {
2089 for (n = index; n <= off->last && to_take; ++n) {
2090 if (off[n].type != EF_BAD && off[n].troops > 0) {
2095 for (n = 0; n < index && to_take; ++n) {
2096 if (off[n].type != EF_BAD && off[n].troops > 0) {
2102 return CASUALTY_LUMP;
2106 return CASUALTY_LUMP - to_take;
2109 * Need to take some casualties from attacking units
2110 * Procedure: find the biggest unit remaining (in
2111 * terms of mil) and give it the casualties.
2115 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2116 llp = (struct llist *)qp;
2118 if (llp->land.lnd_item[I_MILIT] > biggest_mil) {
2119 biggest_mil = llp->land.lnd_item[I_MILIT];
2123 if (biggest == NULL)
2124 return CASUALTY_LUMP - to_take;
2126 llp = (struct llist *)biggest;
2127 cas = lnd_take_casualty(combat_mode, llp, to_take);
2128 return CASUALTY_LUMP - (to_take - cas);
2131 /* Send reacting defense units back to where they came from (at no mob cost) */
2134 send_reacting_units_home(struct emp_qelem *list)
2136 struct emp_qelem *qp, *next;
2140 for (qp = list->q_forw; qp != list; qp = next) {
2142 llp = (struct llist *)qp;
2143 if ((llp->land.lnd_x != llp->x) || (llp->land.lnd_y != llp->y)) {
2144 sprintf(buf, "returns to %s",
2145 xyas(llp->x, llp->y, llp->land.lnd_own));
2146 llp->land.lnd_x = llp->x;
2147 llp->land.lnd_y = llp->y;
2148 lnd_delete(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 llist *llp, *delete_me = 0;
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 llist *)list->q_forw;
2215 llp->land.lnd_x = def->x;
2216 llp->land.lnd_y = def->y;
2217 take_move_in_mob(combat_mode, llp, off, def);
2218 if (def->type == EF_SHIP) {
2219 llp->land.lnd_ship = def->shp_uid;
2220 sprintf(buf, "boards %s", prcom(0, def));
2223 llp->land.lnd_ship = -1;
2224 sprintf(buf, "moves in to occupy %s",
2225 xyas(def->x, def->y, player->cnum));
2226 lnd_delete(llp, buf);
2231 if (def->type == EF_SECTOR) {
2232 getsect(def->x, def->y, §);
2233 takeover(§, player->cnum);
2234 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2235 caploss(§, def->own,
2236 "* We have captured %s's capital, sir! *\n");
2238 } else if (def->type == EF_SHIP) {
2239 getship(def->shp_uid, &ship);
2240 takeover_ship(&ship, player->cnum, 1);
2241 putship(ship.shp_uid, &ship);
2242 } else if (def->type == EF_LAND) {
2243 getland(def->lnd_uid, &land);
2244 takeover_land(&land, player->cnum, 1);
2245 putland(land.lnd_uid, &land);
2248 lnd_delete(delete_me, buf);
2249 att_get_combat(def, 0);
2254 * Ask the attacker which mil & land units they'd like to move into the
2259 ask_move_in(struct combat *off, struct emp_qelem *olist,
2263 struct emp_qelem *qp, *next;
2267 char land_answer[256];
2270 for (n = 0; n <= off->last; ++n)
2271 if (off[n].type != EF_BAD && off[n].troops > 0)
2273 ask_move_in_off(&off[n], def);
2274 if (player->aborted)
2280 memset(land_answer, 0, sizeof(land_answer));
2281 for (qp = olist->q_forw; qp != olist; qp = next) {
2283 llp = (struct llist *)qp;
2284 answerp = &land_answer[(int)llp->land.lnd_army];
2285 if (player->aborted || att_get_combat(def, 0) < 0)
2287 if (*answerp == 'Y')
2289 if (*answerp != 'N') {
2290 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2292 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2294 llp->land.lnd_army ? llp->land.lnd_army : '~',
2295 llp->land.lnd_effic);
2296 *answerp = att_prompt(prompt, llp->land.lnd_army);
2297 if (player->aborted || att_get_combat(def, 0) < 0)
2299 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2302 if (*answerp == 'y' || *answerp == 'Y')
2304 sprintf(buf, "stays in %s",
2305 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2306 lnd_delete(llp, buf);
2310 if (att_get_combat(def, 0) < 0) {
2311 for (qp = olist->q_forw; qp != olist; qp = next) {
2313 llp = (struct llist *)qp;
2314 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2316 sprintf(buf, "stays in %s",
2317 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2318 lnd_delete(llp, buf);
2322 if (opt_INTERDICT_ATT)
2323 lnd_interdict(olist, def->x, def->y, player->cnum);
2324 move_in_land(A_ATTACK, off, olist, def);
2327 /* Move offensive land units to the conquered sector or ship */
2330 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2333 struct emp_qelem *qp, *next;
2339 for (qp = olist->q_forw; qp != olist; qp = next) {
2341 llp = (struct llist *)qp;
2342 if (!get_land(combat_mode, def, llp->land.lnd_uid, llp, 0))
2344 take_move_in_mob(combat_mode, llp, off, def);
2345 llp->land.lnd_x = def->x;
2346 llp->land.lnd_y = def->y;
2347 if (def->type == EF_SHIP)
2348 llp->land.lnd_ship = def->shp_uid;
2350 llp->land.lnd_ship = -1;
2354 if (def->type == EF_SECTOR) {
2355 if (opt_INTERDICT_ATT) {
2356 lnd_sweep(olist, 0, 0, def->own);
2357 lnd_check_mines(olist);
2359 sprintf(buf, "now occupies %s", prcom(0, def));
2361 sprintf(buf, "boards %s", prcom(0, def));
2365 for (qp = olist->q_forw; qp != olist; qp = next) {
2367 llp = (struct llist *)qp;
2368 lnd_print(llp, buf);
2376 * Move assaulting, paradropping, or boarding mil & units into def
2377 * If the mil are coming from a ship, then pack a lunch.
2381 att_move_in_off(int combat_mode, struct combat *off,
2382 struct emp_qelem *olist, struct combat *def)
2389 move_in_land(combat_mode, off, olist, def);
2391 for (n = 0; n <= off->last; ++n) {
2392 if (off[n].type == EF_BAD || !off[n].troops)
2394 troops = off[n].troops;
2396 off[n].mil -= troops;
2398 put_combat(off + n);
2399 if (combat_mode == A_ASSAULT) {
2400 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2402 getship(off[n].shp_uid, &ship);
2403 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2404 / (ship.shp_item[I_MILIT] + troops
2405 + ship.shp_item[I_CIVIL] + 0.5));
2407 ship.shp_item[I_FOOD] -= lunchbox;
2408 putship(ship.shp_uid, &ship);
2414 if (combat_mode == A_ASSAULT) {
2415 if (CANT_HAPPEN(def->type != EF_SECTOR))
2417 getsect(def->x, def->y, §);
2418 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2419 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2420 sect.sct_item[I_FOOD] += lunchbox;
2426 /* Ask how many mil to move in from each sector */
2429 ask_move_in_off(struct combat *off, struct combat *def)
2432 int num_mil, dam = 0, left;
2438 if (att_get_combat(off, 0) <= 0)
2440 if (att_get_combat(def, 0) < 0)
2442 if (off->own != player->cnum)
2444 d = sector_mcost(getsectp(def->x, def->y), MOB_MOVE);
2445 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2447 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2448 xyas(off->x, off->y, player->cnum), mob_support);
2449 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0)
2451 /* Make sure we don't move in more than we can support mobility-wise */
2452 if (num_mil > mob_support)
2453 num_mil = mob_support;
2454 if (att_get_combat(off, 0) <= 0)
2456 if (att_get_combat(def, 0) < 0)
2458 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2459 pr("No mil moved in from %s\n",
2460 xyas(off->x, off->y, player->cnum));
2463 mob_support = MAX(1, (int)(num_mil * d));
2464 off->mob -= MIN(off->mob, mob_support);
2465 off->mil -= num_mil;
2466 off->troops -= num_mil;
2469 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2470 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2471 if (chance(weight / 100.0))
2473 ground_interdict(def->x, def->y, player->cnum, "military");
2474 dam += check_lmines(def->x, def->y, weight);
2478 left = commdamage(num_mil, dam, I_MILIT);
2479 if (left < num_mil) {
2481 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));
2483 pr("All of the mil you were moving were destroyed!\n");
2486 /* maybe got nuked */
2487 if (att_get_combat(def, 0) < 0)
2495 /* Charge land units for moving into a sector or onto a ship */
2498 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off,
2504 switch (combat_mode) {
2507 lnd_mobcost(&llp->land, getsectp(def->x, def->y), MOB_NONE);
2508 new = llp->land.lnd_mobil - mobcost;
2511 llp->land.lnd_mobil = new;
2514 if (off->shp_mcp->m_flags & M_LAND) {
2515 if (llp->lcp->l_flags & L_MARINE)
2516 llp->land.lnd_mobil -=
2517 (float)etu_per_update * land_mob_scale * 0.5;
2519 llp->land.lnd_mobil -= (float)etu_per_update * land_mob_scale;
2521 if (llp->lcp->l_flags & L_MARINE)
2522 llp->land.lnd_mobil = 0;
2524 llp->land.lnd_mobil = -(float)etu_per_update * land_mob_scale;
2528 /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2529 if (llp->lcp->l_flags & L_MARINE)
2530 llp->land.lnd_mobil -= 10;
2532 llp->land.lnd_mobil -= 40;
2535 llp->land.lnd_harden = 0;
2539 free_list(struct emp_qelem *list)
2541 struct emp_qelem *qp, *next;
2543 if (!list || QEMPTY(list))
2547 while (qp != list) {
2556 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2564 * sector_strength - Everyone starts at 1. You can get up to a max
2565 * of d_dstr, depending on how much you build up the
2566 * defenses of the sector.
2570 sector_strength(struct sctstr *sp)
2572 double def = SCT_DEFENSE(sp) / 100.0;
2573 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2574 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2576 if (CANT_HAPPEN(d > dchr[sp->sct_type].d_dstr))
2577 d = dchr[sp->sct_type].d_dstr;
2578 if (CANT_HAPPEN(d < base))