2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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
52 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
53 static int tend_land(struct shpstr *tenderp, s_char *units);
58 struct nstr_item targets;
59 struct nstr_item tenders;
63 struct mchrstr *vbase;
77 if (!(p = getstarg(player->argp[1],
78 "Tend what commodity (or 'land')? ", buf)) || !*p)
81 if (!strncmp(p, "land", 4))
83 else if (NULL != (ip = item_by_name(p)))
86 pr("Can't tend '%s'\n", p);
90 if (!snxtitem(&tenders, EF_SHIP,
91 getstarg(player->argp[2], "Tender(s)? ", buf)))
94 while (nxtitem(&tenders, (s_char *)&tender)) {
97 if (type == EF_LAND) {
98 sprintf(prompt, "Land unit(s) to tend from %s? ",
100 if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
102 if (!check_ship_ok(&tender))
104 if (0 != (retval = tend_land(&tender, p)))
108 sprintf(prompt, "Number of %s to tend from %s? ",
109 ip->i_name, prship(&tender));
110 if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
112 if (!check_ship_ok(&tender))
114 if (!(amt = atoi(p))) {
115 pr("Amount must be non-zero!\n");
118 ontender = tender.shp_item[ip->i_vtype];
119 if (ontender == 0 && amt > 0) {
120 pr("No %s on %s\n", ip->i_name, prship(&tender));
123 vbase = &mchr[(int)tender.shp_type];
124 maxtender = vbase->m_item[ip->i_vtype];
125 if (maxtender == 0) {
126 pr("A %s cannot hold any %s\n",
127 mchr[(int)tender.shp_type].m_name, ip->i_name);
130 if (!snxtitem(&targets, EF_SHIP,
131 getstarg(player->argp[4], "Ships to be tended? ",
134 if (!check_ship_ok(&tender))
137 while (tend_nxtitem(&targets, (s_char *)&target)) {
138 if (!player->owner &&
139 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
141 if (target.shp_uid == tender.shp_uid)
143 if (tender.shp_x != target.shp_x ||
144 tender.shp_y != target.shp_y)
146 ontarget = target.shp_item[ip->i_vtype];
147 if (ontarget == 0 && amt < 0) {
148 pr("No %s on %s\n", ip->i_name, prship(&target));
151 vbase = &mchr[(int)target.shp_type];
152 maxtarget = vbase->m_item[ip->i_vtype];
157 /* take from target and give to tender */
158 transfer = min(ontarget, -amt);
159 transfer = min(maxtender - ontender, transfer);
162 target.shp_item[ip->i_vtype] = ontarget - transfer;
163 ontender += transfer;
166 /* give to target from tender */
167 transfer = min(ontender, amt);
168 transfer = min(transfer, maxtarget - ontarget);
171 target.shp_item[ip->i_vtype] = ontarget + transfer;
172 ontender -= transfer;
175 expose_ship(&tender, &target);
176 putship(target.shp_uid, &target);
177 if (amt > 0 && ontender == 0) {
178 pr("%s out of %s\n", prship(&tender), ip->i_name);
182 pr("%d total %s transferred %s %s\n",
183 total, ip->i_name, (amt > 0) ? "off of" : "to",
185 tender.shp_item[ip->i_vtype] = ontender;
186 tender.shp_mission = 0;
187 putship(tender.shp_uid, &tender);
193 expose_ship(struct shpstr *s1, struct shpstr *s2)
195 if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
196 s2->shp_pstage = PLG_EXPOSED;
197 if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
198 s1->shp_pstage = PLG_EXPOSED;
204 * get next item from list. Stolen from nxtitem to make 1 itsy-bitsy change
210 tend_nxtitem(struct nstr_item *np, void *ptr)
215 if (np->sel == NS_UNDEF)
217 gp = (struct genitem *)ptr;
219 if (np->sel == NS_LIST) {
221 if (np->index >= np->size)
223 np->cur = np->list[np->index];
227 if (!np->read(np->type, np->cur, ptr)) {
228 /* if read fails, fatal */
234 /* The change is to take the player->owner check out here */
237 /* XXX maybe combine NS_LIST and NS_ALL later */
240 if (!xyinrange(gp->x, gp->y, &np->range)) {
244 np->curdist = mapdist((int)gp->x, (int)gp->y,
245 (int)np->cx, (int)np->cy);
246 if (np->curdist > np->dist)
250 if (!xyinrange(gp->x, gp->y, &np->range))
252 if (gp->x == np->range.hx || gp->y == np->range.hy)
256 if (gp->x != np->cx || gp->y != np->cy)
260 if (np->group != gp->group)
264 CANT_HAPPEN("bad np->sel");
267 if (selected && np->ncond) {
268 /* nstr_exec is expensive, so we do it last */
269 if (!nstr_exec(np->cond, np->ncond, ptr))
277 tend_land(struct shpstr *tenderp, s_char *units)
279 struct nstr_item lni;
280 struct nstr_item targets;
281 struct shpstr target;
284 struct nstr_item pni;
287 if (!snxtitem(&lni, EF_LAND, units))
290 while (nxtitem(&lni, (s_char *)&land)) {
293 if (land.lnd_ship != tenderp->shp_uid) {
294 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
297 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
298 pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
301 if (!snxtitem(&targets, EF_SHIP,
302 getstarg(player->argp[4], "Ship to be tended? ",
305 if (!check_land_ok(&land))
307 while (tend_nxtitem(&targets, (s_char *)&target)) {
308 if (!player->owner &&
309 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
311 if (target.shp_uid == tenderp->shp_uid)
313 if (tenderp->shp_x != target.shp_x ||
314 tenderp->shp_y != target.shp_y)
317 /* Fit unit on ship */
318 count_units(&target);
319 getship(target.shp_uid, &target);
321 if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
322 if (mchr[(int)target.shp_type].m_nland)
323 pr("%s doesn't have room for any more land units!\n",
326 pr("%s doesn't carry land units!\n", prship(&target));
329 pr("%s transferred from %s to %s\n",
330 prland(&land), prship(tenderp), prship(&target));
331 sprintf(buf, "loaded on your %s at %s",
332 prship(&target), xyas(target.shp_x, target.shp_y,
334 gift(target.shp_own, player->cnum, (s_char *)&land,
336 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
338 land.lnd_own = target.shp_own;
339 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
341 land.lnd_ship = target.shp_uid;
343 land.lnd_mission = 0;
345 putland(land.lnd_uid, &land);
346 expose_ship(tenderp, &target);
347 putship(target.shp_uid, &target);
348 count_units(tenderp);
349 putship(tenderp->shp_uid, tenderp);
350 snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
351 while (nxtitem(&pni, (s_char *)&plane)) {
352 if (plane.pln_flags & PLN_LAUNCHED)
354 if (plane.pln_land != land.lnd_uid)
356 sprintf(buf, "loaded on %s", prship(&target));
357 gift(target.shp_own, player->cnum, (s_char *)&plane,
359 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
360 plane.pln_x, plane.pln_y);
361 plane.pln_own = target.shp_own;
362 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
363 plane.pln_x, plane.pln_y);
364 plane.pln_mission = 0;
365 putplane(plane.pln_uid, &plane);