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