2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * cargo.c: Cargo lists
30 * Known contributors to this file:
31 * Markus Armbruster, 2008
43 short head[EF_NUKE - EF_PLANE + 1];
49 * Persistent game state encodes "who carries what" by storing the
50 * carrier uid in the cargo. Cargo lists augment that: they store
51 * lists of cargo for each carrier.
53 * clink[TYPE] points to an array of cargo list links. The array has
54 * nclink[TYPE] elements. nclink[TYPE] tracks ef_nelem(TYPE).
55 * clink[TYPE][UID] is the cargo list link for unit UID of type TYPE.
56 * TYPE must be a unit file type: EF_SHIP, EF_PLANE, EF_LAND or
57 * EF_NUKE. Other slots of clink[] and nclink[] are unused and remain
60 * clink[TYPE][UID].next is the uid of the next unit of the same type
61 * in the same carrier, -1 if none.
63 * clink[TYPE][UID].head[CARGO-EF_PLANE] is the uid of the first unit
64 * of type CARGO carried by this unit, -1 if none. The next unit, if
65 * any, is clink[CARGO][clink[TYPE][UID].head[CARGO-EF_PLANE]].next,
68 * Each type of carrier can only carry certain types of cargo, but
69 * cargo lists know nothing about that.
71 static struct clink *clink[EF_NUKE + 1];
72 static short nclink[EF_NUKE + 1];
75 * Return pointer to CL's cargo list head for file type TYPE.
78 clink_headp(struct clink *cl, int type)
82 if (CANT_HAPPEN(type < EF_PLANE || type > EF_NUKE)) {
86 return &cl->head[type - EF_PLANE];
90 * Initialize cargo list link CL to empty.
93 clink_init(struct clink *cl)
98 for (i = 0; i < sizeof(cl->head) / sizeof(*cl->head); i++)
103 * Check whether *UIDP is a valid uid for file type TYPE.
106 clink_check1(short *uidp, int type)
108 if (CANT_HAPPEN(*uidp >= nclink[type]))
113 * Check validity of cargo lists for file type TYPE.
116 clink_check(int type)
120 /* check the heads for all carriers */
121 if (type != EF_SHIP) {
122 for (carr_type = EF_PLANE; carr_type <= EF_NUKE; carr_type++) {
123 for (i = 0; i < nclink[carr_type]; i++)
124 clink_check1(clink_headp(&clink[carr_type][i], type),
128 /* check the nexts */
129 for (i = 0; i < nclink[type]; i++)
130 clink_check1(&clink[type][i].next, type);
134 * Add to CL's cargo list for type TYPE the uid UID.
135 * UID must not be on any cargo list already.
138 clink_add(struct clink *cl, int type, int uid)
140 short *head = clink_headp(cl, type);
142 if (CANT_HAPPEN(type < 0 || type > EF_NUKE
143 || uid < 0 || uid >= nclink[type]))
145 if (CANT_HAPPEN(*head >= nclink[type]))
147 CANT_HAPPEN(clink[type][uid].next >= 0);
148 clink[type][uid].next = *head;
153 * Remove from CL's cargo list for type TYPE the uid UID.
154 * UID must be on that cargo list.
157 clink_rem(struct clink *cl, int type, int uid)
159 short *head = clink_headp(cl, type);
164 if (CANT_HAPPEN(type < 0 || type > EF_NUKE))
169 for (p = head; *p != uid; p = &linkv[*p].next) {
170 if (CANT_HAPPEN(*p < 0 || *p >= n))
174 *p = linkv[uid].next;
175 linkv[uid].next = -1;
179 * Update cargo lists for a change of CARGO's carrier.
180 * Carrier is of type TYPE, and changes from uid OLD to NEW.
181 * Negative uids mean no carrier.
184 unit_carrier_change(struct empobj *cargo, int type, int old, int new)
186 if (CANT_HAPPEN(type < 0 || type > EF_NUKE))
189 clink_rem(&clink[type][old], cargo->ef_type, cargo->uid);
191 clink_add(&clink[type][new], cargo->ef_type, cargo->uid);
195 * Update cargo lists for a change of PP's carrier.
196 * Carrier is of type TYPE, and changes from uid OLD to NEW.
197 * Negative uids mean no carrier.
200 pln_carrier_change(struct plnstr *pp, int type, int old, int new)
202 unit_carrier_change((struct empobj *)pp, type, old, new);
206 * Update cargo lists for a change of LP's carrier.
207 * Carrier is of type TYPE, and changes from uid OLD to NEW.
208 * Negative uids mean no carrier.
211 lnd_carrier_change(struct lndstr *lp, int type, int old, int new)
213 unit_carrier_change((struct empobj *)lp, type, old, new);
217 * Update cargo lists for a change of NP's carrier.
218 * Carrier is of type TYPE, and changes from uid OLD to NEW.
219 * Negative uids mean no carrier.
222 nuk_carrier_change(struct nukstr *np, int type, int old, int new)
224 unit_carrier_change((struct empobj *)np, type, old, new);
228 * Initialize cargo lists from game state.
231 unit_cargo_init(void)
238 for (i = EF_SHIP; i <= EF_NUKE; i++) {
243 for (i = 0; (pp = getplanep(i)); i++) {
246 if (CANT_HAPPEN(pp->pln_ship >= 0 && pp->pln_land >= 0))
248 pln_carrier_change(pp, EF_SHIP, -1, pp->pln_ship);
249 pln_carrier_change(pp, EF_LAND, -1, pp->pln_land);
251 for (i = 0; (lp = getlandp(i)); i++) {
254 if (CANT_HAPPEN(lp->lnd_ship >= 0 && lp->lnd_land >= 0))
256 lnd_carrier_change(lp, EF_SHIP, -1, lp->lnd_ship);
257 lnd_carrier_change(lp, EF_LAND, -1, lp->lnd_land);
259 for (i = 0; (np = getnukep(i)); i++) {
262 nuk_carrier_change(np, EF_PLANE, -1, np->nuk_plane);
267 * Resize clink[TYPE] to match ef_nelem(TYPE).
268 * Return 0 on success, -1 on error.
269 * This is the struct empfile onresize callback for units.
272 unit_onresize(int type)
277 if (CANT_HAPPEN(type < EF_SHIP || type > EF_NUKE))
281 cl = realloc(clink[type], n * sizeof(*clink[type]));
284 for (i = nclink[type]; i < n; i++)
293 * Find first unit on a carrier's cargo list for file type CARGO_TYPE.
294 * Search carrier UID of type TYPE.
295 * Return first unit's uid, or -1 if the carrier isn't carrying such
299 unit_cargo_first(int type, int uid, int cargo_type)
303 if (CANT_HAPPEN(type < EF_SHIP || type > EF_NUKE))
305 if (CANT_HAPPEN(uid < 0 || uid >= nclink[type]))
307 headp = clink_headp(&clink[type][uid], cargo_type);
308 if (CANT_HAPPEN(!headp))
314 * Find the next unit on a cargo list for file type CARGO_TYPE.
315 * Get the unit after CARGO_UID.
316 * Return its uid, or -1 if there are no more on this list.
319 unit_cargo_next(int cargo_type, int cargo_uid)
321 if (CANT_HAPPEN(cargo_type < EF_SHIP || cargo_type > EF_NUKE))
323 if (CANT_HAPPEN(cargo_uid < 0 || cargo_uid >= nclink[cargo_type]))
325 return clink[cargo_type][cargo_uid].next;
329 * If SP carries planes, return the uid of the first one, else -1.
332 pln_first_on_ship(struct shpstr *sp)
334 return unit_cargo_first(EF_SHIP, sp->shp_uid, EF_PLANE);
338 * If LP carries planes, return the uid of the first one, else -1.
341 pln_first_on_land(struct lndstr *lp)
343 return unit_cargo_first(EF_LAND, lp->lnd_uid, EF_PLANE);
347 * Find the next plane on the same carrier as plane#UID.
348 * Return its uid, or -1 if there are no more.
351 pln_next_on_unit(int uid)
353 return unit_cargo_next(EF_PLANE, uid);
357 * If SP carries land units, return the uid of the first one, else -1.
360 lnd_first_on_ship(struct shpstr *sp)
362 return unit_cargo_first(EF_SHIP, sp->shp_uid, EF_LAND);
366 * If SP carries land units, return the uid of the first one, else -1.
369 lnd_first_on_land(struct lndstr *lp)
371 return unit_cargo_first(EF_LAND, lp->lnd_uid, EF_LAND);
375 * Find the next land unit on the same carrier as land#UID.
376 * Return its uid, or -1 if there are no more.
379 lnd_next_on_unit(int uid)
381 return unit_cargo_next(EF_LAND, uid);
385 * If PP carries a nuke, return its uid, else -1.
388 nuk_on_plane(struct plnstr *pp)
390 return unit_cargo_first(EF_PLANE, pp->pln_uid, EF_NUKE);
394 * Return length of a carrier's cargo list for file type CARGO_TYPE.
397 unit_cargo_count(int type, int uid, int cargo_type)
402 for (cargo = unit_cargo_first(type, uid, cargo_type);
404 cargo = unit_cargo_next(cargo_type, cargo))
411 * Return number of land units loaded on SP.
414 shp_nland(struct shpstr *sp)
416 return unit_cargo_count(EF_SHIP, sp->shp_uid, EF_LAND);
420 * Return number of land units loaded on LP.
423 lnd_nland(struct lndstr *lp)
425 return unit_cargo_count(EF_LAND, lp->lnd_uid, EF_LAND);
429 unit_nplane(int type, int uid, int *nchopper, int *nxlight, int *nmsl)
431 int n, nch, nxl, nms, cargo;
433 struct plchrstr *pcp;
435 n = nxl = nch = nms = 0;
436 for (cargo = unit_cargo_first(type, uid, EF_PLANE);
437 (pp = getplanep(cargo));
438 cargo = pln_next_on_unit(cargo)) {
439 pcp = &plchr[pp->pln_type];
440 if (pcp->pl_flags & P_K)
442 else if (pcp->pl_flags & P_E)
444 else if (pcp->pl_flags & P_M)
459 shp_nplane(struct shpstr *sp, int *nchopper, int *nxlight, int *nmsl)
461 return unit_nplane(EF_SHIP, sp->shp_uid, nchopper, nxlight, nmsl);
465 lnd_nxlight(struct lndstr *lp)
469 unit_nplane(EF_LAND, lp->lnd_uid, NULL, &n, NULL);