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 emp_qelem *list);
82 static int take_def(int combat_mode, struct emp_qelem *list,
83 struct combat *off, struct combat *def);
85 static int get_land(int combat_mode, struct combat *def, int uid,
86 struct llist *llp, int victim_land);
88 s_char *att_mode[] = {
89 /* must match combat types in combat.h */
90 "defend", "attack", "assault", "paradrop", "board", "lboard"
95 * The principal object in this code is the "combat" object. A combat object
96 * is either a sector or ship. There are
97 * usually two instances of this, the "def" or defense combat object, and
98 * the array of "off" or offense objects. The number of offense objects is
99 * determined by the value of off->last (e.g. more than one attacking sector).
100 * the type of the object is determined by combat->type which can take the
101 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
102 * which is often passed to these functions is combat_mode. This can take
103 * the value A_DEFENSE, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
104 * As these six modes of being in combat affect things like mobcost and combat
105 * value, there are often switches made on combat_mode. Note that in all cases
106 * no mobility is taken from sectors, ships, or land units until the player
107 * has committed to a fight. Instead, the cost is temporarily placed in
108 * combat->mobcost, or llp->mobil as the case may be, and then when the object
109 * is "put" back onto disk, then the amounts in these variables are subtracted
110 * from the object's mobility. It needs to be done this way as the objects
111 * are constantly being re-read from disk, and we don't want to take any mob
112 * unless a fight actually occurrs.
116 /* initialize combat object */
119 att_combat_init(struct combat *com, int type)
121 memset(com, 0, sizeof(*com));
126 /* print a combat object with optional preposition */
129 pr_com(int inon, struct combat *com, natid who)
131 if (com->type == EF_SECTOR) {
133 inon ? inon == 1 ? "in " : "into " : "",
134 xyas(com->x, com->y, who));
135 } else if (com->type == EF_SHIP) {
137 return prbuf("%s%s %s(#%d)",
138 inon ? inon == 1 ? "on " : "onto " : "",
139 com->shp_mcp->m_name, com->shp_name,
142 return prbuf("%s%s #%d",
143 inon ? inon == 1 ? "on " : "onto " : "",
144 com->shp_mcp->m_name, com->shp_uid);
146 } else if (com->type == EF_LAND) {
147 return prbuf("%s%s #%d",
148 inon ? inon == 1 ? "on " : "onto " : "",
149 com->lnd_lcp->l_name, com->lnd_uid);
151 return "your forces";
156 prcom(int inon, struct combat *com)
158 return pr_com(inon, com, player->cnum);
161 /* Doing a sneak attack */
163 do_sneak(struct combat *def, int success)
166 struct natstr *natp = getnatp(player->cnum);
167 int issneak = getrel(natp, def->own);
169 if (def->type != EF_SECTOR)
172 getsect(def->x, def->y, §);
174 if (issneak == AT_WAR || !def->own || sect.sct_oldown == player->cnum)
178 pr("Your sneak attack was successful\nBut ");
180 pr("Your sneak attack was unsuccessful\nAnd ");
182 pr("it will cost you $5000\n");
183 pr("War has been declared!!!!\n");
184 wu(0, def->own, "Country %s (#%d) has Sneak Attacked!!\n",
185 cname(player->cnum), player->cnum);
186 wu(0, def->own, "Country %s (#%d) has Declared WAR on you!!\n",
187 cname(player->cnum), player->cnum);
188 player->dolcost += 5000;
189 issneak = min(issneak, MOBILIZATION);
190 nreport(player->cnum, N_DECL_WAR, def->own, 1);
191 setrel(player->cnum, def->own, issneak);
195 * This is the combat object "type" based integrity check. It basically
196 * splits along three divisions: ship/sector, attacker/defender,
197 * first time/not first time.
201 att_get_combat(struct combat *com, int isdef)
215 if (!getsect(com->x, com->y, §)) {
216 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
217 return att_combat_init(com, EF_BAD);
219 com->sct_type = sect.sct_type;
220 com->sct_dcp = &dchr[sect.sct_type];
221 mil = sect.sct_item[I_MILIT];
222 pstage = sect.sct_pstage;
223 owner = sect.sct_own;
224 eff = sect.sct_effic;
225 mob = sect.sct_mobil;
230 if (!getland(com->lnd_uid, &land)) {
232 pr("Land unit #%d is not in the same sector!\n",
234 return att_combat_init(com, EF_BAD);
236 if (isdef && player->owner) {
237 pr("Boarding yourself? Try using the 'load' command.\n");
238 return att_combat_init(com, EF_BAD);
240 com->lnd_lcp = &lchr[(int)land.lnd_type];
241 mil = land.lnd_item[I_MILIT];
242 pstage = land.lnd_pstage;
243 owner = land.lnd_own;
244 eff = land.lnd_effic;
245 mob = land.lnd_mobil;
250 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
252 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
254 pr("Ship #%d is not your ship!\n", com->shp_uid);
255 return att_combat_init(com, EF_BAD);
258 if (isdef && player->owner &&
259 ontradingblock(EF_SHIP, (int *)&ship)) {
260 pr("%s is on the trading block.\n", prcom(0, com));
261 return att_combat_init(com, EF_BAD);
264 if (isdef && player->owner) {
265 pr("Boarding yourself? Try using the 'tend' command.\n");
266 return att_combat_init(com, EF_BAD);
268 com->shp_mcp = &mchr[(int)ship.shp_type];
270 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
271 if (!isdef && !player->owner) {
273 pr("%s was just sunk!\n", prcom(0, com));
275 pr("Ship #%d is not your ship!\n", com->shp_uid);
276 return att_combat_init(com, EF_BAD);
278 mil = ship.shp_item[I_MILIT];
279 pstage = ship.shp_pstage;
280 owner = ship.shp_own;
281 eff = ship.shp_effic;
282 mob = ship.shp_mobil;
291 return att_combat_init(com, EF_BAD);
294 if (!com->set) { /* first time */
295 if (isdef) { /* defender */
297 } else { /* attacker */
299 pr("No mil %s\n", prcom(1, com));
301 pr("Only 1 mil %s\n", prcom(1, com));
302 /* don't abandon attacking sectors or ships */
303 com->troops = max(0, mil - 1);
305 com->plague = pstage == PLG_INFECT;
306 } else { /* not first time */
307 if (isdef) { /* defender */
308 if (com->x != x || com->y != y) {
309 pr("%s has moved!\n", prcom(0, com));
310 return att_combat_init(com, EF_BAD);
312 if (owner != com->own) {
314 pr("WARNING: The ownership of %s just changed from %s to %s!\n", prcom(0, com), cname(com->own), cname(owner));
315 } else if (com->type == EF_SECTOR) {
316 pr("WARNING: %s just abandoned sector %s!\n",
317 cname(com->own), xyas(com->x, com->y,
322 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
324 com->mil < mil ? "increased" : "decreased", com->mil,
327 } else { /* attacker */
328 if (owner != player->cnum && getrel(getnatp(owner), player->cnum) != ALLIED) { /* must be EF_SECTOR */
330 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));
332 pr("You no longer own %s\n",
333 xyas(com->x, com->y, player->cnum));
334 return att_combat_init(com, EF_BAD);
336 if (com->troops && com->troops + 1 > mil) {
337 if (com->own == owner && player->cnum == owner) /* not a takeover */
338 pr("WARNING: Your mil %s has been reduced from %d to %d!\n", prcom(1, com), com->troops, max(0, mil - 1));
339 com->troops = max(0, mil - 1);
354 * In the course of the fight, the combat object may have lost mil, eff, or
355 * mobility. This is the place where the data in the object gets flushed to
356 * disk to make it "real".
360 put_combat(struct combat *com)
369 getsect(com->x, com->y, §);
370 sect.sct_type = com->sct_type;
371 deff = sect.sct_effic - com->eff;
373 sect.sct_road -= (sect.sct_road * deff / 100.0);
374 sect.sct_rail -= (sect.sct_rail * deff / 100.0);
375 sect.sct_defense -= (sect.sct_defense * deff / 100.0);
376 if (sect.sct_road <= 0)
378 if (sect.sct_rail <= 0)
380 if (sect.sct_defense <= 0)
381 sect.sct_defense = 0;
383 sect.sct_effic = com->eff;
384 if (!opt_DEFENSE_INFRA)
385 sect.sct_defense = sect.sct_effic;
387 if (opt_MOB_ACCESS) {
388 if ((com->mob - com->mobcost) < -127)
389 sect.sct_mobil = -127;
391 sect.sct_mobil = (short)(com->mob - com->mobcost);
393 if ((com->mob - com->mobcost) < 0)
396 sect.sct_mobil = (short)(com->mob - com->mobcost);
399 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
400 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
401 sect.sct_own = com->own;
403 if (sect.sct_pstage == PLG_HEALTHY)
404 sect.sct_pstage = PLG_EXPOSED;
406 sect.sct_item[I_MILIT] = com->mil;
408 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
411 getland(com->lnd_uid, &land);
412 land.lnd_effic = com->eff;
414 if (com->mob - com->mobcost < -127)
415 land.lnd_mobil = -127;
417 land.lnd_mobil = (s_char)(com->mob - com->mobcost);
419 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
421 land.lnd_own = com->own;
422 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
425 if (land.lnd_pstage == PLG_HEALTHY)
426 land.lnd_pstage = PLG_EXPOSED;
428 if (!(com->lnd_lcp->l_flags & L_SPY))
429 land.lnd_item[I_MILIT] = com->mil;
430 lnd_count_units(&land);
431 if (com->own == player->cnum) {
432 land.lnd_mission = 0;
434 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
436 putland(com->lnd_uid, &land);
439 getship(com->shp_uid, &ship);
440 ship.shp_effic = com->eff;
442 if (com->mob - com->mobcost < -127)
443 ship.shp_mobil = -127;
445 ship.shp_mobil = (s_char)(com->mob - com->mobcost);
447 makelost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
449 ship.shp_own = com->own;
450 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
453 if (ship.shp_pstage == PLG_HEALTHY)
454 ship.shp_pstage = PLG_EXPOSED;
456 ship.shp_item[I_MILIT] = com->mil;
458 if (com->own == player->cnum) {
459 ship.shp_mission = 0;
461 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
463 putship(com->shp_uid, &ship);
466 att_get_combat(com, com->own != player->cnum);
469 /* If pre-attack, abort fight. If post-attack, don't move anything in */
474 return player->aborted = 1;
478 * This is the combat_mode based integrity check. It splits among two main
479 * divisions: first time/not first time, and attack/assault/para/board.
483 att_abort(int combat_mode, struct combat *off, struct combat *def)
492 if (att_get_combat(def, 1) < 0)
493 return abort_attack();
495 if (off && combat_mode != A_ATTACK) {
496 if (att_get_combat(off, 0) < 0)
497 return abort_attack();
498 if (off->type == EF_SHIP &&
499 (!getsect(off->x, off->y, §) ||
500 sect.sct_type != SCT_WATER)) {
501 pr("%s can not %s from that far inland!\n",
502 prcom(0, off), att_mode[combat_mode]);
503 return abort_attack();
506 switch (combat_mode) {
508 if (!neigh(def->x, def->y, player->cnum) &&
509 !adj_units(def->x, def->y, player->cnum)) {
510 pr("You are not adjacent to %s\n",
511 xyas(def->x, def->y, player->cnum));
512 return abort_attack();
514 if (def->own == player->cnum) {
515 pr("You can't attack your own sector.\n");
516 return abort_attack();
520 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
521 pr("You'll have to get there first...\n");
522 return abort_attack();
524 if (off && def->sct_type == SCT_MOUNT) {
525 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
526 return abort_attack();
530 if (def->own == player->cnum) {
531 pr("You can't air-assault your own sector.\n");
532 return abort_attack();
534 if (off && (def->sct_type == SCT_MOUNT ||
535 def->sct_type == SCT_WATER ||
536 def->sct_type == SCT_CAPIT ||
537 def->sct_type == SCT_FORTR ||
538 def->sct_type == SCT_WASTE)) {
539 pr("You can't air-assault a %s sector!\n",
540 def->sct_dcp->d_name);
541 return abort_attack();
545 return board_abort(off, def);
547 return land_board_abort(off, def);
550 if (off && def->sct_dcp->d_mcst <= 0) {
551 pr("You can't %s a %s sector!\n", att_mode[combat_mode],
552 def->sct_dcp->d_name);
553 return abort_attack();
555 if (!off || off->relations_checked)
557 off->relations_checked = 1;
560 setcont(player->cnum, def->own, FOUND_SPY);
561 setcont(def->own, player->cnum, FOUND_SPY);
563 if (opt_SLOW_WAR && def->own != player->cnum) {
564 natp = getnatp(player->cnum);
565 issneak = getrel(natp, def->own);
567 if (issneak == ALLIED) {
568 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
569 cname(def->own), att_mode[combat_mode]);
570 if (!confirm(y_or_n))
571 return abort_attack();
574 if (opt_SNEAK_ATTACK) {
575 getsect(def->x, def->y, §);
576 if ((issneak != AT_WAR) && (def->own)
577 && (def->own != player->cnum)
578 && (sect.sct_oldown != player->cnum)
579 && (issneak != SITZKRIEG) && (issneak != MOBILIZATION)) {
580 pr("You're not at war with them!\n");
581 if (!confirm("Do you really want to sneak attack (it will cost you $5000) [yn]? ")) {
582 pr("Sneak attack cancelled!\n");
583 return abort_attack();
586 if ((issneak != AT_WAR) && (def->own)
587 && (def->own != player->cnum)
588 && (sect.sct_oldown != player->cnum)
589 && ((issneak == MOBILIZATION) || (issneak == SITZKRIEG))) {
590 pr("You're not at war with them!\n");
591 return abort_attack();
594 if ((issneak != AT_WAR) && (def->own) &&
595 (sect.sct_oldown != player->cnum)) {
596 pr("You're not at war with them!\n");
597 return abort_attack();
605 * Lots of special things need to be checked for boarding, so I put it in
610 board_abort(struct combat *off, struct combat *def)
612 struct shpstr aship, dship; /* for tech levels */
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("Ship #%d is not in the same sector!\n", def->shp_uid);
626 return abort_attack();
628 if (off->type == EF_SHIP) {
630 pr("%s has no mobility!\n", prcom(0, off));
631 return abort_attack();
633 getship(off->shp_uid, &aship);
634 getship(def->shp_uid, &dship);
635 if (techfact(aship.shp_tech, 1.0) *
636 aship.shp_speed * off->eff <=
637 techfact(dship.shp_tech, 1.0) * dship.shp_speed * def->eff) {
638 pr("Victim ship moves faster than you do!\n");
641 "%s (#%d) %s failed to catch %s\n",
642 cname(aship.shp_own), aship.shp_own,
643 pr_com(0, off, def->own), pr_com(0, def, def->own));
644 return abort_attack();
646 } else if (off->type != EF_SECTOR) {
647 pr("Please tell the deity that you got the 'banana boat' error\n");
648 return abort_attack();
650 if (def->shp_mcp->m_flags & M_SUB) {
651 getsect(def->x, def->y, §);
652 if (sect.sct_type == SCT_WATER) {
653 pr("You can't board a submarine!\n");
654 return abort_attack();
661 * Lots of special things need to be checked for boarding, so I put it in
663 * STM - I copied it for land unit boarding. :)
667 land_board_abort(struct combat *off, struct combat *def)
669 if (att_get_combat(def, 1) < 0)
670 return abort_attack();
675 if (att_get_combat(off, 0) < 0)
676 return abort_attack();
678 if (off->x != def->x || off->y != def->y) {
679 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
680 return abort_attack();
686 /* If we are boarding, then the defending ship gets a chance to fire back */
688 att_approach(struct combat *off, struct combat *def)
694 pr("Approaching %s...\n", prcom(0, def));
697 "%s is being approached by %s...\n",
698 pr_com(0, def, def->own), pr_com(0, off, def->own));
699 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
702 pr("They're firing at us sir!\n");
705 "Your fleet at %s does %d damage to %s\n",
706 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
708 if (off->type == EF_SECTOR) {
709 getsect(off->x, off->y, §);
710 sectdamage(§, dam, 0);
712 pr("Enemy fleet at %s does %d damage to %s\n",
713 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
714 } else if (off->type == EF_SHIP) {
715 getship(off->shp_uid, &ship);
716 shipdamage(&ship, dam);
717 putship(off->shp_uid, &ship);
718 if (def->own && ship.shp_effic < SHIP_MINEFF) {
719 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
720 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
723 if (att_get_combat(off, 0) < 0)
724 return abort_attack();
728 /* The attack is valid. Tell the attacker about what they're going to hit */
731 att_show(struct combat *def)
733 /* Note that we tell the player about the treaty BEFORE we tell them
734 about the item. If we didn't, then they gain free information */
735 if (def->type == EF_SECTOR) {
736 if (!trechk(player->cnum, def->own, LANATT))
737 return abort_attack();
738 pr("%s is a %d%% %s %s with approximately %d military.\n",
739 xyas(def->x, def->y, player->cnum),
740 roundintby((int)def->eff, 10),
741 cname(def->own), def->sct_dcp->d_name,
742 roundintby(def->troops, 10));
743 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
744 writemap(player->cnum);
745 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
746 if (def->type == EF_SHIP) {
747 if (!trechk(player->cnum, def->own, SEAATT))
748 return abort_attack();
750 if (!trechk(player->cnum, def->own, LNDATT))
751 return abort_attack();
753 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));
755 /* Ok, everything is fine */
759 /* Attack and assault ask the user which kind of support they want */
762 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
767 *fortp = *shipp = *landp = *planep = 1;
769 if (player->argp[offset] != (s_char *)0) {
770 if ((player->argp[offset + 1] == (s_char *)0) ||
771 (player->argp[offset + 2] == (s_char *)0) ||
772 (player->argp[offset + 3] == (s_char *)0)) {
773 pr("If any support arguments are used, all must be!\n");
778 *landp = *planep = 0;
780 if (!(p = getstarg(player->argp[offset], "Use fort support? ",
784 if ((*p == 'y') || (*p == 'Y'))
787 if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
791 if ((*p == 'y') || (*p == 'Y'))
794 if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
798 if ((*p == 'y') || (*p == 'Y'))
801 if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
805 if ((*p == 'y') || (*p == 'Y'))
812 * Attack, assault, and board ask the attacker what they'd like to attack
813 * with. This includes mil and land units from each "off" object. Note that
814 * after each sub-prompt, we check to make sure that the attack is still
815 * valid, and if it's not, then we abort the attack.
819 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
820 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
823 s_char land_answer[1024];
826 if (att_abort(combat_mode, off, def))
828 memset(land_answer, 0, sizeof(land_answer));
829 for (n = 0; n <= off->last; ++n) {
830 off[n].troops = ask_off(combat_mode, off + n, def);
831 if (att_abort(combat_mode, off, def))
833 ask_olist(combat_mode, off + n, def, olist, land_answer,
834 a_spyp, a_engineerp);
835 if (att_abort(combat_mode, off, def))
841 /* How many mil is off allowed to attack with when it attacks def? */
844 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
848 switch (combat_mode) {
850 mob_support = off->mob / sector_mcost(getsectp(def->x, def->y),
854 /* mob_support = off->mob / sector_mcost(def->sct_type, def->eff);*/
855 if (mob_support < off->troops)
856 pr("Sector %s has %d mobility which can only support %d mil,\n", xyas(off->x, off->y, player->cnum), off->mob, mob_support);
858 mob_support = off->troops;
861 if (def->own != player->cnum && def->mil) {
862 if (off->shp_mcp->m_flags & M_SEMILAND)
863 return off->troops / 4;
864 else if (!(off->shp_mcp->m_flags & M_LAND))
865 return off->troops / 10;
869 if (off->type == EF_SECTOR && off->mob <= 0)
871 mob_support = vl_find(V_MILIT, def->shp_mcp->m_vtype,
872 def->shp_mcp->m_vamt, def->shp_mcp->m_nv);
873 if (mob_support < off->troops)
874 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
876 mob_support = off->troops;
881 if (def->lnd_lcp->l_flags & L_SPY)
883 mob_support = vl_find(V_MILIT, def->lnd_lcp->l_vtype,
884 def->lnd_lcp->l_vamt, def->lnd_lcp->l_nv);
885 if (mob_support < off->troops)
886 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
888 mob_support = off->troops;
895 * If the attacker decides to go through with the attack, then the
896 * sectors/ships they are attacking with may be charged some mobility.
897 * This is where that amount of mobility is calculated. It is actually
898 * subtracted "for real" from the object's mobility in put_combat().
902 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
909 switch (combat_mode) {
913 (int)(attacking_mil *
914 sector_mcost(getsectp(def->x, def->y), MOB_ROAD)));
917 off->mobcost += max(1, attacking_mil / 5);
922 off->mobcost += max(1, attacking_mil / 5);
925 /* the 2 in the formula below is a fudge factor */
926 getship(def->shp_uid, &ship);
927 off->mobcost += (def->eff / 100) * (ship.shp_speed / 2);
932 /* How many mil to we want to attack from off against def? */
935 ask_off(int combat_mode, struct combat *off, struct combat *def)
941 if (att_get_combat(off, 0) <= 0)
943 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
945 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
947 if (off->type == EF_SECTOR) {
948 if (off->own != player->cnum)
950 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
951 off->sct_dcp->d_name,
952 xyas(off->x, off->y, player->cnum), mob_support);
954 sprintf(prompt, "Number of mil from %s (max %d) : ",
955 prcom(0, off), mob_support);
957 if ((attacking_mil = onearg(0, prompt)) < 0)
959 if (att_abort(combat_mode, off, def))
961 if (att_get_combat(off, 0) <= 0)
964 min(attacking_mil, min(mob_support, off->troops))) <= 0)
967 calc_mobcost(combat_mode, off, def, attacking_mil);
968 return attacking_mil;
972 * Which units would you like to attack with or move in with [ynYNq?]
976 att_prompt(s_char *prompt, s_char army)
984 while (!p || (*p != 'y' && *p != 'n' && *p != 'Y' && *p != 'N')) {
985 if (p && *p == 'q') {
992 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);
993 p = getstring(prompt, buf);
994 if (player->aborted) {
1002 /* Ask the attacker which units they want to attack/assault/board with */
1005 ask_olist(int combat_mode, struct combat *off, struct combat *def,
1006 struct emp_qelem *olist, s_char *land_answer, int *a_spyp,
1009 struct nstr_item ni;
1013 struct lchrstr *lcp;
1020 if (def->type == EF_LAND)
1022 if (def->type == EF_SHIP)
1023 maxland = def->shp_mcp->m_nland;
1025 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
1026 while (nxtitem(&ni, (s_char *)&land)) {
1027 if (land.lnd_own != player->cnum)
1029 if (land.lnd_effic < LAND_MINEFF)
1031 if (land_answer[(int)land.lnd_army] == 'N')
1033 if (!lnd_can_attack(&land))
1035 lcp = &lchr[(int)land.lnd_type];
1037 if (def->type == EF_SHIP && !maxland) {
1038 pr("Land units are not able to board this kind of ship\n");
1041 if (land.lnd_mobil <= 0) {
1042 pr("%s is out of mobility, and cannot %s\n",
1043 prland(&land), att_mode[combat_mode]);
1048 if (ontradingblock(EF_LAND, (int *)&land)) {
1049 pr("%s is on the trading block, and cannot %s\n",
1050 prland(&land), att_mode[combat_mode]);
1055 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1056 pr("%s is on ship #%d, and cannot %s\n",
1057 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1059 } else if (off->type == EF_SHIP) {
1060 if (land.lnd_ship != off->shp_uid)
1062 } else if (land.lnd_land >= 0) {
1063 pr("%s is on unit #%d, and cannot %s\n",
1064 prland(&land), land.lnd_land, att_mode[combat_mode]);
1067 switch (combat_mode) {
1070 lnd_mobcost(&land, getsectp(def->x, def->y), MOB_NONE);
1071 if (land.lnd_mobil < mobcost) {
1072 pr("%s does not have enough mobility (%d needed)\n",
1073 prland(&land), (int)ceil(mobcost));
1080 if (!(lcp->l_flags & L_ASSAULT))
1083 att_val = attack_val(combat_mode, &land);
1085 pr("%s has no offensive strength\n", prland(&land));
1088 resupply_all(&land);
1089 putland(land.lnd_uid, &land);
1090 if (!has_supply(&land)) {
1091 pr("%s is out of supply, and cannot %s\n",
1092 prland(&land), att_mode[combat_mode]);
1095 if (def->type == EF_SHIP && first_time) {
1097 pr("You may board with a maximum of %d land units\n", maxland);
1099 pr("%s has a base %s value of %d\n",
1100 prland(&land), att_mode[combat_mode], att_val);
1101 if (land_answer[(int)land.lnd_army] != 'Y') {
1103 "%s with %s %s (%c %d%%) [ynYNq?] ",
1104 att_mode[combat_mode],
1107 land.lnd_army == ' ' ? '~' : land.lnd_army,
1109 land_answer[(int)land.lnd_army] =
1110 att_prompt(prompt, land.lnd_army);
1111 if (att_abort(combat_mode, off, def))
1113 if (land_answer[(int)land.lnd_army] != 'y' &&
1114 land_answer[(int)land.lnd_army] != 'Y')
1117 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1118 logerror("Malloc failed in attack!\n");
1122 memset(llp, 0, sizeof(struct llist));
1123 emp_insque(&llp->queue, olist);
1124 llp->mobil = mobcost;
1125 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1127 if (lnd_spyval(&land) > *a_spyp)
1128 *a_spyp = lnd_spyval(&land);
1129 if (llp->lcp->l_flags & L_ENGINEER)
1131 if (def->type == EF_SHIP && ++count >= maxland)
1136 /* What's the offense or defense multiplier? */
1139 att_combat_eff(struct combat *com)
1145 if (com->type == EF_SECTOR) {
1146 eff = com->eff / 100.0;
1147 if (com->own == player->cnum) {
1148 str = com->sct_dcp->d_ostr;
1149 eff = 1.0 + ((str - 1.0) * eff);
1151 eff = sector_strength(getsectp(com->x, com->y));
1152 /* str = com->sct_dcp->d_dstr;*/
1153 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1154 getship(com->shp_uid, &ship);
1155 eff = (1.0 + ship.shp_armor / 100.0);
1161 * Estimate the defense strength and give the attacker a chance to abort
1162 * if the odds are less than 50%
1166 att_estimate_defense(int combat_mode, struct combat *off,
1167 struct emp_qelem *olist, struct combat *def,
1175 * Get the attacker units & mil again in case they changed while the
1176 * attacker was answering sub-prompts.
1179 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1180 if (att_empty_attack(combat_mode, ototal, def))
1181 return abort_attack();
1182 if (combat_mode == A_PARA)
1184 pr("\n Initial attack strength: %8d\n", ototal);
1186 estimate = att_combat_eff(def) * roundintby(def->troops, 10);
1187 estimate += att_combat_eff(def) * get_dlist(def, 0, a_spy, 0);
1190 * Calculate the initial (pre-support) attack odds. If they're less
1191 * than 50%, ask for a confirmation.
1194 odds = (int)(att_calcodds(ototal, estimate) * 100);
1198 pr(" Estimated defense strength: %8d\n", estimate);
1199 pr(" Estimated odds: %8d%%\n\n",odds);
1200 sprintf(prompt, "Are you sure you want to %s [yn]? ",
1201 att_mode[combat_mode]);
1202 if (!confirm(prompt))
1203 return abort_attack();
1204 ototal = get_ototal(combat_mode, off, olist,1.0,1);
1205 if (att_empty_attack(combat_mode, ototal, def))
1206 return abort_attack();
1212 /* Get the defensive units and reacting units */
1214 att_get_defense(struct emp_qelem *olist, struct combat *def,
1215 struct emp_qelem *dlist, int a_spy, int ototal)
1218 struct emp_qelem *qp;
1224 get_dlist(def, dlist, 0, &d_spy);
1225 dtotal = get_dtotal(def, dlist, 1.0, 0);
1228 * Call in reacting units
1231 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1232 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1234 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1235 llp = (struct llist *)qp;
1236 intelligence_report(def->own, &llp->land, d_spy,
1237 "Scouts report attacking unit:");
1240 old_dtotal = dtotal;
1241 dtotal = get_dtotal(def, dlist, 1.0, 0);
1242 if (dtotal != old_dtotal)
1243 pr("Defense strength with reacting units: %8d\n", dtotal);
1248 /* Get the defensive land units in the sector or on the ship */
1251 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1254 struct nstr_item ni;
1259 /* In here is where you need to take out spies and trains from the defending
1260 lists. Spies try to hide, trains get trapped and can be boarded. */
1262 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1263 while (nxtitem(&ni, (s_char *)&land)) {
1266 if (land.lnd_own != def->own)
1268 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1270 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1272 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1274 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1276 if (!list) { /* Just estimating the enemy strength */
1277 estimate += intelligence_report(player->cnum,
1279 "Scouts report defending unit:");
1282 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1283 logerror("Malloc failed in attack!\n");
1287 memset(llp, 0, sizeof(struct llist));
1288 emp_insque(&llp->queue, list);
1289 llp->supplied = has_supply(&land);
1290 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1292 if (lnd_spyval(&land) > *d_spyp)
1293 *d_spyp = lnd_spyval(&land);
1298 /* Calculate the total offensive strength */
1301 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1302 double osupport, int check)
1304 double ototal = 0.0;
1305 struct emp_qelem *qp, *next;
1310 * first, total the attacking mil
1313 for (n = 0; n <= off->last; ++n) {
1314 if (off[n].type == EF_BAD || (check &&
1315 att_get_combat(&off[n], 0) <= 0))
1317 ototal += off[n].troops * att_combat_eff(off + n);
1321 * next, add in the attack_values of all
1322 * the attacking units
1325 for (qp = olist->q_forw; qp != olist; qp = next) {
1327 llp = (struct llist *)qp;
1328 if (check && !get_land(combat_mode, 0, llp->land.lnd_uid, llp, 0))
1330 if (combat_mode == A_ATTACK) {
1332 for (n = 0; n <= off->last; ++n) {
1333 if (off[n].type == EF_BAD)
1335 if ((off[n].x == llp->land.lnd_x) &&
1336 (off[n].y == llp->land.lnd_y))
1340 lnd_delete(llp, "is in a sector not owned by you");
1343 ototal += attack_val(combat_mode, &llp->land) *
1344 att_combat_eff(off + w);
1346 ototal += attack_val(combat_mode, &llp->land);
1351 return ldround(ototal, 1);
1354 /* Calculate the total defensive strength */
1357 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1360 double dtotal = 0.0, eff = 1.0, d_unit;
1361 struct emp_qelem *qp, *next;
1364 if (check && att_get_combat(def, 1) < 0)
1366 eff = att_combat_eff(def);
1367 dtotal = def->troops * eff;
1370 * next, add in the defense_values of all
1371 * the defending non-retreating units
1374 for (qp = list->q_forw; qp != list; qp = next) {
1376 llp = (struct llist *)qp;
1378 if (check && !get_land(A_DEFEND, def, llp->land.lnd_uid, llp, 1))
1380 d_unit = defense_val(&llp->land);
1382 d_unit = ((double)defense_val(&llp->land) / 2.0);
1383 dtotal += d_unit * eff;
1388 return ldround(dtotal, 1);
1392 * This is the land unit integrity check. Note that we don't print
1393 * warnings about victim land units because the attacker may not have seen them
1397 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp,
1400 struct lndstr *lp = &llp->land;
1405 if (!llp->lcp) { /* first time */
1406 llp->x = llp->land.lnd_x;
1407 llp->y = llp->land.lnd_y;
1408 llp->lcp = &lchr[(int)llp->land.lnd_type];
1409 } else { /* not first time */
1410 if (lp->lnd_effic < LAND_MINEFF) {
1411 sprintf(buf, "was destroyed and is no longer a part of the %s",
1412 att_mode[combat_mode]);
1413 lnd_delete(llp, buf);
1417 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1419 "left to go fight another battle and is no longer a part of the defense");
1423 if (lp->lnd_own != player->cnum) {
1425 "was destroyed and is no longer a part of the %s",
1426 att_mode[combat_mode]);
1427 lnd_delete(llp, buf);
1430 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1432 "left to fight another battle and is no longer a part of the %s",
1433 att_mode[combat_mode]);
1434 lnd_delete(llp, buf);
1437 if (lp->lnd_effic < llp->eff) {
1438 sprintf(buf, "damaged from %d%% to %d%%",
1439 llp->eff, lp->lnd_effic);
1440 lnd_print(llp, buf);
1444 llp->eff = llp->land.lnd_effic;
1450 * Put the land unit on the disk. If there was some mobility cost, then
1451 * subtract it from the units mobility. Note that this works the same way
1452 * as sectors & ships in that no mobility is actually taken until the attacker
1453 * has committed to attacking.
1457 kill_land(struct emp_qelem *list)
1459 struct emp_qelem *qp, *next;
1462 for (qp = list->q_forw; qp != list; qp = next) {
1464 llp = (struct llist *)qp;
1465 if (llp->land.lnd_ship >= 0) {
1466 llp->land.lnd_effic = 0;
1467 lnd_delete(llp, "cannot return to the ship, and dies!");
1473 att_infect_units(struct emp_qelem *list, int plague)
1475 struct emp_qelem *qp, *next;
1480 for (qp = list->q_forw; qp != list; qp = next) {
1482 llp = (struct llist *)qp;
1483 if (llp->land.lnd_pstage == PLG_HEALTHY)
1484 llp->land.lnd_pstage = PLG_EXPOSED;
1489 put_land(struct emp_qelem *list)
1491 struct emp_qelem *qp, *next;
1494 for (qp = list->q_forw; qp != list; qp = next) {
1496 llp = (struct llist *)qp;
1497 llp->land.lnd_mission = 0;
1498 llp->land.lnd_harden = 0;
1499 llp->land.lnd_mobil -= (int)llp->mobil;
1501 putland(llp->land.lnd_uid, &llp->land);
1502 if (llp->land.lnd_own != player->cnum) {
1503 emp_remque((struct emp_qelem *)llp);
1504 free((s_char *)llp);
1506 get_land(A_ATTACK, 0, llp->land.lnd_uid, llp, 0);
1511 * Keep sending in reinforcements until it looks like we're going to win.
1512 * Note that the "strength" command also calls this routine.
1516 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1517 int *d_spyp, int ototal)
1519 struct nstr_item ni;
1521 struct sctstr sect, dsect;
1531 double eff = att_combat_eff(def);
1537 * All units that are within their reaction radius and not damaged
1538 * below their morale value now get to react to the threatened sect.
1539 * Once we've sent enough to counter the threat, stop sending them.
1541 * Not anymore. All units get to react. :)
1545 dtotal = get_dtotal(def, list, 1.0, 1);
1548 snxtitem_all(&ni, EF_LAND);
1549 while (nxtitem(&ni, (s_char *)&land) &&
1550 (dtotal + new_land * eff < (int)(1.2 * (float)ototal))) {
1553 if (!land.lnd_rad_max)
1555 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1557 if (land.lnd_own != def->own)
1559 if (land.lnd_ship >= 0)
1561 if (!defense_val(&land))
1564 if (land.lnd_effic <= land.lnd_retreat)
1567 if (!lnd_can_attack(&land))
1570 /* Only supplied units can react */
1571 if (!(supply_state = has_supply(&land)))
1574 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1576 getsect(land.lnd_x, land.lnd_y, §);
1577 /* Units on efficient headquarters can react 1 farther */
1578 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1579 radius = land.lnd_rad_max + 1;
1581 radius = land.lnd_rad_max;
1583 if (land.lnd_mission == MI_RESERVE)
1589 getsect(def->x, def->y, &dsect);
1590 if (!(p = BestLandPath(buf, §, &dsect, &move_cost, MOB_ROAD)))
1593 mobcost = land.lnd_effic * 0.01 * lchr[(int)land.lnd_type].l_spd;
1596 mobcost = 480.0 / (mobcost + techfact(land.lnd_tech, mobcost));
1597 mobcost *= (move_cost * 5.0);
1599 if (land.lnd_mobil < mobcost)
1602 new_land += defense_val(&land);
1604 if (!list) /* we are in the "strength" command */
1607 /* move to defending sector */
1608 land.lnd_mobil -= ldround(mobcost, 1);
1611 land.lnd_x = def->x;
1612 land.lnd_y = def->y;
1613 putland(land.lnd_uid, &land);
1614 wu(0, land.lnd_own, "%s reacts to %s.\n",
1615 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1617 llp = (struct llist *)
1618 malloc(sizeof(struct llist));
1620 memset(llp, 0, sizeof(struct llist));
1621 llp->supplied = supply_state;
1624 llp->lcp = &lchr[(int)land.lnd_type];
1626 emp_insque(&llp->queue, list);
1627 if (lnd_spyval(&land) > *d_spyp)
1628 *d_spyp = lnd_spyval(&land);
1630 intelligence_report(player->cnum, &land, a_spy,
1631 "Scouts sight reacting enemy unit:");
1636 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1639 get_osupport(s_char *outs, struct combat *def, int fort_sup, int ship_sup,
1640 int land_sup, int plane_sup)
1642 double osupport = 1.0;
1644 double af, as, au, ap;
1646 af = as = au = ap = 0.0;
1648 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1649 af = ((double)dam / 100.0);
1653 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1655 as = ((double)dam / 100.0);
1660 dam = lnd_support(def->own, player->cnum, def->x, def->y);
1661 au = ((double)dam / 100.0);
1666 dam = off_support(def->x, def->y, def->own, player->cnum);
1667 ap = (((double)dam) / 100.0);
1670 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1675 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1678 get_dsupport(s_char *outs, struct emp_qelem *list, struct combat *def,
1679 int ototal, int dtotal)
1681 double dsupport = 1.0;
1683 double df, ds, du, dp;
1686 df = ds = du = dp = 0.0;
1687 if (dtotal < 0.1 * ototal) {
1689 } else if (dtotal >= 1.2 * ototal) {
1692 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1693 df = ((double)dam / 100.0);
1696 dtotal = get_dtotal(def, list, dsupport, 0);
1697 if (dtotal < 1.2 * ototal) {
1698 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1699 ds = ((double)dam / 100.0);
1701 dtotal = get_dtotal(def, list, dsupport, 0);
1703 if (dtotal < 1.2 * ototal) {
1704 dam = lnd_support(player->cnum, def->own, def->x, def->y);
1705 du = ((double)dam / 100.0);
1707 dtotal = get_dtotal(def, list, dsupport, 1);
1709 if (dtotal < 1.2 * ototal) {
1710 dam = def_support(def->x, def->y, player->cnum, def->own);
1711 dp = (((double)dam) / 100.0);
1718 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1723 "\nOdds are bad for us...support cancelled.\n\n");
1726 "\nOdds are good for us...support cancelled.\n\n");
1732 * Land mines add to the defense multiplier. If the attacker has engineers
1733 * then this multiplier is cut in half.
1737 get_mine_dsupport(struct combat *def, int a_engineer)
1742 getsect(def->x, def->y, §);
1744 if (sect.sct_oldown != player->cnum) {
1745 mines = min(sect.sct_mines, 20);
1747 mines = ldround(((double)mines / 2.0), 1);
1750 wu(0, def->own, "Defending mines add %1.2f\n",
1752 pr("Defending mines add %1.2f\n", mines * 0.02);
1753 return mines * 0.02;
1759 /* Get the offensive and defensive support */
1761 att_get_support(int combat_mode, int ofort, int oship, int oland,
1762 int oplane, struct emp_qelem *olist, struct combat *off,
1763 struct emp_qelem *dlist, struct combat *def,
1764 double *osupportp, double *dsupportp, int a_engineer)
1767 s_char osupports[512];
1768 s_char dsupports[512];
1770 if (combat_mode == A_PARA)
1773 *osupportp = get_osupport(osupports, def,
1774 ofort, oship, oland, oplane);
1777 * I need to put a 1 at the end of the next four total_stren calls
1778 * becauase units & mil may have been damaged by collateral damage or
1779 * neclear warheads from the offensive & defensive support.
1782 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1783 if (att_empty_attack(combat_mode, ototal, def))
1784 return abort_attack();
1785 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1788 * Calculate defensive support. If odds are too good or too bad
1789 * then don't call in support.
1792 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1793 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1794 if (att_empty_attack(combat_mode, ototal, def))
1795 return abort_attack();
1797 if ((*osupports || *dsupports) &&
1798 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1799 pr("\n\t\tsupport values\n");
1800 pr("\t\tforts\tships\tunits\tplanes\n");
1801 if (*osupportp != 1.0)
1802 pr("%s", osupports);
1803 if (*dsupportp != 1.0)
1804 pr("%s", dsupports);
1806 wu(0, def->own, "\n\t\tsupport values\n");
1807 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1808 if (*osupportp != 1.0)
1809 wu(0, def->own, "%s", osupports);
1810 if (*dsupportp != 1.0)
1811 wu(0, def->own, "%s", dsupports);
1815 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1816 if (dtotal && def->type == EF_SECTOR)
1817 *dsupportp += get_mine_dsupport(def, a_engineer);
1821 /* How many two-legged bipeds are in this combat force? */
1824 count_bodies(struct combat *off, struct emp_qelem *list)
1828 struct emp_qelem *qp;
1831 for (n = 0; n <= off->last; ++n)
1832 bodies += off[n].troops;
1833 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1834 llp = (struct llist *)qp;
1835 bodies += total_mil(&llp->land);
1840 /* This is where the fighting actually occurs. */
1843 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1844 double osupport, struct combat *def, struct emp_qelem *dlist,
1848 int a_cas = 0; /* Casualty counts */
1850 int ototal; /* total attacking strength */
1851 int dtotal; /* total defending strength */
1852 int a_bodies; /* total attacking mil (incl. mil in units) */
1853 int d_bodies; /* total defending mil (incl. mil in units) */
1862 ototal = get_ototal(combat_mode, off, olist, osupport,
1863 combat_mode != A_PARA);
1864 dtotal = get_dtotal(def, dlist, dsupport, 0);
1868 a_bodies = count_bodies(off, olist);
1869 d_bodies = count_bodies(def, dlist);
1870 d_mil = def->troops;
1871 for (n = 0; n <= off->last; ++n)
1872 if (off[n].type == EF_BAD)
1875 a_troops[n] = off[n].troops;
1877 /* This switch is required to get the spacing right */
1878 switch (combat_mode) {
1880 pr(" Final attack strength: %8d\n", ototal);
1883 pr(" Final assault strength: %8d\n", ototal);
1886 if (def->sct_type == SCT_MOUNT ||
1887 def->sct_type == SCT_WATER ||
1888 def->sct_type == SCT_CAPIT ||
1889 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1890 pr("You can't air-assault a %s sector!\n",
1891 def->sct_dcp->d_name);
1894 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1896 pr(" Final air-assault strength: %8d\n", ototal);
1900 pr(" Final board strength: %8d\n", ototal);
1904 pr(" Final defense strength: %8d\n", dtotal);
1905 odds = att_calcodds(ototal, dtotal);
1906 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1908 /* spread the plague */
1909 if (combat_mode != A_PARA) {
1911 for (n = 0; n <= off->last; ++n)
1912 if (off[n].type != EF_BAD)
1913 def->plague |= off[n].plague;
1914 for (n = 0; n <= off->last; ++n)
1915 if (off[n].type != EF_BAD)
1916 off[n].plague |= def->plague;
1918 att_infect_units(olist, off->plague);
1919 att_infect_units(dlist, def->plague);
1921 /* Fighting is slightly random. There is always that last little
1922 * effort you see people put in. Or the stray bullet that takes out
1923 * an officer and the rest go into chaos. Things like that.
1924 * Thus, we have added a very slight random factor that will sometimes
1925 * allow the little guy to win. We modify the odds a little
1926 * (either +- 5%) to account for this randomness. We also only
1927 * recalculate the odds every 8-50 casualties, not every cacsualty,
1928 * since a single dead guy normally wouldn't cause a commander to
1929 * rethink his strategies, but 50 dead guys might. */
1930 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1935 recalctime = 8 + (random() % 43);
1936 while (!success && ototal) {
1939 d_cas += take_casualty(A_DEFEND, def, dlist);
1940 dtotal = get_dtotal(def, dlist, dsupport, 0);
1945 a_cas += take_casualty(combat_mode, off, olist);
1946 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1948 if (((a_cas + d_cas) % 70) == 69)
1950 if (recalctime-- <= 0) {
1951 recalctime = 8 + (random() % 43);
1952 odds = att_calcodds(ototal, dtotal);
1953 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1961 /* update defense mobility & mil */
1965 if (def->type == EF_SECTOR && d_mil && d_cas) {
1968 /* Make sure we use a positive mobility here */
1969 tmob = ((def->mob < 0) ? -(def->mob) : (def->mob));
1971 min(20, min(1, tmob - damage(tmob, 100 * d_cas / d_mil)));
1973 def->mil = def->troops;
1976 /* update attack mobility & mil */
1977 for (n = 0; n <= off->last; ++n)
1978 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1979 if (off[n].type == EF_SECTOR && off[n].mil)
1983 off[n].mob - damage(off[n].mob,
1984 100 * (a_troops[n] -
1987 off[n].mil -= a_troops[n] - off[n].troops;
1990 /* update land unit mobility */
1991 if (d_bodies && d_cas)
1992 lnd_takemob(dlist, (double)d_cas / d_bodies);
1993 if (a_bodies && a_cas)
1994 lnd_takemob(olist, (double)a_cas / a_bodies);
1996 /* damage attacked sector */
1997 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1999 pr("- Casualties -\n Yours: %d\n", a_cas);
2000 pr(" Theirs: %d\n", d_cas);
2001 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
2002 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
2005 switch (combat_mode) {
2007 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
2008 pr("We have captured %s, sir!\n", prcom(0, def));
2012 news_item = def->own ? N_AWON_SECT : N_START_COL;
2013 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
2014 action = "assaulting and taking";
2017 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
2018 pr("We have captured %s, sir!\n", prcom(0, def));
2019 action = "air-assaulting and taking";
2022 news_item = N_BOARD_SHIP;
2023 pr("We have boarded %s, sir!\n", prcom(0, def));
2024 action = "boarding";
2027 news_item = N_BOARD_LAND;
2028 pr("We have boarded %s, sir!\n", prcom(0, def));
2029 action = "boarding";
2033 switch (combat_mode) {
2035 news_item = N_SCT_LOSE;
2036 pr("You have been defeated!\n");
2037 action = "attacking";
2040 news_item = N_ALOSE_SCT;
2041 pr("You have been defeated!\n");
2043 action = "trying to assault";
2046 news_item = N_PLOSE_SCT;
2047 pr("All of your troops were destroyed\n");
2048 action = "trying to air-assault";
2051 news_item = N_SHP_LOSE;
2052 pr("You have been repelled\n");
2054 action = "trying to board";
2057 news_item = N_LND_LOSE;
2058 pr("You have been repelled\n");
2060 action = "trying to board";
2064 nreport(player->cnum, news_item, def->own, 1);
2067 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2068 cname(player->cnum), player->cnum, a_cas,
2069 action, pr_com(0, def, def->own), d_cas);
2072 if (opt_SNEAK_ATTACK) {
2073 do_sneak(def, success);
2076 send_reacting_units_home(dlist);
2078 /* putland the defending land */
2081 /* putland the attacking land */
2084 /* put the victim sector/ship/land */
2085 if (!success || !take_def(combat_mode, olist, off, def))
2088 /* put the attacking sectors/ship */
2089 for (n = 0; n <= off->last; ++n)
2090 if (off[n].type != EF_BAD)
2091 put_combat(&off[n]);
2096 switch (combat_mode) {
2098 ask_move_in(off, olist, def);
2100 /* put sectors again to get abandon warnings */
2101 for (n = 0; n <= off->last; ++n)
2102 if (off[n].type != EF_BAD)
2103 put_combat(&off[n]);
2106 att_move_in_off(combat_mode, off, olist, def);
2109 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2113 /* What percentage of the combat forces going head-to-head are we? */
2116 att_calcodds(int ototal, int dtotal)
2120 /* calculate odds */
2123 else if (dtotal <= 0)
2126 odds = ((double)ototal) / (dtotal + ototal);
2131 /* Here's where the dead soldiers get dragged off the battlefield */
2134 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2136 int to_take = CASUALTY_LUMP;
2137 int biggest_troops = 0, index = -1;
2138 int n, tot_troops = 0, biggest_mil, cas;
2139 struct emp_qelem *qp, *biggest;
2142 for (n = 0; n <= off->last; ++n) {
2143 if (off[n].type != EF_BAD) {
2144 tot_troops += off[n].troops;
2145 if (off[n].troops > biggest_troops) {
2146 biggest_troops = off[n].troops;
2153 to_take -= tot_troops;
2156 for (n = 0; n <= off->last; ++n)
2157 if (off[n].type != EF_BAD)
2161 * They can all come off mil. We rotate the casualties,
2162 * starting with the sector containing the most mil.
2164 to_take = CASUALTY_LUMP;
2166 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2169 while (to_take > 0) {
2170 for (n = index; n <= off->last && to_take; ++n) {
2171 if (off[n].type != EF_BAD && off[n].troops > 0) {
2176 for (n = 0; n < index && to_take; ++n) {
2177 if (off[n].type != EF_BAD && off[n].troops > 0) {
2183 return CASUALTY_LUMP;
2187 return (CASUALTY_LUMP - to_take);
2190 * Need to take some casualties from attacking units
2191 * Procedure: find the biggest unit remaining (in
2192 * terms of mil) and give it the casualties.
2194 biggest = (struct emp_qelem *)0;
2196 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2197 llp = (struct llist *)qp;
2199 if (total_mil(&llp->land) > biggest_mil) {
2200 biggest_mil = total_mil(&llp->land);
2204 if (biggest == (struct emp_qelem *)0)
2205 return (CASUALTY_LUMP - to_take);
2207 llp = (struct llist *)biggest;
2208 cas = lnd_take_casualty(combat_mode, llp, to_take);
2209 return (CASUALTY_LUMP - (to_take - cas));
2212 /* Send reacting defense units back to where they came from (at no mob cost) */
2215 send_reacting_units_home(struct emp_qelem *list)
2217 struct emp_qelem *qp, *next;
2221 for (qp = list->q_forw; qp != list; qp = next) {
2223 llp = (struct llist *)qp;
2224 if ((llp->land.lnd_x != llp->x) || (llp->land.lnd_y != llp->y)) {
2225 sprintf(buf, "returns to %s",
2226 xyas(llp->x, llp->y, llp->land.lnd_own));
2227 llp->land.lnd_x = llp->x;
2228 llp->land.lnd_y = llp->y;
2229 lnd_delete(llp, buf);
2234 /* Check for 0 offense strength. This call will always preceed an abort */
2237 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2240 if (def->own && player->cnum != def->own) {
2242 "%s (#%d) considered %sing you @%s\n",
2243 cname(player->cnum), player->cnum,
2244 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2246 pr("No troops for %s...\n", att_mode[combat_mode]);
2253 * Take the defending sector or ship from the defender and give it to the
2258 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2263 struct llist *llp, *delete_me = 0;
2269 for (n = 0; n <= off->last && !occuppied; ++n) {
2270 if (off[n].type != EF_BAD &&
2271 off[n].troops > 0 &&
2272 (off[n].type != EF_SECTOR || off[n].mob)) {
2274 if (def->type == EF_LAND) {
2275 if (def->lnd_lcp->l_flags & L_SPY) {
2282 pr("1 mil from %s moves %s\n",
2283 prcom(0, off + n), prcom(2, def));
2288 pr("%s left unoccupied\n", prcom(0, def));
2291 "No enemy troops moved %s so you still own it!\n",
2292 pr_com(2, def, def->own));
2295 llp = (struct llist *)list->q_forw;
2296 llp->land.lnd_x = def->x;
2297 llp->land.lnd_y = def->y;
2298 take_move_in_mob(combat_mode, llp, off, def);
2299 if (def->type == EF_SHIP) {
2300 llp->land.lnd_ship = def->shp_uid;
2301 sprintf(buf, "boards %s", prcom(0, def));
2304 llp->land.lnd_ship = -1;
2305 sprintf(buf, "moves in to occupy %s",
2306 xyas(def->x, def->y, player->cnum));
2307 lnd_delete(llp, buf);
2312 if (def->type == EF_SECTOR) {
2313 getsect(def->x, def->y, §);
2314 takeover(§, player->cnum);
2315 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2316 caploss(§, def->own,
2317 "* We have captured %s's capital, sir! *\n");
2319 } else if (def->type == EF_SHIP) {
2320 getship(def->shp_uid, &ship);
2321 takeover_ship(&ship, player->cnum, 1);
2322 putship(ship.shp_uid, &ship);
2323 } else if (def->type == EF_LAND) {
2324 getland(def->lnd_uid, &land);
2325 takeover_land(&land, player->cnum, 1);
2326 putland(land.lnd_uid, &land);
2329 lnd_delete(delete_me, buf);
2330 att_get_combat(def, 0);
2335 * Ask the attacker which mil & land units they'd like to move into the
2340 ask_move_in(struct combat *off, struct emp_qelem *olist,
2344 struct emp_qelem *qp, *next;
2348 s_char land_answer[1024];
2351 for (n = 0; n <= off->last; ++n)
2352 if (off[n].type != EF_BAD && off[n].troops > 0)
2354 ask_move_in_off(&off[n], def);
2355 if (player->aborted)
2361 memset(land_answer, 0, sizeof(land_answer));
2362 for (qp = olist->q_forw; qp != olist; qp = next) {
2364 llp = (struct llist *)qp;
2365 answerp = &land_answer[(int)llp->land.lnd_army];
2366 if (player->aborted || att_get_combat(def, 0) < 0)
2368 if (*answerp == 'Y')
2370 if (*answerp != 'N') {
2371 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2373 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2375 llp->land.lnd_army == ' ' ? '~' : llp->land.lnd_army,
2376 llp->land.lnd_effic);
2377 *answerp = att_prompt(prompt, llp->land.lnd_army);
2378 if (player->aborted || att_get_combat(def, 0) < 0)
2380 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2383 if (*answerp == 'y' || *answerp == 'Y')
2385 sprintf(buf, "stays in %s",
2386 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2387 lnd_delete(llp, buf);
2391 if (att_get_combat(def, 0) < 0) {
2392 for (qp = olist->q_forw; qp != olist; qp = next) {
2394 llp = (struct llist *)qp;
2395 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2397 sprintf(buf, "stays in %s",
2398 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2399 lnd_delete(llp, buf);
2403 if (opt_INTERDICT_ATT)
2404 lnd_interdict(olist, def->x, def->y, player->cnum);
2405 move_in_land(A_ATTACK, off, olist, def);
2408 /* Move offensive land units to the conquered sector or ship */
2411 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2414 struct emp_qelem *qp, *next;
2420 for (qp = olist->q_forw; qp != olist; qp = next) {
2422 llp = (struct llist *)qp;
2423 if (!get_land(combat_mode, def, llp->land.lnd_uid, llp, 0))
2425 take_move_in_mob(combat_mode, llp, off, def);
2426 llp->land.lnd_x = def->x;
2427 llp->land.lnd_y = def->y;
2428 if (def->type == EF_SHIP)
2429 llp->land.lnd_ship = def->shp_uid;
2431 llp->land.lnd_ship = -1;
2435 if (def->type == EF_SECTOR) {
2436 if (opt_INTERDICT_ATT) {
2437 lnd_sweep(olist, 0, 0, def->own);
2438 lnd_check_mines(olist);
2440 sprintf(buf, "now occupies %s", prcom(0, def));
2442 sprintf(buf, "boards %s", prcom(0, def));
2446 for (qp = olist->q_forw; qp != olist; qp = next) {
2448 llp = (struct llist *)qp;
2449 lnd_print(llp, buf);
2457 * Move assaulting, paradropping, or boarding mil & units into def
2458 * If the mil are coming from a ship, then pack a lunch.
2462 att_move_in_off(int combat_mode, struct combat *off,
2463 struct emp_qelem *olist, struct combat *def)
2468 int n, lunchbox = 0;
2470 move_in_land(combat_mode, off, olist, def);
2472 for (n = 0; n <= off->last; ++n) {
2473 if (off[n].type == EF_BAD || !off[n].troops)
2475 troops = off[n].troops;
2477 off[n].mil -= troops;
2479 put_combat(off + n);
2480 if (combat_mode == A_ASSAULT) {
2481 if (off[n].type != EF_SHIP || def->type != EF_SECTOR) {
2482 logerror("att_move_in_off: strange assault");
2485 getship(off[n].shp_uid, &ship);
2486 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2487 / (ship.shp_item[I_MILIT] + troops
2488 + ship.shp_item[I_CIVIL] + 0.5));
2489 ship.shp_item[I_FOOD] -= lunchbox;
2490 putship(ship.shp_uid, &ship);
2496 if (def->type != EF_SECTOR) {
2497 pr("Please tell the deity that you got the 'hungry mole' error\n");
2498 logerror("att_move_in_off: hungry mole");
2501 getsect(def->x, def->y, §);
2502 sect.sct_item[I_FOOD] += lunchbox;
2507 /* Ask how many mil to move in from each sector */
2510 ask_move_in_off(struct combat *off, struct combat *def)
2513 int num_mil, dam = 0, left;
2519 if (att_get_combat(off, 0) <= 0)
2521 if (att_get_combat(def, 0) < 0)
2523 if (off->own != player->cnum)
2525 d = sector_mcost(getsectp(def->x, def->y), MOB_ROAD);
2526 if ((mob_support = min(off->troops, (int)(off->mob / d))) <= 0)
2528 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2529 xyas(off->x, off->y, player->cnum), mob_support);
2530 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0) {
2534 /* Make sure we don't move in more than we can support mobility-wise */
2535 if (num_mil > mob_support)
2536 num_mil = mob_support;
2537 if (att_get_combat(off, 0) <= 0)
2539 if (att_get_combat(def, 0) < 0)
2541 if ((num_mil = min(off->troops, num_mil)) <= 0) {
2542 pr("No mil moved in from %s\n",
2543 xyas(off->x, off->y, player->cnum));
2546 mob_support = max(1, (int)(num_mil * d));
2547 off->mob -= min(off->mob, mob_support);
2548 off->mil -= num_mil;
2549 off->troops -= num_mil;
2552 weight = num_mil * ichr[I_MILIT].i_lbs;
2553 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2554 if (chance(weight / 100.0))
2556 ground_interdict(def->x, def->y, player->cnum, "military");
2557 dam += check_lmines(def->x, def->y, weight);
2561 left = commdamage(num_mil, dam, V_MILIT);
2562 if (left < num_mil) {
2564 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));
2566 pr("All of the mil you were moving were destroyed!\n");
2569 /* maybe got nuked */
2570 if (att_get_combat(def, 0) < 0)
2578 /* Charge land units for moving into a sector or onto a ship */
2581 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off,
2587 switch (combat_mode) {
2590 lnd_mobcost(&llp->land, getsectp(def->x, def->y), MOB_NONE);
2591 new = llp->land.lnd_mobil - mobcost;
2594 llp->land.lnd_mobil = new;
2597 if (off->shp_mcp->m_flags & M_LAND) {
2598 if (llp->lcp->l_flags & L_MARINE)
2599 llp->land.lnd_mobil -=
2600 ((float)etu_per_update * land_mob_scale * 0.5);
2602 llp->land.lnd_mobil -= ((float)etu_per_update
2605 if (llp->lcp->l_flags & L_MARINE)
2606 llp->land.lnd_mobil = 0;
2608 llp->land.lnd_mobil = (((float)etu_per_update
2609 * land_mob_scale) * (-1));
2613 /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2614 if (llp->lcp->l_flags & L_MARINE)
2615 llp->land.lnd_mobil -= 10;
2617 llp->land.lnd_mobil -= 40;
2620 llp->land.lnd_harden = 0;
2624 free_list(struct emp_qelem *list)
2626 register struct emp_qelem *qp, *next;
2628 if (!list || QEMPTY(list))
2632 while (qp != list) {
2641 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2649 * sector_strength - Everyone starts at 1. You can get up to a max
2650 * of d_dstr, depending on how much you build up the
2651 * defenses of the sector.
2655 sector_strength(struct sctstr *sp)
2661 if (sp->sct_type == SCT_MOUNT)
2664 d = d + ((double)(dchr[sp->sct_type].d_dstr - d) *
2665 ((double)sp->sct_defense / 100.0));
2667 if (d > dchr[sp->sct_type].d_dstr)
2668 d = dchr[sp->sct_type].d_dstr;