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
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.
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.
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/>.
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.
27 * nav_ship.c: Navigate ships and such
29 * Known contributors to this file:
32 * Markus Armbruster, 2004-2011
43 static void swap(struct shpstr *);
46 scuttle_it(struct shpstr *sp)
50 sp->shp_autonav &= ~AN_SCUTTLE;
51 if (!(sectp = getsectp(sp->shp_x, sp->shp_y))) {
52 wu(0, 0, "bad sector (%d,%d) ship %d\n",
53 sp->shp_x, sp->shp_y, sp->shp_uid);
56 if (CANT_HAPPEN(!(mchr[sp->shp_type].m_flags & M_TRADE)))
58 if (!scuttle_tradeship(sp, 0)) {
60 "%s doesn't pay here! Not scuttled.\n", prship(sp));
63 wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
64 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
65 if (sectp->sct_own == sp->shp_own)
66 unit_drop_cargo((struct empobj *)sp, sectp->sct_own);
68 putship(sp->shp_uid, sp);
72 nav_check_atdest(struct shpstr *sp)
74 if ((sp->shp_x == sp->shp_destx[0]) && (sp->shp_y == sp->shp_desty[0])) {
75 if ((sp->shp_destx[0] == sp->shp_destx[1]) &&
76 (sp->shp_desty[0] == sp->shp_desty[1])) {
80 sp->shp_autonav &= ~AN_AUTONAV;
81 wu(0, sp->shp_own, "%s arrived at %s, finished\n",
82 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
83 if (sp->shp_autonav & AN_SCUTTLE) {
87 /* unload all cargo */
89 wu(0, sp->shp_own, "%s arrived at %s\n",
90 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
95 sp->shp_autonav &= ~AN_LOADING;
98 /* flip the 2 fields that deal with autonav movement. */
102 swap(struct shpstr *sp)
109 tcord = sp->shp_destx[0];
110 sp->shp_destx[0] = sp->shp_destx[1];
111 sp->shp_destx[1] = tcord;
112 tcord = sp->shp_desty[0];
113 sp->shp_desty[0] = sp->shp_desty[1];
114 sp->shp_desty[1] = tcord;
116 for (i = 0; i < TMAX; ++i) {
117 lev[i] = sp->shp_lstart[i];
118 tcomm[i] = sp->shp_tstart[i];
121 for (i = 0; i < TMAX; ++i) {
122 sp->shp_lstart[i] = sp->shp_lend[i];
123 sp->shp_tstart[i] = sp->shp_tend[i];
126 for (i = 0; i < TMAX; ++i) {
127 sp->shp_lend[i] = lev[i];
128 sp->shp_tend[i] = tcomm[i];
132 sp->shp_autonav |= AN_LOADING;
141 nav_loadship(struct shpstr *sp)
143 struct sctstr *sectp;
144 int i, didsomething[TMAX], rel;
146 for (i = 0; i < TMAX; i++)
149 /* Turn off the loading flag.
150 * if any of the loads fail on the ship
151 * it will be turned back on.
154 sp->shp_autonav &= ~AN_LOADING;
156 if (!(sectp = getsectp(sp->shp_x, sp->shp_y)))
157 return 0; /* safety */
159 rel = relations_with(sectp->sct_own, sp->shp_own);
161 /* loop through each field for that ship */
162 for (i = 0; i < TMAX; ++i) {
163 /* check and see if the data fields have been set. */
165 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
166 /* nothing to do move on. */
170 if (sectp->sct_own == 0) {
171 /* either sea or deity harbor */
175 if (!sect_has_dock(sectp)) {
176 /* we can only load in harbors */
181 didsomething[i] = load_it(sp, sectp, i);
184 /* check for any unsucessful loads */
185 /* if we have any return 0 to stop */
186 /* the nav_ship loop. */
188 for (i = 0; i < TMAX; i++) {
189 if (didsomething[i] == 0)
192 /* All loads were succesful */
197 nav_load_ship_at_sea(struct shpstr *sp)
201 int max_amt, item_amt;
203 struct sctstr *sectp;
204 struct check_list_st {
207 } check_list[] = {{M_FOOD, I_FOOD}, {M_OIL, I_OIL}};
209 n_items = sizeof(check_list) / sizeof(check_list[0]);
211 mcp = &mchr[(int)sp->shp_type];
212 sectp = getsectp(sp->shp_x, sp->shp_y);
213 for (i = 0; i < n_items; i++) {
214 if (mcp->m_flags & check_list[i].cap) {
215 item_amt = sp->shp_item[check_list[i].item];
216 max_amt = mcp->m_item[check_list[i].item];
217 if (item_amt < max_amt && sectp->sct_type == SCT_WATER)
226 * 1. Try and move to the next sector/harbor given by the player.
227 * 2. Once we reach a harbor try and load all cargo holds for that ship.
228 * 3. If the ship reaches its max levels set by the player try to use
229 * up all mobility getting to the next harbor.
230 * Continue to loop until the ship runs out of mobility, a load fails,
231 * the ship gets sunk (forts,ect..), the ship hits a mine.
233 * Questions, bugs (fixes) , or new ideas should be directed at
236 * Modified to use shp_nav by Ken Stevens 1995
239 nav_ship(struct shpstr *sp)
244 int didsomething = 0;
246 struct emp_qelem ship_list;
247 struct emp_qelem *qp, *newqp;
253 /* just return if no autonaving to do for this ship */
254 if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
257 /* Make a list of one ships so we can use the navi.c code */
258 emp_initque(&ship_list);
259 mlp = malloc(sizeof(struct ulist));
260 mlp->chrp = (struct empobj_chr *)(mchr + sp->shp_type);
261 mlp->unit.ship = *sp;
262 ef_mark_fresh(EF_SHIP, &mlp->unit.ship);
263 mlp->mobil = sp->shp_mobil;
264 emp_insque(&mlp->queue, &ship_list);
267 if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
268 (!(sp->shp_autonav & AN_STANDBY))) {
269 shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
271 if (QEMPTY(&ship_list))
274 if (path_find(sp->shp_x, sp->shp_y,
275 sp->shp_destx[0], sp->shp_desty[0],
276 sp->shp_own, MOB_SAIL) < 0) {
278 "%s bad path, ship put on standby\n", prship(sp));
279 sp->shp_autonav |= AN_STANDBY;
280 putship(sp->shp_uid, sp);
282 /* We need to free the ship list */
283 qp = ship_list.q_forw;
284 while (qp != &ship_list) {
292 path_find_route(buf, sizeof(buf), sp->shp_x, sp->shp_y,
293 sp->shp_destx[0], sp->shp_desty[0]);
297 while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
299 stopping |= shp_nav_one_sector(&ship_list, dir,
305 nav_check_atdest(sp);
308 quit = 0; /* stop loop */
310 /* Try to load the ship */
311 if (sp->shp_autonav & AN_LOADING) {
312 didsomething = nav_loadship(sp);
316 /* special case for fishing boats and oil derricks */
317 if (nav_load_ship_at_sea(sp))
319 /* reset flag and check if we can move. */
321 } while (quit); /* end loop */
323 putship(sp->shp_uid, sp);
325 /* We need to free the ship list (just in case) */
326 qp = ship_list.q_forw;
327 while (qp != &ship_list) {