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