2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * attsub.c: Attack subroutines
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
53 #include "prototypes.h"
55 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
57 static void ask_olist(int combat_mode, struct combat *off,
58 struct combat *def, struct emp_qelem *olist,
59 s_char *land_answer, int *a_spyp, int *a_engineerp);
60 static void take_move_in_mob(int combat_mode, struct llist *llp,
61 struct combat *off, struct combat *def);
62 static void move_in_land(int combat_mode, struct combat *off,
63 struct emp_qelem *olist, struct combat *def);
64 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
66 static void ask_move_in_off(struct combat *off, struct combat *def);
68 static int board_abort(struct combat *off, struct combat *def);
69 static int land_board_abort(struct combat *off, struct combat *def);
70 static int ask_off(int combat_mode, struct combat *off,
72 static int get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
74 static int get_ototal(int combat_mode, struct combat *off,
75 struct emp_qelem *olist, double osupport, int check);
76 static int get_dtotal(struct combat *def, struct emp_qelem *list,
77 double dsupport, int check);
78 static int take_casualty(int combat_mode, struct combat *off,
79 struct emp_qelem *olist);
81 static void send_reacting_units_home(struct combat *def,
82 struct emp_qelem *list);
83 static int take_def(int combat_mode, struct emp_qelem *list,
84 struct combat *off, struct combat *def);
86 void att_move_in_off(int combat_mode, struct combat *off,
87 struct emp_qelem *olist, struct combat *def);
88 static int get_land(int combat_mode, struct combat *def, int uid,
89 struct llist *llp, int victim_land);
92 * The principal object in this code is the "combat" object. A combat object
93 * is either a sector or ship. There are
94 * usually two instances of this, the "def" or defense combat object, and
95 * the array of "off" or offense objects. The number of offense objects is
96 * determined by the value of off->last (e.g. more than one attacking sector).
97 * the type of the object is determined by combat->type which can take the
98 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
99 * which is often passed to these functions is combat_mode. This can take
100 * the value A_DEFENSE, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
101 * As these six modes of being in combat affect things like mobcost and combat
102 * value, there are often switches made on combat_mode. Note that in all cases
103 * no mobility is taken from sectors, ships, or land units until the player
104 * has committed to a fight. Instead, the cost is temporarily placed in
105 * combat->mobcost, or llp->mobil as the case may be, and then when the object
106 * is "put" back onto disk, then the amounts in these variables are subtracted
107 * from the object's mobility. It needs to be done this way as the objects
108 * are constantly being re-read from disk, and we don't want to take any mob
109 * unless a fight actually occurrs.
113 /* initialize combat object */
116 att_combat_init(struct combat *com, int type)
118 memset(com, 0, sizeof(*com));
123 /* print a combat object with optional preposition */
126 pr_com(int inon, struct combat *com, natid who)
128 if (com->type == EF_SECTOR) {
130 inon ? inon == 1 ? "in " : "into " : "",
131 xyas(com->x, com->y, who));
132 } else if (com->type == EF_SHIP) {
134 return prbuf("%s%s %s(#%d)",
135 inon ? inon == 1 ? "on " : "onto " : "",
136 com->shp_mcp->m_name, com->shp_name,
139 return prbuf("%s%s #%d",
140 inon ? inon == 1 ? "on " : "onto " : "",
141 com->shp_mcp->m_name, com->shp_uid);
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);
158 /* Doing a sneak attack */
160 do_sneak(struct combat *def, int success)
163 struct natstr *natp = getnatp(player->cnum);
164 int issneak = getrel(natp, def->own);
166 if (def->type != EF_SECTOR)
169 getsect(def->x, def->y, §);
171 if (issneak == AT_WAR || !def->own || sect.sct_oldown == player->cnum)
175 pr("Your sneak attack was successful\nBut ");
177 pr("Your sneak attack was unsuccessful\nAnd ");
179 pr("it will cost you $5000\n");
180 pr("War has been declared!!!!\n");
181 wu(0, def->own, "Country %s (#%d) has Sneak Attacked!!\n",
182 cname(player->cnum), player->cnum);
183 wu(0, def->own, "Country %s (#%d) has Declared WAR on you!!\n",
184 cname(player->cnum), player->cnum);
185 player->dolcost += 5000;
186 issneak = min(issneak, MOBILIZATION);
187 nreport(player->cnum, N_DECL_WAR, def->own, 1);
188 setrel(player->cnum, def->own, issneak);
192 * This is the combat object "type" based integrity check. It basically
193 * splits along three divisions: ship/sector, attacker/defender,
194 * first time/not first time.
198 att_get_combat(struct combat *com, int isdef)
212 if (!getsect(com->x, com->y, §)) {
213 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
214 return att_combat_init(com, EF_BAD);
216 com->sct_type = sect.sct_type;
217 com->sct_dcp = &dchr[sect.sct_type];
218 thing = (s_char *)§
219 owner = sect.sct_own;
220 eff = sect.sct_effic;
221 mob = sect.sct_mobil;
226 if (!getland(com->lnd_uid, &land)) {
228 pr("Land unit #%d is not in the same sector!\n",
230 return att_combat_init(com, EF_BAD);
232 if (isdef && player->owner) {
233 pr("Boarding yourself? Try using the 'load' command.\n");
234 return att_combat_init(com, EF_BAD);
236 com->lnd_lcp = &lchr[(int)land.lnd_type];
237 thing = (s_char *)&land;
238 owner = land.lnd_own;
239 eff = land.lnd_effic;
240 mob = land.lnd_mobil;
245 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
247 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
249 pr("Ship #%d is not your ship!\n", com->shp_uid);
250 return att_combat_init(com, EF_BAD);
253 if (isdef && player->owner &&
254 ontradingblock(EF_SHIP, (int *)&ship)) {
255 pr("%s is on the trading block.\n", prcom(0, com));
256 return att_combat_init(com, EF_BAD);
259 if (isdef && player->owner) {
260 pr("Boarding yourself? Try using the 'tend' command.\n");
261 return att_combat_init(com, EF_BAD);
263 com->shp_mcp = &mchr[(int)ship.shp_type];
265 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
266 if (!isdef && !player->owner) {
268 pr("%s was just sunk!\n", prcom(0, com));
270 pr("Ship #%d is not your ship!\n", com->shp_uid);
271 return att_combat_init(com, EF_BAD);
273 thing = (s_char *)&ship;
274 owner = ship.shp_own;
275 eff = ship.shp_effic;
276 mob = ship.shp_mobil;
285 return att_combat_init(com, EF_BAD);
288 mil = getvar(V_MILIT, thing, com->type);
289 if (!com->set) { /* first time */
290 if (isdef) { /* defender */
292 } else { /* attacker */
294 pr("No mil %s\n", prcom(1, com));
296 pr("Only 1 mil %s\n", prcom(1, com));
297 /* don't abandon attacking sectors or ships */
298 com->troops = max(0, mil - 1);
300 com->plague = (getvar(V_PSTAGE, thing, com->type)) == PLG_INFECT;
301 } else { /* not first time */
302 if (isdef) { /* defender */
303 if (com->x != x || com->y != y) {
304 pr("%s has moved!\n", prcom(0, com));
305 return att_combat_init(com, EF_BAD);
307 if (owner != com->own) {
309 pr("WARNING: The ownership of %s just changed from %s to %s!\n", prcom(0, com), cname(com->own), cname(owner));
310 } else if (com->type == EF_SECTOR) {
311 pr("WARNING: %s just abandoned sector %s!\n",
312 cname(com->own), xyas(com->x, com->y,
317 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
319 com->mil < mil ? "increased" : "decreased", com->mil,
322 } else { /* attacker */
323 if (owner != player->cnum && getrel(getnatp(owner), player->cnum) != ALLIED) { /* must be EF_SECTOR */
325 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n", com->mil, xyas(com->x, com->y, player->cnum), cname(owner));
327 pr("You no longer own %s\n",
328 xyas(com->x, com->y, player->cnum));
329 return att_combat_init(com, EF_BAD);
331 if (com->troops && com->troops + 1 > mil) {
332 if (com->own == owner && player->cnum == owner) /* not a takeover */
333 pr("WARNING: Your mil %s has been reduced from %d to %d!\n", prcom(1, com), com->troops, max(0, mil - 1));
334 com->troops = max(0, mil - 1);
349 * In the course of the fight, the combat object may have lost mil, eff, or
350 * mobility. This is the place where the data in the object gets flushed to
351 * disk to make it "real".
355 put_combat(struct combat *com)
364 getsect(com->x, com->y, §);
365 sect.sct_type = com->sct_type;
366 deff = sect.sct_effic - com->eff;
368 sect.sct_road -= (sect.sct_road * deff / 100.0);
369 sect.sct_rail -= (sect.sct_rail * deff / 100.0);
370 sect.sct_defense -= (sect.sct_defense * deff / 100.0);
371 if (sect.sct_road <= 0)
373 if (sect.sct_rail <= 0)
375 if (sect.sct_defense <= 0)
376 sect.sct_defense = 0;
378 sect.sct_effic = com->eff;
379 if (!opt_DEFENSE_INFRA)
380 sect.sct_defense = sect.sct_effic;
382 if (opt_MOB_ACCESS) {
383 if ((com->mob - com->mobcost) < -127)
384 sect.sct_mobil = -127;
386 sect.sct_mobil = (short)(com->mob - com->mobcost);
388 if ((com->mob - com->mobcost) < 0)
391 sect.sct_mobil = (short)(com->mob - com->mobcost);
394 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
395 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
396 sect.sct_own = com->own;
398 if (getvar(V_PSTAGE, (s_char *)§, EF_SECTOR) ==
400 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)§, EF_SECTOR);
402 putvar(V_MILIT, com->mil, (s_char *)§, EF_SECTOR);
404 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
407 getland(com->lnd_uid, &land);
408 land.lnd_effic = com->eff;
410 if (com->mob - com->mobcost < -127)
411 land.lnd_mobil = -127;
413 land.lnd_mobil = (s_char)(com->mob - com->mobcost);
415 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
417 land.lnd_own = com->own;
418 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
421 if (getvar(V_PSTAGE, (s_char *)&land, EF_LAND) == PLG_HEALTHY)
422 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&land, EF_LAND);
424 if (!(com->lnd_lcp->l_flags & L_SPY))
425 putvar(V_MILIT, com->mil, (s_char *)&land, EF_LAND);
426 lnd_count_units(&land);
427 if (com->own == player->cnum) {
428 land.lnd_mission = 0;
430 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
432 putland(com->lnd_uid, &land);
435 getship(com->shp_uid, &ship);
436 ship.shp_effic = com->eff;
438 if (com->mob - com->mobcost < -127)
439 ship.shp_mobil = -127;
441 ship.shp_mobil = (s_char)(com->mob - com->mobcost);
443 makelost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
445 ship.shp_own = com->own;
446 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
449 if (getvar(V_PSTAGE, (s_char *)&ship, EF_SHIP) == PLG_HEALTHY)
450 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&ship, EF_SHIP);
452 putvar(V_MILIT, com->mil, (s_char *)&ship, EF_SHIP);
454 if (com->own == player->cnum) {
455 ship.shp_mission = 0;
457 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
459 putship(com->shp_uid, &ship);
462 att_get_combat(com, com->own != player->cnum);
465 /* If pre-attack, abort fight. If post-attack, don't move anything in */
470 return player->aborted = 1;
474 * This is the combat_mode based integrity check. It splits among two main
475 * divisions: first time/not first time, and attack/assault/para/board.
479 att_abort(int combat_mode, struct combat *off, struct combat *def)
488 if (att_get_combat(def, 1) < 0)
489 return abort_attack();
491 if (off && combat_mode != A_ATTACK) {
492 if (att_get_combat(off, 0) < 0)
493 return abort_attack();
494 if (off->type == EF_SHIP &&
495 (!getsect(off->x, off->y, §) ||
496 sect.sct_type != SCT_WATER)) {
497 pr("%s can not %s from that far inland!\n",
498 prcom(0, off), att_mode[combat_mode]);
499 return abort_attack();
502 switch (combat_mode) {
504 if (!neigh(def->x, def->y, player->cnum) &&
505 !adj_units(def->x, def->y, player->cnum)) {
506 pr("You are not adjacent to %s\n",
507 xyas(def->x, def->y, player->cnum));
508 return abort_attack();
510 if (def->own == player->cnum) {
511 pr("You can't attack your own sector.\n");
512 return abort_attack();
516 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
517 pr("You'll have to get there first...\n");
518 return abort_attack();
520 if (off && def->sct_type == SCT_MOUNT) {
521 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
522 return abort_attack();
526 if (def->own == player->cnum) {
527 pr("You can't air-assault your own sector.\n");
528 return abort_attack();
530 if (off && (def->sct_type == SCT_MOUNT ||
531 def->sct_type == SCT_WATER ||
532 def->sct_type == SCT_CAPIT ||
533 def->sct_type == SCT_FORTR ||
534 def->sct_type == SCT_WASTE)) {
535 pr("You can't air-assault a %s sector!\n",
536 def->sct_dcp->d_name);
537 return abort_attack();
541 return board_abort(off, def);
543 return land_board_abort(off, def);
546 if (off && def->sct_dcp->d_mcst <= 0) {
547 pr("You can't %s a %s sector!\n", att_mode[combat_mode],
548 def->sct_dcp->d_name);
549 return abort_attack();
551 if (!off || off->relations_checked)
553 off->relations_checked = 1;
556 setcont(player->cnum, def->own, FOUND_SPY);
557 setcont(def->own, player->cnum, FOUND_SPY);
559 if (opt_SLOW_WAR && def->own != player->cnum) {
560 natp = getnatp(player->cnum);
561 issneak = getrel(natp, def->own);
563 if (issneak == ALLIED) {
564 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
565 cname(def->own), att_mode[combat_mode]);
566 if (!confirm(y_or_n))
567 return abort_attack();
570 if (opt_SNEAK_ATTACK) {
571 getsect(def->x, def->y, §);
572 if ((issneak != AT_WAR) && (def->own)
573 && (def->own != player->cnum)
574 && (sect.sct_oldown != player->cnum)
575 && (issneak != SITZKRIEG) && (issneak != MOBILIZATION)) {
576 pr("You're not at war with them!\n");
578 ("Do you really want to sneak attack (it will cost you $5000) [yn]? "))
580 pr("Sneak attack cancelled!\n");
581 return abort_attack();
584 if ((issneak != AT_WAR) && (def->own)
585 && (def->own != player->cnum)
586 && (sect.sct_oldown != player->cnum)
587 && ((issneak == MOBILIZATION) || (issneak == SITZKRIEG))) {
588 pr("You're not at war with them!\n");
589 return abort_attack();
592 if ((issneak != AT_WAR) && (def->own) &&
593 (sect.sct_oldown != player->cnum)) {
594 pr("You're not at war with them!\n");
595 return abort_attack();
603 * Lots of special things need to be checked for boarding, so I put it in
608 board_abort(struct combat *off, struct combat *def)
610 struct shpstr aship, dship; /* for tech levels */
613 if (att_get_combat(def, 1) < 0)
614 return abort_attack();
619 if (att_get_combat(off, 0) < 0)
620 return abort_attack();
622 if (off->x != def->x || off->y != def->y) {
623 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
624 return abort_attack();
626 if (off->type == EF_SHIP) {
628 pr("%s has no mobility!\n", prcom(0, off));
629 return abort_attack();
631 getship(off->shp_uid, &aship);
632 getship(def->shp_uid, &dship);
633 if (techfact(aship.shp_tech, 1.0) *
634 aship.shp_speed * off->eff <=
635 techfact(dship.shp_tech, 1.0) * dship.shp_speed * def->eff) {
636 pr("Victim ship moves faster than you do!\n");
639 "%s (#%d) %s failed to catch %s\n",
640 cname(aship.shp_own), aship.shp_own,
641 pr_com(0, off, def->own), pr_com(0, def, def->own));
642 return abort_attack();
644 } else if (off->type != EF_SECTOR) {
645 pr("Please tell the deity that you got the 'banana boat' error\n");
646 return abort_attack();
648 if (def->shp_mcp->m_flags & M_SUB) {
649 getsect(def->x, def->y, §);
650 if (sect.sct_type == SCT_WATER) {
651 pr("You can't board a submarine!\n");
652 return abort_attack();
659 * Lots of special things need to be checked for boarding, so I put it in
661 * STM - I copied it for land unit boarding. :)
665 land_board_abort(struct combat *off, struct combat *def)
667 if (att_get_combat(def, 1) < 0)
668 return abort_attack();
673 if (att_get_combat(off, 0) < 0)
674 return abort_attack();
676 if (off->x != def->x || off->y != def->y) {
677 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
678 return abort_attack();
684 /* If we are boarding, then the defending ship gets a chance to fire back */
686 att_approach(struct combat *off, struct combat *def)
692 pr("Approaching %s...\n", prcom(0, def));
695 "%s is being approached by %s...\n",
696 pr_com(0, def, def->own), pr_com(0, off, def->own));
697 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
700 pr("They're firing at us sir!\n");
703 "Your fleet at %s does %d damage to %s\n",
704 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
706 if (off->type == EF_SECTOR) {
707 getsect(off->x, off->y, §);
708 sectdamage(§, dam, 0);
710 pr("Enemy fleet at %s does %d damage to %s\n",
711 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
712 } else if (off->type == EF_SHIP) {
713 getship(off->shp_uid, &ship);
714 shipdamage(&ship, dam);
715 putship(off->shp_uid, &ship);
716 if (def->own && ship.shp_effic < SHIP_MINEFF) {
717 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
718 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
721 if (att_get_combat(off, 0) < 0)
722 return abort_attack();
726 /* The attack is valid. Tell the attacker about what they're going to hit */
729 att_show(struct combat *def)
731 /* Note that we tell the player about the treaty BEFORE we tell them
732 about the item. If we didn't, then they gain free information */
733 if (def->type == EF_SECTOR) {
734 if (!trechk(player->cnum, def->own, LANATT))
735 return abort_attack();
736 pr("%s is a %d%% %s %s with approximately %d military.\n",
737 xyas(def->x, def->y, player->cnum),
738 roundintby((int)def->eff, 10),
739 cname(def->own), def->sct_dcp->d_name,
740 roundintby(def->troops, 10));
741 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
742 writemap(player->cnum);
743 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
744 if (def->type == EF_SHIP) {
745 if (!trechk(player->cnum, def->own, SEAATT))
746 return abort_attack();
748 if (!trechk(player->cnum, def->own, LNDATT))
749 return abort_attack();
751 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));
753 /* Ok, everything is fine */
757 /* Attack and assault ask the user which kind of support they want */
760 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
765 *fortp = *shipp = *landp = *planep = 1;
767 if (player->argp[offset] != (s_char *)0) {
768 if ((player->argp[offset + 1] == (s_char *)0) ||
769 (player->argp[offset + 2] == (s_char *)0) ||
770 (player->argp[offset + 3] == (s_char *)0)) {
771 pr("If any support arguments are used, all must be!\n");
776 *landp = *planep = 0;
780 getstarg(player->argp[offset], "Use fort support? ", buf)))
783 if ((*p == 'y') || (*p == 'Y'))
788 getstarg(player->argp[offset + 1], "Use ship support? ",
792 if ((*p == 'y') || (*p == 'Y'))
797 getstarg(player->argp[offset + 2], "Use land support? ",
801 if ((*p == 'y') || (*p == 'Y'))
806 getstarg(player->argp[offset + 3], "Use plane support? ",
810 if ((*p == 'y') || (*p == 'Y'))
817 * Attack, assault, and board ask the attacker what they'd like to attack
818 * with. This includes mil and land units from each "off" object. Note that
819 * after each sub-prompt, we check to make sure that the attack is still
820 * valid, and if it's not, then we abort the attack.
824 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
825 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
828 s_char land_answer[1024];
831 if (att_abort(combat_mode, off, def))
833 memset(land_answer, 0, sizeof(land_answer));
834 for (n = 0; n <= off->last; ++n) {
835 off[n].troops = ask_off(combat_mode, off + n, def);
836 if (att_abort(combat_mode, off, def))
838 ask_olist(combat_mode, off + n, def, olist, land_answer,
839 a_spyp, a_engineerp);
840 if (att_abort(combat_mode, off, def))
846 /* How many mil is off allowed to attack with when it attacks def? */
849 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
853 switch (combat_mode) {
855 mob_support = off->mob / sector_mcost(getsectp(def->x, def->y),
859 /* mob_support = off->mob / sector_mcost(def->sct_type, def->eff);*/
860 if (mob_support < off->troops)
861 pr("Sector %s has %d mobility which can only support %d mil,\n", xyas(off->x, off->y, player->cnum), off->mob, mob_support);
863 mob_support = off->troops;
866 if (def->own != player->cnum && def->mil) {
867 if (off->shp_mcp->m_flags & M_SEMILAND)
868 return off->troops / 4;
869 else if (!(off->shp_mcp->m_flags & M_LAND))
870 return off->troops / 10;
874 if (off->type == EF_SECTOR && off->mob <= 0)
876 mob_support = vl_find(V_MILIT, def->shp_mcp->m_vtype,
877 def->shp_mcp->m_vamt, def->shp_mcp->m_nv);
878 if (mob_support < off->troops)
879 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
881 mob_support = off->troops;
886 if (def->lnd_lcp->l_flags & L_SPY)
888 mob_support = vl_find(V_MILIT, def->lnd_lcp->l_vtype,
889 def->lnd_lcp->l_vamt, def->lnd_lcp->l_nv);
890 if (mob_support < off->troops)
891 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
893 mob_support = off->troops;
900 * If the attacker decides to go through with the attack, then the
901 * sectors/ships they are attacking with may be charged some mobility.
902 * This is where that amount of mobility is calculated. It is actually
903 * subtracted "for real" from the object's mobility in put_combat().
907 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
914 switch (combat_mode) {
918 (int)(attacking_mil *
919 sector_mcost(getsectp(def->x, def->y), MOB_ROAD)));
922 off->mobcost += max(1, attacking_mil / 5);
927 off->mobcost += max(1, attacking_mil / 5);
930 /* the 2 in the formula below is a fudge factor */
931 getship(def->shp_uid, &ship);
932 off->mobcost += (def->eff / 100) * (ship.shp_speed / 2);
937 /* How many mil to we want to attack from off against def? */
940 ask_off(int combat_mode, struct combat *off, struct combat *def)
946 if (att_get_combat(off, 0) <= 0)
948 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
950 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
952 if (off->type == EF_SECTOR) {
953 if (off->own != player->cnum)
955 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
956 off->sct_dcp->d_name,
957 xyas(off->x, off->y, player->cnum), mob_support);
959 sprintf(prompt, "Number of mil from %s (max %d) : ",
960 prcom(0, off), mob_support);
962 if ((attacking_mil = onearg(0, prompt)) < 0)
964 if (att_abort(combat_mode, off, def))
966 if (att_get_combat(off, 0) <= 0)
969 min(attacking_mil, min(mob_support, off->troops))) <= 0)
972 calc_mobcost(combat_mode, off, def, attacking_mil);
973 return attacking_mil;
977 * Which units would you like to attack with or move in with [ynYNq?]
981 att_prompt(s_char *prompt, s_char army)
989 while (!p || (*p != 'y' && *p != 'n' && *p != 'Y' && *p != 'N')) {
990 if (p && *p == 'q') {
997 pr("y - yes this unit\nn - no this unit\nY - yes to all units in army '%c'\nN - no to all units in army '%c'\nq - quit\n? - this help message\n\n", army, army);
998 p = getstring(prompt, buf);
999 if (player->aborted) {
1007 /* Ask the attacker which units they want to attack/assault/board with */
1010 ask_olist(int combat_mode, struct combat *off, struct combat *def,
1011 struct emp_qelem *olist, s_char *land_answer, int *a_spyp,
1014 struct nstr_item ni;
1018 struct lchrstr *lcp;
1025 if (def->type == EF_LAND)
1027 if (def->type == EF_SHIP)
1028 maxland = def->shp_mcp->m_nland;
1030 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
1031 while (nxtitem(&ni, (s_char *)&land)) {
1032 if (land.lnd_own != player->cnum)
1034 if (land.lnd_effic < LAND_MINEFF)
1036 if (land_answer[(int)land.lnd_army] == 'N')
1038 if (!lnd_can_attack(&land))
1040 lcp = &lchr[(int)land.lnd_type];
1042 if (def->type == EF_SHIP && !maxland) {
1043 pr("Land units are not able to board this kind of ship\n");
1046 if (land.lnd_mobil <= 0) {
1047 pr("%s is out of mobility, and cannot %s\n",
1048 prland(&land), att_mode[combat_mode]);
1053 if (ontradingblock(EF_LAND, (int *)&land)) {
1054 pr("%s is on the trading block, and cannot %s\n",
1055 prland(&land), att_mode[combat_mode]);
1060 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1061 pr("%s is on ship #%d, and cannot %s\n",
1062 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1064 } else if (off->type == EF_SHIP) {
1065 if (land.lnd_ship != off->shp_uid)
1067 } else if (land.lnd_land >= 0) {
1068 pr("%s is on unit #%d, and cannot %s\n",
1069 prland(&land), land.lnd_land, att_mode[combat_mode]);
1072 switch (combat_mode) {
1075 lnd_mobcost(&land, getsectp(def->x, def->y), MOB_NONE);
1076 if (land.lnd_mobil < mobcost) {
1077 pr("%s does not have enough mobility (%d needed)\n",
1078 prland(&land), (int)ceil(mobcost));
1085 if (!(lcp->l_flags & L_ASSAULT))
1088 att_val = attack_val(combat_mode, &land);
1090 pr("%s has no offensive strength\n", prland(&land));
1093 resupply_all(&land);
1094 putland(land.lnd_uid, &land);
1095 if (!has_supply(&land)) {
1096 pr("%s is out of supply, and cannot %s\n",
1097 prland(&land), att_mode[combat_mode]);
1100 if (def->type == EF_SHIP && first_time) {
1102 pr("You may board with a maximum of %d land units\n", maxland);
1104 pr("%s has a base %s value of %d\n",
1105 prland(&land), att_mode[combat_mode], att_val);
1106 if (land_answer[(int)land.lnd_army] != 'Y') {
1108 "%s with %s %s (%c %d%%) [ynYNq?] ",
1109 att_mode[combat_mode],
1112 land.lnd_army == ' ' ? '~' : land.lnd_army,
1114 land_answer[(int)land.lnd_army] =
1115 att_prompt(prompt, land.lnd_army);
1116 if (att_abort(combat_mode, off, def))
1118 if (land_answer[(int)land.lnd_army] != 'y' &&
1119 land_answer[(int)land.lnd_army] != 'Y')
1122 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1123 logerror("Malloc failed in attack!\n");
1127 memset(llp, 0, sizeof(struct llist));
1128 emp_insque(&llp->queue, olist);
1129 llp->mobil = mobcost;
1130 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1132 if (lnd_spyval(&land) > *a_spyp)
1133 *a_spyp = lnd_spyval(&land);
1134 if (llp->lcp->l_flags & L_ENGINEER)
1136 if (def->type == EF_SHIP && ++count >= maxland)
1141 /* What's the offense or defense multiplier? */
1144 att_combat_eff(struct combat *com)
1150 if (com->type == EF_SECTOR) {
1151 eff = com->eff / 100.0;
1152 if (com->own == player->cnum) {
1153 str = com->sct_dcp->d_ostr;
1154 eff = 1.0 + ((str - 1.0) * eff);
1156 eff = sector_strength(getsectp(com->x, com->y));
1157 /* str = com->sct_dcp->d_dstr;*/
1158 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1159 getship(com->shp_uid, &ship);
1160 eff = (1.0 + ship.shp_armor / 100.0);
1166 * Estimate the defense strength and give the attacker a chance to abort
1167 * if the odds are less than 50%
1171 att_estimate_defense(int combat_mode, struct combat *off,
1172 struct emp_qelem *olist, struct combat *def,
1180 * Get the attacker units & mil again in case they changed while the
1181 * attacker was answering sub-prompts.
1184 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1185 if (att_empty_attack(combat_mode, ototal, def))
1186 return abort_attack();
1187 if (combat_mode == A_PARA)
1189 pr("\n Initial attack strength: %8d\n", ototal);
1191 estimate = att_combat_eff(def) * roundintby(def->troops, 10);
1192 estimate += att_combat_eff(def) * get_dlist(def, 0, a_spy, 0);
1195 * Calculate the initial (pre-support) attack odds. If they're less
1196 * than 50%, ask for a confirmation.
1199 odds = (int)(att_calcodds(ototal, estimate) * 100);
1203 pr(" Estimated defense strength: %8d\n", estimate);
1204 pr(" Estimated odds: %8d%%\n\n",odds);
1205 sprintf(prompt, "Are you sure you want to %s [yn]? ",
1206 att_mode[combat_mode]);
1207 if (!confirm(prompt))
1208 return abort_attack();
1209 ototal = get_ototal(combat_mode, off, olist,1.0,1);
1210 if (att_empty_attack(combat_mode, ototal, def))
1211 return abort_attack();
1217 /* Get the defensive units and reacting units */
1219 att_get_defense(struct emp_qelem *olist, struct combat *def,
1220 struct emp_qelem *dlist, int a_spy, int ototal)
1223 struct emp_qelem *qp;
1229 get_dlist(def, dlist, 0, &d_spy);
1230 dtotal = get_dtotal(def, dlist, 1.0, 0);
1233 * Call in reacting units
1236 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1237 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1239 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1240 llp = (struct llist *)qp;
1241 intelligence_report(def->own, &llp->land, d_spy,
1242 "Scouts report attacking unit:");
1245 old_dtotal = dtotal;
1246 dtotal = get_dtotal(def, dlist, 1.0, 0);
1247 if (dtotal != old_dtotal)
1248 pr("Defense strength with reacting units: %8d\n", dtotal);
1253 /* Get the defensive land units in the sector or on the ship */
1256 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1259 struct nstr_item ni;
1264 /* In here is where you need to take out spies and trains from the defending
1265 lists. Spies try to hide, trains get trapped and can be boarded. */
1267 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1268 while (nxtitem(&ni, (s_char *)&land)) {
1271 if (land.lnd_own != def->own)
1273 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1275 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1277 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1279 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1281 if (!list) { /* Just estimating the enemy strength */
1282 estimate += intelligence_report(player->cnum,
1284 "Scouts report defending unit:");
1287 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1288 logerror("Malloc failed in attack!\n");
1292 memset(llp, 0, sizeof(struct llist));
1293 emp_insque(&llp->queue, list);
1294 llp->supplied = has_supply(&land);
1295 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1297 if (lnd_spyval(&land) > *d_spyp)
1298 *d_spyp = lnd_spyval(&land);
1303 /* Calculate the total offensive strength */
1306 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1307 double osupport, int check)
1309 double ototal = 0.0;
1310 struct emp_qelem *qp, *next;
1315 * first, total the attacking mil
1318 for (n = 0; n <= off->last; ++n) {
1319 if (off[n].type == EF_BAD || (check &&
1320 att_get_combat(&off[n], 0) <= 0))
1322 ototal += off[n].troops * att_combat_eff(off + n);
1326 * next, add in the attack_values of all
1327 * the attacking units
1330 for (qp = olist->q_forw; qp != olist; qp = next) {
1332 llp = (struct llist *)qp;
1333 if (check && !get_land(combat_mode, 0, llp->land.lnd_uid, llp, 0))
1335 if (combat_mode == A_ATTACK) {
1337 for (n = 0; n <= off->last; ++n) {
1338 if (off[n].type == EF_BAD)
1340 if ((off[n].x == llp->land.lnd_x) &&
1341 (off[n].y == llp->land.lnd_y))
1345 lnd_delete(llp, "is in a sector not owned by you");
1348 ototal += attack_val(combat_mode, &llp->land) *
1349 att_combat_eff(off + w);
1351 ototal += attack_val(combat_mode, &llp->land);
1356 return ldround(ototal, 1);
1359 /* Calculate the total defensive strength */
1362 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1365 double dtotal = 0.0, eff = 1.0, d_unit;
1366 struct emp_qelem *qp, *next;
1369 if (check && att_get_combat(def, 1) < 0)
1371 eff = att_combat_eff(def);
1372 dtotal = def->troops * eff;
1375 * next, add in the defense_values of all
1376 * the defending non-retreating units
1379 for (qp = list->q_forw; qp != list; qp = next) {
1381 llp = (struct llist *)qp;
1383 if (check && !get_land(A_DEFEND, def, llp->land.lnd_uid, llp, 1))
1385 d_unit = defense_val(&llp->land);
1387 d_unit = ((double)defense_val(&llp->land) / 2.0);
1388 dtotal += d_unit * eff;
1393 return ldround(dtotal, 1);
1397 * This is the land unit integrity check. Note that we don't print
1398 * warnings about victim land units because the attacker may not have seen them
1402 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp,
1405 struct lndstr *lp = &llp->land;
1410 if (!llp->lcp) { /* first time */
1411 llp->x = llp->land.lnd_x;
1412 llp->y = llp->land.lnd_y;
1413 llp->lcp = &lchr[(int)llp->land.lnd_type];
1414 } else { /* not first time */
1415 if (lp->lnd_effic < LAND_MINEFF) {
1416 sprintf(buf, "was destroyed and is no longer a part of the %s",
1417 att_mode[combat_mode]);
1418 lnd_delete(llp, buf);
1422 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1424 "left to go fight another battle and is no longer a part of the defense");
1428 if (lp->lnd_own != player->cnum) {
1430 "was destroyed and is no longer a part of the %s",
1431 att_mode[combat_mode]);
1432 lnd_delete(llp, buf);
1435 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1437 "left to fight another battle and is no longer a part of the %s",
1438 att_mode[combat_mode]);
1439 lnd_delete(llp, buf);
1442 if (lp->lnd_effic < llp->eff) {
1443 sprintf(buf, "damaged from %d%% to %d%%",
1444 llp->eff, lp->lnd_effic);
1445 lnd_print(llp, buf);
1449 llp->eff = llp->land.lnd_effic;
1455 * Put the land unit on the disk. If there was some mobility cost, then
1456 * subtract it from the units mobility. Note that this works the same way
1457 * as sectors & ships in that no mobility is actually taken until the attacker
1458 * has committed to attacking.
1462 kill_land(struct emp_qelem *list)
1464 struct emp_qelem *qp, *next;
1467 for (qp = list->q_forw; qp != list; qp = next) {
1469 llp = (struct llist *)qp;
1470 if (llp->land.lnd_ship >= 0) {
1471 llp->land.lnd_effic = 0;
1472 lnd_delete(llp, "cannot return to the ship, and dies!");
1478 att_infect_units(struct emp_qelem *list, int plague)
1480 struct emp_qelem *qp, *next;
1485 for (qp = list->q_forw; qp != list; qp = next) {
1487 llp = (struct llist *)qp;
1488 if (getvar(V_PSTAGE, (s_char *)&(llp->land), EF_LAND) ==
1490 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&(llp->land), EF_LAND);
1495 put_land(struct emp_qelem *list)
1497 struct emp_qelem *qp, *next;
1500 for (qp = list->q_forw; qp != list; qp = next) {
1502 llp = (struct llist *)qp;
1503 llp->land.lnd_mission = 0;
1504 llp->land.lnd_harden = 0;
1505 llp->land.lnd_mobil -= (int)llp->mobil;
1507 putland(llp->land.lnd_uid, &llp->land);
1508 if (llp->land.lnd_own != player->cnum) {
1509 emp_remque((struct emp_qelem *)llp);
1510 free((s_char *)llp);
1512 get_land(A_ATTACK, 0, llp->land.lnd_uid, llp, 0);
1517 * Keep sending in reinforcements until it looks like we're going to win.
1518 * Note that the "strength" command also calls this routine.
1522 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1523 int *d_spyp, int ototal)
1525 struct nstr_item ni;
1527 struct sctstr sect, dsect;
1537 double eff = att_combat_eff(def);
1543 * All units that are within their reaction radius and not damaged
1544 * below their morale value now get to react to the threatened sect.
1545 * Once we've sent enough to counter the threat, stop sending them.
1547 * Not anymore. All units get to react. :)
1551 dtotal = get_dtotal(def, list, 1.0, 1);
1554 snxtitem_all(&ni, EF_LAND);
1555 while (nxtitem(&ni, (s_char *)&land) &&
1556 (dtotal + new_land * eff < (int)(1.2 * (float)ototal))) {
1559 if (!land.lnd_rad_max)
1561 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1563 if (land.lnd_own != def->own)
1565 if (land.lnd_ship >= 0)
1567 if (!defense_val(&land))
1570 if (land.lnd_effic <= land.lnd_retreat)
1573 if (!lnd_can_attack(&land))
1576 /* Only supplied units can react */
1577 if (!(supply_state = has_supply(&land)))
1580 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1582 getsect(land.lnd_x, land.lnd_y, §);
1583 /* Units on efficient headquarters can react 1 farther */
1584 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1585 radius = land.lnd_rad_max + 1;
1587 radius = land.lnd_rad_max;
1589 if (land.lnd_mission == MI_RESERVE)
1595 getsect(def->x, def->y, &dsect);
1598 (s_char *)BestLandPath(buf, §, &dsect, &move_cost,
1602 mobcost = land.lnd_effic * 0.01 * lchr[(int)land.lnd_type].l_spd;
1605 mobcost = 480.0 / (mobcost + techfact(land.lnd_tech, mobcost));
1606 mobcost *= (move_cost * 5.0);
1608 if (land.lnd_mobil < mobcost)
1611 new_land += defense_val(&land);
1613 if (!list) /* we are in the "strength" command */
1616 /* move to defending sector */
1617 land.lnd_mobil -= ldround(mobcost, 1);
1620 land.lnd_x = def->x;
1621 land.lnd_y = def->y;
1622 putland(land.lnd_uid, &land);
1623 wu(0, land.lnd_own, "%s reacts to %s.\n",
1624 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1626 llp = (struct llist *)
1627 malloc(sizeof(struct llist));
1629 memset(llp, 0, sizeof(struct llist));
1630 llp->supplied = supply_state;
1633 llp->lcp = &lchr[(int)land.lnd_type];
1635 emp_insque(&llp->queue, list);
1636 if (lnd_spyval(&land) > *d_spyp)
1637 *d_spyp = lnd_spyval(&land);
1639 intelligence_report(player->cnum, &land, a_spy,
1640 "Scouts sight reacting enemy unit:");
1645 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1648 get_osupport(s_char *outs, struct combat *def, int fort_sup, int ship_sup,
1649 int land_sup, int plane_sup)
1651 double osupport = 1.0;
1653 double af, as, au, ap;
1655 af = as = au = ap = 0.0;
1657 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1658 af = ((double)dam / 100.0);
1662 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1664 as = ((double)dam / 100.0);
1669 dam = lnd_support(def->own, player->cnum, def->x, def->y);
1670 au = ((double)dam / 100.0);
1675 dam = off_support(def->x, def->y, def->own, player->cnum);
1676 ap = (((double)dam) / 100.0);
1679 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1684 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1687 get_dsupport(s_char *outs, struct emp_qelem *list, struct combat *def,
1688 int ototal, int dtotal)
1690 double dsupport = 1.0;
1692 double df, ds, du, dp;
1695 df = ds = du = dp = 0.0;
1696 if (dtotal < 0.1 * ototal) {
1698 } else if (dtotal >= 1.2 * ototal) {
1701 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1702 df = ((double)dam / 100.0);
1705 dtotal = get_dtotal(def, list, dsupport, 0);
1706 if (dtotal < 1.2 * ototal) {
1707 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1708 ds = ((double)dam / 100.0);
1710 dtotal = get_dtotal(def, list, dsupport, 0);
1712 if (dtotal < 1.2 * ototal) {
1713 dam = lnd_support(player->cnum, def->own, def->x, def->y);
1714 du = ((double)dam / 100.0);
1716 dtotal = get_dtotal(def, list, dsupport, 1);
1718 if (dtotal < 1.2 * ototal) {
1719 dam = def_support(def->x, def->y, player->cnum, def->own);
1720 dp = (((double)dam) / 100.0);
1727 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1732 "\nOdds are bad for us...support cancelled.\n\n");
1735 "\nOdds are good for us...support cancelled.\n\n");
1741 * Land mines add to the defense multiplier. If the attacker has engineers
1742 * then this multiplier is cut in half.
1746 get_mine_dsupport(struct combat *def, int a_engineer)
1751 getsect(def->x, def->y, §);
1753 if (sect.sct_oldown != player->cnum) {
1754 mines = getvar(V_MINE, (s_char *)§, EF_SECTOR);
1755 mines = min(mines, 20);
1757 mines = ldround(((double)mines / 2.0), 1);
1760 wu(0, def->own, "Defending mines add %1.2f\n",
1762 pr("Defending mines add %1.2f\n", mines * 0.02);
1763 return mines * 0.02;
1769 /* Get the offensive and defensive support */
1771 att_get_support(int combat_mode, int ofort, int oship, int oland,
1772 int oplane, struct emp_qelem *olist, struct combat *off,
1773 struct emp_qelem *dlist, struct combat *def,
1774 double *osupportp, double *dsupportp, int a_engineer)
1777 s_char osupports[512];
1778 s_char dsupports[512];
1780 if (combat_mode == A_PARA)
1783 *osupportp = get_osupport(osupports, def,
1784 ofort, oship, oland, oplane);
1787 * I need to put a 1 at the end of the next four total_stren calls
1788 * becauase units & mil may have been damaged by collateral damage or
1789 * neclear warheads from the offensive & defensive support.
1792 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1793 if (att_empty_attack(combat_mode, ototal, def))
1794 return abort_attack();
1795 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1798 * Calculate defensive support. If odds are too good or too bad
1799 * then don't call in support.
1802 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1803 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1804 if (att_empty_attack(combat_mode, ototal, def))
1805 return abort_attack();
1807 if ((*osupports || *dsupports) &&
1808 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1809 pr("\n\t\tsupport values\n");
1810 pr("\t\tforts\tships\tunits\tplanes\n");
1811 if (*osupportp != 1.0)
1812 pr("%s", osupports);
1813 if (*dsupportp != 1.0)
1814 pr("%s", dsupports);
1816 wu(0, def->own, "\n\t\tsupport values\n");
1817 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1818 if (*osupportp != 1.0)
1819 wu(0, def->own, "%s", osupports);
1820 if (*dsupportp != 1.0)
1821 wu(0, def->own, "%s", dsupports);
1825 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1826 if (dtotal && def->type == EF_SECTOR)
1827 *dsupportp += get_mine_dsupport(def, a_engineer);
1831 /* How many two-legged bipeds are in this combat force? */
1834 count_bodies(struct combat *off, struct emp_qelem *list)
1838 struct emp_qelem *qp;
1841 for (n = 0; n <= off->last; ++n)
1842 bodies += off[n].troops;
1843 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1844 llp = (struct llist *)qp;
1845 bodies += total_mil(&llp->land);
1850 /* This is where the fighting actually occurs. */
1853 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1854 double osupport, struct combat *def, struct emp_qelem *dlist,
1858 int a_cas = 0; /* Casualty counts */
1860 int ototal; /* total attacking strength */
1861 int dtotal; /* total defending strength */
1862 int a_bodies; /* total attacking mil (incl. mil in units) */
1863 int d_bodies; /* total defending mil (incl. mil in units) */
1872 ototal = get_ototal(combat_mode, off, olist, osupport,
1873 combat_mode != A_PARA);
1874 dtotal = get_dtotal(def, dlist, dsupport, 0);
1878 a_bodies = count_bodies(off, olist);
1879 d_bodies = count_bodies(def, dlist);
1880 d_mil = def->troops;
1881 for (n = 0; n <= off->last; ++n)
1882 if (off[n].type == EF_BAD)
1885 a_troops[n] = off[n].troops;
1887 /* This switch is required to get the spacing right */
1888 switch (combat_mode) {
1890 pr(" Final attack strength: %8d\n", ototal);
1893 pr(" Final assault strength: %8d\n", ototal);
1896 if (def->sct_type == SCT_MOUNT ||
1897 def->sct_type == SCT_WATER ||
1898 def->sct_type == SCT_CAPIT ||
1899 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1900 pr("You can't air-assault a %s sector!\n",
1901 def->sct_dcp->d_name);
1904 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1906 pr(" Final air-assault strength: %8d\n", ototal);
1910 pr(" Final board strength: %8d\n", ototal);
1914 pr(" Final defense strength: %8d\n", dtotal);
1915 odds = att_calcodds(ototal, dtotal);
1916 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1918 /* spread the plague */
1919 if (combat_mode != A_PARA) {
1921 for (n = 0; n <= off->last; ++n)
1922 if (off[n].type != EF_BAD)
1923 def->plague |= off[n].plague;
1924 for (n = 0; n <= off->last; ++n)
1925 if (off[n].type != EF_BAD)
1926 off[n].plague |= def->plague;
1928 att_infect_units(olist, off->plague);
1929 att_infect_units(dlist, def->plague);
1931 /* Fighting is slightly random. There is always that last little
1932 * effort you see people put in. Or the stray bullet that takes out
1933 * an officer and the rest go into chaos. Things like that.
1934 * Thus, we have added a very slight random factor that will sometimes
1935 * allow the little guy to win. We modify the odds a little
1936 * (either +- 5%) to account for this randomness. We also only
1937 * recalculate the odds every 8-50 casualties, not every cacsualty,
1938 * since a single dead guy normally wouldn't cause a commander to
1939 * rethink his strategies, but 50 dead guys might. */
1940 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1945 recalctime = 8 + (random() % 43);
1946 while (!success && ototal) {
1949 d_cas += take_casualty(A_DEFEND, def, dlist);
1950 dtotal = get_dtotal(def, dlist, dsupport, 0);
1955 a_cas += take_casualty(combat_mode, off, olist);
1956 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1958 if (((a_cas + d_cas) % 70) == 69)
1960 if (recalctime-- <= 0) {
1961 recalctime = 8 + (random() % 43);
1962 odds = att_calcodds(ototal, dtotal);
1963 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1971 /* update defense mobility & mil */
1975 if (def->type == EF_SECTOR && d_mil && d_cas) {
1978 /* Make sure we use a positive mobility here */
1979 tmob = ((def->mob < 0) ? -(def->mob) : (def->mob));
1981 min(20, min(1, tmob - damage(tmob, 100 * d_cas / d_mil)));
1983 def->mil = def->troops;
1986 /* update attack mobility & mil */
1987 for (n = 0; n <= off->last; ++n)
1988 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1989 if (off[n].type == EF_SECTOR && off[n].mil)
1993 off[n].mob - damage(off[n].mob,
1994 100 * (a_troops[n] -
1997 off[n].mil -= a_troops[n] - off[n].troops;
2000 /* update land unit mobility */
2001 if (d_bodies && d_cas)
2002 lnd_takemob(dlist, (double)d_cas / d_bodies);
2003 if (a_bodies && a_cas)
2004 lnd_takemob(olist, (double)a_cas / a_bodies);
2006 /* damage attacked sector */
2007 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
2009 pr("- Casualties -\n Yours: %d\n", a_cas);
2010 pr(" Theirs: %d\n", d_cas);
2011 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
2012 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
2015 switch (combat_mode) {
2017 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
2018 pr("We have captured %s, sir!\n", prcom(0, def));
2022 news_item = def->own ? N_AWON_SECT : N_START_COL;
2023 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
2024 action = "assaulting and taking";
2027 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
2028 pr("We have captured %s, sir!\n", prcom(0, def));
2029 action = "air-assaulting and taking";
2032 news_item = N_BOARD_SHIP;
2033 pr("We have boarded %s, sir!\n", prcom(0, def));
2034 action = "boarding";
2037 news_item = N_BOARD_LAND;
2038 pr("We have boarded %s, sir!\n", prcom(0, def));
2039 action = "boarding";
2043 switch (combat_mode) {
2045 news_item = N_SCT_LOSE;
2046 pr("You have been defeated!\n");
2047 action = "attacking";
2050 news_item = N_ALOSE_SCT;
2051 pr("You have been defeated!\n");
2053 action = "trying to assault";
2056 news_item = N_PLOSE_SCT;
2057 pr("All of your troops were destroyed\n");
2058 action = "trying to air-assault";
2061 news_item = N_SHP_LOSE;
2062 pr("You have been repelled\n");
2064 action = "trying to board";
2067 news_item = N_LND_LOSE;
2068 pr("You have been repelled\n");
2070 action = "trying to board";
2074 nreport(player->cnum, news_item, def->own, 1);
2077 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2078 cname(player->cnum), player->cnum, a_cas,
2079 action, pr_com(0, def, def->own), d_cas);
2082 if (opt_SNEAK_ATTACK) {
2083 do_sneak(def, success);
2086 send_reacting_units_home(def, dlist);
2088 /* putland the defending land */
2091 /* putland the attacking land */
2094 /* put the victim sector/ship/land */
2095 if (!success || !take_def(combat_mode, olist, off, def))
2098 /* put the attacking sectors/ship */
2099 for (n = 0; n <= off->last; ++n)
2100 if (off[n].type != EF_BAD)
2101 put_combat(&off[n]);
2106 switch (combat_mode) {
2108 ask_move_in(off, olist, def);
2110 /* put sectors again to get abandon warnings */
2111 for (n = 0; n <= off->last; ++n)
2112 if (off[n].type != EF_BAD)
2113 put_combat(&off[n]);
2116 att_move_in_off(combat_mode, off, olist, def);
2119 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2123 /* What percentage of the combat forces going head-to-head are we? */
2126 att_calcodds(int ototal, int dtotal)
2130 /* calculate odds */
2133 else if (dtotal <= 0)
2136 odds = ((double)ototal) / (dtotal + ototal);
2141 /* Here's where the dead soldiers get dragged off the battlefield */
2144 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2146 int to_take = CASUALTY_LUMP;
2147 int biggest_troops = 0, index = -1;
2148 int n, tot_troops = 0, biggest_mil, cas;
2149 struct emp_qelem *qp, *biggest;
2152 for (n = 0; n <= off->last; ++n) {
2153 if (off[n].type != EF_BAD) {
2154 tot_troops += off[n].troops;
2155 if (off[n].troops > biggest_troops) {
2156 biggest_troops = off[n].troops;
2163 to_take -= tot_troops;
2166 for (n = 0; n <= off->last; ++n)
2167 if (off[n].type != EF_BAD)
2171 * They can all come off mil. We rotate the casualties,
2172 * starting with the sector containing the most mil.
2174 to_take = CASUALTY_LUMP;
2176 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2179 while (to_take > 0) {
2180 for (n = index; n <= off->last && to_take; ++n) {
2181 if (off[n].type != EF_BAD && off[n].troops > 0) {
2186 for (n = 0; n < index && to_take; ++n) {
2187 if (off[n].type != EF_BAD && off[n].troops > 0) {
2193 return CASUALTY_LUMP;
2197 return (CASUALTY_LUMP - to_take);
2200 * Need to take some casualties from attacking units
2201 * Procedure: find the biggest unit remaining (in
2202 * terms of mil) and give it the casualties.
2204 biggest = (struct emp_qelem *)0;
2206 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2207 llp = (struct llist *)qp;
2209 if (total_mil(&llp->land) > biggest_mil) {
2210 biggest_mil = total_mil(&llp->land);
2214 if (biggest == (struct emp_qelem *)0)
2215 return (CASUALTY_LUMP - to_take);
2217 llp = (struct llist *)biggest;
2218 cas = lnd_take_casualty(combat_mode, llp, to_take);
2219 return (CASUALTY_LUMP - (to_take - cas));
2222 /* Send reacting defense units back to where they came from (at no mob cost) */
2225 send_reacting_units_home(struct combat *def, struct emp_qelem *list)
2227 struct emp_qelem *qp, *next;
2231 for (qp = list->q_forw; qp != list; qp = next) {
2233 llp = (struct llist *)qp;
2234 if ((llp->land.lnd_x != llp->x) || (llp->land.lnd_y != llp->y)) {
2235 sprintf(buf, "returns to %s",
2236 xyas(llp->x, llp->y, llp->land.lnd_own));
2237 llp->land.lnd_x = llp->x;
2238 llp->land.lnd_y = llp->y;
2239 lnd_delete(llp, buf);
2244 /* Check for 0 offense strength. This call will always preceed an abort */
2247 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2250 if (def->own && player->cnum != def->own) {
2252 "%s (#%d) considered %sing you @%s\n",
2253 cname(player->cnum), player->cnum,
2254 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2256 pr("No troops for %s...\n", att_mode[combat_mode]);
2263 * Take the defending sector or ship from the defender and give it to the
2268 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2273 struct llist *llp, *delete_me = 0;
2279 for (n = 0; n <= off->last && !occuppied; ++n) {
2280 if (off[n].type != EF_BAD &&
2281 off[n].troops > 0 &&
2282 (off[n].type != EF_SECTOR || off[n].mob)) {
2284 if (def->type == EF_LAND) {
2285 if (def->lnd_lcp->l_flags & L_SPY) {
2292 pr("1 mil from %s moves %s\n",
2293 prcom(0, off + n), prcom(2, def));
2298 pr("%s left unoccupied\n", prcom(0, def));
2301 "No enemy troops moved %s so you still own it!\n",
2302 pr_com(2, def, def->own));
2305 llp = (struct llist *)list->q_forw;
2306 llp->land.lnd_x = def->x;
2307 llp->land.lnd_y = def->y;
2308 take_move_in_mob(combat_mode, llp, off, def);
2309 if (def->type == EF_SHIP) {
2310 llp->land.lnd_ship = def->shp_uid;
2311 sprintf(buf, "boards %s", prcom(0, def));
2314 llp->land.lnd_ship = -1;
2315 sprintf(buf, "moves in to occupy %s",
2316 xyas(def->x, def->y, player->cnum));
2317 lnd_delete(llp, buf);
2322 if (def->type == EF_SECTOR) {
2323 getsect(def->x, def->y, §);
2324 takeover(§, player->cnum);
2325 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2326 caploss(§, def->own,
2327 "* We have captured %s's capital, sir! *\n");
2329 } else if (def->type == EF_SHIP) {
2330 getship(def->shp_uid, &ship);
2331 takeover_ship(&ship, player->cnum, 1);
2332 putship(ship.shp_uid, &ship);
2333 } else if (def->type == EF_LAND) {
2334 getland(def->lnd_uid, &land);
2335 takeover_land(&land, player->cnum, 1);
2336 putland(land.lnd_uid, &land);
2339 lnd_delete(delete_me, buf);
2340 att_get_combat(def, 0);
2345 * Ask the attacker which mil & land units they'd like to move into the
2350 ask_move_in(struct combat *off, struct emp_qelem *olist,
2354 struct emp_qelem *qp, *next;
2358 s_char land_answer[1024];
2361 for (n = 0; n <= off->last; ++n)
2362 if (off[n].type != EF_BAD && off[n].troops > 0)
2364 ask_move_in_off(&off[n], def);
2365 if (player->aborted)
2371 memset(land_answer, 0, sizeof(land_answer));
2372 for (qp = olist->q_forw; qp != olist; qp = next) {
2374 llp = (struct llist *)qp;
2375 answerp = &land_answer[(int)llp->land.lnd_army];
2376 if (player->aborted || att_get_combat(def, 0) < 0)
2378 if (*answerp == 'Y')
2380 if (*answerp != 'N') {
2381 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2383 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2385 llp->land.lnd_army == ' ' ? '~' : llp->land.lnd_army,
2386 llp->land.lnd_effic);
2387 *answerp = att_prompt(prompt, llp->land.lnd_army);
2388 if (player->aborted || att_get_combat(def, 0) < 0)
2390 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2393 if (*answerp == 'y' || *answerp == 'Y')
2395 sprintf(buf, "stays in %s",
2396 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2397 lnd_delete(llp, buf);
2401 if (att_get_combat(def, 0) < 0) {
2402 for (qp = olist->q_forw; qp != olist; qp = next) {
2404 llp = (struct llist *)qp;
2405 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2407 sprintf(buf, "stays in %s",
2408 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2409 lnd_delete(llp, buf);
2413 if (opt_INTERDICT_ATT)
2414 lnd_interdict(olist, def->x, def->y, player->cnum);
2415 move_in_land(A_ATTACK, off, olist, def);
2418 /* Move offensive land units to the conquered sector or ship */
2421 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2424 struct emp_qelem *qp, *next;
2430 for (qp = olist->q_forw; qp != olist; qp = next) {
2432 llp = (struct llist *)qp;
2433 if (!get_land(combat_mode, def, llp->land.lnd_uid, llp, 0))
2435 take_move_in_mob(combat_mode, llp, off, def);
2436 llp->land.lnd_x = def->x;
2437 llp->land.lnd_y = def->y;
2438 if (def->type == EF_SHIP)
2439 llp->land.lnd_ship = def->shp_uid;
2441 llp->land.lnd_ship = -1;
2445 if (def->type == EF_SECTOR) {
2446 if (opt_INTERDICT_ATT) {
2447 lnd_sweep(olist, 0, 0, def->own);
2448 lnd_check_mines(olist);
2450 sprintf(buf, "now occupies %s", prcom(0, def));
2452 sprintf(buf, "boards %s", prcom(0, def));
2456 for (qp = olist->q_forw; qp != olist; qp = next) {
2458 llp = (struct llist *)qp;
2459 lnd_print(llp, buf);
2467 * Move assaulting, paradropping, or boarding mil & units into def
2468 * If the mil are coming from a ship, then pack a lunch.
2472 att_move_in_off(int combat_mode, struct combat *off,
2473 struct emp_qelem *olist, struct combat *def)
2478 int defvec[I_MAX + 1];
2479 int shipvec[I_MAX + 1];
2481 int n, lunchbox = 0;
2484 move_in_land(combat_mode, off, olist, def);
2486 for (n = 0; n <= off->last; ++n) {
2487 if (off[n].type == EF_BAD || !off[n].troops)
2489 troops = off[n].troops;
2491 off[n].mil -= troops;
2493 put_combat(off + n);
2494 if (combat_mode == A_ASSAULT) {
2495 getship(off[n].shp_uid, &ship);
2496 getvec(VT_ITEM, shipvec, (s_char *)&ship, EF_SHIP);
2497 lunchbox += (int)((troops + 1) * shipvec[I_FOOD] /
2498 (shipvec[I_MILIT] + troops +
2499 shipvec[I_CIVIL] + 0.5));
2500 shipvec[I_FOOD] -= lunchbox;
2501 putvec(VT_ITEM, shipvec, (s_char *)&ship, EF_SHIP);
2502 putship(ship.shp_uid, &ship);
2509 if (def->type == EF_SECTOR) {
2510 getsect(def->x, def->y, §);
2511 thing = (s_char *)§
2512 } else if (def->type == EF_SHIP) {
2513 getship(def->shp_uid, &ship);
2514 thing = (s_char *)&ship;
2515 } else if (def->type == EF_LAND) {
2516 getship(def->lnd_uid, &land);
2517 thing = (s_char *)&land;
2519 pr("Please tell the deity that you got the 'hungry mole' error\n");
2522 getvec(VT_ITEM, defvec, thing, def->type);
2523 defvec[I_FOOD] += lunchbox;
2524 putvec(VT_ITEM, defvec, thing, def->type);
2525 if (def->type == EF_SECTOR)
2527 else if (def->type == EF_SHIP)
2528 putship(ship.shp_uid, &ship);
2530 putland(land.lnd_uid, &land);
2534 /* Ask how many mil to move in from each sector */
2537 ask_move_in_off(struct combat *off, struct combat *def)
2540 int num_mil, dam = 0, left;
2546 if (att_get_combat(off, 0) <= 0)
2548 if (att_get_combat(def, 0) < 0)
2550 if (off->own != player->cnum)
2552 d = sector_mcost(getsectp(def->x, def->y), MOB_ROAD);
2553 if ((mob_support = min(off->troops, (int)(off->mob / d))) <= 0)
2555 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2556 xyas(off->x, off->y, player->cnum), mob_support);
2557 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0) {
2561 /* Make sure we don't move in more than we can support mobility-wise */
2562 if (num_mil > mob_support)
2563 num_mil = mob_support;
2564 if (att_get_combat(off, 0) <= 0)
2566 if (att_get_combat(def, 0) < 0)
2568 if ((num_mil = min(off->troops, num_mil)) <= 0) {
2569 pr("No mil moved in from %s\n",
2570 xyas(off->x, off->y, player->cnum));
2573 mob_support = max(1, (int)(num_mil * d));
2574 off->mob -= min(off->mob, mob_support);
2575 off->mil -= num_mil;
2576 off->troops -= num_mil;
2579 weight = num_mil * ichr[I_MILIT].i_lbs;
2580 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2581 if (chance(weight / 100.0))
2583 ground_interdict(def->x, def->y, player->cnum, "military");
2584 dam += check_lmines(def->x, def->y, weight);
2588 left = commdamage(num_mil, dam, V_MILIT);
2589 if (left < num_mil) {
2591 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));
2593 pr("All of the mil you were moving were destroyed!\n");
2596 /* maybe got nuked */
2597 if (att_get_combat(def, 0) < 0)
2605 /* Charge land units for moving into a sector or onto a ship */
2608 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off,
2614 switch (combat_mode) {
2617 lnd_mobcost(&llp->land, getsectp(def->x, def->y), MOB_NONE);
2618 new = llp->land.lnd_mobil - mobcost;
2621 llp->land.lnd_mobil = new;
2624 if (off->shp_mcp->m_flags & M_LAND) {
2625 if (llp->lcp->l_flags & L_MARINE)
2626 llp->land.lnd_mobil -=
2627 ((float)etu_per_update * land_mob_scale * 0.5);
2629 llp->land.lnd_mobil -= ((float)etu_per_update
2632 if (llp->lcp->l_flags & L_MARINE)
2633 llp->land.lnd_mobil = 0;
2635 llp->land.lnd_mobil = (((float)etu_per_update
2636 * land_mob_scale) * (-1));
2640 /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2641 if (llp->lcp->l_flags & L_MARINE)
2642 llp->land.lnd_mobil -= 10;
2644 llp->land.lnd_mobil -= 40;
2647 llp->land.lnd_harden = 0;
2651 free_list(struct emp_qelem *list)
2653 register struct emp_qelem *qp, *next;
2655 if (!list || QEMPTY(list))
2659 while (qp != list) {
2668 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2676 * sector_strength - Everyone starts at 1. You can get up to a max
2677 * of d_dstr, depending on how much you build up the
2678 * defenses of the sector.
2682 sector_strength(struct sctstr *sp)
2688 if (sp->sct_type == SCT_MOUNT)
2691 d = d + ((double)(dchr[sp->sct_type].d_dstr - d) *
2692 ((double)sp->sct_defense / 100.0));
2694 if (d > dchr[sp->sct_type].d_dstr)
2695 d = dchr[sp->sct_type].d_dstr;