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