2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * attsub.c: Attack subroutines
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
33 * Markus Armbruster, 2006-2008
50 #include "prototypes.h"
55 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
57 static void ask_olist(int combat_mode, struct combat *off,
58 struct combat *def, struct emp_qelem *olist,
59 char *land_answer, int *a_spyp, int *a_engineerp);
60 static void take_move_in_mob(int combat_mode, struct ulist *llp,
61 struct combat *off, struct combat *def);
62 static void move_in_land(int combat_mode, struct combat *off,
63 struct emp_qelem *olist, struct combat *def);
64 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
66 static void ask_move_in_off(struct combat *off, struct combat *def);
68 static int board_abort(struct combat *off, struct combat *def);
69 static int land_board_abort(struct combat *off, struct combat *def);
70 static int ask_off(int combat_mode, struct combat *off,
72 static void get_dlist(struct combat *def, struct emp_qelem *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 double att_calcodds(int, int);
79 static int take_casualty(int combat_mode, struct combat *off,
80 struct emp_qelem *olist);
82 static void send_reacting_units_home(struct emp_qelem *list);
83 static int take_def(int combat_mode, struct emp_qelem *list,
84 struct combat *off, struct combat *def);
86 static int get_land(int combat_mode, struct combat *def, int uid,
87 struct ulist *llp, int victim_land);
90 /* must match combat types in combat.h */
91 "defend", "attack", "assault", "paradrop", "board", "lboard"
96 * The principal object in this code is the "combat" object. A combat object
97 * is either a sector or ship. There are
98 * usually two instances of this, the "def" or defense combat object, and
99 * the array of "off" or offense objects. The number of offense objects is
100 * determined by the value of off->last (e.g. more than one attacking sector).
101 * the type of the object is determined by combat->type which can take the
102 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
103 * which is often passed to these functions is combat_mode. This can take
104 * the value A_DEFEND, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
105 * As these six modes of being in combat affect things like mobcost and combat
106 * value, there are often switches made on combat_mode. Note that in all cases
107 * no mobility is taken from sectors, ships, or land units until the player
108 * has committed to a fight. Instead, the cost is temporarily placed in
109 * combat->mobcost, or llp->mobil as the case may be, and then when the object
110 * is "put" back onto disk, then the amounts in these variables are subtracted
111 * from the object's mobility. It needs to be done this way as the objects
112 * are constantly being re-read from disk, and we don't want to take any mob
113 * unless a fight actually occurrs.
117 /* initialize combat object */
120 att_combat_init(struct combat *com, int type)
122 memset(com, 0, sizeof(*com));
127 /* print a combat object with optional preposition */
130 pr_com(int inon, struct combat *com, natid who)
132 if (com->type == EF_SECTOR) {
134 inon ? inon == 1 ? "in " : "into " : "",
135 xyas(com->x, com->y, who));
136 } else if (com->type == EF_SHIP) {
137 return prbuf("%s%s %s(#%d)",
138 inon ? inon == 1 ? "on " : "onto " : "",
139 com->shp_mcp->m_name, com->shp_name,
141 } else if (com->type == EF_LAND) {
142 return prbuf("%s%s #%d",
143 inon ? inon == 1 ? "on " : "onto " : "",
144 com->lnd_lcp->l_name, com->lnd_uid);
146 return "your forces";
151 prcom(int inon, struct combat *com)
153 return pr_com(inon, com, player->cnum);
157 * This is the combat object "type" based integrity check. It basically
158 * splits along three divisions: ship/sector, attacker/defender,
159 * first time/not first time.
163 att_get_combat(struct combat *com, int isdef)
177 if (!getsect(com->x, com->y, §)) {
178 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
179 return att_combat_init(com, EF_BAD);
181 com->sct_type = sect.sct_type;
182 com->sct_dcp = &dchr[sect.sct_type];
183 mil = sect.sct_item[I_MILIT];
184 pstage = sect.sct_pstage;
185 owner = sect.sct_own;
186 eff = sect.sct_effic;
187 mob = sect.sct_mobil;
192 if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
194 pr("Land unit #%d is not in the same sector!\n",
196 return att_combat_init(com, EF_BAD);
198 if (isdef && player->owner) {
199 pr("Boarding yourself? Try using the 'load' command.\n");
200 return att_combat_init(com, EF_BAD);
202 com->lnd_lcp = &lchr[(int)land.lnd_type];
203 mil = land.lnd_item[I_MILIT];
204 pstage = land.lnd_pstage;
205 owner = land.lnd_own;
206 eff = land.lnd_effic;
207 mob = land.lnd_mobil;
212 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
214 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
216 pr("Ship #%d is not your ship!\n", com->shp_uid);
217 return att_combat_init(com, EF_BAD);
220 if (isdef && player->owner &&
221 ontradingblock(EF_SHIP, &ship)) {
222 pr("%s is on the trading block.\n", prcom(0, com));
223 return att_combat_init(com, EF_BAD);
226 if (isdef && player->owner) {
227 pr("Boarding yourself? Try using the 'tend' command.\n");
228 return att_combat_init(com, EF_BAD);
230 com->shp_mcp = &mchr[(int)ship.shp_type];
231 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
232 if (!isdef && !player->owner) {
234 pr("%s was just sunk!\n", prcom(0, com));
236 pr("Ship #%d is not your ship!\n", com->shp_uid);
237 return att_combat_init(com, EF_BAD);
239 mil = ship.shp_item[I_MILIT];
240 pstage = ship.shp_pstage;
241 owner = ship.shp_own;
242 eff = ship.shp_effic;
243 mob = ship.shp_mobil;
252 return att_combat_init(com, EF_BAD);
255 if (!com->set) { /* first time */
256 if (isdef) { /* defender */
258 } else { /* attacker */
260 pr("No mil %s\n", prcom(1, com));
262 pr("Only 1 mil %s\n", prcom(1, com));
263 /* don't abandon attacking sectors or ships */
264 com->troops = MAX(0, mil - 1);
266 com->plague = pstage == PLG_INFECT;
267 } else { /* not first time */
268 if (isdef) { /* defender */
269 if (com->x != x || com->y != y) {
270 pr("%s has moved!\n", prcom(0, com));
271 return att_combat_init(com, EF_BAD);
273 if (owner != com->own) {
275 pr("WARNING: The ownership of %s just changed from %s to %s!\n",
276 prcom(0, com), cname(com->own), cname(owner));
277 } else if (com->type == EF_SECTOR) {
278 pr("WARNING: %s just abandoned sector %s!\n",
280 xyas(com->x, com->y, player->cnum));
284 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
286 com->mil < mil ? "increased" : "decreased", com->mil,
289 } else { /* attacker */
290 if (owner != player->cnum && getrel(getnatp(owner), player->cnum) != ALLIED) {
291 /* must be EF_SECTOR */
293 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
294 com->mil, xyas(com->x, com->y, player->cnum),
297 pr("You no longer own %s\n",
298 xyas(com->x, com->y, player->cnum));
299 return att_combat_init(com, EF_BAD);
301 if (com->troops && com->troops + 1 > mil) {
302 if (com->own == owner && player->cnum == owner)
304 pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
305 prcom(1, com), com->troops, MAX(0, mil - 1));
306 com->troops = MAX(0, mil - 1);
321 * In the course of the fight, the combat object may have lost mil, eff, or
322 * mobility. This is the place where the data in the object gets flushed to
323 * disk to make it "real".
327 put_combat(struct combat *com)
336 getsect(com->x, com->y, §);
337 sect.sct_type = com->sct_type;
338 deff = sect.sct_effic - com->eff;
340 sect.sct_road -= sect.sct_road * deff / 100.0;
341 sect.sct_rail -= sect.sct_rail * deff / 100.0;
342 sect.sct_defense -= sect.sct_defense * deff / 100.0;
343 if (sect.sct_road <= 0)
345 if (sect.sct_rail <= 0)
347 if (sect.sct_defense <= 0)
348 sect.sct_defense = 0;
350 sect.sct_effic = com->eff;
352 if (opt_MOB_ACCESS) {
353 if ((com->mob - com->mobcost) < -127)
354 sect.sct_mobil = -127;
356 sect.sct_mobil = (short)(com->mob - com->mobcost);
358 if ((com->mob - com->mobcost) < 0)
361 sect.sct_mobil = (short)(com->mob - com->mobcost);
364 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
365 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
366 sect.sct_own = com->own;
368 if (sect.sct_pstage == PLG_HEALTHY)
369 sect.sct_pstage = PLG_EXPOSED;
371 sect.sct_item[I_MILIT] = com->mil;
373 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
376 getland(com->lnd_uid, &land);
377 land.lnd_effic = com->eff;
379 if (com->mob - com->mobcost < -127)
380 land.lnd_mobil = -127;
382 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
384 makelost(EF_LAND, land.lnd_own, land.lnd_uid,
385 land.lnd_x, land.lnd_y);
386 land.lnd_own = com->own;
387 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid,
388 land.lnd_x, land.lnd_y);
390 if (land.lnd_pstage == PLG_HEALTHY)
391 land.lnd_pstage = PLG_EXPOSED;
393 if (!(com->lnd_lcp->l_flags & L_SPY))
394 land.lnd_item[I_MILIT] = com->mil;
395 lnd_count_units(&land);
396 if (com->own == player->cnum) {
397 land.lnd_mission = 0;
399 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
401 putland(com->lnd_uid, &land);
404 getship(com->shp_uid, &ship);
405 ship.shp_effic = com->eff;
407 if (com->mob - com->mobcost < -127)
408 ship.shp_mobil = -127;
410 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
412 makelost(EF_SHIP, ship.shp_own, ship.shp_uid,
413 ship.shp_x, ship.shp_y);
414 ship.shp_own = com->own;
415 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid,
416 ship.shp_x, ship.shp_y);
418 if (ship.shp_pstage == PLG_HEALTHY)
419 ship.shp_pstage = PLG_EXPOSED;
421 ship.shp_item[I_MILIT] = com->mil;
423 if (com->own == player->cnum) {
424 ship.shp_mission = 0;
426 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
428 putship(com->shp_uid, &ship);
431 att_get_combat(com, com->own != player->cnum);
434 /* If pre-attack, abort fight. If post-attack, don't move anything in */
439 return player->aborted = 1;
443 * This is the combat_mode based integrity check. It splits among two main
444 * divisions: first time/not first time, and attack/assault/para/board.
448 att_abort(int combat_mode, struct combat *off, struct combat *def)
457 if (att_get_combat(def, 1) < 0)
458 return abort_attack();
460 if (off && combat_mode != A_ATTACK) {
461 if (att_get_combat(off, 0) < 0)
462 return abort_attack();
463 if (off->type == EF_SHIP &&
464 (!getsect(off->x, off->y, §) ||
465 sect.sct_type != SCT_WATER)) {
466 pr("%s can not %s from that far inland!\n",
467 prcom(0, off), att_mode[combat_mode]);
468 return abort_attack();
471 switch (combat_mode) {
473 if (!neigh(def->x, def->y, player->cnum) &&
474 !adj_units(def->x, def->y, player->cnum)) {
475 pr("You are not adjacent to %s\n",
476 xyas(def->x, def->y, player->cnum));
477 return abort_attack();
479 if (def->own == player->cnum) {
480 pr("You can't attack your own sector.\n");
481 return abort_attack();
485 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
486 pr("You'll have to get there first...\n");
487 return abort_attack();
489 if (off && def->sct_type == SCT_MOUNT) {
490 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
491 return abort_attack();
495 if (def->own == player->cnum) {
496 pr("You can't air-assault your own sector.\n");
497 return abort_attack();
499 if (off && (def->sct_type == SCT_MOUNT ||
500 def->sct_type == SCT_WATER ||
501 def->sct_type == SCT_CAPIT ||
502 def->sct_type == SCT_FORTR ||
503 def->sct_type == SCT_WASTE)) {
504 pr("You can't air-assault a %s sector!\n",
505 def->sct_dcp->d_name);
506 return abort_attack();
510 return board_abort(off, def);
512 return land_board_abort(off, def);
515 if (off && def->sct_dcp->d_mob0 < 0) {
516 pr("You can't %s a %s sector!\n",
517 att_mode[combat_mode], def->sct_dcp->d_name);
518 return abort_attack();
520 if (!off || off->relations_checked)
522 off->relations_checked = 1;
525 setcont(player->cnum, def->own, FOUND_SPY);
526 setcont(def->own, player->cnum, FOUND_SPY);
528 if (opt_SLOW_WAR && def->own != player->cnum) {
529 natp = getnatp(player->cnum);
530 rel = getrel(natp, def->own);
533 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
534 cname(def->own), att_mode[combat_mode]);
535 if (!confirm(y_or_n))
536 return abort_attack();
539 if ((rel != AT_WAR) && (def->own) &&
540 (sect.sct_oldown != player->cnum)) {
541 pr("You're not at war with them!\n");
542 return abort_attack();
549 * Lots of special things need to be checked for boarding, so I put it in
554 board_abort(struct combat *off, struct combat *def)
556 struct shpstr aship, dship; /* for tech levels */
559 if (att_get_combat(def, 1) < 0)
560 return abort_attack();
565 if (att_get_combat(off, 0) < 0)
566 return abort_attack();
568 if (off->x != def->x || off->y != def->y) {
569 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
570 return abort_attack();
572 if (off->type == EF_SHIP) {
574 pr("%s has no mobility!\n", prcom(0, off));
575 return abort_attack();
577 getship(off->shp_uid, &aship);
578 getship(def->shp_uid, &dship);
579 if (techfact(aship.shp_tech, shp_speed(&aship)) * off->eff
580 <= techfact(dship.shp_tech, shp_speed(&dship)) * def->eff) {
581 pr("Victim ship moves faster than you do!\n");
584 "%s (#%d) %s failed to catch %s\n",
585 cname(aship.shp_own), aship.shp_own,
586 pr_com(0, off, def->own), pr_com(0, def, def->own));
587 return abort_attack();
589 } else if (off->type != EF_SECTOR) {
590 pr("Please tell the deity that you got the 'banana boat' error\n");
591 return abort_attack();
593 if (def->shp_mcp->m_flags & M_SUB) {
594 getsect(def->x, def->y, §);
595 if (sect.sct_type == SCT_WATER) {
596 pr("You can't board a submarine!\n");
597 return abort_attack();
604 * Lots of special things need to be checked for boarding, so I put it in
606 * STM - I copied it for land unit boarding. :)
610 land_board_abort(struct combat *off, struct combat *def)
612 if (att_get_combat(def, 1) < 0)
613 return abort_attack();
618 if (att_get_combat(off, 0) < 0)
619 return abort_attack();
621 if (off->x != def->x || off->y != def->y) {
622 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
623 return abort_attack();
629 /* If we are boarding, then the defending ship gets a chance to fire back */
631 att_approach(struct combat *off, struct combat *def)
637 pr("Approaching %s...\n", prcom(0, def));
640 "%s is being approached by %s...\n",
641 pr_com(0, def, def->own), pr_com(0, off, def->own));
642 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
645 pr("They're firing at us sir!\n");
648 "Your fleet at %s does %d damage to %s\n",
649 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
651 if (off->type == EF_SECTOR) {
652 getsect(off->x, off->y, §);
653 sectdamage(§, dam);
655 pr("Enemy fleet at %s does %d damage to %s\n",
656 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
657 } else if (off->type == EF_SHIP) {
658 getship(off->shp_uid, &ship);
659 shipdamage(&ship, dam);
660 putship(off->shp_uid, &ship);
661 if (def->own && ship.shp_effic < SHIP_MINEFF) {
662 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
663 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
666 if (att_get_combat(off, 0) < 0)
667 return abort_attack();
671 /* The attack is valid. Tell the attacker about what they're going to hit */
674 att_show(struct combat *def)
676 /* Note that we tell the player about the treaty BEFORE we tell them
677 about the item. If we didn't, then they gain free information */
678 if (def->type == EF_SECTOR) {
679 if (!trechk(player->cnum, def->own, LANATT))
680 return abort_attack();
681 pr("%s is a %d%% %s %s with approximately %d military.\n",
682 xyas(def->x, def->y, player->cnum),
683 roundintby((int)def->eff, 10),
684 cname(def->own), def->sct_dcp->d_name,
685 roundintby(def->troops, 10));
686 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
687 writemap(player->cnum);
688 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
689 if (def->type == EF_SHIP) {
690 if (!trechk(player->cnum, def->own, SEAATT))
691 return abort_attack();
693 if (!trechk(player->cnum, def->own, LNDATT))
694 return abort_attack();
696 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));
698 /* Ok, everything is fine */
702 /* Attack and assault ask the user which kind of support they want */
705 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
710 *fortp = *shipp = *landp = *planep = 1;
712 if (player->argp[offset] != NULL) {
713 if ((player->argp[offset + 1] == NULL) ||
714 (player->argp[offset + 2] == NULL) ||
715 (player->argp[offset + 3] == NULL)) {
716 pr("If any support arguments are used, all must be!\n");
721 *landp = *planep = 0;
723 if (!(p = getstarg(player->argp[offset], "Use fort support? ",
727 if ((*p == 'y') || (*p == 'Y'))
730 if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
734 if ((*p == 'y') || (*p == 'Y'))
737 if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
741 if ((*p == 'y') || (*p == 'Y'))
744 if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
748 if ((*p == 'y') || (*p == 'Y'))
755 * Attack, assault, and board ask the attacker what they'd like to attack
756 * with. This includes mil and land units from each "off" object. Note that
757 * after each sub-prompt, we check to make sure that the attack is still
758 * valid, and if it's not, then we abort the attack.
762 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
763 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
766 char land_answer[256];
769 if (att_abort(combat_mode, off, def))
771 memset(land_answer, 0, sizeof(land_answer));
772 for (n = 0; n <= off->last; ++n) {
773 off[n].troops = ask_off(combat_mode, off + n, def);
774 if (att_abort(combat_mode, off, def))
776 ask_olist(combat_mode, off + n, def, olist, land_answer,
777 a_spyp, a_engineerp);
778 if (att_abort(combat_mode, off, def))
785 * Return path cost for ATTACKER to enter sector given by DEF.
786 * MOBTYPE is a mobility type accepted by sector_mcost().
789 att_mobcost(natid attacker, struct combat *def, int mobtype)
794 if (CANT_HAPPEN(def->type != EF_SECTOR))
796 ok = getsect(def->x, def->y, §);
797 if (CANT_HAPPEN(!ok))
801 * We want the cost to move/march into the sector. If we just
802 * called sector_mcost(), we'd get the defender's cost. The
803 * attacker's cost is higher unless he's the old-owner. Note: if
804 * there are no civilians, a victorious attacker will become the
805 * old-owner. But he isn't now.
807 sect.sct_own = attacker;
809 return sector_mcost(§, mobtype);
812 /* How many mil is off allowed to attack with when it attacks def? */
815 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
820 switch (combat_mode) {
822 mobcost = att_mobcost(off->own, def, MOB_MOVE);
823 if (mobcost < 0 || off->mob <= 0)
825 mob_support = off->mob / mobcost;
826 if (mob_support < off->troops)
827 pr("Sector %s has %d mobility which can only support %d mil,\n",
828 xyas(off->x, off->y, player->cnum), off->mob, mob_support);
830 mob_support = off->troops;
833 if (def->own != player->cnum && def->mil) {
834 if (off->shp_mcp->m_flags & M_SEMILAND)
835 return off->troops / 4;
836 else if (!(off->shp_mcp->m_flags & M_LAND))
837 return off->troops / 10;
841 if (off->type == EF_SECTOR && off->mob <= 0)
843 mob_support = def->shp_mcp->m_item[I_MILIT];
844 if (mob_support < off->troops)
845 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
847 mob_support = off->troops;
852 if (def->lnd_lcp->l_flags & L_SPY)
854 mob_support = def->lnd_lcp->l_item[I_MILIT];
855 if (mob_support < off->troops)
856 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
858 mob_support = off->troops;
865 * If the attacker decides to go through with the attack, then the
866 * sectors/ships they are attacking with may be charged some mobility.
867 * This is where that amount of mobility is calculated. It is actually
868 * subtracted "for real" from the object's mobility in put_combat().
872 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
879 switch (combat_mode) {
881 off->mobcost += MAX(1,
883 * att_mobcost(off->own, def, MOB_MOVE)));
886 off->mobcost += MAX(1, attacking_mil / 5);
891 off->mobcost += MAX(1, attacking_mil / 5);
894 /* the 2 in the formula below is a fudge factor */
895 getship(def->shp_uid, &ship);
896 off->mobcost += (def->eff / 100) * (shp_speed(&ship) / 2);
901 /* How many mil to we want to attack from off against def? */
904 ask_off(int combat_mode, struct combat *off, struct combat *def)
910 if (att_get_combat(off, 0) <= 0)
912 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
914 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
916 if (off->type == EF_SECTOR) {
917 if (off->own != player->cnum)
919 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
920 off->sct_dcp->d_name,
921 xyas(off->x, off->y, player->cnum), mob_support);
923 sprintf(prompt, "Number of mil from %s (max %d) : ",
924 prcom(0, off), mob_support);
926 if ((attacking_mil = onearg(0, prompt)) < 0)
928 if (att_abort(combat_mode, off, def))
930 if (att_get_combat(off, 0) <= 0)
933 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
936 calc_mobcost(combat_mode, off, def, attacking_mil);
937 return attacking_mil;
941 * Which units would you like to attack with or move in with [ynYNq?]
945 att_prompt(char *prompt, char army)
953 p = getstring(prompt, buf);
954 if (!p || *p == 'q') {
960 if (tolower(*p) == 'y' || tolower(*p) == 'n')
962 pr("y - yes this unit\n"
964 "Y - yes to all units in army '%c'\n"
965 "N - no to all units in army '%c'\n"
966 "q - quit\n? - this help message\n\n",
971 /* Ask the attacker which units they want to attack/assault/board with */
974 ask_olist(int combat_mode, struct combat *off, struct combat *def,
975 struct emp_qelem *olist, char *land_answer, int *a_spyp,
989 if (def->type == EF_LAND)
991 if (def->type == EF_SHIP)
992 maxland = def->shp_mcp->m_nland;
994 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
995 while (nxtitem(&ni, &land)) {
996 if (land.lnd_own != player->cnum)
998 if (land.lnd_effic < LAND_MINEFF)
1000 if (land_answer[(int)land.lnd_army] == 'N')
1002 if (!lnd_can_attack(&land))
1004 lcp = &lchr[(int)land.lnd_type];
1006 if (def->type == EF_SHIP && !maxland) {
1007 pr("Land units are not able to board this kind of ship\n");
1010 if (def->type == EF_SHIP
1011 && (def->shp_mcp->m_flags & (M_SUPPLY | M_SUB)) != M_SUPPLY
1012 && !(lcp->l_flags & L_LIGHT)) {
1013 pr("Only light land units can board this kind of ship\n");
1016 if (land.lnd_mobil <= 0) {
1017 pr("%s is out of mobility, and cannot %s\n",
1018 prland(&land), att_mode[combat_mode]);
1023 if (ontradingblock(EF_LAND, &land)) {
1024 pr("%s is on the trading block, and cannot %s\n",
1025 prland(&land), att_mode[combat_mode]);
1030 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1031 pr("%s is on ship #%d, and cannot %s\n",
1032 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1034 } else if (off->type == EF_SHIP) {
1035 if (land.lnd_ship != off->shp_uid)
1037 } else if (land.lnd_land >= 0) {
1038 pr("%s is on unit #%d, and cannot %s\n",
1039 prland(&land), land.lnd_land, att_mode[combat_mode]);
1042 switch (combat_mode) {
1045 * We used to let land units attack only if they have the
1046 * mobility consumed by the attack, not counting combat
1047 * and moving in to occupy. Making sure your land units
1048 * reach attack positions with enough mobility left is a
1049 * pain in the neck. We now require positive mobility,
1050 * just like for marching. Except we don't allow rushing
1051 * of high-mobility sectors (mountains): for those we
1052 * still require attack mobility.
1054 mobcost = att_mobcost(off->own, def, lnd_mobtype(&land));
1055 if (mobcost < 1.0) {
1056 if (land.lnd_mobil <= 0) {
1057 pr("%s is out of mobility\n", prland(&land));
1061 mobcost = lnd_pathcost(&land, mobcost);
1062 if (land.lnd_mobil < mobcost) {
1063 pr("%s does not have enough mobility (%d needed)\n",
1064 prland(&land), (int)ceil(mobcost));
1072 if (!(lcp->l_flags & L_ASSAULT))
1079 att_val = attack_val(combat_mode, &land);
1080 if (att_val < 1.0) {
1081 pr("%s has no offensive strength\n", prland(&land));
1084 resupply_all(&land);
1085 putland(land.lnd_uid, &land);
1086 if (!has_supply(&land)) {
1087 pr("%s is out of supply, and cannot %s\n",
1088 prland(&land), att_mode[combat_mode]);
1091 if (def->type == EF_SHIP && first_time) {
1093 pr("You may board with a maximum of %d land units\n", maxland);
1095 pr("%s has a base %s value of %.0f\n",
1096 prland(&land), att_mode[combat_mode], att_val);
1097 if (land_answer[(int)land.lnd_army] != 'Y') {
1099 "%s with %s %s (%c %d%%) [ynYNq?] ",
1100 att_mode[combat_mode],
1103 land.lnd_army ? land.lnd_army : '~',
1105 land_answer[(int)land.lnd_army] =
1106 att_prompt(prompt, land.lnd_army);
1107 if (att_abort(combat_mode, off, def))
1109 if (land_answer[(int)land.lnd_army] != 'y' &&
1110 land_answer[(int)land.lnd_army] != 'Y')
1113 if (!(llp = malloc(sizeof(struct ulist)))) {
1114 logerror("Malloc failed in attack!\n");
1118 memset(llp, 0, sizeof(struct ulist));
1119 emp_insque(&llp->queue, olist);
1120 llp->mobil = mobcost;
1121 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1123 if (lnd_spyval(&land) > *a_spyp)
1124 *a_spyp = lnd_spyval(&land);
1125 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
1127 if (def->type == EF_SHIP && ++count >= maxland)
1132 /* What's the offense or defense multiplier? */
1135 att_combat_eff(struct combat *com)
1141 if (com->type == EF_SECTOR) {
1142 eff = com->eff / 100.0;
1143 if (com->own == player->cnum) {
1144 str = com->sct_dcp->d_ostr;
1145 eff = 1.0 + (str - 1.0) * eff;
1147 eff = sector_strength(getsectp(com->x, com->y));
1148 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1149 getship(com->shp_uid, &ship);
1150 eff = 1.0 + shp_armor(&ship) / 100.0;
1156 att_get_offense(int combat_mode, struct combat *off,
1157 struct emp_qelem *olist, struct combat *def)
1162 * Get the attacker units & mil again in case they changed while the
1163 * attacker was answering sub-prompts.
1166 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1167 if (att_empty_attack(combat_mode, ototal, def))
1168 return abort_attack();
1169 if (combat_mode == A_PARA)
1171 pr("\n Initial attack strength: %8d\n", ototal);
1175 /* Get the defensive units and reacting units */
1177 att_get_defense(struct emp_qelem *olist, struct combat *def,
1178 struct emp_qelem *dlist, int a_spy, int ototal)
1181 struct emp_qelem *qp;
1187 get_dlist(def, dlist, 0, &d_spy);
1188 dtotal = get_dtotal(def, dlist, 1.0, 0);
1191 * Call in reacting units
1194 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1195 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1197 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1198 llp = (struct ulist *)qp;
1199 intelligence_report(def->own, &llp->unit.land, d_spy,
1200 "Scouts report attacking unit:");
1203 old_dtotal = dtotal;
1204 dtotal = get_dtotal(def, dlist, 1.0, 0);
1205 if (dtotal != old_dtotal)
1206 pr("Defense strength with reacting units: %8d\n", dtotal);
1211 /* Get the defensive land units in the sector or on the ship */
1214 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1217 struct nstr_item ni;
1221 /* In here is where you need to take out spies and trains from the defending
1222 lists. Spies try to hide, trains get trapped and can be boarded. */
1224 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1225 while (nxtitem(&ni, &land)) {
1228 if (land.lnd_own != def->own)
1230 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1232 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1234 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1236 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1238 intelligence_report(player->cnum, &land, a_spy,
1239 "Scouts report defending unit:");
1240 if (!(llp = malloc(sizeof(struct ulist)))) {
1241 logerror("Malloc failed in attack!\n");
1245 memset(llp, 0, sizeof(struct ulist));
1246 emp_insque(&llp->queue, list);
1247 llp->supplied = has_supply(&land);
1248 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1250 if (lnd_spyval(&land) > *d_spyp)
1251 *d_spyp = lnd_spyval(&land);
1255 /* Calculate the total offensive strength */
1258 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1259 double osupport, int check)
1261 double ototal = 0.0;
1262 struct emp_qelem *qp, *next;
1267 * first, total the attacking mil
1270 for (n = 0; n <= off->last; ++n) {
1271 if (off[n].type == EF_BAD || (check &&
1272 att_get_combat(&off[n], 0) <= 0))
1274 ototal += off[n].troops * att_combat_eff(off + n);
1278 * next, add in the attack_values of all
1279 * the attacking units
1282 for (qp = olist->q_forw; qp != olist; qp = next) {
1284 llp = (struct ulist *)qp;
1285 if (check && !get_land(combat_mode, 0, llp->unit.land.lnd_uid, llp, 0))
1287 if (combat_mode == A_ATTACK) {
1289 for (n = 0; n <= off->last; ++n) {
1290 if (off[n].type == EF_BAD)
1292 if ((off[n].x == llp->unit.land.lnd_x) &&
1293 (off[n].y == llp->unit.land.lnd_y))
1297 lnd_delete(llp, "is in a sector not owned by you");
1300 ototal += attack_val(combat_mode, &llp->unit.land) *
1301 att_combat_eff(off + w);
1303 ototal += attack_val(combat_mode, &llp->unit.land);
1308 return ldround(ototal, 1);
1311 /* Calculate the total defensive strength */
1314 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1317 double dtotal = 0.0, eff = 1.0, d_unit;
1318 struct emp_qelem *qp, *next;
1321 if (check && att_get_combat(def, 1) < 0)
1323 eff = att_combat_eff(def);
1324 dtotal = def->troops * eff;
1327 * next, add in the defense_values of all
1328 * the defending non-retreating units
1331 for (qp = list->q_forw; qp != list; qp = next) {
1333 llp = (struct ulist *)qp;
1334 if (check && !get_land(A_DEFEND, def, llp->unit.land.lnd_uid, llp, 1))
1336 d_unit = defense_val(&llp->unit.land);
1339 dtotal += d_unit * eff;
1344 return ldround(dtotal, 1);
1348 * This is the land unit integrity check. Note that we don't print
1349 * warnings about victim land units because the attacker may not have seen them
1353 get_land(int combat_mode, struct combat *def, int uid, struct ulist *llp,
1356 struct lndstr *lp = &llp->unit.land;
1361 if (!llp->chrp) { /* first time */
1362 llp->x = llp->unit.land.lnd_x;
1363 llp->y = llp->unit.land.lnd_y;
1364 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1365 } else { /* not first time */
1366 if (lp->lnd_effic < LAND_MINEFF) {
1367 sprintf(buf, "was destroyed and is no longer a part of the %s",
1368 att_mode[combat_mode]);
1369 lnd_delete(llp, buf);
1373 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1375 "left to go fight another battle and is no longer a part of the defense");
1379 if (lp->lnd_own != player->cnum) {
1381 "was destroyed and is no longer a part of the %s",
1382 att_mode[combat_mode]);
1383 lnd_delete(llp, buf);
1386 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1388 "left to fight another battle and is no longer a part of the %s",
1389 att_mode[combat_mode]);
1390 lnd_delete(llp, buf);
1393 if (lp->lnd_effic < llp->eff) {
1394 sprintf(buf, "damaged from %d%% to %d%%",
1395 llp->eff, lp->lnd_effic);
1396 lnd_print(llp, buf);
1400 llp->eff = llp->unit.land.lnd_effic;
1406 * Put the land unit on the disk. If there was some mobility cost, then
1407 * subtract it from the units mobility. Note that this works the same way
1408 * as sectors & ships in that no mobility is actually taken until the attacker
1409 * has committed to attacking.
1413 kill_land(struct emp_qelem *list)
1415 struct emp_qelem *qp, *next;
1418 for (qp = list->q_forw; qp != list; qp = next) {
1420 llp = (struct ulist *)qp;
1421 if (llp->unit.land.lnd_ship >= 0) {
1422 llp->unit.land.lnd_effic = 0;
1423 lnd_delete(llp, "cannot return to the ship, and dies!");
1429 att_infect_units(struct emp_qelem *list, int plague)
1431 struct emp_qelem *qp, *next;
1436 for (qp = list->q_forw; qp != list; qp = next) {
1438 llp = (struct ulist *)qp;
1439 if (llp->unit.land.lnd_pstage == PLG_HEALTHY)
1440 llp->unit.land.lnd_pstage = PLG_EXPOSED;
1445 put_land(struct emp_qelem *list)
1447 struct emp_qelem *qp, *next;
1450 for (qp = list->q_forw; qp != list; qp = next) {
1452 llp = (struct ulist *)qp;
1453 llp->unit.land.lnd_mission = 0;
1454 llp->unit.land.lnd_harden = 0;
1455 llp->unit.land.lnd_mobil -= (int)llp->mobil;
1457 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1458 if (llp->unit.land.lnd_own != player->cnum) {
1459 emp_remque((struct emp_qelem *)llp);
1462 get_land(A_ATTACK, 0, llp->unit.land.lnd_uid, llp, 0);
1467 * Keep sending in reinforcements until it looks like we're going to win.
1468 * Note that the "strength" command also calls this routine.
1472 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1473 int *d_spyp, int ototal)
1475 struct nstr_item ni;
1477 struct sctstr sect, dsect;
1480 double new_land = 0;
1486 double eff = att_combat_eff(def);
1490 dtotal = get_dtotal(def, list, 1.0, 1);
1493 snxtitem_all(&ni, EF_LAND);
1494 while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1497 if (!land.lnd_rad_max)
1499 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1501 if (land.lnd_own != def->own)
1503 if (land.lnd_ship >= 0)
1505 if (land.lnd_land >= 0)
1507 if (defense_val(&land) < 1.0)
1509 if (!lnd_can_attack(&land))
1512 /* Only supplied units can react */
1513 if (!has_supply(&land))
1516 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1518 getsect(land.lnd_x, land.lnd_y, §);
1519 /* Units on efficient headquarters can react 1 farther */
1520 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1521 radius = land.lnd_rad_max + 1;
1523 radius = land.lnd_rad_max;
1525 if (land.lnd_mission == MI_RESERVE)
1531 getsect(def->x, def->y, &dsect);
1532 if (!BestLandPath(buf, §, &dsect, &pathcost,
1533 lnd_mobtype(&land)))
1536 mobcost = lnd_pathcost(&land, pathcost);
1537 if (land.lnd_mobil < mobcost)
1540 new_land += defense_val(&land);
1542 if (!list) /* we are in the "strength" command */
1545 /* move to defending sector */
1546 land.lnd_mobil -= ldround(mobcost, 1);
1549 land.lnd_x = def->x;
1550 land.lnd_y = def->y;
1551 putland(land.lnd_uid, &land);
1552 wu(0, land.lnd_own, "%s reacts to %s.\n",
1553 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1555 llp = malloc(sizeof(struct ulist));
1557 memset(llp, 0, sizeof(struct ulist));
1561 llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
1562 llp->unit.land = land;
1563 emp_insque(&llp->queue, list);
1564 if (lnd_spyval(&land) > *d_spyp)
1565 *d_spyp = lnd_spyval(&land);
1567 intelligence_report(player->cnum, &land, a_spy,
1568 "Scouts sight reacting enemy unit:");
1573 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1576 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1577 int land_sup, int plane_sup)
1579 double osupport = 1.0;
1581 double af, as, au, ap;
1583 af = as = au = ap = 0.0;
1585 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1590 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1597 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1603 dam = off_support(def->x, def->y, def->own, player->cnum);
1607 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1612 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1615 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1616 int ototal, int dtotal)
1618 double dsupport = 1.0;
1620 double df, ds, du, dp;
1623 df = ds = du = dp = 0.0;
1624 if (dtotal < 0.1 * ototal) {
1626 } else if (dtotal >= 1.2 * ototal) {
1629 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1633 dtotal = get_dtotal(def, list, dsupport, 0);
1634 if (dtotal < 1.2 * ototal) {
1635 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1638 dtotal = get_dtotal(def, list, dsupport, 0);
1640 if (dtotal < 1.2 * ototal) {
1641 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1644 dtotal = get_dtotal(def, list, dsupport, 1);
1646 if (dtotal < 1.2 * ototal) {
1647 dam = def_support(def->x, def->y, player->cnum, def->own);
1655 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1660 "\nOdds are bad for us...support cancelled.\n\n");
1663 "\nOdds are good for us...support cancelled.\n\n");
1669 * Land mines add to the defense multiplier. If the attacker has engineers
1670 * then this multiplier is cut in half.
1674 get_mine_dsupport(struct combat *def, int a_engineer)
1679 getsect(def->x, def->y, §);
1681 if (sect.sct_oldown != player->cnum) {
1682 mines = MIN(sect.sct_mines, 20);
1684 mines = ldround(mines / 2.0, 1);
1687 wu(0, def->own, "Defending mines add %1.2f\n",
1689 pr("Defending mines add %1.2f\n", mines * 0.02);
1690 return mines * 0.02;
1696 /* Get the offensive and defensive support */
1698 att_get_support(int combat_mode, int ofort, int oship, int oland,
1699 int oplane, struct emp_qelem *olist, struct combat *off,
1700 struct emp_qelem *dlist, struct combat *def,
1701 double *osupportp, double *dsupportp, int a_engineer)
1704 char osupports[512];
1705 char dsupports[512];
1707 if (combat_mode == A_PARA)
1710 *osupportp = get_osupport(osupports, def,
1711 ofort, oship, oland, oplane);
1714 * I need to put a 1 at the end of the next four total_stren calls
1715 * because units & mil may have been damaged by collateral damage or
1716 * nuclear warheads from the offensive & defensive support.
1719 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1720 if (att_empty_attack(combat_mode, ototal, def))
1721 return abort_attack();
1722 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1725 * Calculate defensive support. If odds are too good or too bad
1726 * then don't call in support.
1729 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1730 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1731 if (att_empty_attack(combat_mode, ototal, def))
1732 return abort_attack();
1734 if ((*osupports || *dsupports) &&
1735 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1736 pr("\n\t\tsupport values\n");
1737 pr("\t\tforts\tships\tunits\tplanes\n");
1738 if (*osupportp != 1.0)
1739 pr("%s", osupports);
1740 if (*dsupportp != 1.0)
1741 pr("%s", dsupports);
1743 wu(0, def->own, "\n\t\tsupport values\n");
1744 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1745 if (*osupportp != 1.0)
1746 wu(0, def->own, "%s", osupports);
1747 if (*dsupportp != 1.0)
1748 wu(0, def->own, "%s", dsupports);
1752 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1753 if (dtotal && def->type == EF_SECTOR)
1754 *dsupportp += get_mine_dsupport(def, a_engineer);
1758 /* How many two-legged bipeds are in this combat force? */
1761 count_bodies(struct combat *off, struct emp_qelem *list)
1765 struct emp_qelem *qp;
1768 for (n = 0; n <= off->last; ++n)
1769 bodies += off[n].troops;
1770 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1771 llp = (struct ulist *)qp;
1772 bodies += llp->unit.land.lnd_item[I_MILIT];
1777 /* This is where the fighting actually occurs. */
1780 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1781 double osupport, struct combat *def, struct emp_qelem *dlist,
1785 int a_cas = 0; /* Casualty counts */
1787 int ototal; /* total attacking strength */
1788 int dtotal; /* total defending strength */
1789 int a_bodies; /* total attacking mil (incl. mil in units) */
1790 int d_bodies; /* total defending mil (incl. mil in units) */
1800 ototal = get_ototal(combat_mode, off, olist, osupport,
1801 combat_mode != A_PARA);
1802 dtotal = get_dtotal(def, dlist, dsupport, 0);
1806 a_bodies = count_bodies(off, olist);
1807 d_bodies = count_bodies(def, dlist);
1808 d_mil = def->troops;
1809 for (n = 0; n <= off->last; ++n)
1810 if (off[n].type == EF_BAD)
1813 a_troops[n] = off[n].troops;
1815 /* This switch is required to get the spacing right */
1816 switch (combat_mode) {
1818 pr(" Final attack strength: %8d\n", ototal);
1821 pr(" Final assault strength: %8d\n", ototal);
1824 if (def->sct_type == SCT_MOUNT ||
1825 def->sct_type == SCT_WATER ||
1826 def->sct_type == SCT_CAPIT ||
1827 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1828 pr("You can't air-assault a %s sector!\n",
1829 def->sct_dcp->d_name);
1832 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1834 pr(" Final air-assault strength: %8d\n", ototal);
1838 pr(" Final board strength: %8d\n", ototal);
1842 pr(" Final defense strength: %8d\n", dtotal);
1843 odds = att_calcodds(ototal, dtotal);
1844 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1846 /* spread the plague */
1847 if (combat_mode != A_PARA) {
1849 for (n = 0; n <= off->last; ++n)
1850 if (off[n].type != EF_BAD)
1851 def->plague |= off[n].plague;
1852 for (n = 0; n <= off->last; ++n)
1853 if (off[n].type != EF_BAD)
1854 off[n].plague |= def->plague;
1856 att_infect_units(olist, off->plague);
1857 att_infect_units(dlist, def->plague);
1860 * Fighting is slightly random. There is always that last little
1861 * effort you see people put in. Or the stray bullet that takes out
1862 * an officer and the rest go into chaos. Things like that.
1863 * Thus, we have added a very slight random factor that will sometimes
1864 * allow the little guy to win. We modify the odds a little
1865 * (either +- 5%) to account for this randomness. We also only
1866 * recalculate the odds every 8-50 casualties, not every cacsualty,
1867 * since a single dead guy normally wouldn't cause a commander to
1868 * rethink his strategies, but 50 dead guys might.
1870 odds += (random() % 11 - 5) / 100.0;
1875 recalctime = 8 + (random() % 43);
1876 while (!success && ototal) {
1879 d_cas += take_casualty(A_DEFEND, def, dlist);
1880 dtotal = get_dtotal(def, dlist, dsupport, 0);
1885 a_cas += take_casualty(combat_mode, off, olist);
1886 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1888 if (((a_cas + d_cas) % 70) == 69)
1890 if (recalctime-- <= 0) {
1891 recalctime = 8 + (random() % 43);
1892 odds = att_calcodds(ototal, dtotal);
1893 odds += (random() % 11 - 5) / 100.0;
1901 /* update defense mobility & mil */
1905 if (def->type == EF_SECTOR && d_mil && d_cas) {
1909 newmob = damage(def->mob, 100 * d_cas / d_mil);
1910 def->mobcost = MIN(20, def->mob - newmob);
1913 def->mil = def->troops;
1916 /* update attack mobility & mil */
1917 for (n = 0; n <= off->last; ++n)
1918 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1919 if (off[n].type == EF_SECTOR && off[n].mil) {
1920 if (!CANT_HAPPEN(off[n].mob < 0)) {
1921 newmob = damage(off[n].mob,
1922 100 * (a_troops[n] - off[n].troops)
1924 off[n].mobcost += MIN(20, off[n].mob - newmob);
1927 off[n].mil -= a_troops[n] - off[n].troops;
1930 /* update land unit mobility */
1931 if (d_bodies && d_cas)
1932 lnd_takemob(dlist, (double)d_cas / d_bodies);
1933 if (a_bodies && a_cas)
1934 lnd_takemob(olist, (double)a_cas / a_bodies);
1936 /* damage attacked sector */
1937 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1939 pr("- Casualties -\n Yours: %d\n", a_cas);
1940 pr(" Theirs: %d\n", d_cas);
1941 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1942 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1945 switch (combat_mode) {
1947 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1948 pr("We have captured %s, sir!\n", prcom(0, def));
1952 news_item = def->own ? N_AWON_SECT : N_START_COL;
1953 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1954 action = "assaulting and taking";
1957 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1958 pr("We have captured %s, sir!\n", prcom(0, def));
1959 action = "air-assaulting and taking";
1962 news_item = N_BOARD_SHIP;
1963 pr("We have boarded %s, sir!\n", prcom(0, def));
1964 action = "boarding";
1967 news_item = N_BOARD_LAND;
1968 pr("We have boarded %s, sir!\n", prcom(0, def));
1969 action = "boarding";
1974 action = "defeating";
1977 switch (combat_mode) {
1979 news_item = N_SCT_LOSE;
1980 pr("You have been defeated!\n");
1981 action = "attacking";
1984 news_item = N_ALOSE_SCT;
1985 pr("You have been defeated!\n");
1987 action = "trying to assault";
1990 news_item = N_PLOSE_SCT;
1991 pr("All of your troops were destroyed\n");
1992 action = "trying to air-assault";
1995 news_item = N_SHP_LOSE;
1996 pr("You have been repelled\n");
1998 action = "trying to board";
2001 news_item = N_LND_LOSE;
2002 pr("You have been repelled\n");
2004 action = "trying to board";
2009 action = "fighting";
2012 nreport(player->cnum, news_item, def->own, 1);
2015 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2016 cname(player->cnum), player->cnum, a_cas,
2017 action, pr_com(0, def, def->own), d_cas);
2020 send_reacting_units_home(dlist);
2022 /* putland the defending land */
2025 /* putland the attacking land */
2028 /* put the victim sector/ship/land */
2029 if (!success || !take_def(combat_mode, olist, off, def))
2032 /* put the attacking sectors/ship */
2033 for (n = 0; n <= off->last; ++n)
2034 if (off[n].type != EF_BAD)
2035 put_combat(&off[n]);
2040 switch (combat_mode) {
2042 ask_move_in(off, olist, def);
2044 /* put sectors again to get abandon warnings */
2045 for (n = 0; n <= off->last; ++n)
2046 if (off[n].type != EF_BAD)
2047 put_combat(&off[n]);
2050 att_move_in_off(combat_mode, off, olist, def);
2053 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2057 /* What percentage of the combat forces going head-to-head are we? */
2060 att_calcodds(int ototal, int dtotal)
2064 /* calculate odds */
2067 else if (dtotal <= 0)
2070 odds = (double)ototal / (dtotal + ototal);
2075 /* Here's where the dead soldiers get dragged off the battlefield */
2078 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2080 int to_take = CASUALTY_LUMP;
2081 int biggest_troops = 0, index = -1;
2082 int n, tot_troops = 0, biggest_mil, cas;
2083 struct emp_qelem *qp, *biggest;
2086 for (n = 0; n <= off->last; ++n) {
2087 if (off[n].type != EF_BAD) {
2088 tot_troops += off[n].troops;
2089 if (off[n].troops > biggest_troops) {
2090 biggest_troops = off[n].troops;
2097 to_take -= tot_troops;
2100 for (n = 0; n <= off->last; ++n)
2101 if (off[n].type != EF_BAD)
2105 * They can all come off mil. We rotate the casualties,
2106 * starting with the sector containing the most mil.
2108 to_take = CASUALTY_LUMP;
2110 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2113 while (to_take > 0) {
2114 for (n = index; n <= off->last && to_take; ++n) {
2115 if (off[n].type != EF_BAD && off[n].troops > 0) {
2120 for (n = 0; n < index && to_take; ++n) {
2121 if (off[n].type != EF_BAD && off[n].troops > 0) {
2127 return CASUALTY_LUMP;
2131 return CASUALTY_LUMP - to_take;
2134 * Need to take some casualties from attacking units
2135 * Procedure: find the biggest unit remaining (in
2136 * terms of mil) and give it the casualties.
2140 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2141 llp = (struct ulist *)qp;
2143 if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2144 biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2148 if (biggest == NULL)
2149 return CASUALTY_LUMP - to_take;
2151 llp = (struct ulist *)biggest;
2152 cas = lnd_take_casualty(combat_mode, llp, to_take);
2153 return CASUALTY_LUMP - (to_take - cas);
2156 /* Send reacting defense units back to where they came from (at no mob cost) */
2159 send_reacting_units_home(struct emp_qelem *list)
2161 struct emp_qelem *qp, *next;
2165 for (qp = list->q_forw; qp != list; qp = next) {
2167 llp = (struct ulist *)qp;
2168 if ((llp->unit.land.lnd_x != llp->x) ||
2169 (llp->unit.land.lnd_y != llp->y)) {
2170 sprintf(buf, "returns to %s",
2171 xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2172 llp->unit.land.lnd_x = llp->x;
2173 llp->unit.land.lnd_y = llp->y;
2174 lnd_delete(llp, buf);
2179 /* Check for 0 offense strength. This call will always preceed an abort */
2182 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2185 if (def->own && player->cnum != def->own) {
2187 "%s (#%d) considered %sing you @%s\n",
2188 cname(player->cnum), player->cnum,
2189 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2191 pr("No troops for %s...\n", att_mode[combat_mode]);
2198 * Take the defending sector or ship from the defender and give it to the
2203 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2208 struct ulist *llp, *delete_me = 0;
2214 for (n = 0; n <= off->last && !occuppied; ++n) {
2215 if (off[n].type != EF_BAD &&
2216 off[n].troops > 0 &&
2217 (off[n].type != EF_SECTOR || off[n].mob)) {
2219 if (def->type == EF_LAND) {
2220 if (def->lnd_lcp->l_flags & L_SPY) {
2227 pr("1 mil from %s moves %s\n",
2228 prcom(0, off + n), prcom(2, def));
2233 pr("%s left unoccupied\n", prcom(0, def));
2236 "No enemy troops moved %s so you still own it!\n",
2237 pr_com(2, def, def->own));
2240 llp = (struct ulist *)list->q_forw;
2241 llp->unit.land.lnd_x = def->x;
2242 llp->unit.land.lnd_y = def->y;
2243 take_move_in_mob(combat_mode, llp, off, def);
2244 if (def->type == EF_SHIP) {
2245 llp->unit.land.lnd_ship = def->shp_uid;
2246 sprintf(buf, "boards %s", prcom(0, def));
2249 llp->unit.land.lnd_ship = -1;
2250 sprintf(buf, "moves in to occupy %s",
2251 xyas(def->x, def->y, player->cnum));
2252 lnd_delete(llp, buf);
2257 if (def->type == EF_SECTOR) {
2258 getsect(def->x, def->y, §);
2259 takeover(§, player->cnum);
2260 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2261 caploss(§, def->own,
2262 "* We have captured %s's capital, sir! *\n");
2264 } else if (def->type == EF_SHIP) {
2265 getship(def->shp_uid, &ship);
2266 takeover_ship(&ship, player->cnum, 1);
2267 putship(ship.shp_uid, &ship);
2268 } else if (def->type == EF_LAND) {
2269 getland(def->lnd_uid, &land);
2270 takeover_land(&land, player->cnum, 1);
2271 putland(land.lnd_uid, &land);
2274 lnd_delete(delete_me, buf);
2275 att_get_combat(def, 0);
2280 * Ask the attacker which mil & land units they'd like to move into the
2285 ask_move_in(struct combat *off, struct emp_qelem *olist,
2289 struct emp_qelem *qp, *next;
2293 char land_answer[256];
2296 for (n = 0; n <= off->last; ++n)
2297 if (off[n].type != EF_BAD && off[n].troops > 0)
2299 ask_move_in_off(&off[n], def);
2300 if (player->aborted)
2306 memset(land_answer, 0, sizeof(land_answer));
2307 for (qp = olist->q_forw; qp != olist; qp = next) {
2309 llp = (struct ulist *)qp;
2310 answerp = &land_answer[(int)llp->unit.land.lnd_army];
2311 if (player->aborted || att_get_combat(def, 0) < 0)
2313 if (*answerp == 'Y')
2315 if (*answerp != 'N') {
2316 if (!get_land(A_ATTACK, def, llp->unit.land.lnd_uid, llp, 0))
2318 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2319 prland(&llp->unit.land),
2320 llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2321 llp->unit.land.lnd_effic);
2322 *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2323 if (player->aborted || att_get_combat(def, 0) < 0)
2325 if (!get_land(A_ATTACK, def, llp->unit.land.lnd_uid, llp, 0))
2328 if (*answerp == 'y' || *answerp == 'Y')
2330 sprintf(buf, "stays in %s",
2331 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2333 lnd_delete(llp, buf);
2337 if (att_get_combat(def, 0) < 0) {
2338 for (qp = olist->q_forw; qp != olist; qp = next) {
2340 llp = (struct ulist *)qp;
2341 if (!get_land(A_ATTACK, def, llp->unit.land.lnd_uid, llp, 0))
2343 sprintf(buf, "stays in %s",
2344 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2346 lnd_delete(llp, buf);
2350 if (opt_INTERDICT_ATT)
2351 lnd_interdict(olist, def->x, def->y, player->cnum);
2352 move_in_land(A_ATTACK, off, olist, def);
2355 /* Move offensive land units to the conquered sector or ship */
2358 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2361 struct emp_qelem *qp, *next;
2367 for (qp = olist->q_forw; qp != olist; qp = next) {
2369 llp = (struct ulist *)qp;
2370 if (!get_land(combat_mode, def, llp->unit.land.lnd_uid, llp, 0))
2372 take_move_in_mob(combat_mode, llp, off, def);
2373 llp->unit.land.lnd_x = def->x;
2374 llp->unit.land.lnd_y = def->y;
2375 if (def->type == EF_SHIP)
2376 llp->unit.land.lnd_ship = def->shp_uid;
2378 llp->unit.land.lnd_ship = -1;
2382 if (def->type == EF_SECTOR) {
2383 if (opt_INTERDICT_ATT) {
2384 lnd_sweep(olist, 0, 0, def->own);
2385 lnd_check_mines(olist);
2387 sprintf(buf, "now occupies %s", prcom(0, def));
2389 sprintf(buf, "boards %s", prcom(0, def));
2393 for (qp = olist->q_forw; qp != olist; qp = next) {
2395 llp = (struct ulist *)qp;
2396 lnd_print(llp, buf);
2404 * Move assaulting, paradropping, or boarding mil & units into def
2405 * If the mil are coming from a ship, then pack a lunch.
2409 att_move_in_off(int combat_mode, struct combat *off,
2410 struct emp_qelem *olist, struct combat *def)
2417 move_in_land(combat_mode, off, olist, def);
2419 for (n = 0; n <= off->last; ++n) {
2420 if (off[n].type == EF_BAD || !off[n].troops)
2422 troops = off[n].troops;
2424 off[n].mil -= troops;
2426 put_combat(off + n);
2427 if (combat_mode == A_ASSAULT) {
2428 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2430 getship(off[n].shp_uid, &ship);
2431 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2432 / (ship.shp_item[I_MILIT] + troops
2433 + ship.shp_item[I_CIVIL] + 0.5));
2435 ship.shp_item[I_FOOD] -= lunchbox;
2436 putship(ship.shp_uid, &ship);
2442 if (combat_mode == A_ASSAULT) {
2443 if (CANT_HAPPEN(def->type != EF_SECTOR))
2445 getsect(def->x, def->y, §);
2446 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2447 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2448 sect.sct_item[I_FOOD] += lunchbox;
2454 /* Ask how many mil to move in from each sector */
2457 ask_move_in_off(struct combat *off, struct combat *def)
2460 int num_mil, dam = 0, left;
2466 if (att_get_combat(off, 0) <= 0)
2468 if (att_get_combat(def, 0) < 0)
2470 if (off->own != player->cnum)
2472 d = att_mobcost(off->own, def, MOB_MOVE);
2473 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2475 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2476 xyas(off->x, off->y, player->cnum), mob_support);
2477 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0)
2479 /* Make sure we don't move in more than we can support mobility-wise */
2480 if (num_mil > mob_support)
2481 num_mil = mob_support;
2482 if (att_get_combat(off, 0) <= 0)
2484 if (att_get_combat(def, 0) < 0)
2486 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2487 pr("No mil moved in from %s\n",
2488 xyas(off->x, off->y, player->cnum));
2491 mob_support = MAX(1, (int)(num_mil * d));
2492 off->mob -= MIN(off->mob, mob_support);
2493 off->mil -= num_mil;
2494 off->troops -= num_mil;
2497 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2498 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2499 if (chance(weight / 100.0))
2501 ground_interdict(def->x, def->y, player->cnum, "military");
2502 dam += check_lmines(def->x, def->y, weight);
2506 left = commdamage(num_mil, dam, I_MILIT);
2507 if (left < num_mil) {
2509 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));
2511 pr("All of the mil you were moving were destroyed!\n");
2514 /* maybe got nuked */
2515 if (att_get_combat(def, 0) < 0)
2523 /* Charge land units for moving into a sector or onto a ship */
2526 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2529 double mob = llp->unit.land.lnd_mobil;
2530 double gain = etu_per_update * land_mob_scale;
2531 double mobcost, moblim;
2533 switch (combat_mode) {
2535 mobcost = lnd_pathcost(&llp->unit.land,
2536 att_mobcost(off->own, def,
2537 lnd_mobtype(&llp->unit.land)));
2541 * Set mobcost to basic assault cost, moblim to maximum
2542 * mobility to keep when assaulting from non-landing ship
2544 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE) {
2545 mobcost = gain / 2.0;
2551 if (!(off->shp_mcp->m_flags & M_LAND))
2552 /* Not a landing ship, ensure we go to or below moblim */
2553 mobcost = MAX(mobcost, mob - moblim);
2556 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
2569 llp->unit.land.lnd_mobil = (signed char)mob;
2570 llp->unit.land.lnd_harden = 0;
2574 free_list(struct emp_qelem *list)
2576 struct emp_qelem *qp, *next;
2578 if (!list || QEMPTY(list))
2582 while (qp != list) {
2591 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2599 * sector_strength - Everyone starts at 1. You can get up to a max
2600 * of d_dstr, depending on how much you build up the
2601 * defenses of the sector.
2605 sector_strength(struct sctstr *sp)
2607 double def = SCT_DEFENSE(sp) / 100.0;
2608 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2609 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2611 if (d > dchr[sp->sct_type].d_dstr)
2612 d = dchr[sp->sct_type].d_dstr;