]> git.pond.sub.org Git - empserver/blob - src/lib/commands/boar.c
356eb3797d7b7bb8c391ed00d8520ce3cf453d9b
[empserver] / src / lib / commands / boar.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, 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  *  boar.c: Board an enemy ship
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Markus Armbruster, 2011-2015
32  */
33
34 #include <config.h>
35
36 #include "combat.h"
37 #include "commands.h"
38 #include "land.h"
39 #include "retreat.h"
40 #include "ship.h"
41
42 int
43 boar(void)
44 {
45     struct combat off[1];       /* boarding ship or sector */
46     struct combat def[1];       /* defending ship */
47     struct emp_qelem olist;     /* boarding units */
48     struct emp_qelem dlist;     /* defending units */
49     int ototal;                 /* total boarding strength */
50     int a_engineer = 0;         /* boarder engineers are present */
51     int a_spy = 0;              /* the best boarder scout */
52     struct shpstr ship;         /* for retreating */
53     struct sctstr sect;
54     struct lndstr land;
55     struct nstr_item ni;
56     int foundland, def_uid;
57     char *p;
58     char buf[1024];
59
60     att_combat_init(def, EF_SHIP);
61     /*
62      * Collect input from the boarder
63      */
64
65     /* What are we boarding? */
66     p = getstarg(player->argp[1], "Victim ship #?  ", buf);
67     if (!p || (def->shp_uid = atoi(p)) < 0)
68         return RET_SYN;
69
70     /*
71      * Ask the boarder what he wants to board with
72      */
73
74     if (!(p = getstarg(player->argp[2], "Boarding party from? ", buf)))
75         return RET_SYN;
76     if (issector(p)) {
77         att_combat_init(off, EF_SECTOR);
78         if (!sarg_xy(p, &off->x, &off->y))
79             return RET_SYN;
80         getsect(off->x, off->y, &sect);
81         if (sect.sct_own != player->cnum) {
82             pr("You don't own %s!\n", xyas(off->x, off->y, player->cnum));
83             return RET_SYN;
84         }
85         if (sect.sct_mobil <= 0) {
86             /* Look for land units with mobility */
87             snxtitem_xy(&ni, EF_LAND, off->x, off->y);
88             foundland = 0;
89             while (nxtitem(&ni, &land)) {
90                 if (land.lnd_own != player->cnum)
91                     continue;
92                 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
93                     continue;
94                 if (land.lnd_mobil <= 0)
95                     continue;
96                 /* Only land units with assault can board */
97                 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT))
98                     continue;
99                 foundland = 1;
100             }
101             if (!foundland) {
102                 pr("You don't have any mobility (sector or land units) in %s!\n",
103                    xyas(off->x, off->y, player->cnum));
104                 return RET_SYN;
105             }
106         }
107     } else {
108         att_combat_init(off, EF_SHIP);
109         if ((off->shp_uid = atoi(p)) < 0)
110             return RET_SYN;
111     }
112     if (att_abort(A_BOARD, off, def)) {
113         pr("Board aborted\n");
114         return RET_OK;
115     }
116
117     /* Fire at the attacking ship */
118
119     att_approach(off, def);
120     if (att_abort(A_BOARD, off, def)) {
121         pr("Board aborted\n");
122         att_empty_attack(A_BOARD, 0, def);
123         return RET_OK;
124     }
125
126     /* Show what we're boarding */
127     att_show(def);
128
129     /* Ask the player what he wants to board with */
130
131     att_ask_offense(A_BOARD, off, def, &olist, &a_spy, &a_engineer);
132     if (att_abort(A_BOARD, off, def)) {
133         pr("Board aborted\n");
134         att_empty_attack(A_BOARD, 0, def);
135         return att_free_lists(&olist, NULL);
136     }
137
138     ototal = att_get_offense(A_BOARD, off, &olist, def);
139     if (att_abort(A_BOARD, off, def)) {
140         pr("Board aborted\n");
141         att_empty_attack(A_BOARD, 0, def);
142         return att_free_lists(&olist, NULL);
143     }
144
145     /*
146      * We have now got all the answers from the boarder.  From this point
147      * forward, we can assume that this battle is the _only_ thing
148      * happening in the game.
149      */
150
151     /* Get the real defense */
152
153     att_get_defense(&olist, def, &dlist, a_spy, ototal);
154
155     /*
156      * Death, carnage, and destruction.
157      */
158
159     /*
160      * Careful: when the fight sinks the ship, putcombat() clobbers
161      * *def (see FIXME there).
162      */
163     def_uid = def->shp_uid;
164     if (!(att_fight(A_BOARD, off, &olist, 1.0, def, &dlist, 1.0))) {
165         getship(def_uid, &ship);
166         if (ship.shp_rflags & RET_BOARDED) {
167             retreat_ship(&ship, 'u');
168             putship(def_uid, &ship);
169         }
170     }
171
172     return RET_OK;
173 }