]> git.pond.sub.org Git - empserver/blob - src/lib/commands/atta.c
Import of Empire 4.2.12
[empserver] / src / lib / commands / atta.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  atta.c: Attack another sector
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare
32  *     Ken Stevens, 1995
33  *     Steve McClure, 1996
34  */
35
36 #include <math.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "file.h"
40 #include "var.h"
41 #include "sect.h"
42 #include "path.h"
43 #include "news.h"
44 #include "treaty.h"
45 #include "nat.h"
46 #include "xy.h"
47 #include "land.h"
48 #include "nsc.h"
49 #include "mission.h"
50 #include "ship.h"
51 #include "combat.h"
52 #include "commands.h"
53
54 int
55 atta(void) {
56         struct  combat off[6];  /* attacking sectors */
57         struct  combat def[1];  /* defending sector */
58         coord   newx, newy;
59         struct  sctstr sect;
60         int     fort_sup, ship_sup, land_sup, plane_sup;
61         struct  emp_qelem olist;        /* attacking units */
62         struct  emp_qelem dlist;        /* defending units */
63         int     ototal;         /* total attacking strength */
64         int     a_engineer = 0; /* attacker engineers are present */
65         int     a_spy = 0;      /* the best attacker scout */
66         double  osupport = 1.0; /* attack support */
67         double  dsupport = 1.0; /* defense support */
68         int     last, n;
69         s_char  *p;
70         s_char  buf[1024];
71         int     rel;
72
73         att_combat_init(def, EF_SECTOR);
74         /*
75          * Collect input from the attacker
76          */
77
78         /* What are we attacking? */
79
80         if (!(p = getstarg(player->argp[1], "Sector :  ", buf)))
81                 return RET_SYN;
82         if (!sarg_xy(p, &def->x, &def->y))
83                 return RET_SYN;
84         if (att_abort(A_ATTACK, 0, def))
85                 return RET_FAIL;
86
87         /* Show what we're attacking, and check treaties */
88
89         if (att_show(def))
90             return RET_FAIL;
91
92         /* Ask about offensive support */
93
94         att_ask_support(2, &fort_sup, &ship_sup, &land_sup, &plane_sup);
95         if (att_abort(A_ATTACK, 0, def)) {
96                 att_empty_attack(A_ATTACK, 0, def);
97                 return RET_OK;
98         }
99
100         /* initialize the off[] array */
101
102         for (n = 0, last = -1; n < 6; ++n) {    /* Directions */
103                 newx = def->x + diroff[n+1][0];
104                 newy = def->y + diroff[n+1][1];
105                 getsect(newx,newy,&sect);    /* incase cross world boundary */
106                 rel = getrel(getnatp(sect.sct_own), player->cnum);
107                 if (!player->owner && rel != ALLIED)
108                         continue;
109                 att_combat_init(&off[++last], EF_SECTOR);
110                 off[last].x = sect.sct_x;
111                 off[last].y = sect.sct_y;
112         }
113         off->last = last;
114
115         /* Ask the player what he wants to attack with */
116
117         att_ask_offense(A_ATTACK, off, def, &olist, &a_spy, &a_engineer);
118         if (att_abort(A_ATTACK, off, def)) {
119                 pr("Attack aborted\n");
120                 att_empty_attack(A_ATTACK, 0, def);
121                 return att_free_lists(&olist, 0);
122         }
123
124         /*
125          * Estimate the defense strength and give the player a chance to abort
126          */
127
128         ototal = att_estimate_defense(A_ATTACK, off, &olist, def, a_spy);
129         if (att_abort(A_ATTACK, off, def)) {
130                 pr("Attack aborted\n");
131                 att_empty_attack(A_ATTACK, 0, def);
132                 return att_free_lists(&olist, 0);
133         }
134
135         /*
136          * We have now got all the answers from the attacker.  From this point
137          * forward, we can assume that this battle is the _only_ thing
138          * happening in the game.
139          */
140
141         /* Get the real defense */
142
143         att_get_defense(&olist, def, &dlist, a_spy, ototal);
144
145         /* Get attacker and defender support */
146
147         att_get_support(A_ATTACK, fort_sup, ship_sup, land_sup, plane_sup,
148                         &olist, off, &dlist, def, &osupport, &dsupport,
149                         a_engineer);
150         if (att_abort(A_ATTACK, off, def)) {
151                 pr("Attack aborted\n");
152                 att_empty_attack(A_ATTACK, 0, def);
153                 return att_free_lists(&olist, &dlist);
154         }
155
156         /*
157          * Death, carnage, and destruction.
158          */
159
160         att_fight(A_ATTACK,off,&olist,osupport,def,&dlist,dsupport);
161
162         return RET_OK;
163 }