2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * tend.c: Transfer goodies from one ship to another.
30 * Known contributors to this file:
32 * Thomas Ruschak, 1992
54 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
55 static int tend_land(struct shpstr *tenderp, s_char *units);
60 struct nstr_item targets;
61 struct nstr_item tenders;
65 struct mchrstr *vbase;
79 if (!(p = getstarg(player->argp[1],
80 "Tend what commodity (or 'land')? ", buf)) || !*p)
83 if (!strncmp(p, "land", 4))
85 else if (NULL != (ip = whatitem(p, (s_char *)0)))
88 pr("Bad commodity.\n");
93 (&tenders, EF_SHIP, getstarg(player->argp[2], "Tender(s)? ", buf)))
96 while (nxtitem(&tenders, (s_char *)&tender)) {
99 if (type == EF_LAND) {
100 sprintf(prompt, "Land unit(s) to tend from %s? ",
102 if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
104 if (!check_ship_ok(&tender))
106 if (0 != (retval = tend_land(&tender, p)))
110 sprintf(prompt, "Number of %s to tend from %s? ",
111 ip->i_name, prship(&tender));
112 if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
114 if (!check_ship_ok(&tender))
116 if (!(amt = atoi(p))) {
117 pr("Amount must be non-zero!\n");
120 ontender = getvar(ip->i_vtype, (s_char *)&tender, EF_SHIP);
121 if (ontender == 0 && amt > 0) {
122 pr("No %s on %s\n", ip->i_name, prship(&tender));
125 vbase = &mchr[(int)tender.shp_type];
126 maxtender = vl_find(ip->i_vtype, vbase->m_vtype,
127 vbase->m_vamt, (int)vbase->m_nv);
128 if (maxtender == 0) {
129 pr("A %s cannot hold any %s\n",
130 mchr[(int)tender.shp_type].m_name, ip->i_name);
133 if (!snxtitem(&targets, EF_SHIP,
134 getstarg(player->argp[4], "Ships to be tended? ",
137 if (!check_ship_ok(&tender))
140 while (tend_nxtitem(&targets, (s_char *)&target)) {
141 if (!player->owner &&
142 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
144 if (target.shp_uid == tender.shp_uid)
146 if (tender.shp_x != target.shp_x ||
147 tender.shp_y != target.shp_y)
149 ontarget = getvar(ip->i_vtype, (s_char *)&target, EF_SHIP);
150 if (ontarget == 0 && amt < 0) {
151 pr("No %s on %s\n", ip->i_name, prship(&target));
154 vbase = &mchr[(int)target.shp_type];
155 maxtarget = vl_find(ip->i_vtype, vbase->m_vtype,
156 vbase->m_vamt, (int)vbase->m_nv);
161 /* take from target and give to tender */
162 transfer = min(ontarget, -amt);
163 transfer = min(maxtender - ontender, transfer);
166 putvar(ip->i_vtype, ontarget - transfer,
167 (s_char *)&target, EF_SHIP);
168 ontender += transfer;
171 /* give to target from tender */
172 transfer = min(ontender, amt);
173 transfer = min(transfer, maxtarget - ontarget);
176 putvar(ip->i_vtype, ontarget + transfer,
177 (s_char *)&target, EF_SHIP);
178 ontender -= transfer;
181 expose_ship(&tender, &target);
182 putship(target.shp_uid, &target);
183 if (amt > 0 && ontender == 0) {
184 pr("%s out of %s\n", prship(&tender), ip->i_name);
188 pr("%d total %s transferred %s %s\n",
189 total, ip->i_name, (amt > 0) ? "off of" : "to",
191 putvar(ip->i_vtype, ontender, (s_char *)&tender, EF_SHIP);
192 tender.shp_mission = 0;
193 putship(tender.shp_uid, &tender);
199 expose_ship(struct shpstr *s1, struct shpstr *s2)
201 if (getvar(V_PSTAGE, (s_char *)s1, EF_SHIP) == PLG_INFECT &&
202 getvar(V_PSTAGE, (s_char *)s2, EF_SHIP) == PLG_HEALTHY)
203 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)s2, EF_SHIP);
204 if (getvar(V_PSTAGE, (s_char *)s2, EF_SHIP) == PLG_INFECT &&
205 getvar(V_PSTAGE, (s_char *)s1, EF_SHIP) == PLG_HEALTHY)
206 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)s1, EF_SHIP);
212 * get next item from list. Stolen from nxtitem to make 1 itsy-bitsy change
218 tend_nxtitem(struct nstr_item *np, caddr_t ptr)
223 if (np->sel == NS_UNDEF)
225 gp = (struct genitem *)ptr;
227 if (np->sel == NS_LIST) {
229 if (np->index >= np->size)
231 np->cur = np->list[np->index];
235 if (!np->read(np->type, np->cur, ptr)) {
236 /* if read fails, fatal */
242 /* The change is to take the player->owner check out here */
245 /* XXX maybe combine NS_LIST and NS_ALL later */
248 if (!xyinrange(gp->x, gp->y, &np->range)) {
252 np->curdist = mapdist((int)gp->x, (int)gp->y,
253 (int)np->cx, (int)np->cy);
254 if (np->curdist > np->dist)
258 if (!xyinrange(gp->x, gp->y, &np->range))
260 if (gp->x == np->range.hx || gp->y == np->range.hy)
264 if (gp->x != np->cx || gp->y != np->cy)
268 if (np->group != gp->group)
272 logerror("nxtitem: bad selector %d\n", np->sel);
275 if (selected && np->ncond) {
276 /* nstr_exec is expensive, so we do it last */
277 if (!nstr_exec(np->cond, np->ncond, ptr, np->type))
285 tend_land(struct shpstr *tenderp, s_char *units)
287 struct nstr_item lni;
288 struct nstr_item targets;
289 struct shpstr target;
292 struct nstr_item pni;
295 if (!snxtitem(&lni, EF_LAND, units))
298 while (nxtitem(&lni, (s_char *)&land)) {
301 if (land.lnd_ship != tenderp->shp_uid) {
302 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
305 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
306 pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
309 if (!snxtitem(&targets, EF_SHIP,
310 getstarg(player->argp[4], "Ship to be tended? ",
313 if (!check_land_ok(&land))
315 while (tend_nxtitem(&targets, (s_char *)&target)) {
316 if (!player->owner &&
317 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
319 if (target.shp_uid == tenderp->shp_uid)
321 if (tenderp->shp_x != target.shp_x ||
322 tenderp->shp_y != target.shp_y)
325 /* Fit unit on ship */
326 count_units(&target);
327 getship(target.shp_uid, &target);
329 if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
330 if (mchr[(int)target.shp_type].m_nland)
331 pr("%s doesn't have room for any more land units!\n",
334 pr("%s doesn't carry land units!\n", prship(&target));
337 pr("%s transferred from %s to %s\n",
338 prland(&land), prship(tenderp), prship(&target));
339 sprintf(buf, "loaded on your %s at %s",
340 prship(&target), xyas(target.shp_x, target.shp_y,
342 gift(target.shp_own, player->cnum, (s_char *)&land,
344 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
346 land.lnd_own = target.shp_own;
347 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
349 land.lnd_ship = target.shp_uid;
351 land.lnd_mission = 0;
353 putland(land.lnd_uid, &land);
354 expose_ship(tenderp, &target);
355 putship(target.shp_uid, &target);
356 count_units(tenderp);
357 putship(tenderp->shp_uid, tenderp);
358 snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
359 while (nxtitem(&pni, (s_char *)&plane)) {
360 if (plane.pln_flags & PLN_LAUNCHED)
362 if (plane.pln_land != land.lnd_uid)
364 sprintf(buf, "loaded on %s", prship(&target));
365 gift(target.shp_own, player->cnum, (s_char *)&plane,
367 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
368 plane.pln_x, plane.pln_y);
369 plane.pln_own = target.shp_own;
370 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
371 plane.pln_x, plane.pln_y);
372 plane.pln_mission = 0;
373 putplane(plane.pln_uid, &plane);