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
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 * unitsub.c: Common subroutines for multiple type of units
29 * Known contributors to this file:
30 * Ron Koenderink, 2007
31 * Markus Armbruster, 2009-2014
40 #include "prototypes.h"
44 unit_nameof(struct empobj *gp)
46 switch (gp->ef_type) {
48 return prship((struct shpstr *)gp);
50 return prplane((struct plnstr *)gp);
52 return prland((struct lndstr *)gp);
54 return prnuke((struct nukstr *)gp);
57 return "The Beast #666";
61 unit_list(struct emp_qelem *unit_list)
64 struct emp_qelem *next;
66 int type, npln, nch, nxl;
71 if (CANT_HAPPEN(QEMPTY(unit_list)))
73 qp = unit_list->q_back;
74 ulp = (struct ulist *)qp;
75 type = ulp->unit.gen.ef_type;
76 if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
80 pr("lnd# land type x,y a eff mil sh gun xl ln mu tech retr\n");
82 pr("shp# ship type x,y fl eff mil sh gun pn he xl ln mob tech\n");
84 for (; qp != unit_list; qp = next) {
86 ulp = (struct ulist *)qp;
87 lnd = &ulp->unit.land;
88 shp = &ulp->unit.ship;
89 unit = &ulp->unit.gen;
90 if (CANT_HAPPEN(type != unit->ef_type))
92 pr("%4d ", unit->uid);
93 pr("%-16.16s ", empobj_chr_name(unit));
94 prxy("%4d,%-4d ", unit->x, unit->y);
95 pr("%1.1s", &unit->group);
96 pr("%4d%%", unit->effic);
97 if (type == EF_LAND) {
98 pr("%4d", lnd->lnd_item[I_MILIT]);
99 pr("%4d", lnd->lnd_item[I_SHELL]);
100 pr("%4d", lnd->lnd_item[I_GUN]);
101 pr("%3d%3d", lnd_nxlight(lnd), lnd_nland(lnd));
103 pr("%4d", shp->shp_item[I_MILIT]);
104 pr("%4d", shp->shp_item[I_SHELL]);
105 pr("%4d", shp->shp_item[I_GUN]);
106 npln = shp_nplane(shp, &nch, &nxl, NULL);
107 pr("%3d%3d%3d", npln - nch - nxl, nch, nxl);
108 pr("%3d", shp_nland(shp));
110 pr("%4d", unit->mobil);
111 pr("%4d", unit->tech);
112 if (type == EF_LAND) {
113 pr("%4d%%", lnd->lnd_retreat);
120 unit_path(int together, struct empobj *unit, char *buf, size_t bufsz)
129 if (CANT_HAPPEN(unit->ef_type != EF_LAND && unit->ef_type != EF_SHIP))
132 if (!sarg_xy(buf, &destx, &desty))
135 pr("Cannot go to a destination sector if not all starting in the same sector\n");
138 if (unit->ef_type == EF_SHIP) {
139 c = path_find(unit->x, unit->y, destx, desty,
140 player->cnum, MOB_SAIL);
141 if (c < 0 || unit->mobil <= 0) {
142 pr("Can't get to '%s' right now.\n",
143 xyas(destx, desty, player->cnum));
147 getsect(unit->x, unit->y, §);
148 mtype = lnd_mobtype((struct lndstr *)unit);
150 * Note: passing sect.sct_own for actor is funny, but works:
151 * its only effect is to confine the search to that nation's
152 * land. It doesn't affect mobility costs. The real actor is
153 * different for marching in allied land, and passing it would
154 * break path finding there.
156 c = path_find(unit->x, unit->y, destx, desty, sect.sct_own, mtype);
158 pr("No owned %s from %s to %s!\n",
159 mtype == MOB_RAIL ? "railway" : "path",
160 xyas(unit->x, unit->y, player->cnum),
161 xyas(destx, desty, player->cnum));
165 len = path_find_route(buf, bufsz, unit->x, unit->y, destx, desty);
166 if (len == 0 || unit->ef_type == EF_LAND) {
168 strcpy(buf + len, "h");
172 pr("Can't handle path to %s, it's too long, sorry\n",
173 xyas(destx, desty, player->cnum));
176 pr("Using path '%s'\n", buf);
181 unit_view(struct emp_qelem *list)
184 struct emp_qelem *qp;
185 struct emp_qelem *next;
188 for (qp = list->q_back; qp != list; qp = next) {
190 ulp = (struct ulist *)qp;
191 if (CANT_HAPPEN(!(ef_flags(ulp->unit.gen.ef_type) & EFF_XY)))
193 getsect(ulp->unit.gen.x, ulp->unit.gen.y, §);
194 if (ulp->unit.gen.ef_type == EF_SHIP) {
195 if (((struct mchrstr *)ulp->chrp)->m_flags & M_FOOD)
196 pr("[fert:%d] ", sect.sct_fertil);
197 if (((struct mchrstr *)ulp->chrp)->m_flags & M_OIL)
198 pr("[oil:%d] ", sect.sct_oil);
200 pr("%s @ %s %d%% %s\n", unit_nameof(&ulp->unit.gen),
201 xyas(ulp->unit.gen.x, ulp->unit.gen.y, player->cnum),
202 sect.sct_effic, dchr[sect.sct_type].d_name);
207 * Teleport UNIT to X,Y.
208 * If UNIT's mission op-area is centered on it, keep it centered.
211 unit_teleport(struct empobj *unit, coord x, coord y)
213 if (unit->opx == unit->x && unit->opy == unit->y) {
222 * Update cargo of CARRIER for movement or destruction.
223 * If the carrier is destroyed, destroy its cargo (planes, land units,
225 * Else update their location to the carrier's. Any op sectors equal
226 * to location get updated, too.
227 * Return number of units updated.
230 unit_update_cargo(struct empobj *carrier)
234 union empobj_storage obj;
237 for (cargo_type = EF_PLANE; cargo_type <= EF_NUKE; cargo_type++) {
238 snxtitem_cargo(&ni, cargo_type, carrier->ef_type, carrier->uid);
239 while (nxtitem(&ni, &obj)) {
241 unit_teleport(&obj.gen, carrier->x, carrier->y);
243 mpr(obj.gen.own, "%s lost!\n", unit_nameof(&obj.gen));
246 put_empobj(cargo_type, obj.gen.uid, &obj);
254 * Drop cargo of UNIT.
255 * Give it to NEWOWN, unless it's zero.
258 unit_drop_cargo(struct empobj *unit, natid newown)
262 union empobj_storage cargo;
264 for (type = EF_PLANE; type <= EF_NUKE; type++) {
265 snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
266 while (nxtitem(&ni, &cargo)) {
269 cargo.plane.pln_ship = cargo.plane.pln_land = -1;
272 cargo.land.lnd_ship = cargo.land.lnd_land = -1;
275 cargo.nuke.nuk_plane = -1;
278 mpr(cargo.gen.own, "%s transferred off %s %d to %s\n",
279 unit_nameof(&cargo.gen),
280 ef_nameof(unit->ef_type), unit->uid,
281 xyas(cargo.gen.x, cargo.gen.y, cargo.gen.own));
283 unit_give_away(&cargo.gen, newown, cargo.gen.own);
284 put_empobj(type, cargo.gen.uid, &cargo.gen);
290 * Give UNIT and its cargo to RECIPIENT.
291 * No action if RECIPIENT already owns UNIT.
292 * If GIVER is non-zero, inform RECIPIENT and GIVER of the transaction.
293 * Clears mission and group on the units given away.
296 unit_give_away(struct empobj *unit, natid recipient, natid giver)
300 union empobj_storage cargo;
302 if (unit->own == recipient)
306 mpr(unit->own, "%s given to %s\n",
307 unit_nameof(unit), cname(recipient));
308 mpr(recipient, "%s given to you by %s\n",
309 unit_nameof(unit), cname(giver));
312 unit->own = recipient;
313 unit_wipe_orders(unit);
314 put_empobj(unit->ef_type, unit->uid, unit);
316 for (type = EF_PLANE; type <= EF_NUKE; type++) {
317 snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
318 while (nxtitem(&ni, &cargo))
319 unit_give_away(&cargo.gen, recipient, giver);
324 * Wipe orders and such from UNIT.
327 unit_wipe_orders(struct empobj *unit)
335 unit->opx = unit->opy = 0;
339 switch (unit->ef_type) {
341 sp = (struct shpstr *)unit;
342 sp->shp_destx[0] = sp->shp_desty[0] = 0;
343 sp->shp_destx[1] = sp->shp_desty[1] = 0;
344 for (i = 0; i < TMAX; ++i) {
345 sp->shp_tstart[i] = I_NONE;
346 sp->shp_tend[i] = I_NONE;
347 sp->shp_lstart[i] = 0;
351 sp->shp_mobquota = 0;
353 sp->shp_follow = sp->shp_uid;
355 sp->shp_rpath[0] = 0;
358 pp = (struct plnstr *)unit;
359 pp->pln_range = pln_range_max(pp);
362 lp = (struct lndstr *)unit;
363 lp->lnd_retreat = morale_base;
365 lp->lnd_rpath[0] = 0;