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 * unitsub.c: Common subroutines for multiple type of units
30 * Known contributors to this file:
31 * Ron Koenderink, 2007
32 * Markus Armbruster, 2008
40 #include "prototypes.h"
44 unit_list(struct emp_qelem *unit_list)
47 struct emp_qelem *next;
49 int type, npln, nch, nxl;
54 if (CANT_HAPPEN(QEMPTY(unit_list)))
56 qp = unit_list->q_back;
57 ulp = (struct ulist *)qp;
58 type = ulp->unit.ef_type;
59 if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
63 pr("lnd# land type x,y a eff sh gun xl mu tech retr\n");
65 pr("shp# ship type x,y fl eff mil sh gun pn he xl ln mob tech\n");
67 for (; qp != unit_list; qp = next) {
69 ulp = (struct ulist *)qp;
70 lnd = &ulp->unit.land;
71 shp = &ulp->unit.ship;
72 unit = &ulp->unit.gen;
73 if (CANT_HAPPEN(type != unit->ef_type))
75 pr("%4d ", unit->uid);
76 pr("%-16.16s ", empobj_chr_name(unit));
77 prxy("%4d,%-4d ", unit->x, unit->y, unit->own);
78 pr("%1.1s", &unit->group);
79 pr("%4d%%", unit->effic);
80 if (type == EF_LAND) {
81 pr("%4d", lnd->lnd_item[I_SHELL]);
82 pr("%4d", lnd->lnd_item[I_GUN]);
83 pr("%3d", lnd_nxlight(lnd));
85 pr("%4d", shp->shp_item[I_MILIT]);
86 pr("%4d", shp->shp_item[I_SHELL]);
87 pr("%4d", shp->shp_item[I_GUN]);
88 npln = shp_nplane(shp, &nch, &nxl, NULL);
89 pr("%3d%3d%3d", npln - nch - nxl, nch, nxl);
90 pr("%3d", shp_nland(shp));
92 pr("%4d", unit->mobil);
93 pr("%4d", unit->tech);
94 if (type == EF_LAND) {
95 pr("%4d%%", lnd->lnd_retreat);
102 unit_put(struct emp_qelem *list, natid actor)
104 struct emp_qelem *qp;
105 struct emp_qelem *newqp;
111 ulp = (struct ulist *)qp;
112 unit = &ulp->unit.gen;
113 if (CANT_HAPPEN(unit->ef_type != EF_LAND
114 && unit->ef_type != EF_SHIP))
117 mpr(actor, "%s stopped at %s\n", obj_nameof(unit),
118 xyas(unit->x, unit->y, unit->own));
119 if (unit->ef_type == EF_LAND) {
120 if (ulp->mobil < -127)
122 unit->mobil = ulp->mobil;
125 if (unit->ef_type == EF_SHIP)
126 unit->mobil = (int)ulp->mobil;
127 put_empobj(unit->ef_type, unit->uid, unit);
136 unit_path(int together, struct empobj *unit, char *buf)
140 struct sctstr d_sect, sect;
145 if (CANT_HAPPEN(unit->ef_type != EF_LAND && unit->ef_type != EF_SHIP))
148 if (!sarg_xy(buf, &destx, &desty))
151 pr("Cannot go to a destination sector if not all starting in the same sector\n");
154 if (!getsect(destx, desty, &d_sect)) {
155 pr("%d,%d is not a sector\n", destx, desty);
158 if (unit->ef_type == EF_SHIP) {
159 cp = BestShipPath(buf, unit->x, unit->y,
160 d_sect.sct_x, d_sect.sct_y, player->cnum);
161 if (!cp || unit->mobil <= 0) {
162 pr("Can't get to '%s' right now.\n",
163 xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
167 getsect(unit->x, unit->y, §);
168 mtype = lnd_mobtype((struct lndstr *)unit);
169 cp = BestLandPath(buf, §, &d_sect, &dummy, mtype);
171 pr("No owned %s from %s to %s!\n",
172 mtype == MOB_RAIL ? "railway" : "path",
173 xyas(unit->x, unit->y, player->cnum),
174 xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
177 pr("Using path '%s'\n", cp);
183 unit_view(struct emp_qelem *list)
186 struct emp_qelem *qp;
187 struct emp_qelem *next;
190 for (qp = list->q_back; qp != list; qp = next) {
192 ulp = (struct ulist *)qp;
193 if (CANT_HAPPEN(!(ef_flags(ulp->unit.ef_type) & EFF_XY)))
195 getsect(ulp->unit.gen.x, ulp->unit.gen.y, §);
196 if (ulp->unit.ef_type == EF_SHIP) {
197 if (((struct mchrstr *)ulp->chrp)->m_flags & M_FOOD)
198 pr("[fert:%d] ", sect.sct_fertil);
199 if (((struct mchrstr *)ulp->chrp)->m_flags & M_OIL)
200 pr("[oil:%d] ", sect.sct_oil);
202 pr("%s @ %s %d%% %s\n", obj_nameof(&ulp->unit.gen),
203 xyas(ulp->unit.gen.x, ulp->unit.gen.y, player->cnum),
204 sect.sct_effic, dchr[sect.sct_type].d_name);
209 * Update cargo of CARRIER for movement or destruction.
210 * If the carrier is destroyed, destroy its cargo (planes, land units,
212 * Else update their location to the carrier's. Any op sectors equal
213 * to location get updated, too.
216 unit_update_cargo(struct empobj *carrier)
220 union empobj_storage obj;
222 for (cargo_type = EF_PLANE; cargo_type <= EF_NUKE; cargo_type++) {
223 snxtitem_cargo(&ni, cargo_type, carrier->ef_type, carrier->uid);
224 while (nxtitem(&ni, &obj)) {
226 mpr(obj.gen.own, "%s lost!\n", obj_nameof(&obj.gen));
229 /* mission op-area centered on the obj travels with it */
230 if (obj.gen.opx == obj.gen.x && obj.gen.opy == obj.gen.y) {
231 obj.gen.opx = carrier->x;
232 obj.gen.opy = carrier->y;
234 obj.gen.x = carrier->x;
235 obj.gen.y = carrier->y;
237 put_empobj(cargo_type, obj.gen.uid, &obj);