2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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 * unitsub.c: Common subroutines for multiple type of units
30 * Known contributors to this file:
31 * Ron Koenderink, 2007
32 * Markus Armbruster, 2009
41 #include "prototypes.h"
45 unit_list(struct emp_qelem *unit_list)
48 struct emp_qelem *next;
50 int type, npln, nch, nxl;
55 if (CANT_HAPPEN(QEMPTY(unit_list)))
57 qp = unit_list->q_back;
58 ulp = (struct ulist *)qp;
59 type = ulp->unit.gen.ef_type;
60 if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
64 pr("lnd# land type x,y a eff sh gun xl mu tech retr\n");
66 pr("shp# ship type x,y fl eff mil sh gun pn he xl ln mob tech\n");
68 for (; qp != unit_list; qp = next) {
70 ulp = (struct ulist *)qp;
71 lnd = &ulp->unit.land;
72 shp = &ulp->unit.ship;
73 unit = &ulp->unit.gen;
74 if (CANT_HAPPEN(type != unit->ef_type))
76 pr("%4d ", unit->uid);
77 pr("%-16.16s ", empobj_chr_name(unit));
78 prxy("%4d,%-4d ", unit->x, unit->y, unit->own);
79 pr("%1.1s", &unit->group);
80 pr("%4d%%", unit->effic);
81 if (type == EF_LAND) {
82 pr("%4d", lnd->lnd_item[I_SHELL]);
83 pr("%4d", lnd->lnd_item[I_GUN]);
84 pr("%3d", lnd_nxlight(lnd));
86 pr("%4d", shp->shp_item[I_MILIT]);
87 pr("%4d", shp->shp_item[I_SHELL]);
88 pr("%4d", shp->shp_item[I_GUN]);
89 npln = shp_nplane(shp, &nch, &nxl, NULL);
90 pr("%3d%3d%3d", npln - nch - nxl, nch, nxl);
91 pr("%3d", shp_nland(shp));
93 pr("%4d", unit->mobil);
94 pr("%4d", unit->tech);
95 if (type == EF_LAND) {
96 pr("%4d%%", lnd->lnd_retreat);
103 unit_put(struct emp_qelem *list, natid actor)
105 struct emp_qelem *qp;
106 struct emp_qelem *newqp;
112 ulp = (struct ulist *)qp;
113 unit = &ulp->unit.gen;
114 if (CANT_HAPPEN(unit->ef_type != EF_LAND
115 && unit->ef_type != EF_SHIP))
118 mpr(actor, "%s stopped at %s\n", obj_nameof(unit),
119 xyas(unit->x, unit->y, unit->own));
120 if (unit->ef_type == EF_LAND) {
121 if (ulp->mobil < -127)
123 unit->mobil = ulp->mobil;
126 if (unit->ef_type == EF_SHIP)
127 unit->mobil = (int)ulp->mobil;
128 put_empobj(unit->ef_type, unit->uid, unit);
137 unit_path(int together, struct empobj *unit, char *buf)
141 struct sctstr d_sect, sect;
146 if (CANT_HAPPEN(unit->ef_type != EF_LAND && unit->ef_type != EF_SHIP))
149 if (!sarg_xy(buf, &destx, &desty))
152 pr("Cannot go to a destination sector if not all starting in the same sector\n");
155 if (!getsect(destx, desty, &d_sect)) {
156 pr("%d,%d is not a sector\n", destx, desty);
159 if (unit->ef_type == EF_SHIP) {
160 cp = BestShipPath(buf, unit->x, unit->y,
161 d_sect.sct_x, d_sect.sct_y, player->cnum);
162 if (!cp || unit->mobil <= 0) {
163 pr("Can't get to '%s' right now.\n",
164 xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
168 getsect(unit->x, unit->y, §);
169 mtype = lnd_mobtype((struct lndstr *)unit);
170 cp = BestLandPath(buf, §, &d_sect, &dummy, mtype);
172 pr("No owned %s from %s to %s!\n",
173 mtype == MOB_RAIL ? "railway" : "path",
174 xyas(unit->x, unit->y, player->cnum),
175 xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
178 pr("Using path '%s'\n", cp);
184 unit_view(struct emp_qelem *list)
187 struct emp_qelem *qp;
188 struct emp_qelem *next;
191 for (qp = list->q_back; qp != list; qp = next) {
193 ulp = (struct ulist *)qp;
194 if (CANT_HAPPEN(!(ef_flags(ulp->unit.gen.ef_type) & EFF_XY)))
196 getsect(ulp->unit.gen.x, ulp->unit.gen.y, §);
197 if (ulp->unit.gen.ef_type == EF_SHIP) {
198 if (((struct mchrstr *)ulp->chrp)->m_flags & M_FOOD)
199 pr("[fert:%d] ", sect.sct_fertil);
200 if (((struct mchrstr *)ulp->chrp)->m_flags & M_OIL)
201 pr("[oil:%d] ", sect.sct_oil);
203 pr("%s @ %s %d%% %s\n", obj_nameof(&ulp->unit.gen),
204 xyas(ulp->unit.gen.x, ulp->unit.gen.y, player->cnum),
205 sect.sct_effic, dchr[sect.sct_type].d_name);
210 * Update cargo of CARRIER for movement or destruction.
211 * If the carrier is destroyed, destroy its cargo (planes, land units,
213 * Else update their location to the carrier's. Any op sectors equal
214 * to location get updated, too.
217 unit_update_cargo(struct empobj *carrier)
221 union empobj_storage obj;
223 for (cargo_type = EF_PLANE; cargo_type <= EF_NUKE; cargo_type++) {
224 snxtitem_cargo(&ni, cargo_type, carrier->ef_type, carrier->uid);
225 while (nxtitem(&ni, &obj)) {
227 mpr(obj.gen.own, "%s lost!\n", obj_nameof(&obj.gen));
230 /* mission op-area centered on the obj travels with it */
231 if (obj.gen.opx == obj.gen.x && obj.gen.opy == obj.gen.y) {
232 obj.gen.opx = carrier->x;
233 obj.gen.opy = carrier->y;
235 obj.gen.x = carrier->x;
236 obj.gen.y = carrier->y;
238 put_empobj(cargo_type, obj.gen.uid, &obj);
244 * Drop cargo of UNIT.
245 * Give it to NEWOWN, unless it's zero.
248 unit_drop_cargo(struct empobj *unit, natid newown)
252 union empobj_storage cargo;
254 for (type = EF_PLANE; type <= EF_NUKE; type++) {
255 snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
256 while (nxtitem(&ni, &cargo)) {
259 cargo.plane.pln_ship = cargo.plane.pln_land = -1;
262 cargo.land.lnd_ship = cargo.land.lnd_land = -1;
265 cargo.nuke.nuk_plane = -1;
268 mpr(cargo.gen.own, "%s transferred off %s %d to %s\n",
269 obj_nameof(&cargo.gen),
270 ef_nameof(unit->ef_type), unit->uid,
271 xyas(cargo.gen.x, cargo.gen.y, cargo.gen.own));
273 unit_give_away(&cargo.gen, newown, cargo.gen.own);
274 put_empobj(type, cargo.gen.uid, &cargo.gen);
280 * Give UNIT and its cargo to RECIPIENT.
281 * No action if RECIPIENT already owns UNIT.
282 * If GIVER is non-zero, inform RECIPIENT and GIVER of the transaction.
283 * Clears mission and group on the units given away.
286 unit_give_away(struct empobj *unit, natid recipient, natid giver)
290 union empobj_storage cargo;
292 if (unit->own == recipient)
296 mpr(unit->own, "%s given to %s\n",
297 obj_nameof(unit), cname(recipient));
298 mpr(recipient, "%s given to you by %s\n",
299 obj_nameof(unit), cname(giver));
302 unit->own = recipient;
303 unit_wipe_orders(unit);
304 put_empobj(unit->ef_type, unit->uid, unit);
306 for (type = EF_PLANE; type <= EF_NUKE; type++) {
307 snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
308 while (nxtitem(&ni, &cargo))
309 unit_give_away(&cargo.gen, recipient, giver);
314 * Wipe orders and such from UNIT.
317 unit_wipe_orders(struct empobj *unit)
325 unit->opx = unit->opy = 0;
329 switch (unit->ef_type) {
331 sp = (struct shpstr *)unit;
332 sp->shp_destx[0] = sp->shp_desty[0] = 0;
333 sp->shp_destx[1] = sp->shp_desty[1] = 0;
334 for (i = 0; i < TMAX; ++i) {
335 sp->shp_tstart[i] = I_NONE;
336 sp->shp_tend[i] = I_NONE;
337 sp->shp_lstart[i] = 0;
341 sp->shp_mobquota = 0;
343 sp->shp_follow = sp->shp_uid;
345 sp->shp_rpath[0] = 0;
348 pp = (struct plnstr *)unit;
349 pp->pln_range = pln_range_max(pp);
352 lp = (struct lndstr *)unit;
353 lp->lnd_retreat = morale_base;
355 lp->lnd_rpath[0] = 0;