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 int take_casualty(int combat_mode, struct combat *off,
81 struct emp_qelem *olist);
83 static void send_reacting_units_home(struct emp_qelem *list);
84 static int take_def(int combat_mode, struct emp_qelem *list,
85 struct combat *off, struct combat *def);
87 static int get_land(int combat_mode, struct combat *def, int uid,
88 struct llist *llp, int victim_land);
91 /* must match combat types in combat.h */
92 "defend", "attack", "assault", "paradrop", "board", "lboard"
97 * The principal object in this code is the "combat" object. A combat object
98 * is either a sector or ship. There are
99 * usually two instances of this, the "def" or defense combat object, and
100 * the array of "off" or offense objects. The number of offense objects is
101 * determined by the value of off->last (e.g. more than one attacking sector).
102 * the type of the object is determined by combat->type which can take the
103 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
104 * which is often passed to these functions is combat_mode. This can take
105 * the value A_DEFENSE, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
106 * As these six modes of being in combat affect things like mobcost and combat
107 * value, there are often switches made on combat_mode. Note that in all cases
108 * no mobility is taken from sectors, ships, or land units until the player
109 * has committed to a fight. Instead, the cost is temporarily placed in
110 * combat->mobcost, or llp->mobil as the case may be, and then when the object
111 * is "put" back onto disk, then the amounts in these variables are subtracted
112 * from the object's mobility. It needs to be done this way as the objects
113 * are constantly being re-read from disk, and we don't want to take any mob
114 * unless a fight actually occurrs.
118 /* initialize combat object */
121 att_combat_init(struct combat *com, int type)
123 memset(com, 0, sizeof(*com));
128 /* print a combat object with optional preposition */
131 pr_com(int inon, struct combat *com, natid who)
133 if (com->type == EF_SECTOR) {
135 inon ? inon == 1 ? "in " : "into " : "",
136 xyas(com->x, com->y, who));
137 } else if (com->type == EF_SHIP) {
138 return prbuf("%s%s %s(#%d)",
139 inon ? inon == 1 ? "on " : "onto " : "",
140 com->shp_mcp->m_name, com->shp_name,
142 } else if (com->type == EF_LAND) {
143 return prbuf("%s%s #%d",
144 inon ? inon == 1 ? "on " : "onto " : "",
145 com->lnd_lcp->l_name, com->lnd_uid);
147 return "your forces";
152 prcom(int inon, struct combat *com)
154 return pr_com(inon, com, player->cnum);
158 * This is the combat object "type" based integrity check. It basically
159 * splits along three divisions: ship/sector, attacker/defender,
160 * first time/not first time.
164 att_get_combat(struct combat *com, int isdef)
178 if (!getsect(com->x, com->y, §)) {
179 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
180 return att_combat_init(com, EF_BAD);
182 com->sct_type = sect.sct_type;
183 com->sct_dcp = &dchr[sect.sct_type];
184 mil = sect.sct_item[I_MILIT];
185 pstage = sect.sct_pstage;
186 owner = sect.sct_own;
187 eff = sect.sct_effic;
188 mob = sect.sct_mobil;
193 if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
195 pr("Land unit #%d is not in the same sector!\n",
197 return att_combat_init(com, EF_BAD);
199 if (isdef && player->owner) {
200 pr("Boarding yourself? Try using the 'load' command.\n");
201 return att_combat_init(com, EF_BAD);
203 com->lnd_lcp = &lchr[(int)land.lnd_type];
204 mil = land.lnd_item[I_MILIT];
205 pstage = land.lnd_pstage;
206 owner = land.lnd_own;
207 eff = land.lnd_effic;
208 mob = land.lnd_mobil;
213 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
215 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
217 pr("Ship #%d is not your ship!\n", com->shp_uid);
218 return att_combat_init(com, EF_BAD);
221 if (isdef && player->owner &&
222 ontradingblock(EF_SHIP, &ship)) {
223 pr("%s is on the trading block.\n", prcom(0, com));
224 return att_combat_init(com, EF_BAD);
227 if (isdef && player->owner) {
228 pr("Boarding yourself? Try using the 'tend' command.\n");
229 return att_combat_init(com, EF_BAD);
231 com->shp_mcp = &mchr[(int)ship.shp_type];
232 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
233 if (!isdef && !player->owner) {
235 pr("%s was just sunk!\n", prcom(0, com));
237 pr("Ship #%d is not your ship!\n", com->shp_uid);
238 return att_combat_init(com, EF_BAD);
240 mil = ship.shp_item[I_MILIT];
241 pstage = ship.shp_pstage;
242 owner = ship.shp_own;
243 eff = ship.shp_effic;
244 mob = ship.shp_mobil;
253 return att_combat_init(com, EF_BAD);
256 if (!com->set) { /* first time */
257 if (isdef) { /* defender */
259 } else { /* attacker */
261 pr("No mil %s\n", prcom(1, com));
263 pr("Only 1 mil %s\n", prcom(1, com));
264 /* don't abandon attacking sectors or ships */
265 com->troops = MAX(0, mil - 1);
267 com->plague = pstage == PLG_INFECT;
268 } else { /* not first time */
269 if (isdef) { /* defender */
270 if (com->x != x || com->y != y) {
271 pr("%s has moved!\n", prcom(0, com));
272 return att_combat_init(com, EF_BAD);
274 if (owner != com->own) {
276 pr("WARNING: The ownership of %s just changed from %s to %s!\n",
277 prcom(0, com), cname(com->own), cname(owner));
278 } else if (com->type == EF_SECTOR) {
279 pr("WARNING: %s just abandoned sector %s!\n",
281 xyas(com->x, com->y, player->cnum));
285 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
287 com->mil < mil ? "increased" : "decreased", com->mil,
290 } else { /* attacker */
291 if (owner != player->cnum && getrel(getnatp(owner), player->cnum) != ALLIED) {
292 /* must be EF_SECTOR */
294 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
295 com->mil, xyas(com->x, com->y, player->cnum),
298 pr("You no longer own %s\n",
299 xyas(com->x, com->y, player->cnum));
300 return att_combat_init(com, EF_BAD);
302 if (com->troops && com->troops + 1 > mil) {
303 if (com->own == owner && player->cnum == owner)
305 pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
306 prcom(1, com), com->troops, MAX(0, mil - 1));
307 com->troops = MAX(0, mil - 1);
322 * In the course of the fight, the combat object may have lost mil, eff, or
323 * mobility. This is the place where the data in the object gets flushed to
324 * disk to make it "real".
328 put_combat(struct combat *com)
337 getsect(com->x, com->y, §);
338 sect.sct_type = com->sct_type;
339 deff = sect.sct_effic - com->eff;
341 sect.sct_road -= (sect.sct_road * deff / 100.0);
342 sect.sct_rail -= (sect.sct_rail * deff / 100.0);
343 sect.sct_defense -= (sect.sct_defense * deff / 100.0);
344 if (sect.sct_road <= 0)
346 if (sect.sct_rail <= 0)
348 if (sect.sct_defense <= 0)
349 sect.sct_defense = 0;
351 sect.sct_effic = com->eff;
352 if (!opt_DEFENSE_INFRA)
353 sect.sct_defense = sect.sct_effic;
355 if (opt_MOB_ACCESS) {
356 if ((com->mob - com->mobcost) < -127)
357 sect.sct_mobil = -127;
359 sect.sct_mobil = (short)(com->mob - com->mobcost);
361 if ((com->mob - com->mobcost) < 0)
364 sect.sct_mobil = (short)(com->mob - com->mobcost);
367 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
368 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
369 sect.sct_own = com->own;
371 if (sect.sct_pstage == PLG_HEALTHY)
372 sect.sct_pstage = PLG_EXPOSED;
374 sect.sct_item[I_MILIT] = com->mil;
376 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
379 getland(com->lnd_uid, &land);
380 land.lnd_effic = com->eff;
382 if (com->mob - com->mobcost < -127)
383 land.lnd_mobil = -127;
385 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
387 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
389 land.lnd_own = com->own;
390 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
393 if (land.lnd_pstage == PLG_HEALTHY)
394 land.lnd_pstage = PLG_EXPOSED;
396 if (!(com->lnd_lcp->l_flags & L_SPY))
397 land.lnd_item[I_MILIT] = com->mil;
398 lnd_count_units(&land);
399 if (com->own == player->cnum) {
400 land.lnd_mission = 0;
402 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
404 putland(com->lnd_uid, &land);
407 getship(com->shp_uid, &ship);
408 ship.shp_effic = com->eff;
410 if (com->mob - com->mobcost < -127)
411 ship.shp_mobil = -127;
413 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
415 makelost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
417 ship.shp_own = com->own;
418 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
421 if (ship.shp_pstage == PLG_HEALTHY)
422 ship.shp_pstage = PLG_EXPOSED;
424 ship.shp_item[I_MILIT] = com->mil;
426 if (com->own == player->cnum) {
427 ship.shp_mission = 0;
429 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
431 putship(com->shp_uid, &ship);
434 att_get_combat(com, com->own != player->cnum);
437 /* If pre-attack, abort fight. If post-attack, don't move anything in */
442 return player->aborted = 1;
446 * This is the combat_mode based integrity check. It splits among two main
447 * divisions: first time/not first time, and attack/assault/para/board.
451 att_abort(int combat_mode, struct combat *off, struct combat *def)
460 if (att_get_combat(def, 1) < 0)
461 return abort_attack();
463 if (off && combat_mode != A_ATTACK) {
464 if (att_get_combat(off, 0) < 0)
465 return abort_attack();
466 if (off->type == EF_SHIP &&
467 (!getsect(off->x, off->y, §) ||
468 sect.sct_type != SCT_WATER)) {
469 pr("%s can not %s from that far inland!\n",
470 prcom(0, off), att_mode[combat_mode]);
471 return abort_attack();
474 switch (combat_mode) {
476 if (!neigh(def->x, def->y, player->cnum) &&
477 !adj_units(def->x, def->y, player->cnum)) {
478 pr("You are not adjacent to %s\n",
479 xyas(def->x, def->y, player->cnum));
480 return abort_attack();
482 if (def->own == player->cnum) {
483 pr("You can't attack your own sector.\n");
484 return abort_attack();
488 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
489 pr("You'll have to get there first...\n");
490 return abort_attack();
492 if (off && def->sct_type == SCT_MOUNT) {
493 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
494 return abort_attack();
498 if (def->own == player->cnum) {
499 pr("You can't air-assault your own sector.\n");
500 return abort_attack();
502 if (off && (def->sct_type == SCT_MOUNT ||
503 def->sct_type == SCT_WATER ||
504 def->sct_type == SCT_CAPIT ||
505 def->sct_type == SCT_FORTR ||
506 def->sct_type == SCT_WASTE)) {
507 pr("You can't air-assault a %s sector!\n",
508 def->sct_dcp->d_name);
509 return abort_attack();
513 return board_abort(off, def);
515 return land_board_abort(off, def);
518 if (off && def->sct_dcp->d_mcst <= 0) {
519 pr("You can't %s a %s sector!\n", att_mode[combat_mode],
520 def->sct_dcp->d_name);
521 return abort_attack();
523 if (!off || off->relations_checked)
525 off->relations_checked = 1;
528 setcont(player->cnum, def->own, FOUND_SPY);
529 setcont(def->own, player->cnum, FOUND_SPY);
531 if (opt_SLOW_WAR && def->own != player->cnum) {
532 natp = getnatp(player->cnum);
533 rel = getrel(natp, def->own);
536 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
537 cname(def->own), att_mode[combat_mode]);
538 if (!confirm(y_or_n))
539 return abort_attack();
542 if ((rel != AT_WAR) && (def->own) &&
543 (sect.sct_oldown != player->cnum)) {
544 pr("You're not at war with them!\n");
545 return abort_attack();
552 * Lots of special things need to be checked for boarding, so I put it in
557 board_abort(struct combat *off, struct combat *def)
559 struct shpstr aship, dship; /* for tech levels */
562 if (att_get_combat(def, 1) < 0)
563 return abort_attack();
568 if (att_get_combat(off, 0) < 0)
569 return abort_attack();
571 if (off->x != def->x || off->y != def->y) {
572 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
573 return abort_attack();
575 if (off->type == EF_SHIP) {
577 pr("%s has no mobility!\n", prcom(0, off));
578 return abort_attack();
580 getship(off->shp_uid, &aship);
581 getship(def->shp_uid, &dship);
582 if (techfact(aship.shp_tech, 1.0) * aship.shp_speed * off->eff
583 <= techfact(dship.shp_tech, 1.0) * dship.shp_speed * def->eff) {
584 pr("Victim ship moves faster than you do!\n");
587 "%s (#%d) %s failed to catch %s\n",
588 cname(aship.shp_own), aship.shp_own,
589 pr_com(0, off, def->own), pr_com(0, def, def->own));
590 return abort_attack();
592 } else if (off->type != EF_SECTOR) {
593 pr("Please tell the deity that you got the 'banana boat' error\n");
594 return abort_attack();
596 if (def->shp_mcp->m_flags & M_SUB) {
597 getsect(def->x, def->y, §);
598 if (sect.sct_type == SCT_WATER) {
599 pr("You can't board a submarine!\n");
600 return abort_attack();
607 * Lots of special things need to be checked for boarding, so I put it in
609 * STM - I copied it for land unit boarding. :)
613 land_board_abort(struct combat *off, struct combat *def)
615 if (att_get_combat(def, 1) < 0)
616 return abort_attack();
621 if (att_get_combat(off, 0) < 0)
622 return abort_attack();
624 if (off->x != def->x || off->y != def->y) {
625 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
626 return abort_attack();
632 /* If we are boarding, then the defending ship gets a chance to fire back */
634 att_approach(struct combat *off, struct combat *def)
640 pr("Approaching %s...\n", prcom(0, def));
643 "%s is being approached by %s...\n",
644 pr_com(0, def, def->own), pr_com(0, off, def->own));
645 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
648 pr("They're firing at us sir!\n");
651 "Your fleet at %s does %d damage to %s\n",
652 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
654 if (off->type == EF_SECTOR) {
655 getsect(off->x, off->y, §);
656 sectdamage(§, dam, 0);
658 pr("Enemy fleet at %s does %d damage to %s\n",
659 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
660 } else if (off->type == EF_SHIP) {
661 getship(off->shp_uid, &ship);
662 shipdamage(&ship, dam);
663 putship(off->shp_uid, &ship);
664 if (def->own && ship.shp_effic < SHIP_MINEFF) {
665 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
666 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
669 if (att_get_combat(off, 0) < 0)
670 return abort_attack();
674 /* The attack is valid. Tell the attacker about what they're going to hit */
677 att_show(struct combat *def)
679 /* Note that we tell the player about the treaty BEFORE we tell them
680 about the item. If we didn't, then they gain free information */
681 if (def->type == EF_SECTOR) {
682 if (!trechk(player->cnum, def->own, LANATT))
683 return abort_attack();
684 pr("%s is a %d%% %s %s with approximately %d military.\n",
685 xyas(def->x, def->y, player->cnum),
686 roundintby((int)def->eff, 10),
687 cname(def->own), def->sct_dcp->d_name,
688 roundintby(def->troops, 10));
689 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
690 writemap(player->cnum);
691 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
692 if (def->type == EF_SHIP) {
693 if (!trechk(player->cnum, def->own, SEAATT))
694 return abort_attack();
696 if (!trechk(player->cnum, def->own, LNDATT))
697 return abort_attack();
699 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));
701 /* Ok, everything is fine */
705 /* Attack and assault ask the user which kind of support they want */
708 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
713 *fortp = *shipp = *landp = *planep = 1;
715 if (player->argp[offset] != NULL) {
716 if ((player->argp[offset + 1] == NULL) ||
717 (player->argp[offset + 2] == NULL) ||
718 (player->argp[offset + 3] == NULL)) {
719 pr("If any support arguments are used, all must be!\n");
724 *landp = *planep = 0;
726 if (!(p = getstarg(player->argp[offset], "Use fort support? ",
730 if ((*p == 'y') || (*p == 'Y'))
733 if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
737 if ((*p == 'y') || (*p == 'Y'))
740 if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
744 if ((*p == 'y') || (*p == 'Y'))
747 if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
751 if ((*p == 'y') || (*p == 'Y'))
758 * Attack, assault, and board ask the attacker what they'd like to attack
759 * with. This includes mil and land units from each "off" object. Note that
760 * after each sub-prompt, we check to make sure that the attack is still
761 * valid, and if it's not, then we abort the attack.
765 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
766 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
769 char land_answer[256];
772 if (att_abort(combat_mode, off, def))
774 memset(land_answer, 0, sizeof(land_answer));
775 for (n = 0; n <= off->last; ++n) {
776 off[n].troops = ask_off(combat_mode, off + n, def);
777 if (att_abort(combat_mode, off, def))
779 ask_olist(combat_mode, off + n, def, olist, land_answer,
780 a_spyp, a_engineerp);
781 if (att_abort(combat_mode, off, def))
787 /* How many mil is off allowed to attack with when it attacks def? */
790 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
794 switch (combat_mode) {
796 mob_support = off->mob / sector_mcost(getsectp(def->x, def->y),
800 /* mob_support = off->mob / sector_mcost(def->sct_type, def->eff);*/
801 if (mob_support < off->troops)
802 pr("Sector %s has %d mobility which can only support %d mil,\n", xyas(off->x, off->y, player->cnum), off->mob, mob_support);
804 mob_support = off->troops;
807 if (def->own != player->cnum && def->mil) {
808 if (off->shp_mcp->m_flags & M_SEMILAND)
809 return off->troops / 4;
810 else if (!(off->shp_mcp->m_flags & M_LAND))
811 return off->troops / 10;
815 if (off->type == EF_SECTOR && off->mob <= 0)
817 mob_support = def->shp_mcp->m_item[I_MILIT];
818 if (mob_support < off->troops)
819 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
821 mob_support = off->troops;
826 if (def->lnd_lcp->l_flags & L_SPY)
828 mob_support = def->lnd_lcp->l_item[I_MILIT];
829 if (mob_support < off->troops)
830 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
832 mob_support = off->troops;
839 * If the attacker decides to go through with the attack, then the
840 * sectors/ships they are attacking with may be charged some mobility.
841 * This is where that amount of mobility is calculated. It is actually
842 * subtracted "for real" from the object's mobility in put_combat().
846 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
853 switch (combat_mode) {
857 (int)(attacking_mil *
858 sector_mcost(getsectp(def->x, def->y), MOB_ROAD)));
861 off->mobcost += MAX(1, attacking_mil / 5);
866 off->mobcost += MAX(1, attacking_mil / 5);
869 /* the 2 in the formula below is a fudge factor */
870 getship(def->shp_uid, &ship);
871 off->mobcost += (def->eff / 100) * (ship.shp_speed / 2);
876 /* How many mil to we want to attack from off against def? */
879 ask_off(int combat_mode, struct combat *off, struct combat *def)
885 if (att_get_combat(off, 0) <= 0)
887 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
889 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
891 if (off->type == EF_SECTOR) {
892 if (off->own != player->cnum)
894 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
895 off->sct_dcp->d_name,
896 xyas(off->x, off->y, player->cnum), mob_support);
898 sprintf(prompt, "Number of mil from %s (max %d) : ",
899 prcom(0, off), mob_support);
901 if ((attacking_mil = onearg(0, prompt)) < 0)
903 if (att_abort(combat_mode, off, def))
905 if (att_get_combat(off, 0) <= 0)
908 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
911 calc_mobcost(combat_mode, off, def, attacking_mil);
912 return attacking_mil;
916 * Which units would you like to attack with or move in with [ynYNq?]
920 att_prompt(char *prompt, char army)
928 p = getstring(prompt, buf);
929 if (player->aborted || (p && *p == 'q')) {
935 if (tolower(*p) == 'y' || tolower(*p) == 'n')
937 pr("y - yes this unit\n"
939 "Y - yes to all units in army '%c'\n"
940 "N - no to all units in army '%c'\n"
941 "q - quit\n? - this help message\n\n",
946 /* Ask the attacker which units they want to attack/assault/board with */
949 ask_olist(int combat_mode, struct combat *off, struct combat *def,
950 struct emp_qelem *olist, char *land_answer, int *a_spyp,
964 if (def->type == EF_LAND)
966 if (def->type == EF_SHIP)
967 maxland = def->shp_mcp->m_nland;
969 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
970 while (nxtitem(&ni, &land)) {
971 if (land.lnd_own != player->cnum)
973 if (land.lnd_effic < LAND_MINEFF)
975 if (land_answer[(int)land.lnd_army] == 'N')
977 if (!lnd_can_attack(&land))
979 lcp = &lchr[(int)land.lnd_type];
981 if (def->type == EF_SHIP && !maxland) {
982 pr("Land units are not able to board this kind of ship\n");
985 if (land.lnd_mobil <= 0) {
986 pr("%s is out of mobility, and cannot %s\n",
987 prland(&land), att_mode[combat_mode]);
992 if (ontradingblock(EF_LAND, &land)) {
993 pr("%s is on the trading block, and cannot %s\n",
994 prland(&land), att_mode[combat_mode]);
999 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1000 pr("%s is on ship #%d, and cannot %s\n",
1001 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1003 } else if (off->type == EF_SHIP) {
1004 if (land.lnd_ship != off->shp_uid)
1006 } else if (land.lnd_land >= 0) {
1007 pr("%s is on unit #%d, and cannot %s\n",
1008 prland(&land), land.lnd_land, att_mode[combat_mode]);
1011 switch (combat_mode) {
1014 lnd_mobcost(&land, getsectp(def->x, def->y), MOB_NONE);
1015 if (land.lnd_mobil < mobcost) {
1016 pr("%s does not have enough mobility (%d needed)\n",
1017 prland(&land), (int)ceil(mobcost));
1024 if (!(lcp->l_flags & L_ASSAULT))
1027 att_val = attack_val(combat_mode, &land);
1029 pr("%s has no offensive strength\n", prland(&land));
1032 resupply_all(&land);
1033 putland(land.lnd_uid, &land);
1034 if (!has_supply(&land)) {
1035 pr("%s is out of supply, and cannot %s\n",
1036 prland(&land), att_mode[combat_mode]);
1039 if (def->type == EF_SHIP && first_time) {
1041 pr("You may board with a maximum of %d land units\n", maxland);
1043 pr("%s has a base %s value of %d\n",
1044 prland(&land), att_mode[combat_mode], att_val);
1045 if (land_answer[(int)land.lnd_army] != 'Y') {
1047 "%s with %s %s (%c %d%%) [ynYNq?] ",
1048 att_mode[combat_mode],
1051 land.lnd_army == ' ' ? '~' : land.lnd_army,
1053 land_answer[(int)land.lnd_army] =
1054 att_prompt(prompt, land.lnd_army);
1055 if (att_abort(combat_mode, off, def))
1057 if (land_answer[(int)land.lnd_army] != 'y' &&
1058 land_answer[(int)land.lnd_army] != 'Y')
1061 if (!(llp = malloc(sizeof(struct llist)))) {
1062 logerror("Malloc failed in attack!\n");
1066 memset(llp, 0, sizeof(struct llist));
1067 emp_insque(&llp->queue, olist);
1068 llp->mobil = mobcost;
1069 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1071 if (lnd_spyval(&land) > *a_spyp)
1072 *a_spyp = lnd_spyval(&land);
1073 if (llp->lcp->l_flags & L_ENGINEER)
1075 if (def->type == EF_SHIP && ++count >= maxland)
1080 /* What's the offense or defense multiplier? */
1083 att_combat_eff(struct combat *com)
1089 if (com->type == EF_SECTOR) {
1090 eff = com->eff / 100.0;
1091 if (com->own == player->cnum) {
1092 str = com->sct_dcp->d_ostr;
1093 eff = 1.0 + ((str - 1.0) * eff);
1095 eff = sector_strength(getsectp(com->x, com->y));
1096 /* str = com->sct_dcp->d_dstr;*/
1097 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1098 getship(com->shp_uid, &ship);
1099 eff = (1.0 + ship.shp_armor / 100.0);
1105 * Estimate the defense strength and give the attacker a chance to abort
1106 * if the odds are less than 50%
1110 att_estimate_defense(int combat_mode, struct combat *off,
1111 struct emp_qelem *olist, struct combat *def,
1119 * Get the attacker units & mil again in case they changed while the
1120 * attacker was answering sub-prompts.
1123 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1124 if (att_empty_attack(combat_mode, ototal, def))
1125 return abort_attack();
1126 if (combat_mode == A_PARA)
1128 pr("\n Initial attack strength: %8d\n", ototal);
1130 estimate = att_combat_eff(def) * roundintby(def->troops, 10);
1131 estimate += att_combat_eff(def) * get_dlist(def, 0, a_spy, 0);
1134 * Calculate the initial (pre-support) attack odds. If they're less
1135 * than 50%, ask for a confirmation.
1138 odds = (int)(att_calcodds(ototal, estimate) * 100);
1142 pr(" Estimated defense strength: %8d\n", estimate);
1143 pr(" Estimated odds: %8d%%\n\n",odds);
1144 sprintf(prompt, "Are you sure you want to %s [yn]? ",
1145 att_mode[combat_mode]);
1146 if (!confirm(prompt))
1147 return abort_attack();
1148 ototal = get_ototal(combat_mode, off, olist,1.0,1);
1149 if (att_empty_attack(combat_mode, ototal, def))
1150 return abort_attack();
1156 /* Get the defensive units and reacting units */
1158 att_get_defense(struct emp_qelem *olist, struct combat *def,
1159 struct emp_qelem *dlist, int a_spy, int ototal)
1162 struct emp_qelem *qp;
1168 get_dlist(def, dlist, 0, &d_spy);
1169 dtotal = get_dtotal(def, dlist, 1.0, 0);
1172 * Call in reacting units
1175 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1176 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1178 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1179 llp = (struct llist *)qp;
1180 intelligence_report(def->own, &llp->land, d_spy,
1181 "Scouts report attacking unit:");
1184 old_dtotal = dtotal;
1185 dtotal = get_dtotal(def, dlist, 1.0, 0);
1186 if (dtotal != old_dtotal)
1187 pr("Defense strength with reacting units: %8d\n", dtotal);
1192 /* Get the defensive land units in the sector or on the ship */
1195 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1198 struct nstr_item ni;
1203 /* In here is where you need to take out spies and trains from the defending
1204 lists. Spies try to hide, trains get trapped and can be boarded. */
1206 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1207 while (nxtitem(&ni, &land)) {
1210 if (land.lnd_own != def->own)
1212 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1214 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1216 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1218 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1220 if (!list) { /* Just estimating the enemy strength */
1221 estimate += intelligence_report(player->cnum,
1223 "Scouts report defending unit:");
1226 if (!(llp = malloc(sizeof(struct llist)))) {
1227 logerror("Malloc failed in attack!\n");
1231 memset(llp, 0, sizeof(struct llist));
1232 emp_insque(&llp->queue, list);
1233 llp->supplied = has_supply(&land);
1234 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1236 if (lnd_spyval(&land) > *d_spyp)
1237 *d_spyp = lnd_spyval(&land);
1242 /* Calculate the total offensive strength */
1245 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1246 double osupport, int check)
1248 double ototal = 0.0;
1249 struct emp_qelem *qp, *next;
1254 * first, total the attacking mil
1257 for (n = 0; n <= off->last; ++n) {
1258 if (off[n].type == EF_BAD || (check &&
1259 att_get_combat(&off[n], 0) <= 0))
1261 ototal += off[n].troops * att_combat_eff(off + n);
1265 * next, add in the attack_values of all
1266 * the attacking units
1269 for (qp = olist->q_forw; qp != olist; qp = next) {
1271 llp = (struct llist *)qp;
1272 if (check && !get_land(combat_mode, 0, llp->land.lnd_uid, llp, 0))
1274 if (combat_mode == A_ATTACK) {
1276 for (n = 0; n <= off->last; ++n) {
1277 if (off[n].type == EF_BAD)
1279 if ((off[n].x == llp->land.lnd_x) &&
1280 (off[n].y == llp->land.lnd_y))
1284 lnd_delete(llp, "is in a sector not owned by you");
1287 ototal += attack_val(combat_mode, &llp->land) *
1288 att_combat_eff(off + w);
1290 ototal += attack_val(combat_mode, &llp->land);
1295 return ldround(ototal, 1);
1298 /* Calculate the total defensive strength */
1301 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1304 double dtotal = 0.0, eff = 1.0, d_unit;
1305 struct emp_qelem *qp, *next;
1308 if (check && att_get_combat(def, 1) < 0)
1310 eff = att_combat_eff(def);
1311 dtotal = def->troops * eff;
1314 * next, add in the defense_values of all
1315 * the defending non-retreating units
1318 for (qp = list->q_forw; qp != list; qp = next) {
1320 llp = (struct llist *)qp;
1322 if (check && !get_land(A_DEFEND, def, llp->land.lnd_uid, llp, 1))
1324 d_unit = defense_val(&llp->land);
1326 d_unit = ((double)defense_val(&llp->land) / 2.0);
1327 dtotal += d_unit * eff;
1332 return ldround(dtotal, 1);
1336 * This is the land unit integrity check. Note that we don't print
1337 * warnings about victim land units because the attacker may not have seen them
1341 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp,
1344 struct lndstr *lp = &llp->land;
1349 if (!llp->lcp) { /* first time */
1350 llp->x = llp->land.lnd_x;
1351 llp->y = llp->land.lnd_y;
1352 llp->lcp = &lchr[(int)llp->land.lnd_type];
1353 } else { /* not first time */
1354 if (lp->lnd_effic < LAND_MINEFF) {
1355 sprintf(buf, "was destroyed and is no longer a part of the %s",
1356 att_mode[combat_mode]);
1357 lnd_delete(llp, buf);
1361 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1363 "left to go fight another battle and is no longer a part of the defense");
1367 if (lp->lnd_own != player->cnum) {
1369 "was destroyed and is no longer a part of the %s",
1370 att_mode[combat_mode]);
1371 lnd_delete(llp, buf);
1374 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1376 "left to fight another battle and is no longer a part of the %s",
1377 att_mode[combat_mode]);
1378 lnd_delete(llp, buf);
1381 if (lp->lnd_effic < llp->eff) {
1382 sprintf(buf, "damaged from %d%% to %d%%",
1383 llp->eff, lp->lnd_effic);
1384 lnd_print(llp, buf);
1388 llp->eff = llp->land.lnd_effic;
1394 * Put the land unit on the disk. If there was some mobility cost, then
1395 * subtract it from the units mobility. Note that this works the same way
1396 * as sectors & ships in that no mobility is actually taken until the attacker
1397 * has committed to attacking.
1401 kill_land(struct emp_qelem *list)
1403 struct emp_qelem *qp, *next;
1406 for (qp = list->q_forw; qp != list; qp = next) {
1408 llp = (struct llist *)qp;
1409 if (llp->land.lnd_ship >= 0) {
1410 llp->land.lnd_effic = 0;
1411 lnd_delete(llp, "cannot return to the ship, and dies!");
1417 att_infect_units(struct emp_qelem *list, int plague)
1419 struct emp_qelem *qp, *next;
1424 for (qp = list->q_forw; qp != list; qp = next) {
1426 llp = (struct llist *)qp;
1427 if (llp->land.lnd_pstage == PLG_HEALTHY)
1428 llp->land.lnd_pstage = PLG_EXPOSED;
1433 put_land(struct emp_qelem *list)
1435 struct emp_qelem *qp, *next;
1438 for (qp = list->q_forw; qp != list; qp = next) {
1440 llp = (struct llist *)qp;
1441 llp->land.lnd_mission = 0;
1442 llp->land.lnd_harden = 0;
1443 llp->land.lnd_mobil -= (int)llp->mobil;
1445 putland(llp->land.lnd_uid, &llp->land);
1446 if (llp->land.lnd_own != player->cnum) {
1447 emp_remque((struct emp_qelem *)llp);
1450 get_land(A_ATTACK, 0, llp->land.lnd_uid, llp, 0);
1455 * Keep sending in reinforcements until it looks like we're going to win.
1456 * Note that the "strength" command also calls this routine.
1460 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1461 int *d_spyp, int ototal)
1463 struct nstr_item ni;
1465 struct sctstr sect, dsect;
1475 double eff = att_combat_eff(def);
1480 * All units that are within their reaction radius and not damaged
1481 * below their morale value now get to react to the threatened sect.
1482 * Once we've sent enough to counter the threat, stop sending them.
1484 * Not anymore. All units get to react. :)
1488 dtotal = get_dtotal(def, list, 1.0, 1);
1491 snxtitem_all(&ni, EF_LAND);
1492 while (nxtitem(&ni, &land) &&
1493 (dtotal + new_land * eff < (int)(1.2 * (float)ototal))) {
1496 if (!land.lnd_rad_max)
1498 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1500 if (land.lnd_own != def->own)
1502 if (land.lnd_ship >= 0)
1504 if (!defense_val(&land))
1507 if (land.lnd_effic <= land.lnd_retreat)
1510 if (!lnd_can_attack(&land))
1513 /* Only supplied units can react */
1514 if (!(supply_state = has_supply(&land)))
1517 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1519 getsect(land.lnd_x, land.lnd_y, §);
1520 /* Units on efficient headquarters can react 1 farther */
1521 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1522 radius = land.lnd_rad_max + 1;
1524 radius = land.lnd_rad_max;
1526 if (land.lnd_mission == MI_RESERVE)
1532 getsect(def->x, def->y, &dsect);
1533 if (!BestLandPath(buf, §, &dsect, &move_cost, MOB_ROAD))
1536 mobcost = land.lnd_effic * 0.01 * lchr[(int)land.lnd_type].l_spd;
1539 mobcost = 480.0 / (mobcost + techfact(land.lnd_tech, mobcost));
1540 mobcost *= (move_cost * 5.0);
1542 if (land.lnd_mobil < mobcost)
1545 new_land += defense_val(&land);
1547 if (!list) /* we are in the "strength" command */
1550 /* move to defending sector */
1551 land.lnd_mobil -= ldround(mobcost, 1);
1554 land.lnd_x = def->x;
1555 land.lnd_y = def->y;
1556 putland(land.lnd_uid, &land);
1557 wu(0, land.lnd_own, "%s reacts to %s.\n",
1558 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1560 llp = (struct llist *)
1561 malloc(sizeof(struct llist));
1563 memset(llp, 0, sizeof(struct llist));
1564 llp->supplied = supply_state;
1567 llp->lcp = &lchr[(int)land.lnd_type];
1569 emp_insque(&llp->queue, list);
1570 if (lnd_spyval(&land) > *d_spyp)
1571 *d_spyp = lnd_spyval(&land);
1573 intelligence_report(player->cnum, &land, a_spy,
1574 "Scouts sight reacting enemy unit:");
1579 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1582 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1583 int land_sup, int plane_sup)
1585 double osupport = 1.0;
1587 double af, as, au, ap;
1589 af = as = au = ap = 0.0;
1591 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1592 af = ((double)dam / 100.0);
1596 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1598 as = ((double)dam / 100.0);
1603 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1604 au = ((double)dam / 100.0);
1609 dam = off_support(def->x, def->y, def->own, player->cnum);
1610 ap = (((double)dam) / 100.0);
1613 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1618 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1621 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1622 int ototal, int dtotal)
1624 double dsupport = 1.0;
1626 double df, ds, du, dp;
1629 df = ds = du = dp = 0.0;
1630 if (dtotal < 0.1 * ototal) {
1632 } else if (dtotal >= 1.2 * ototal) {
1635 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1636 df = ((double)dam / 100.0);
1639 dtotal = get_dtotal(def, list, dsupport, 0);
1640 if (dtotal < 1.2 * ototal) {
1641 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1642 ds = ((double)dam / 100.0);
1644 dtotal = get_dtotal(def, list, dsupport, 0);
1646 if (dtotal < 1.2 * ototal) {
1647 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1648 du = ((double)dam / 100.0);
1650 dtotal = get_dtotal(def, list, dsupport, 1);
1652 if (dtotal < 1.2 * ototal) {
1653 dam = def_support(def->x, def->y, player->cnum, def->own);
1654 dp = (((double)dam) / 100.0);
1661 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1666 "\nOdds are bad for us...support cancelled.\n\n");
1669 "\nOdds are good for us...support cancelled.\n\n");
1675 * Land mines add to the defense multiplier. If the attacker has engineers
1676 * then this multiplier is cut in half.
1680 get_mine_dsupport(struct combat *def, int a_engineer)
1685 getsect(def->x, def->y, §);
1687 if (sect.sct_oldown != player->cnum) {
1688 mines = MIN(sect.sct_mines, 20);
1690 mines = ldround(((double)mines / 2.0), 1);
1693 wu(0, def->own, "Defending mines add %1.2f\n",
1695 pr("Defending mines add %1.2f\n", mines * 0.02);
1696 return mines * 0.02;
1702 /* Get the offensive and defensive support */
1704 att_get_support(int combat_mode, int ofort, int oship, int oland,
1705 int oplane, struct emp_qelem *olist, struct combat *off,
1706 struct emp_qelem *dlist, struct combat *def,
1707 double *osupportp, double *dsupportp, int a_engineer)
1710 char osupports[512];
1711 char dsupports[512];
1713 if (combat_mode == A_PARA)
1716 *osupportp = get_osupport(osupports, def,
1717 ofort, oship, oland, oplane);
1720 * I need to put a 1 at the end of the next four total_stren calls
1721 * becauase units & mil may have been damaged by collateral damage or
1722 * neclear warheads from the offensive & defensive support.
1725 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1726 if (att_empty_attack(combat_mode, ototal, def))
1727 return abort_attack();
1728 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1731 * Calculate defensive support. If odds are too good or too bad
1732 * then don't call in support.
1735 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1736 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1737 if (att_empty_attack(combat_mode, ototal, def))
1738 return abort_attack();
1740 if ((*osupports || *dsupports) &&
1741 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1742 pr("\n\t\tsupport values\n");
1743 pr("\t\tforts\tships\tunits\tplanes\n");
1744 if (*osupportp != 1.0)
1745 pr("%s", osupports);
1746 if (*dsupportp != 1.0)
1747 pr("%s", dsupports);
1749 wu(0, def->own, "\n\t\tsupport values\n");
1750 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1751 if (*osupportp != 1.0)
1752 wu(0, def->own, "%s", osupports);
1753 if (*dsupportp != 1.0)
1754 wu(0, def->own, "%s", dsupports);
1758 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1759 if (dtotal && def->type == EF_SECTOR)
1760 *dsupportp += get_mine_dsupport(def, a_engineer);
1764 /* How many two-legged bipeds are in this combat force? */
1767 count_bodies(struct combat *off, struct emp_qelem *list)
1771 struct emp_qelem *qp;
1774 for (n = 0; n <= off->last; ++n)
1775 bodies += off[n].troops;
1776 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1777 llp = (struct llist *)qp;
1778 bodies += total_mil(&llp->land);
1783 /* This is where the fighting actually occurs. */
1786 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1787 double osupport, struct combat *def, struct emp_qelem *dlist,
1791 int a_cas = 0; /* Casualty counts */
1793 int ototal; /* total attacking strength */
1794 int dtotal; /* total defending strength */
1795 int a_bodies; /* total attacking mil (incl. mil in units) */
1796 int d_bodies; /* total defending mil (incl. mil in units) */
1805 ototal = get_ototal(combat_mode, off, olist, osupport,
1806 combat_mode != A_PARA);
1807 dtotal = get_dtotal(def, dlist, dsupport, 0);
1811 a_bodies = count_bodies(off, olist);
1812 d_bodies = count_bodies(def, dlist);
1813 d_mil = def->troops;
1814 for (n = 0; n <= off->last; ++n)
1815 if (off[n].type == EF_BAD)
1818 a_troops[n] = off[n].troops;
1820 /* This switch is required to get the spacing right */
1821 switch (combat_mode) {
1823 pr(" Final attack strength: %8d\n", ototal);
1826 pr(" Final assault strength: %8d\n", ototal);
1829 if (def->sct_type == SCT_MOUNT ||
1830 def->sct_type == SCT_WATER ||
1831 def->sct_type == SCT_CAPIT ||
1832 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1833 pr("You can't air-assault a %s sector!\n",
1834 def->sct_dcp->d_name);
1837 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1839 pr(" Final air-assault strength: %8d\n", ototal);
1843 pr(" Final board strength: %8d\n", ototal);
1847 pr(" Final defense strength: %8d\n", dtotal);
1848 odds = att_calcodds(ototal, dtotal);
1849 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1851 /* spread the plague */
1852 if (combat_mode != A_PARA) {
1854 for (n = 0; n <= off->last; ++n)
1855 if (off[n].type != EF_BAD)
1856 def->plague |= off[n].plague;
1857 for (n = 0; n <= off->last; ++n)
1858 if (off[n].type != EF_BAD)
1859 off[n].plague |= def->plague;
1861 att_infect_units(olist, off->plague);
1862 att_infect_units(dlist, def->plague);
1864 /* Fighting is slightly random. There is always that last little
1865 * effort you see people put in. Or the stray bullet that takes out
1866 * an officer and the rest go into chaos. Things like that.
1867 * Thus, we have added a very slight random factor that will sometimes
1868 * allow the little guy to win. We modify the odds a little
1869 * (either +- 5%) to account for this randomness. We also only
1870 * recalculate the odds every 8-50 casualties, not every cacsualty,
1871 * since a single dead guy normally wouldn't cause a commander to
1872 * rethink his strategies, but 50 dead guys might. */
1873 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1878 recalctime = 8 + (random() % 43);
1879 while (!success && ototal) {
1882 d_cas += take_casualty(A_DEFEND, def, dlist);
1883 dtotal = get_dtotal(def, dlist, dsupport, 0);
1888 a_cas += take_casualty(combat_mode, off, olist);
1889 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1891 if (((a_cas + d_cas) % 70) == 69)
1893 if (recalctime-- <= 0) {
1894 recalctime = 8 + (random() % 43);
1895 odds = att_calcodds(ototal, dtotal);
1896 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1904 /* update defense mobility & mil */
1908 if (def->type == EF_SECTOR && d_mil && d_cas) {
1911 /* Make sure we use a positive mobility here */
1912 tmob = ((def->mob < 0) ? -(def->mob) : (def->mob));
1914 MIN(20, MIN(1, tmob - damage(tmob, 100 * d_cas / d_mil)));
1916 def->mil = def->troops;
1919 /* update attack mobility & mil */
1920 for (n = 0; n <= off->last; ++n)
1921 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1922 if (off[n].type == EF_SECTOR && off[n].mil)
1925 off[n].mob - damage(off[n].mob,
1926 100 * (a_troops[n] - off[n].troops)
1928 off[n].mil -= a_troops[n] - off[n].troops;
1931 /* update land unit mobility */
1932 if (d_bodies && d_cas)
1933 lnd_takemob(dlist, (double)d_cas / d_bodies);
1934 if (a_bodies && a_cas)
1935 lnd_takemob(olist, (double)a_cas / a_bodies);
1937 /* damage attacked sector */
1938 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1940 pr("- Casualties -\n Yours: %d\n", a_cas);
1941 pr(" Theirs: %d\n", d_cas);
1942 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1943 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1946 switch (combat_mode) {
1948 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1949 pr("We have captured %s, sir!\n", prcom(0, def));
1953 news_item = def->own ? N_AWON_SECT : N_START_COL;
1954 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1955 action = "assaulting and taking";
1958 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1959 pr("We have captured %s, sir!\n", prcom(0, def));
1960 action = "air-assaulting and taking";
1963 news_item = N_BOARD_SHIP;
1964 pr("We have boarded %s, sir!\n", prcom(0, def));
1965 action = "boarding";
1968 news_item = N_BOARD_LAND;
1969 pr("We have boarded %s, sir!\n", prcom(0, def));
1970 action = "boarding";
1974 switch (combat_mode) {
1976 news_item = N_SCT_LOSE;
1977 pr("You have been defeated!\n");
1978 action = "attacking";
1981 news_item = N_ALOSE_SCT;
1982 pr("You have been defeated!\n");
1984 action = "trying to assault";
1987 news_item = N_PLOSE_SCT;
1988 pr("All of your troops were destroyed\n");
1989 action = "trying to air-assault";
1992 news_item = N_SHP_LOSE;
1993 pr("You have been repelled\n");
1995 action = "trying to board";
1998 news_item = N_LND_LOSE;
1999 pr("You have been repelled\n");
2001 action = "trying to board";
2005 nreport(player->cnum, news_item, def->own, 1);
2008 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2009 cname(player->cnum), player->cnum, a_cas,
2010 action, pr_com(0, def, def->own), d_cas);
2013 send_reacting_units_home(dlist);
2015 /* putland the defending land */
2018 /* putland the attacking land */
2021 /* put the victim sector/ship/land */
2022 if (!success || !take_def(combat_mode, olist, off, def))
2025 /* put the attacking sectors/ship */
2026 for (n = 0; n <= off->last; ++n)
2027 if (off[n].type != EF_BAD)
2028 put_combat(&off[n]);
2033 switch (combat_mode) {
2035 ask_move_in(off, olist, def);
2037 /* put sectors again to get abandon warnings */
2038 for (n = 0; n <= off->last; ++n)
2039 if (off[n].type != EF_BAD)
2040 put_combat(&off[n]);
2043 att_move_in_off(combat_mode, off, olist, def);
2046 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2050 /* What percentage of the combat forces going head-to-head are we? */
2053 att_calcodds(int ototal, int dtotal)
2057 /* calculate odds */
2060 else if (dtotal <= 0)
2063 odds = ((double)ototal) / (dtotal + ototal);
2068 /* Here's where the dead soldiers get dragged off the battlefield */
2071 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2073 int to_take = CASUALTY_LUMP;
2074 int biggest_troops = 0, index = -1;
2075 int n, tot_troops = 0, biggest_mil, cas;
2076 struct emp_qelem *qp, *biggest;
2079 for (n = 0; n <= off->last; ++n) {
2080 if (off[n].type != EF_BAD) {
2081 tot_troops += off[n].troops;
2082 if (off[n].troops > biggest_troops) {
2083 biggest_troops = off[n].troops;
2090 to_take -= tot_troops;
2093 for (n = 0; n <= off->last; ++n)
2094 if (off[n].type != EF_BAD)
2098 * They can all come off mil. We rotate the casualties,
2099 * starting with the sector containing the most mil.
2101 to_take = CASUALTY_LUMP;
2103 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2106 while (to_take > 0) {
2107 for (n = index; n <= off->last && to_take; ++n) {
2108 if (off[n].type != EF_BAD && off[n].troops > 0) {
2113 for (n = 0; n < index && to_take; ++n) {
2114 if (off[n].type != EF_BAD && off[n].troops > 0) {
2120 return CASUALTY_LUMP;
2124 return CASUALTY_LUMP - to_take;
2127 * Need to take some casualties from attacking units
2128 * Procedure: find the biggest unit remaining (in
2129 * terms of mil) and give it the casualties.
2133 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2134 llp = (struct llist *)qp;
2136 if (total_mil(&llp->land) > biggest_mil) {
2137 biggest_mil = total_mil(&llp->land);
2141 if (biggest == NULL)
2142 return CASUALTY_LUMP - to_take;
2144 llp = (struct llist *)biggest;
2145 cas = lnd_take_casualty(combat_mode, llp, to_take);
2146 return CASUALTY_LUMP - (to_take - cas);
2149 /* Send reacting defense units back to where they came from (at no mob cost) */
2152 send_reacting_units_home(struct emp_qelem *list)
2154 struct emp_qelem *qp, *next;
2158 for (qp = list->q_forw; qp != list; qp = next) {
2160 llp = (struct llist *)qp;
2161 if ((llp->land.lnd_x != llp->x) || (llp->land.lnd_y != llp->y)) {
2162 sprintf(buf, "returns to %s",
2163 xyas(llp->x, llp->y, llp->land.lnd_own));
2164 llp->land.lnd_x = llp->x;
2165 llp->land.lnd_y = llp->y;
2166 lnd_delete(llp, buf);
2171 /* Check for 0 offense strength. This call will always preceed an abort */
2174 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2177 if (def->own && player->cnum != def->own) {
2179 "%s (#%d) considered %sing you @%s\n",
2180 cname(player->cnum), player->cnum,
2181 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2183 pr("No troops for %s...\n", att_mode[combat_mode]);
2190 * Take the defending sector or ship from the defender and give it to the
2195 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2200 struct llist *llp, *delete_me = 0;
2206 for (n = 0; n <= off->last && !occuppied; ++n) {
2207 if (off[n].type != EF_BAD &&
2208 off[n].troops > 0 &&
2209 (off[n].type != EF_SECTOR || off[n].mob)) {
2211 if (def->type == EF_LAND) {
2212 if (def->lnd_lcp->l_flags & L_SPY) {
2219 pr("1 mil from %s moves %s\n",
2220 prcom(0, off + n), prcom(2, def));
2225 pr("%s left unoccupied\n", prcom(0, def));
2228 "No enemy troops moved %s so you still own it!\n",
2229 pr_com(2, def, def->own));
2232 llp = (struct llist *)list->q_forw;
2233 llp->land.lnd_x = def->x;
2234 llp->land.lnd_y = def->y;
2235 take_move_in_mob(combat_mode, llp, off, def);
2236 if (def->type == EF_SHIP) {
2237 llp->land.lnd_ship = def->shp_uid;
2238 sprintf(buf, "boards %s", prcom(0, def));
2241 llp->land.lnd_ship = -1;
2242 sprintf(buf, "moves in to occupy %s",
2243 xyas(def->x, def->y, player->cnum));
2244 lnd_delete(llp, buf);
2249 if (def->type == EF_SECTOR) {
2250 getsect(def->x, def->y, §);
2251 takeover(§, player->cnum);
2252 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2253 caploss(§, def->own,
2254 "* We have captured %s's capital, sir! *\n");
2256 } else if (def->type == EF_SHIP) {
2257 getship(def->shp_uid, &ship);
2258 takeover_ship(&ship, player->cnum, 1);
2259 putship(ship.shp_uid, &ship);
2260 } else if (def->type == EF_LAND) {
2261 getland(def->lnd_uid, &land);
2262 takeover_land(&land, player->cnum, 1);
2263 putland(land.lnd_uid, &land);
2266 lnd_delete(delete_me, buf);
2267 att_get_combat(def, 0);
2272 * Ask the attacker which mil & land units they'd like to move into the
2277 ask_move_in(struct combat *off, struct emp_qelem *olist,
2281 struct emp_qelem *qp, *next;
2285 char land_answer[256];
2288 for (n = 0; n <= off->last; ++n)
2289 if (off[n].type != EF_BAD && off[n].troops > 0)
2291 ask_move_in_off(&off[n], def);
2292 if (player->aborted)
2298 memset(land_answer, 0, sizeof(land_answer));
2299 for (qp = olist->q_forw; qp != olist; qp = next) {
2301 llp = (struct llist *)qp;
2302 answerp = &land_answer[(int)llp->land.lnd_army];
2303 if (player->aborted || att_get_combat(def, 0) < 0)
2305 if (*answerp == 'Y')
2307 if (*answerp != 'N') {
2308 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2310 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2312 llp->land.lnd_army == ' ' ? '~' : llp->land.lnd_army,
2313 llp->land.lnd_effic);
2314 *answerp = att_prompt(prompt, llp->land.lnd_army);
2315 if (player->aborted || att_get_combat(def, 0) < 0)
2317 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2320 if (*answerp == 'y' || *answerp == 'Y')
2322 sprintf(buf, "stays in %s",
2323 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2324 lnd_delete(llp, buf);
2328 if (att_get_combat(def, 0) < 0) {
2329 for (qp = olist->q_forw; qp != olist; qp = next) {
2331 llp = (struct llist *)qp;
2332 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2334 sprintf(buf, "stays in %s",
2335 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2336 lnd_delete(llp, buf);
2340 if (opt_INTERDICT_ATT)
2341 lnd_interdict(olist, def->x, def->y, player->cnum);
2342 move_in_land(A_ATTACK, off, olist, def);
2345 /* Move offensive land units to the conquered sector or ship */
2348 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2351 struct emp_qelem *qp, *next;
2357 for (qp = olist->q_forw; qp != olist; qp = next) {
2359 llp = (struct llist *)qp;
2360 if (!get_land(combat_mode, def, llp->land.lnd_uid, llp, 0))
2362 take_move_in_mob(combat_mode, llp, off, def);
2363 llp->land.lnd_x = def->x;
2364 llp->land.lnd_y = def->y;
2365 if (def->type == EF_SHIP)
2366 llp->land.lnd_ship = def->shp_uid;
2368 llp->land.lnd_ship = -1;
2372 if (def->type == EF_SECTOR) {
2373 if (opt_INTERDICT_ATT) {
2374 lnd_sweep(olist, 0, 0, def->own);
2375 lnd_check_mines(olist);
2377 sprintf(buf, "now occupies %s", prcom(0, def));
2379 sprintf(buf, "boards %s", prcom(0, def));
2383 for (qp = olist->q_forw; qp != olist; qp = next) {
2385 llp = (struct llist *)qp;
2386 lnd_print(llp, buf);
2394 * Move assaulting, paradropping, or boarding mil & units into def
2395 * If the mil are coming from a ship, then pack a lunch.
2399 att_move_in_off(int combat_mode, struct combat *off,
2400 struct emp_qelem *olist, struct combat *def)
2407 move_in_land(combat_mode, off, olist, def);
2409 for (n = 0; n <= off->last; ++n) {
2410 if (off[n].type == EF_BAD || !off[n].troops)
2412 troops = off[n].troops;
2414 off[n].mil -= troops;
2416 put_combat(off + n);
2417 if (combat_mode == A_ASSAULT) {
2418 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2420 getship(off[n].shp_uid, &ship);
2421 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2422 / (ship.shp_item[I_MILIT] + troops
2423 + ship.shp_item[I_CIVIL] + 0.5));
2425 ship.shp_item[I_FOOD] -= lunchbox;
2426 putship(ship.shp_uid, &ship);
2432 if (combat_mode == A_ASSAULT) {
2433 if (CANT_HAPPEN(def->type != EF_SECTOR))
2435 getsect(def->x, def->y, §);
2436 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2437 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2438 sect.sct_item[I_FOOD] += lunchbox;
2444 /* Ask how many mil to move in from each sector */
2447 ask_move_in_off(struct combat *off, struct combat *def)
2450 int num_mil, dam = 0, left;
2456 if (att_get_combat(off, 0) <= 0)
2458 if (att_get_combat(def, 0) < 0)
2460 if (off->own != player->cnum)
2462 d = sector_mcost(getsectp(def->x, def->y), MOB_ROAD);
2463 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2465 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2466 xyas(off->x, off->y, player->cnum), mob_support);
2467 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0)
2469 /* Make sure we don't move in more than we can support mobility-wise */
2470 if (num_mil > mob_support)
2471 num_mil = mob_support;
2472 if (att_get_combat(off, 0) <= 0)
2474 if (att_get_combat(def, 0) < 0)
2476 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2477 pr("No mil moved in from %s\n",
2478 xyas(off->x, off->y, player->cnum));
2481 mob_support = MAX(1, (int)(num_mil * d));
2482 off->mob -= MIN(off->mob, mob_support);
2483 off->mil -= num_mil;
2484 off->troops -= num_mil;
2487 weight = num_mil * ichr[I_MILIT].i_lbs;
2488 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2489 if (chance(weight / 100.0))
2491 ground_interdict(def->x, def->y, player->cnum, "military");
2492 dam += check_lmines(def->x, def->y, weight);
2496 left = commdamage(num_mil, dam, I_MILIT);
2497 if (left < num_mil) {
2499 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));
2501 pr("All of the mil you were moving were destroyed!\n");
2504 /* maybe got nuked */
2505 if (att_get_combat(def, 0) < 0)
2513 /* Charge land units for moving into a sector or onto a ship */
2516 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off,
2522 switch (combat_mode) {
2525 lnd_mobcost(&llp->land, getsectp(def->x, def->y), MOB_NONE);
2526 new = llp->land.lnd_mobil - mobcost;
2529 llp->land.lnd_mobil = new;
2532 if (off->shp_mcp->m_flags & M_LAND) {
2533 if (llp->lcp->l_flags & L_MARINE)
2534 llp->land.lnd_mobil -=
2535 (float)etu_per_update * land_mob_scale * 0.5;
2537 llp->land.lnd_mobil -= (float)etu_per_update * land_mob_scale;
2539 if (llp->lcp->l_flags & L_MARINE)
2540 llp->land.lnd_mobil = 0;
2542 llp->land.lnd_mobil = -(float)etu_per_update * land_mob_scale;
2546 /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2547 if (llp->lcp->l_flags & L_MARINE)
2548 llp->land.lnd_mobil -= 10;
2550 llp->land.lnd_mobil -= 40;
2553 llp->land.lnd_harden = 0;
2557 free_list(struct emp_qelem *list)
2559 struct emp_qelem *qp, *next;
2561 if (!list || QEMPTY(list))
2565 while (qp != list) {
2574 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2582 * sector_strength - Everyone starts at 1. You can get up to a max
2583 * of d_dstr, depending on how much you build up the
2584 * defenses of the sector.
2588 sector_strength(struct sctstr *sp)
2594 if (sp->sct_type == SCT_MOUNT)
2597 d = d + ((double)(dchr[sp->sct_type].d_dstr - d) *
2598 ((double)sp->sct_defense / 100.0));
2600 if (d > dchr[sp->sct_type].d_dstr)
2601 d = dchr[sp->sct_type].d_dstr;