]> git.pond.sub.org Git - empserver/blob - src/lib/commands/assa.c
Update copyright notice.
[empserver] / src / lib / commands / assa.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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  *  assa.c: Hit the beaches!
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1997
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "file.h"
38 #include "path.h"
39 #include "nat.h"
40 #include "xy.h"
41 #include "land.h"
42 #include "nsc.h"
43 #include "mission.h"
44 #include "ship.h"
45 #include "combat.h"
46 #include "commands.h"
47
48 int
49 assa(void)
50 {
51     struct combat off[1];       /* assaulting ship */
52     struct combat def[1];       /* defending sector */
53     int fort_sup, ship_sup, land_sup, plane_sup;
54     struct emp_qelem olist;     /* assaulting units */
55     struct emp_qelem dlist;     /* defending units */
56     int ototal;                 /* total assaulting strength */
57     int a_engineer = 0;         /* assaulter engineers are present */
58     int a_spy = 0;              /* the best assaulter scout */
59     double osupport = 1.0;      /* assault support */
60     double dsupport = 1.0;      /* defense support */
61     s_char *p;
62     s_char buf[1024];
63     int n;
64     int ourtotal;
65     struct emp_qelem *qp, *next;
66     struct llist *llp;
67     int rel;
68
69     att_combat_init(off, EF_SHIP);
70     att_combat_init(def, EF_SECTOR);
71     /*
72      * Collect input from the assaulter
73      */
74
75     /* What are we assaulting? */
76
77     if (!(p = getstarg(player->argp[1], "Sector :  ", buf)))
78         return RET_SYN;
79     if (!sarg_xy(p, &def->x, &def->y))
80         return RET_SYN;
81     if (att_abort(A_ASSAULT, 0, def))
82         return RET_FAIL;
83
84     /*
85      * Ask the assaulter what he wants to assault with
86      */
87
88     if ((off->shp_uid =
89          onearg(player->argp[2], "Assault from ship #")) < 0) {
90         pr("You may only assault from one ship!\n");
91         return RET_FAIL;
92     }
93     if (att_abort(A_ASSAULT, off, def)) {
94         pr("Assault aborted\n");
95         return RET_OK;
96     }
97
98     /* Show what we're assaulting, and check treaties */
99
100     if (att_show(def))
101         return RET_FAIL;
102
103     /* Ask about offensive support */
104
105     att_ask_support(3, &fort_sup, &ship_sup, &land_sup, &plane_sup);
106     if (att_abort(A_ASSAULT, off, def)) {
107         att_empty_attack(A_ASSAULT, 0, def);
108         return RET_OK;
109     }
110
111     /* Ask the player what he wants to assault with */
112
113     att_ask_offense(A_ASSAULT, off, def, &olist, &a_spy, &a_engineer);
114     if (att_abort(A_ASSAULT, off, def)) {
115         pr("Assault aborted\n");
116         att_empty_attack(A_ASSAULT, 0, def);
117         return att_free_lists(&olist, 0);
118     }
119
120     /* If we're assaulting our own sector, end here */
121     if (def->own == player->cnum) {
122         if (off->troops)
123             pr("You reinforce %s with %d troops\n",
124                xyas(def->x, def->y, player->cnum), off->troops);
125         if (off->troops || !QEMPTY(&olist))
126             att_move_in_off(A_ASSAULT, off, &olist, def);
127         return RET_OK;
128     }
129
130     /*
131      * Estimate the defense strength and give the player a chance to abort
132      */
133
134     ototal = att_estimate_defense(A_ASSAULT, off, &olist, def, a_spy);
135     if (att_abort(A_ASSAULT, off, def)) {
136         pr("Assault aborted\n");
137         att_empty_attack(A_ASSAULT, 0, def);
138         return att_free_lists(&olist, 0);
139     }
140
141     /*
142      * We have now got all the answers from the assaulter.  From this point
143      * forward, we can assume that this battle is the _only_ thing
144      * happening in the game.
145      */
146
147     /* First, we check to see if the only thing we have are spies
148      * assaulting.  If so, we try to sneak them on land.  If they
149      * make it, the defenders don't see a thing.  If they fail, well,
150      * the spies die, and the defenders see them. */
151
152     ourtotal = 0;
153     for (n = 0; n <= off->last; n++) {
154         if (off[n].type == EF_BAD)
155             continue;
156         ourtotal += off[n].troops * att_combat_eff(off + n);
157     }
158     for (qp = olist.q_forw; qp != &olist; qp = next) {
159         next = qp->q_forw;
160         llp = (struct llist *)qp;
161         if (lchr[(int)llp->land.lnd_type].l_flags & L_SPY)
162             continue;
163         ourtotal++;
164     }
165
166     /* If no attacking forces (i.e. we got here with only spies)
167      * then try to sneak on-land. */
168
169     if (!ourtotal) {
170         pr("Trying to sneak on shore...\n");
171
172         for (qp = olist.q_forw; qp != &olist; qp = next) {
173             next = qp->q_forw;
174             llp = (struct llist *)qp;
175             rel = getrel(getnatp(def->own), player->cnum);
176             if (chance(0.10) || rel == ALLIED || !def->own) {
177                 pr("%s made it on shore safely.\n", prland(&llp->land));
178                 llp->land.lnd_x = def->x;
179                 llp->land.lnd_y = def->y;
180                 llp->land.lnd_ship = -1;
181                 putland(llp->land.lnd_uid, &llp->land);
182             } else {
183                 pr("%s was spotted", prland(&llp->land));
184                 if (rel == HOSTILE || rel == AT_WAR || rel == SITZKRIEG ||
185                     rel == MOBILIZATION) {
186                     wu(0, def->own, "%s spy shot and killed in %s.\n",
187                        cname(player->cnum), xyas(def->x, def->y,
188                                                  def->own));
189                     pr(" and was killed in the attempt.\n");
190                     llp->land.lnd_effic = 0;
191                     putland(llp->land.lnd_uid, &llp->land);
192                     lnd_delete(llp, "");
193                 } else {
194                     wu(0, def->own, "%s spy spotted in %s.\n",
195                        cname(player->cnum), xyas(def->x, def->y,
196                                                  def->own));
197                     pr(" but made it ok.\n");
198                     llp->land.lnd_x = def->x;
199                     llp->land.lnd_y = def->y;
200                     llp->land.lnd_ship = -1;
201                     putland(llp->land.lnd_uid, &llp->land);
202                 }
203             }
204         }
205         return RET_OK;
206     }
207
208     /* Get the real defense */
209
210     att_get_defense(&olist, def, &dlist, a_spy, ototal);
211
212     /* Get assaulter and defender support */
213
214     att_get_support(A_ASSAULT, fort_sup, ship_sup, land_sup, plane_sup,
215                     &olist, off, &dlist, def, &osupport, &dsupport,
216                     a_engineer);
217     if (att_abort(A_ASSAULT, off, def)) {
218         pr("Assault aborted\n");
219         att_empty_attack(A_ASSAULT, 0, def);
220         return att_free_lists(&olist, &dlist);
221     }
222
223     /*
224      * Death, carnage, and destruction.
225      */
226
227     att_fight(A_ASSAULT, off, &olist, osupport, def, &dlist, dsupport);
228
229     return RET_OK;
230 }