2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * attsub.c: Attack subroutines
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
55 #include "prototypes.h"
57 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
59 static void ask_olist(int combat_mode, struct combat *off,
60 struct combat *def, struct emp_qelem *olist,
61 char *land_answer, int *a_spyp, int *a_engineerp);
62 static void take_move_in_mob(int combat_mode, struct llist *llp,
63 struct combat *off, struct combat *def);
64 static void move_in_land(int combat_mode, struct combat *off,
65 struct emp_qelem *olist, struct combat *def);
66 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
68 static void ask_move_in_off(struct combat *off, struct combat *def);
70 static int board_abort(struct combat *off, struct combat *def);
71 static int land_board_abort(struct combat *off, struct combat *def);
72 static int ask_off(int combat_mode, struct combat *off,
74 static void get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
76 static int get_ototal(int combat_mode, struct combat *off,
77 struct emp_qelem *olist, double osupport, int check);
78 static int get_dtotal(struct combat *def, struct emp_qelem *list,
79 double dsupport, int check);
80 static double att_calcodds(int, int);
81 static int take_casualty(int combat_mode, struct combat *off,
82 struct emp_qelem *olist);
84 static void send_reacting_units_home(struct emp_qelem *list);
85 static int take_def(int combat_mode, struct emp_qelem *list,
86 struct combat *off, struct combat *def);
88 static int get_land(int combat_mode, struct combat *def, int uid,
89 struct llist *llp, int victim_land);
92 /* must match combat types in combat.h */
93 "defend", "attack", "assault", "paradrop", "board", "lboard"
98 * The principal object in this code is the "combat" object. A combat object
99 * is either a sector or ship. There are
100 * usually two instances of this, the "def" or defense combat object, and
101 * the array of "off" or offense objects. The number of offense objects is
102 * determined by the value of off->last (e.g. more than one attacking sector).
103 * the type of the object is determined by combat->type which can take the
104 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
105 * which is often passed to these functions is combat_mode. This can take
106 * the value A_DEFEND, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
107 * As these six modes of being in combat affect things like mobcost and combat
108 * value, there are often switches made on combat_mode. Note that in all cases
109 * no mobility is taken from sectors, ships, or land units until the player
110 * has committed to a fight. Instead, the cost is temporarily placed in
111 * combat->mobcost, or llp->mobil as the case may be, and then when the object
112 * is "put" back onto disk, then the amounts in these variables are subtracted
113 * from the object's mobility. It needs to be done this way as the objects
114 * are constantly being re-read from disk, and we don't want to take any mob
115 * unless a fight actually occurrs.
119 /* initialize combat object */
122 att_combat_init(struct combat *com, int type)
124 memset(com, 0, sizeof(*com));
129 /* print a combat object with optional preposition */
132 pr_com(int inon, struct combat *com, natid who)
134 if (com->type == EF_SECTOR) {
136 inon ? inon == 1 ? "in " : "into " : "",
137 xyas(com->x, com->y, who));
138 } else if (com->type == EF_SHIP) {
139 return prbuf("%s%s %s(#%d)",
140 inon ? inon == 1 ? "on " : "onto " : "",
141 com->shp_mcp->m_name, com->shp_name,
143 } else if (com->type == EF_LAND) {
144 return prbuf("%s%s #%d",
145 inon ? inon == 1 ? "on " : "onto " : "",
146 com->lnd_lcp->l_name, com->lnd_uid);
148 return "your forces";
153 prcom(int inon, struct combat *com)
155 return pr_com(inon, com, player->cnum);
159 * This is the combat object "type" based integrity check. It basically
160 * splits along three divisions: ship/sector, attacker/defender,
161 * first time/not first time.
165 att_get_combat(struct combat *com, int isdef)
179 if (!getsect(com->x, com->y, §)) {
180 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
181 return att_combat_init(com, EF_BAD);
183 com->sct_type = sect.sct_type;
184 com->sct_dcp = &dchr[sect.sct_type];
185 mil = sect.sct_item[I_MILIT];
186 pstage = sect.sct_pstage;
187 owner = sect.sct_own;
188 eff = sect.sct_effic;
189 mob = sect.sct_mobil;
194 if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
196 pr("Land unit #%d is not in the same sector!\n",
198 return att_combat_init(com, EF_BAD);
200 if (isdef && player->owner) {
201 pr("Boarding yourself? Try using the 'load' command.\n");
202 return att_combat_init(com, EF_BAD);
204 com->lnd_lcp = &lchr[(int)land.lnd_type];
205 mil = land.lnd_item[I_MILIT];
206 pstage = land.lnd_pstage;
207 owner = land.lnd_own;
208 eff = land.lnd_effic;
209 mob = land.lnd_mobil;
214 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
216 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
218 pr("Ship #%d is not your ship!\n", com->shp_uid);
219 return att_combat_init(com, EF_BAD);
222 if (isdef && player->owner &&
223 ontradingblock(EF_SHIP, &ship)) {
224 pr("%s is on the trading block.\n", prcom(0, com));
225 return att_combat_init(com, EF_BAD);
228 if (isdef && player->owner) {
229 pr("Boarding yourself? Try using the 'tend' command.\n");
230 return att_combat_init(com, EF_BAD);
232 com->shp_mcp = &mchr[(int)ship.shp_type];
233 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
234 if (!isdef && !player->owner) {
236 pr("%s was just sunk!\n", prcom(0, com));
238 pr("Ship #%d is not your ship!\n", com->shp_uid);
239 return att_combat_init(com, EF_BAD);
241 mil = ship.shp_item[I_MILIT];
242 pstage = ship.shp_pstage;
243 owner = ship.shp_own;
244 eff = ship.shp_effic;
245 mob = ship.shp_mobil;
254 return att_combat_init(com, EF_BAD);
257 if (!com->set) { /* first time */
258 if (isdef) { /* defender */
260 } else { /* attacker */
262 pr("No mil %s\n", prcom(1, com));
264 pr("Only 1 mil %s\n", prcom(1, com));
265 /* don't abandon attacking sectors or ships */
266 com->troops = MAX(0, mil - 1);
268 com->plague = pstage == PLG_INFECT;
269 } else { /* not first time */
270 if (isdef) { /* defender */
271 if (com->x != x || com->y != y) {
272 pr("%s has moved!\n", prcom(0, com));
273 return att_combat_init(com, EF_BAD);
275 if (owner != com->own) {
277 pr("WARNING: The ownership of %s just changed from %s to %s!\n",
278 prcom(0, com), cname(com->own), cname(owner));
279 } else if (com->type == EF_SECTOR) {
280 pr("WARNING: %s just abandoned sector %s!\n",
282 xyas(com->x, com->y, player->cnum));
286 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
288 com->mil < mil ? "increased" : "decreased", com->mil,
291 } else { /* attacker */
292 if (owner != player->cnum && getrel(getnatp(owner), player->cnum) != ALLIED) {
293 /* must be EF_SECTOR */
295 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
296 com->mil, xyas(com->x, com->y, player->cnum),
299 pr("You no longer own %s\n",
300 xyas(com->x, com->y, player->cnum));
301 return att_combat_init(com, EF_BAD);
303 if (com->troops && com->troops + 1 > mil) {
304 if (com->own == owner && player->cnum == owner)
306 pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
307 prcom(1, com), com->troops, MAX(0, mil - 1));
308 com->troops = MAX(0, mil - 1);
323 * In the course of the fight, the combat object may have lost mil, eff, or
324 * mobility. This is the place where the data in the object gets flushed to
325 * disk to make it "real".
329 put_combat(struct combat *com)
338 getsect(com->x, com->y, §);
339 sect.sct_type = com->sct_type;
340 deff = sect.sct_effic - com->eff;
342 sect.sct_road -= sect.sct_road * deff / 100.0;
343 sect.sct_rail -= sect.sct_rail * deff / 100.0;
344 sect.sct_defense -= sect.sct_defense * deff / 100.0;
345 if (sect.sct_road <= 0)
347 if (sect.sct_rail <= 0)
349 if (sect.sct_defense <= 0)
350 sect.sct_defense = 0;
352 sect.sct_effic = com->eff;
354 if (opt_MOB_ACCESS) {
355 if ((com->mob - com->mobcost) < -127)
356 sect.sct_mobil = -127;
358 sect.sct_mobil = (short)(com->mob - com->mobcost);
360 if ((com->mob - com->mobcost) < 0)
363 sect.sct_mobil = (short)(com->mob - com->mobcost);
366 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
367 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
368 sect.sct_own = com->own;
370 if (sect.sct_pstage == PLG_HEALTHY)
371 sect.sct_pstage = PLG_EXPOSED;
373 sect.sct_item[I_MILIT] = com->mil;
375 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
378 getland(com->lnd_uid, &land);
379 land.lnd_effic = com->eff;
381 if (com->mob - com->mobcost < -127)
382 land.lnd_mobil = -127;
384 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
386 makelost(EF_LAND, land.lnd_own, land.lnd_uid,
387 land.lnd_x, land.lnd_y);
388 land.lnd_own = com->own;
389 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid,
390 land.lnd_x, land.lnd_y);
392 if (land.lnd_pstage == PLG_HEALTHY)
393 land.lnd_pstage = PLG_EXPOSED;
395 if (!(com->lnd_lcp->l_flags & L_SPY))
396 land.lnd_item[I_MILIT] = com->mil;
397 lnd_count_units(&land);
398 if (com->own == player->cnum) {
399 land.lnd_mission = 0;
401 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
403 putland(com->lnd_uid, &land);
406 getship(com->shp_uid, &ship);
407 ship.shp_effic = com->eff;
409 if (com->mob - com->mobcost < -127)
410 ship.shp_mobil = -127;
412 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
414 makelost(EF_SHIP, ship.shp_own, ship.shp_uid,
415 ship.shp_x, ship.shp_y);
416 ship.shp_own = com->own;
417 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid,
418 ship.shp_x, ship.shp_y);
420 if (ship.shp_pstage == PLG_HEALTHY)
421 ship.shp_pstage = PLG_EXPOSED;
423 ship.shp_item[I_MILIT] = com->mil;
425 if (com->own == player->cnum) {
426 ship.shp_mission = 0;
428 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
430 putship(com->shp_uid, &ship);
433 att_get_combat(com, com->own != player->cnum);
436 /* If pre-attack, abort fight. If post-attack, don't move anything in */
441 return player->aborted = 1;
445 * This is the combat_mode based integrity check. It splits among two main
446 * divisions: first time/not first time, and attack/assault/para/board.
450 att_abort(int combat_mode, struct combat *off, struct combat *def)
459 if (att_get_combat(def, 1) < 0)
460 return abort_attack();
462 if (off && combat_mode != A_ATTACK) {
463 if (att_get_combat(off, 0) < 0)
464 return abort_attack();
465 if (off->type == EF_SHIP &&
466 (!getsect(off->x, off->y, §) ||
467 sect.sct_type != SCT_WATER)) {
468 pr("%s can not %s from that far inland!\n",
469 prcom(0, off), att_mode[combat_mode]);
470 return abort_attack();
473 switch (combat_mode) {
475 if (!neigh(def->x, def->y, player->cnum) &&
476 !adj_units(def->x, def->y, player->cnum)) {
477 pr("You are not adjacent to %s\n",
478 xyas(def->x, def->y, player->cnum));
479 return abort_attack();
481 if (def->own == player->cnum) {
482 pr("You can't attack your own sector.\n");
483 return abort_attack();
487 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
488 pr("You'll have to get there first...\n");
489 return abort_attack();
491 if (off && def->sct_type == SCT_MOUNT) {
492 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
493 return abort_attack();
497 if (def->own == player->cnum) {
498 pr("You can't air-assault your own sector.\n");
499 return abort_attack();
501 if (off && (def->sct_type == SCT_MOUNT ||
502 def->sct_type == SCT_WATER ||
503 def->sct_type == SCT_CAPIT ||
504 def->sct_type == SCT_FORTR ||
505 def->sct_type == SCT_WASTE)) {
506 pr("You can't air-assault a %s sector!\n",
507 def->sct_dcp->d_name);
508 return abort_attack();
512 return board_abort(off, def);
514 return land_board_abort(off, def);
517 if (off && def->sct_dcp->d_mcst <= 0) {
518 pr("You can't %s a %s sector!\n",
519 att_mode[combat_mode], def->sct_dcp->d_name);
520 return abort_attack();
522 if (!off || off->relations_checked)
524 off->relations_checked = 1;
527 setcont(player->cnum, def->own, FOUND_SPY);
528 setcont(def->own, player->cnum, FOUND_SPY);
530 if (opt_SLOW_WAR && def->own != player->cnum) {
531 natp = getnatp(player->cnum);
532 rel = getrel(natp, def->own);
535 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
536 cname(def->own), att_mode[combat_mode]);
537 if (!confirm(y_or_n))
538 return abort_attack();
541 if ((rel != AT_WAR) && (def->own) &&
542 (sect.sct_oldown != player->cnum)) {
543 pr("You're not at war with them!\n");
544 return abort_attack();
551 * Lots of special things need to be checked for boarding, so I put it in
556 board_abort(struct combat *off, struct combat *def)
558 struct shpstr aship, dship; /* for tech levels */
561 if (att_get_combat(def, 1) < 0)
562 return abort_attack();
567 if (att_get_combat(off, 0) < 0)
568 return abort_attack();
570 if (off->x != def->x || off->y != def->y) {
571 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
572 return abort_attack();
574 if (off->type == EF_SHIP) {
576 pr("%s has no mobility!\n", prcom(0, off));
577 return abort_attack();
579 getship(off->shp_uid, &aship);
580 getship(def->shp_uid, &dship);
581 if (techfact(aship.shp_tech, 1.0) * aship.shp_speed * off->eff
582 <= techfact(dship.shp_tech, 1.0) * dship.shp_speed * def->eff) {
583 pr("Victim ship moves faster than you do!\n");
586 "%s (#%d) %s failed to catch %s\n",
587 cname(aship.shp_own), aship.shp_own,
588 pr_com(0, off, def->own), pr_com(0, def, def->own));
589 return abort_attack();
591 } else if (off->type != EF_SECTOR) {
592 pr("Please tell the deity that you got the 'banana boat' error\n");
593 return abort_attack();
595 if (def->shp_mcp->m_flags & M_SUB) {
596 getsect(def->x, def->y, §);
597 if (sect.sct_type == SCT_WATER) {
598 pr("You can't board a submarine!\n");
599 return abort_attack();
606 * Lots of special things need to be checked for boarding, so I put it in
608 * STM - I copied it for land unit boarding. :)
612 land_board_abort(struct combat *off, struct combat *def)
614 if (att_get_combat(def, 1) < 0)
615 return abort_attack();
620 if (att_get_combat(off, 0) < 0)
621 return abort_attack();
623 if (off->x != def->x || off->y != def->y) {
624 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
625 return abort_attack();
631 /* If we are boarding, then the defending ship gets a chance to fire back */
633 att_approach(struct combat *off, struct combat *def)
639 pr("Approaching %s...\n", prcom(0, def));
642 "%s is being approached by %s...\n",
643 pr_com(0, def, def->own), pr_com(0, off, def->own));
644 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
647 pr("They're firing at us sir!\n");
650 "Your fleet at %s does %d damage to %s\n",
651 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
653 if (off->type == EF_SECTOR) {
654 getsect(off->x, off->y, §);
655 sectdamage(§, dam, 0);
657 pr("Enemy fleet at %s does %d damage to %s\n",
658 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
659 } else if (off->type == EF_SHIP) {
660 getship(off->shp_uid, &ship);
661 shipdamage(&ship, dam);
662 putship(off->shp_uid, &ship);
663 if (def->own && ship.shp_effic < SHIP_MINEFF) {
664 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
665 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
668 if (att_get_combat(off, 0) < 0)
669 return abort_attack();
673 /* The attack is valid. Tell the attacker about what they're going to hit */
676 att_show(struct combat *def)
678 /* Note that we tell the player about the treaty BEFORE we tell them
679 about the item. If we didn't, then they gain free information */
680 if (def->type == EF_SECTOR) {
681 if (!trechk(player->cnum, def->own, LANATT))
682 return abort_attack();
683 pr("%s is a %d%% %s %s with approximately %d military.\n",
684 xyas(def->x, def->y, player->cnum),
685 roundintby((int)def->eff, 10),
686 cname(def->own), def->sct_dcp->d_name,
687 roundintby(def->troops, 10));
688 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
689 writemap(player->cnum);
690 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
691 if (def->type == EF_SHIP) {
692 if (!trechk(player->cnum, def->own, SEAATT))
693 return abort_attack();
695 if (!trechk(player->cnum, def->own, LNDATT))
696 return abort_attack();
698 pr("%s is about %d%% efficient and has approximately %d mil on board.\n", prcom(0, def), roundintby((int)def->eff, 10), roundintby(def->troops, 10));
700 /* Ok, everything is fine */
704 /* Attack and assault ask the user which kind of support they want */
707 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
712 *fortp = *shipp = *landp = *planep = 1;
714 if (player->argp[offset] != NULL) {
715 if ((player->argp[offset + 1] == NULL) ||
716 (player->argp[offset + 2] == NULL) ||
717 (player->argp[offset + 3] == NULL)) {
718 pr("If any support arguments are used, all must be!\n");
723 *landp = *planep = 0;
725 if (!(p = getstarg(player->argp[offset], "Use fort support? ",
729 if ((*p == 'y') || (*p == 'Y'))
732 if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
736 if ((*p == 'y') || (*p == 'Y'))
739 if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
743 if ((*p == 'y') || (*p == 'Y'))
746 if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
750 if ((*p == 'y') || (*p == 'Y'))
757 * Attack, assault, and board ask the attacker what they'd like to attack
758 * with. This includes mil and land units from each "off" object. Note that
759 * after each sub-prompt, we check to make sure that the attack is still
760 * valid, and if it's not, then we abort the attack.
764 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
765 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
768 char land_answer[256];
771 if (att_abort(combat_mode, off, def))
773 memset(land_answer, 0, sizeof(land_answer));
774 for (n = 0; n <= off->last; ++n) {
775 off[n].troops = ask_off(combat_mode, off + n, def);
776 if (att_abort(combat_mode, off, def))
778 ask_olist(combat_mode, off + n, def, olist, land_answer,
779 a_spyp, a_engineerp);
780 if (att_abort(combat_mode, off, def))
787 * Return path cost for ATTACKER to enter sector given by DEF.
788 * MOBTYPE is a mobility type accepted by sector_mcost().
791 att_mobcost(natid attacker, struct combat *def, int mobtype)
796 if (CANT_HAPPEN(def->type != EF_SECTOR))
798 ok = getsect(def->x, def->y, §);
799 if (CANT_HAPPEN(!ok))
803 * We want the cost to move/march into the sector. If we just
804 * called sector_mcost(), we'd get the defender's cost. The
805 * attacker's cost is higher unless he's the old-owner. Note: if
806 * there are no civilians, a victorious attacker will become the
807 * old-owner. But he isn't now.
809 sect.sct_own = attacker;
811 return sector_mcost(§, mobtype);
814 /* How many mil is off allowed to attack with when it attacks def? */
817 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
822 switch (combat_mode) {
824 mobcost = att_mobcost(off->own, def, MOB_MOVE);
825 if (mobcost < 0 || off->mob <= 0)
827 mob_support = off->mob / mobcost;
828 if (mob_support < off->troops)
829 pr("Sector %s has %d mobility which can only support %d mil,\n",
830 xyas(off->x, off->y, player->cnum), off->mob, mob_support);
832 mob_support = off->troops;
835 if (def->own != player->cnum && def->mil) {
836 if (off->shp_mcp->m_flags & M_SEMILAND)
837 return off->troops / 4;
838 else if (!(off->shp_mcp->m_flags & M_LAND))
839 return off->troops / 10;
843 if (off->type == EF_SECTOR && off->mob <= 0)
845 mob_support = def->shp_mcp->m_item[I_MILIT];
846 if (mob_support < off->troops)
847 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
849 mob_support = off->troops;
854 if (def->lnd_lcp->l_flags & L_SPY)
856 mob_support = def->lnd_lcp->l_item[I_MILIT];
857 if (mob_support < off->troops)
858 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
860 mob_support = off->troops;
867 * If the attacker decides to go through with the attack, then the
868 * sectors/ships they are attacking with may be charged some mobility.
869 * This is where that amount of mobility is calculated. It is actually
870 * subtracted "for real" from the object's mobility in put_combat().
874 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
881 switch (combat_mode) {
883 off->mobcost += MAX(1,
885 * att_mobcost(off->own, def, MOB_MOVE)));
888 off->mobcost += MAX(1, attacking_mil / 5);
893 off->mobcost += MAX(1, attacking_mil / 5);
896 /* the 2 in the formula below is a fudge factor */
897 getship(def->shp_uid, &ship);
898 off->mobcost += (def->eff / 100) * (ship.shp_speed / 2);
903 /* How many mil to we want to attack from off against def? */
906 ask_off(int combat_mode, struct combat *off, struct combat *def)
912 if (att_get_combat(off, 0) <= 0)
914 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
916 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
918 if (off->type == EF_SECTOR) {
919 if (off->own != player->cnum)
921 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
922 off->sct_dcp->d_name,
923 xyas(off->x, off->y, player->cnum), mob_support);
925 sprintf(prompt, "Number of mil from %s (max %d) : ",
926 prcom(0, off), mob_support);
928 if ((attacking_mil = onearg(0, prompt)) < 0)
930 if (att_abort(combat_mode, off, def))
932 if (att_get_combat(off, 0) <= 0)
935 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
938 calc_mobcost(combat_mode, off, def, attacking_mil);
939 return attacking_mil;
943 * Which units would you like to attack with or move in with [ynYNq?]
947 att_prompt(char *prompt, char army)
955 p = getstring(prompt, buf);
956 if (player->aborted || (p && *p == 'q')) {
962 if (tolower(*p) == 'y' || tolower(*p) == 'n')
964 pr("y - yes this unit\n"
966 "Y - yes to all units in army '%c'\n"
967 "N - no to all units in army '%c'\n"
968 "q - quit\n? - this help message\n\n",
973 /* Ask the attacker which units they want to attack/assault/board with */
976 ask_olist(int combat_mode, struct combat *off, struct combat *def,
977 struct emp_qelem *olist, char *land_answer, int *a_spyp,
991 if (def->type == EF_LAND)
993 if (def->type == EF_SHIP)
994 maxland = def->shp_mcp->m_nland;
996 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
997 while (nxtitem(&ni, &land)) {
998 if (land.lnd_own != player->cnum)
1000 if (land.lnd_effic < LAND_MINEFF)
1002 if (land_answer[(int)land.lnd_army] == 'N')
1004 if (!lnd_can_attack(&land))
1006 lcp = &lchr[(int)land.lnd_type];
1008 if (def->type == EF_SHIP && !maxland) {
1009 pr("Land units are not able to board this kind of ship\n");
1012 if (land.lnd_mobil <= 0) {
1013 pr("%s is out of mobility, and cannot %s\n",
1014 prland(&land), att_mode[combat_mode]);
1019 if (ontradingblock(EF_LAND, &land)) {
1020 pr("%s is on the trading block, and cannot %s\n",
1021 prland(&land), att_mode[combat_mode]);
1026 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1027 pr("%s is on ship #%d, and cannot %s\n",
1028 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1030 } else if (off->type == EF_SHIP) {
1031 if (land.lnd_ship != off->shp_uid)
1033 } else if (land.lnd_land >= 0) {
1034 pr("%s is on unit #%d, and cannot %s\n",
1035 prland(&land), land.lnd_land, att_mode[combat_mode]);
1038 switch (combat_mode) {
1040 mobcost = lnd_pathcost(&land,
1041 att_mobcost(off->own, def,
1042 lnd_mobtype(&land)));
1043 if (land.lnd_mobil < mobcost) {
1044 pr("%s does not have enough mobility (%d needed)\n",
1045 prland(&land), (int)ceil(mobcost));
1052 if (!(lcp->l_flags & L_ASSAULT))
1059 att_val = attack_val(combat_mode, &land);
1060 if (att_val < 1.0) {
1061 pr("%s has no offensive strength\n", prland(&land));
1064 resupply_all(&land);
1065 putland(land.lnd_uid, &land);
1066 if (!has_supply(&land)) {
1067 pr("%s is out of supply, and cannot %s\n",
1068 prland(&land), att_mode[combat_mode]);
1071 if (def->type == EF_SHIP && first_time) {
1073 pr("You may board with a maximum of %d land units\n", maxland);
1075 pr("%s has a base %s value of %.0f\n",
1076 prland(&land), att_mode[combat_mode], att_val);
1077 if (land_answer[(int)land.lnd_army] != 'Y') {
1079 "%s with %s %s (%c %d%%) [ynYNq?] ",
1080 att_mode[combat_mode],
1083 land.lnd_army ? land.lnd_army : '~',
1085 land_answer[(int)land.lnd_army] =
1086 att_prompt(prompt, land.lnd_army);
1087 if (att_abort(combat_mode, off, def))
1089 if (land_answer[(int)land.lnd_army] != 'y' &&
1090 land_answer[(int)land.lnd_army] != 'Y')
1093 if (!(llp = malloc(sizeof(struct llist)))) {
1094 logerror("Malloc failed in attack!\n");
1098 memset(llp, 0, sizeof(struct llist));
1099 emp_insque(&llp->queue, olist);
1100 llp->mobil = mobcost;
1101 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1103 if (lnd_spyval(&land) > *a_spyp)
1104 *a_spyp = lnd_spyval(&land);
1105 if (llp->lcp->l_flags & L_ENGINEER)
1107 if (def->type == EF_SHIP && ++count >= maxland)
1112 /* What's the offense or defense multiplier? */
1115 att_combat_eff(struct combat *com)
1121 if (com->type == EF_SECTOR) {
1122 eff = com->eff / 100.0;
1123 if (com->own == player->cnum) {
1124 str = com->sct_dcp->d_ostr;
1125 eff = 1.0 + ((str - 1.0) * eff);
1127 eff = sector_strength(getsectp(com->x, com->y));
1128 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1129 getship(com->shp_uid, &ship);
1130 eff = 1.0 + ship.shp_armor / 100.0;
1136 att_get_offense(int combat_mode, struct combat *off,
1137 struct emp_qelem *olist, struct combat *def)
1142 * Get the attacker units & mil again in case they changed while the
1143 * attacker was answering sub-prompts.
1146 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1147 if (att_empty_attack(combat_mode, ototal, def))
1148 return abort_attack();
1149 if (combat_mode == A_PARA)
1151 pr("\n Initial attack strength: %8d\n", ototal);
1155 /* Get the defensive units and reacting units */
1157 att_get_defense(struct emp_qelem *olist, struct combat *def,
1158 struct emp_qelem *dlist, int a_spy, int ototal)
1161 struct emp_qelem *qp;
1167 get_dlist(def, dlist, 0, &d_spy);
1168 dtotal = get_dtotal(def, dlist, 1.0, 0);
1171 * Call in reacting units
1174 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1175 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1177 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1178 llp = (struct llist *)qp;
1179 intelligence_report(def->own, &llp->land, d_spy,
1180 "Scouts report attacking unit:");
1183 old_dtotal = dtotal;
1184 dtotal = get_dtotal(def, dlist, 1.0, 0);
1185 if (dtotal != old_dtotal)
1186 pr("Defense strength with reacting units: %8d\n", dtotal);
1191 /* Get the defensive land units in the sector or on the ship */
1194 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1197 struct nstr_item ni;
1201 /* In here is where you need to take out spies and trains from the defending
1202 lists. Spies try to hide, trains get trapped and can be boarded. */
1204 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1205 while (nxtitem(&ni, &land)) {
1208 if (land.lnd_own != def->own)
1210 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1212 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1214 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1216 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1218 if (!list) { /* Just estimating the enemy strength */
1219 intelligence_report(player->cnum, &land, a_spy,
1220 "Scouts report defending unit:");
1223 if (!(llp = malloc(sizeof(struct llist)))) {
1224 logerror("Malloc failed in attack!\n");
1228 memset(llp, 0, sizeof(struct llist));
1229 emp_insque(&llp->queue, list);
1230 llp->supplied = has_supply(&land);
1231 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1233 if (lnd_spyval(&land) > *d_spyp)
1234 *d_spyp = lnd_spyval(&land);
1238 /* Calculate the total offensive strength */
1241 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1242 double osupport, int check)
1244 double ototal = 0.0;
1245 struct emp_qelem *qp, *next;
1250 * first, total the attacking mil
1253 for (n = 0; n <= off->last; ++n) {
1254 if (off[n].type == EF_BAD || (check &&
1255 att_get_combat(&off[n], 0) <= 0))
1257 ototal += off[n].troops * att_combat_eff(off + n);
1261 * next, add in the attack_values of all
1262 * the attacking units
1265 for (qp = olist->q_forw; qp != olist; qp = next) {
1267 llp = (struct llist *)qp;
1268 if (check && !get_land(combat_mode, 0, llp->land.lnd_uid, llp, 0))
1270 if (combat_mode == A_ATTACK) {
1272 for (n = 0; n <= off->last; ++n) {
1273 if (off[n].type == EF_BAD)
1275 if ((off[n].x == llp->land.lnd_x) &&
1276 (off[n].y == llp->land.lnd_y))
1280 lnd_delete(llp, "is in a sector not owned by you");
1283 ototal += attack_val(combat_mode, &llp->land) *
1284 att_combat_eff(off + w);
1286 ototal += attack_val(combat_mode, &llp->land);
1291 return ldround(ototal, 1);
1294 /* Calculate the total defensive strength */
1297 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1300 double dtotal = 0.0, eff = 1.0, d_unit;
1301 struct emp_qelem *qp, *next;
1304 if (check && att_get_combat(def, 1) < 0)
1306 eff = att_combat_eff(def);
1307 dtotal = def->troops * eff;
1310 * next, add in the defense_values of all
1311 * the defending non-retreating units
1314 for (qp = list->q_forw; qp != list; qp = next) {
1316 llp = (struct llist *)qp;
1317 if (check && !get_land(A_DEFEND, def, llp->land.lnd_uid, llp, 1))
1319 d_unit = defense_val(&llp->land);
1322 dtotal += d_unit * eff;
1327 return ldround(dtotal, 1);
1331 * This is the land unit integrity check. Note that we don't print
1332 * warnings about victim land units because the attacker may not have seen them
1336 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp,
1339 struct lndstr *lp = &llp->land;
1344 if (!llp->lcp) { /* first time */
1345 llp->x = llp->land.lnd_x;
1346 llp->y = llp->land.lnd_y;
1347 llp->lcp = &lchr[(int)llp->land.lnd_type];
1348 } else { /* not first time */
1349 if (lp->lnd_effic < LAND_MINEFF) {
1350 sprintf(buf, "was destroyed and is no longer a part of the %s",
1351 att_mode[combat_mode]);
1352 lnd_delete(llp, buf);
1356 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1358 "left to go fight another battle and is no longer a part of the defense");
1362 if (lp->lnd_own != player->cnum) {
1364 "was destroyed and is no longer a part of the %s",
1365 att_mode[combat_mode]);
1366 lnd_delete(llp, buf);
1369 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1371 "left to fight another battle and is no longer a part of the %s",
1372 att_mode[combat_mode]);
1373 lnd_delete(llp, buf);
1376 if (lp->lnd_effic < llp->eff) {
1377 sprintf(buf, "damaged from %d%% to %d%%",
1378 llp->eff, lp->lnd_effic);
1379 lnd_print(llp, buf);
1383 llp->eff = llp->land.lnd_effic;
1389 * Put the land unit on the disk. If there was some mobility cost, then
1390 * subtract it from the units mobility. Note that this works the same way
1391 * as sectors & ships in that no mobility is actually taken until the attacker
1392 * has committed to attacking.
1396 kill_land(struct emp_qelem *list)
1398 struct emp_qelem *qp, *next;
1401 for (qp = list->q_forw; qp != list; qp = next) {
1403 llp = (struct llist *)qp;
1404 if (llp->land.lnd_ship >= 0) {
1405 llp->land.lnd_effic = 0;
1406 lnd_delete(llp, "cannot return to the ship, and dies!");
1412 att_infect_units(struct emp_qelem *list, int plague)
1414 struct emp_qelem *qp, *next;
1419 for (qp = list->q_forw; qp != list; qp = next) {
1421 llp = (struct llist *)qp;
1422 if (llp->land.lnd_pstage == PLG_HEALTHY)
1423 llp->land.lnd_pstage = PLG_EXPOSED;
1428 put_land(struct emp_qelem *list)
1430 struct emp_qelem *qp, *next;
1433 for (qp = list->q_forw; qp != list; qp = next) {
1435 llp = (struct llist *)qp;
1436 llp->land.lnd_mission = 0;
1437 llp->land.lnd_harden = 0;
1438 llp->land.lnd_mobil -= (int)llp->mobil;
1440 putland(llp->land.lnd_uid, &llp->land);
1441 if (llp->land.lnd_own != player->cnum) {
1442 emp_remque((struct emp_qelem *)llp);
1445 get_land(A_ATTACK, 0, llp->land.lnd_uid, llp, 0);
1450 * Keep sending in reinforcements until it looks like we're going to win.
1451 * Note that the "strength" command also calls this routine.
1455 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1456 int *d_spyp, int ototal)
1458 struct nstr_item ni;
1460 struct sctstr sect, dsect;
1463 double new_land = 0;
1469 double eff = att_combat_eff(def);
1473 dtotal = get_dtotal(def, list, 1.0, 1);
1476 snxtitem_all(&ni, EF_LAND);
1477 while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1480 if (!land.lnd_rad_max)
1482 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1484 if (land.lnd_own != def->own)
1486 if (land.lnd_ship >= 0)
1488 if (land.lnd_land >= 0)
1490 if (defense_val(&land) < 1.0)
1492 if (!lnd_can_attack(&land))
1495 /* Only supplied units can react */
1496 if (!has_supply(&land))
1499 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1501 getsect(land.lnd_x, land.lnd_y, §);
1502 /* Units on efficient headquarters can react 1 farther */
1503 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1504 radius = land.lnd_rad_max + 1;
1506 radius = land.lnd_rad_max;
1508 if (land.lnd_mission == MI_RESERVE)
1514 getsect(def->x, def->y, &dsect);
1515 if (!BestLandPath(buf, §, &dsect, &pathcost,
1516 lnd_mobtype(&land)))
1519 mobcost = lnd_pathcost(&land, pathcost);
1520 if (land.lnd_mobil < mobcost)
1523 new_land += defense_val(&land);
1525 if (!list) /* we are in the "strength" command */
1528 /* move to defending sector */
1529 land.lnd_mobil -= ldround(mobcost, 1);
1532 land.lnd_x = def->x;
1533 land.lnd_y = def->y;
1534 putland(land.lnd_uid, &land);
1535 wu(0, land.lnd_own, "%s reacts to %s.\n",
1536 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1538 llp = malloc(sizeof(struct llist));
1540 memset(llp, 0, sizeof(struct llist));
1544 llp->lcp = &lchr[(int)land.lnd_type];
1546 emp_insque(&llp->queue, list);
1547 if (lnd_spyval(&land) > *d_spyp)
1548 *d_spyp = lnd_spyval(&land);
1550 intelligence_report(player->cnum, &land, a_spy,
1551 "Scouts sight reacting enemy unit:");
1556 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1559 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1560 int land_sup, int plane_sup)
1562 double osupport = 1.0;
1564 double af, as, au, ap;
1566 af = as = au = ap = 0.0;
1568 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1573 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1580 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1586 dam = off_support(def->x, def->y, def->own, player->cnum);
1590 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1595 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1598 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1599 int ototal, int dtotal)
1601 double dsupport = 1.0;
1603 double df, ds, du, dp;
1606 df = ds = du = dp = 0.0;
1607 if (dtotal < 0.1 * ototal) {
1609 } else if (dtotal >= 1.2 * ototal) {
1612 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1616 dtotal = get_dtotal(def, list, dsupport, 0);
1617 if (dtotal < 1.2 * ototal) {
1618 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1621 dtotal = get_dtotal(def, list, dsupport, 0);
1623 if (dtotal < 1.2 * ototal) {
1624 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1627 dtotal = get_dtotal(def, list, dsupport, 1);
1629 if (dtotal < 1.2 * ototal) {
1630 dam = def_support(def->x, def->y, player->cnum, def->own);
1638 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1643 "\nOdds are bad for us...support cancelled.\n\n");
1646 "\nOdds are good for us...support cancelled.\n\n");
1652 * Land mines add to the defense multiplier. If the attacker has engineers
1653 * then this multiplier is cut in half.
1657 get_mine_dsupport(struct combat *def, int a_engineer)
1662 getsect(def->x, def->y, §);
1664 if (sect.sct_oldown != player->cnum) {
1665 mines = MIN(sect.sct_mines, 20);
1667 mines = ldround(mines / 2.0, 1);
1670 wu(0, def->own, "Defending mines add %1.2f\n",
1672 pr("Defending mines add %1.2f\n", mines * 0.02);
1673 return mines * 0.02;
1679 /* Get the offensive and defensive support */
1681 att_get_support(int combat_mode, int ofort, int oship, int oland,
1682 int oplane, struct emp_qelem *olist, struct combat *off,
1683 struct emp_qelem *dlist, struct combat *def,
1684 double *osupportp, double *dsupportp, int a_engineer)
1687 char osupports[512];
1688 char dsupports[512];
1690 if (combat_mode == A_PARA)
1693 *osupportp = get_osupport(osupports, def,
1694 ofort, oship, oland, oplane);
1697 * I need to put a 1 at the end of the next four total_stren calls
1698 * becauase units & mil may have been damaged by collateral damage or
1699 * neclear warheads from the offensive & defensive support.
1702 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1703 if (att_empty_attack(combat_mode, ototal, def))
1704 return abort_attack();
1705 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1708 * Calculate defensive support. If odds are too good or too bad
1709 * then don't call in support.
1712 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1713 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1714 if (att_empty_attack(combat_mode, ototal, def))
1715 return abort_attack();
1717 if ((*osupports || *dsupports) &&
1718 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1719 pr("\n\t\tsupport values\n");
1720 pr("\t\tforts\tships\tunits\tplanes\n");
1721 if (*osupportp != 1.0)
1722 pr("%s", osupports);
1723 if (*dsupportp != 1.0)
1724 pr("%s", dsupports);
1726 wu(0, def->own, "\n\t\tsupport values\n");
1727 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1728 if (*osupportp != 1.0)
1729 wu(0, def->own, "%s", osupports);
1730 if (*dsupportp != 1.0)
1731 wu(0, def->own, "%s", dsupports);
1735 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1736 if (dtotal && def->type == EF_SECTOR)
1737 *dsupportp += get_mine_dsupport(def, a_engineer);
1741 /* How many two-legged bipeds are in this combat force? */
1744 count_bodies(struct combat *off, struct emp_qelem *list)
1748 struct emp_qelem *qp;
1751 for (n = 0; n <= off->last; ++n)
1752 bodies += off[n].troops;
1753 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1754 llp = (struct llist *)qp;
1755 bodies += llp->land.lnd_item[I_MILIT];
1760 /* This is where the fighting actually occurs. */
1763 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1764 double osupport, struct combat *def, struct emp_qelem *dlist,
1768 int a_cas = 0; /* Casualty counts */
1770 int ototal; /* total attacking strength */
1771 int dtotal; /* total defending strength */
1772 int a_bodies; /* total attacking mil (incl. mil in units) */
1773 int d_bodies; /* total defending mil (incl. mil in units) */
1783 ototal = get_ototal(combat_mode, off, olist, osupport,
1784 combat_mode != A_PARA);
1785 dtotal = get_dtotal(def, dlist, dsupport, 0);
1789 a_bodies = count_bodies(off, olist);
1790 d_bodies = count_bodies(def, dlist);
1791 d_mil = def->troops;
1792 for (n = 0; n <= off->last; ++n)
1793 if (off[n].type == EF_BAD)
1796 a_troops[n] = off[n].troops;
1798 /* This switch is required to get the spacing right */
1799 switch (combat_mode) {
1801 pr(" Final attack strength: %8d\n", ototal);
1804 pr(" Final assault strength: %8d\n", ototal);
1807 if (def->sct_type == SCT_MOUNT ||
1808 def->sct_type == SCT_WATER ||
1809 def->sct_type == SCT_CAPIT ||
1810 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1811 pr("You can't air-assault a %s sector!\n",
1812 def->sct_dcp->d_name);
1815 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1817 pr(" Final air-assault strength: %8d\n", ototal);
1821 pr(" Final board strength: %8d\n", ototal);
1825 pr(" Final defense strength: %8d\n", dtotal);
1826 odds = att_calcodds(ototal, dtotal);
1827 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1829 /* spread the plague */
1830 if (combat_mode != A_PARA) {
1832 for (n = 0; n <= off->last; ++n)
1833 if (off[n].type != EF_BAD)
1834 def->plague |= off[n].plague;
1835 for (n = 0; n <= off->last; ++n)
1836 if (off[n].type != EF_BAD)
1837 off[n].plague |= def->plague;
1839 att_infect_units(olist, off->plague);
1840 att_infect_units(dlist, def->plague);
1843 * Fighting is slightly random. There is always that last little
1844 * effort you see people put in. Or the stray bullet that takes out
1845 * an officer and the rest go into chaos. Things like that.
1846 * Thus, we have added a very slight random factor that will sometimes
1847 * allow the little guy to win. We modify the odds a little
1848 * (either +- 5%) to account for this randomness. We also only
1849 * recalculate the odds every 8-50 casualties, not every cacsualty,
1850 * since a single dead guy normally wouldn't cause a commander to
1851 * rethink his strategies, but 50 dead guys might.
1853 odds += (random() % 11 - 5) / 100.0;
1858 recalctime = 8 + (random() % 43);
1859 while (!success && ototal) {
1862 d_cas += take_casualty(A_DEFEND, def, dlist);
1863 dtotal = get_dtotal(def, dlist, dsupport, 0);
1868 a_cas += take_casualty(combat_mode, off, olist);
1869 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1871 if (((a_cas + d_cas) % 70) == 69)
1873 if (recalctime-- <= 0) {
1874 recalctime = 8 + (random() % 43);
1875 odds = att_calcodds(ototal, dtotal);
1876 odds += (random() % 11 - 5) / 100.0;
1884 /* update defense mobility & mil */
1888 if (def->type == EF_SECTOR && d_mil && d_cas) {
1892 newmob = damage(def->mob, 100 * d_cas / d_mil);
1893 def->mobcost = MIN(20, def->mob - newmob);
1896 def->mil = def->troops;
1899 /* update attack mobility & mil */
1900 for (n = 0; n <= off->last; ++n)
1901 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1902 if (off[n].type == EF_SECTOR && off[n].mil) {
1903 if (!CANT_HAPPEN(off[n].mob < 0)) {
1904 newmob = damage(off[n].mob,
1905 100 * (a_troops[n] - off[n].troops)
1907 off[n].mobcost += MIN(20, off[n].mob - newmob);
1910 off[n].mil -= a_troops[n] - off[n].troops;
1913 /* update land unit mobility */
1914 if (d_bodies && d_cas)
1915 lnd_takemob(dlist, (double)d_cas / d_bodies);
1916 if (a_bodies && a_cas)
1917 lnd_takemob(olist, (double)a_cas / a_bodies);
1919 /* damage attacked sector */
1920 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1922 pr("- Casualties -\n Yours: %d\n", a_cas);
1923 pr(" Theirs: %d\n", d_cas);
1924 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1925 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1928 switch (combat_mode) {
1930 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1931 pr("We have captured %s, sir!\n", prcom(0, def));
1935 news_item = def->own ? N_AWON_SECT : N_START_COL;
1936 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1937 action = "assaulting and taking";
1940 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1941 pr("We have captured %s, sir!\n", prcom(0, def));
1942 action = "air-assaulting and taking";
1945 news_item = N_BOARD_SHIP;
1946 pr("We have boarded %s, sir!\n", prcom(0, def));
1947 action = "boarding";
1950 news_item = N_BOARD_LAND;
1951 pr("We have boarded %s, sir!\n", prcom(0, def));
1952 action = "boarding";
1957 action = "defeating";
1960 switch (combat_mode) {
1962 news_item = N_SCT_LOSE;
1963 pr("You have been defeated!\n");
1964 action = "attacking";
1967 news_item = N_ALOSE_SCT;
1968 pr("You have been defeated!\n");
1970 action = "trying to assault";
1973 news_item = N_PLOSE_SCT;
1974 pr("All of your troops were destroyed\n");
1975 action = "trying to air-assault";
1978 news_item = N_SHP_LOSE;
1979 pr("You have been repelled\n");
1981 action = "trying to board";
1984 news_item = N_LND_LOSE;
1985 pr("You have been repelled\n");
1987 action = "trying to board";
1992 action = "fighting";
1995 nreport(player->cnum, news_item, def->own, 1);
1998 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
1999 cname(player->cnum), player->cnum, a_cas,
2000 action, pr_com(0, def, def->own), d_cas);
2003 send_reacting_units_home(dlist);
2005 /* putland the defending land */
2008 /* putland the attacking land */
2011 /* put the victim sector/ship/land */
2012 if (!success || !take_def(combat_mode, olist, off, def))
2015 /* put the attacking sectors/ship */
2016 for (n = 0; n <= off->last; ++n)
2017 if (off[n].type != EF_BAD)
2018 put_combat(&off[n]);
2023 switch (combat_mode) {
2025 ask_move_in(off, olist, def);
2027 /* put sectors again to get abandon warnings */
2028 for (n = 0; n <= off->last; ++n)
2029 if (off[n].type != EF_BAD)
2030 put_combat(&off[n]);
2033 att_move_in_off(combat_mode, off, olist, def);
2036 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2040 /* What percentage of the combat forces going head-to-head are we? */
2043 att_calcodds(int ototal, int dtotal)
2047 /* calculate odds */
2050 else if (dtotal <= 0)
2053 odds = (double)ototal / (dtotal + ototal);
2058 /* Here's where the dead soldiers get dragged off the battlefield */
2061 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2063 int to_take = CASUALTY_LUMP;
2064 int biggest_troops = 0, index = -1;
2065 int n, tot_troops = 0, biggest_mil, cas;
2066 struct emp_qelem *qp, *biggest;
2069 for (n = 0; n <= off->last; ++n) {
2070 if (off[n].type != EF_BAD) {
2071 tot_troops += off[n].troops;
2072 if (off[n].troops > biggest_troops) {
2073 biggest_troops = off[n].troops;
2080 to_take -= tot_troops;
2083 for (n = 0; n <= off->last; ++n)
2084 if (off[n].type != EF_BAD)
2088 * They can all come off mil. We rotate the casualties,
2089 * starting with the sector containing the most mil.
2091 to_take = CASUALTY_LUMP;
2093 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2096 while (to_take > 0) {
2097 for (n = index; n <= off->last && to_take; ++n) {
2098 if (off[n].type != EF_BAD && off[n].troops > 0) {
2103 for (n = 0; n < index && to_take; ++n) {
2104 if (off[n].type != EF_BAD && off[n].troops > 0) {
2110 return CASUALTY_LUMP;
2114 return CASUALTY_LUMP - to_take;
2117 * Need to take some casualties from attacking units
2118 * Procedure: find the biggest unit remaining (in
2119 * terms of mil) and give it the casualties.
2123 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2124 llp = (struct llist *)qp;
2126 if (llp->land.lnd_item[I_MILIT] > biggest_mil) {
2127 biggest_mil = llp->land.lnd_item[I_MILIT];
2131 if (biggest == NULL)
2132 return CASUALTY_LUMP - to_take;
2134 llp = (struct llist *)biggest;
2135 cas = lnd_take_casualty(combat_mode, llp, to_take);
2136 return CASUALTY_LUMP - (to_take - cas);
2139 /* Send reacting defense units back to where they came from (at no mob cost) */
2142 send_reacting_units_home(struct emp_qelem *list)
2144 struct emp_qelem *qp, *next;
2148 for (qp = list->q_forw; qp != list; qp = next) {
2150 llp = (struct llist *)qp;
2151 if ((llp->land.lnd_x != llp->x) || (llp->land.lnd_y != llp->y)) {
2152 sprintf(buf, "returns to %s",
2153 xyas(llp->x, llp->y, llp->land.lnd_own));
2154 llp->land.lnd_x = llp->x;
2155 llp->land.lnd_y = llp->y;
2156 lnd_delete(llp, buf);
2161 /* Check for 0 offense strength. This call will always preceed an abort */
2164 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2167 if (def->own && player->cnum != def->own) {
2169 "%s (#%d) considered %sing you @%s\n",
2170 cname(player->cnum), player->cnum,
2171 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2173 pr("No troops for %s...\n", att_mode[combat_mode]);
2180 * Take the defending sector or ship from the defender and give it to the
2185 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2190 struct llist *llp, *delete_me = 0;
2196 for (n = 0; n <= off->last && !occuppied; ++n) {
2197 if (off[n].type != EF_BAD &&
2198 off[n].troops > 0 &&
2199 (off[n].type != EF_SECTOR || off[n].mob)) {
2201 if (def->type == EF_LAND) {
2202 if (def->lnd_lcp->l_flags & L_SPY) {
2209 pr("1 mil from %s moves %s\n",
2210 prcom(0, off + n), prcom(2, def));
2215 pr("%s left unoccupied\n", prcom(0, def));
2218 "No enemy troops moved %s so you still own it!\n",
2219 pr_com(2, def, def->own));
2222 llp = (struct llist *)list->q_forw;
2223 llp->land.lnd_x = def->x;
2224 llp->land.lnd_y = def->y;
2225 take_move_in_mob(combat_mode, llp, off, def);
2226 if (def->type == EF_SHIP) {
2227 llp->land.lnd_ship = def->shp_uid;
2228 sprintf(buf, "boards %s", prcom(0, def));
2231 llp->land.lnd_ship = -1;
2232 sprintf(buf, "moves in to occupy %s",
2233 xyas(def->x, def->y, player->cnum));
2234 lnd_delete(llp, buf);
2239 if (def->type == EF_SECTOR) {
2240 getsect(def->x, def->y, §);
2241 takeover(§, player->cnum);
2242 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2243 caploss(§, def->own,
2244 "* We have captured %s's capital, sir! *\n");
2246 } else if (def->type == EF_SHIP) {
2247 getship(def->shp_uid, &ship);
2248 takeover_ship(&ship, player->cnum, 1);
2249 putship(ship.shp_uid, &ship);
2250 } else if (def->type == EF_LAND) {
2251 getland(def->lnd_uid, &land);
2252 takeover_land(&land, player->cnum, 1);
2253 putland(land.lnd_uid, &land);
2256 lnd_delete(delete_me, buf);
2257 att_get_combat(def, 0);
2262 * Ask the attacker which mil & land units they'd like to move into the
2267 ask_move_in(struct combat *off, struct emp_qelem *olist,
2271 struct emp_qelem *qp, *next;
2275 char land_answer[256];
2278 for (n = 0; n <= off->last; ++n)
2279 if (off[n].type != EF_BAD && off[n].troops > 0)
2281 ask_move_in_off(&off[n], def);
2282 if (player->aborted)
2288 memset(land_answer, 0, sizeof(land_answer));
2289 for (qp = olist->q_forw; qp != olist; qp = next) {
2291 llp = (struct llist *)qp;
2292 answerp = &land_answer[(int)llp->land.lnd_army];
2293 if (player->aborted || att_get_combat(def, 0) < 0)
2295 if (*answerp == 'Y')
2297 if (*answerp != 'N') {
2298 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2300 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2302 llp->land.lnd_army ? llp->land.lnd_army : '~',
2303 llp->land.lnd_effic);
2304 *answerp = att_prompt(prompt, llp->land.lnd_army);
2305 if (player->aborted || att_get_combat(def, 0) < 0)
2307 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2310 if (*answerp == 'y' || *answerp == 'Y')
2312 sprintf(buf, "stays in %s",
2313 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2314 lnd_delete(llp, buf);
2318 if (att_get_combat(def, 0) < 0) {
2319 for (qp = olist->q_forw; qp != olist; qp = next) {
2321 llp = (struct llist *)qp;
2322 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2324 sprintf(buf, "stays in %s",
2325 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2326 lnd_delete(llp, buf);
2330 if (opt_INTERDICT_ATT)
2331 lnd_interdict(olist, def->x, def->y, player->cnum);
2332 move_in_land(A_ATTACK, off, olist, def);
2335 /* Move offensive land units to the conquered sector or ship */
2338 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2341 struct emp_qelem *qp, *next;
2347 for (qp = olist->q_forw; qp != olist; qp = next) {
2349 llp = (struct llist *)qp;
2350 if (!get_land(combat_mode, def, llp->land.lnd_uid, llp, 0))
2352 take_move_in_mob(combat_mode, llp, off, def);
2353 llp->land.lnd_x = def->x;
2354 llp->land.lnd_y = def->y;
2355 if (def->type == EF_SHIP)
2356 llp->land.lnd_ship = def->shp_uid;
2358 llp->land.lnd_ship = -1;
2362 if (def->type == EF_SECTOR) {
2363 if (opt_INTERDICT_ATT) {
2364 lnd_sweep(olist, 0, 0, def->own);
2365 lnd_check_mines(olist);
2367 sprintf(buf, "now occupies %s", prcom(0, def));
2369 sprintf(buf, "boards %s", prcom(0, def));
2373 for (qp = olist->q_forw; qp != olist; qp = next) {
2375 llp = (struct llist *)qp;
2376 lnd_print(llp, buf);
2384 * Move assaulting, paradropping, or boarding mil & units into def
2385 * If the mil are coming from a ship, then pack a lunch.
2389 att_move_in_off(int combat_mode, struct combat *off,
2390 struct emp_qelem *olist, struct combat *def)
2397 move_in_land(combat_mode, off, olist, def);
2399 for (n = 0; n <= off->last; ++n) {
2400 if (off[n].type == EF_BAD || !off[n].troops)
2402 troops = off[n].troops;
2404 off[n].mil -= troops;
2406 put_combat(off + n);
2407 if (combat_mode == A_ASSAULT) {
2408 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2410 getship(off[n].shp_uid, &ship);
2411 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2412 / (ship.shp_item[I_MILIT] + troops
2413 + ship.shp_item[I_CIVIL] + 0.5));
2415 ship.shp_item[I_FOOD] -= lunchbox;
2416 putship(ship.shp_uid, &ship);
2422 if (combat_mode == A_ASSAULT) {
2423 if (CANT_HAPPEN(def->type != EF_SECTOR))
2425 getsect(def->x, def->y, §);
2426 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2427 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2428 sect.sct_item[I_FOOD] += lunchbox;
2434 /* Ask how many mil to move in from each sector */
2437 ask_move_in_off(struct combat *off, struct combat *def)
2440 int num_mil, dam = 0, left;
2446 if (att_get_combat(off, 0) <= 0)
2448 if (att_get_combat(def, 0) < 0)
2450 if (off->own != player->cnum)
2452 d = att_mobcost(off->own, def, MOB_MOVE);
2453 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2455 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2456 xyas(off->x, off->y, player->cnum), mob_support);
2457 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0)
2459 /* Make sure we don't move in more than we can support mobility-wise */
2460 if (num_mil > mob_support)
2461 num_mil = mob_support;
2462 if (att_get_combat(off, 0) <= 0)
2464 if (att_get_combat(def, 0) < 0)
2466 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2467 pr("No mil moved in from %s\n",
2468 xyas(off->x, off->y, player->cnum));
2471 mob_support = MAX(1, (int)(num_mil * d));
2472 off->mob -= MIN(off->mob, mob_support);
2473 off->mil -= num_mil;
2474 off->troops -= num_mil;
2477 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2478 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2479 if (chance(weight / 100.0))
2481 ground_interdict(def->x, def->y, player->cnum, "military");
2482 dam += check_lmines(def->x, def->y, weight);
2486 left = commdamage(num_mil, dam, I_MILIT);
2487 if (left < num_mil) {
2489 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));
2491 pr("All of the mil you were moving were destroyed!\n");
2494 /* maybe got nuked */
2495 if (att_get_combat(def, 0) < 0)
2503 /* Charge land units for moving into a sector or onto a ship */
2506 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off,
2512 switch (combat_mode) {
2514 mobcost = lnd_pathcost(&llp->land,
2515 att_mobcost(off->own, def,
2516 lnd_mobtype(&llp->land)));
2517 new = llp->land.lnd_mobil - mobcost;
2520 llp->land.lnd_mobil = new;
2523 if (off->shp_mcp->m_flags & M_LAND) {
2524 if (llp->lcp->l_flags & L_MARINE)
2525 llp->land.lnd_mobil -=
2526 (float)etu_per_update * land_mob_scale * 0.5;
2528 llp->land.lnd_mobil -= (float)etu_per_update * land_mob_scale;
2530 if (llp->lcp->l_flags & L_MARINE)
2531 llp->land.lnd_mobil = 0;
2533 llp->land.lnd_mobil = -(float)etu_per_update * land_mob_scale;
2537 /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2538 if (llp->lcp->l_flags & L_MARINE)
2539 llp->land.lnd_mobil -= 10;
2541 llp->land.lnd_mobil -= 40;
2544 llp->land.lnd_harden = 0;
2548 free_list(struct emp_qelem *list)
2550 struct emp_qelem *qp, *next;
2552 if (!list || QEMPTY(list))
2556 while (qp != list) {
2565 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2573 * sector_strength - Everyone starts at 1. You can get up to a max
2574 * of d_dstr, depending on how much you build up the
2575 * defenses of the sector.
2579 sector_strength(struct sctstr *sp)
2581 double def = SCT_DEFENSE(sp) / 100.0;
2582 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2583 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2585 if (CANT_HAPPEN(d > dchr[sp->sct_type].d_dstr))
2586 d = dchr[sp->sct_type].d_dstr;
2587 if (CANT_HAPPEN(d < base))