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