2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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
51 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
52 static int tend_land(struct shpstr *tenderp, s_char *units);
57 struct nstr_item targets;
58 struct nstr_item tenders;
62 struct mchrstr *vbase;
76 if (!(p = getstarg(player->argp[1],
77 "Tend what commodity (or 'land')? ", buf)) || !*p)
80 if (!strncmp(p, "land", 4))
82 else if (NULL != (ip = item_by_name(p)))
85 pr("Can't tend '%s'\n", p);
89 if (!snxtitem(&tenders, EF_SHIP,
90 getstarg(player->argp[2], "Tender(s)? ", buf)))
93 while (nxtitem(&tenders, (s_char *)&tender)) {
96 if (type == EF_LAND) {
97 sprintf(prompt, "Land unit(s) to tend from %s? ",
99 if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
101 if (!check_ship_ok(&tender))
103 if (0 != (retval = tend_land(&tender, p)))
107 sprintf(prompt, "Number of %s to tend from %s? ",
108 ip->i_name, prship(&tender));
109 if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
111 if (!check_ship_ok(&tender))
113 if (!(amt = atoi(p))) {
114 pr("Amount must be non-zero!\n");
117 ontender = tender.shp_item[ip->i_vtype];
118 if (ontender == 0 && amt > 0) {
119 pr("No %s on %s\n", ip->i_name, prship(&tender));
122 vbase = &mchr[(int)tender.shp_type];
123 maxtender = vbase->m_item[ip->i_vtype];
124 if (maxtender == 0) {
125 pr("A %s cannot hold any %s\n",
126 mchr[(int)tender.shp_type].m_name, ip->i_name);
129 if (!snxtitem(&targets, EF_SHIP,
130 getstarg(player->argp[4], "Ships to be tended? ",
133 if (!check_ship_ok(&tender))
136 while (tend_nxtitem(&targets, (s_char *)&target)) {
137 if (!player->owner &&
138 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
140 if (target.shp_uid == tender.shp_uid)
142 if (tender.shp_x != target.shp_x ||
143 tender.shp_y != target.shp_y)
145 ontarget = target.shp_item[ip->i_vtype];
146 if (ontarget == 0 && amt < 0) {
147 pr("No %s on %s\n", ip->i_name, prship(&target));
150 vbase = &mchr[(int)target.shp_type];
151 maxtarget = vbase->m_item[ip->i_vtype];
156 /* take from target and give to tender */
157 transfer = min(ontarget, -amt);
158 transfer = min(maxtender - ontender, transfer);
161 target.shp_item[ip->i_vtype] = ontarget - transfer;
162 ontender += transfer;
165 /* give to target from tender */
166 transfer = min(ontender, amt);
167 transfer = min(transfer, maxtarget - ontarget);
170 target.shp_item[ip->i_vtype] = ontarget + transfer;
171 ontender -= transfer;
174 expose_ship(&tender, &target);
175 putship(target.shp_uid, &target);
176 if (amt > 0 && ontender == 0) {
177 pr("%s out of %s\n", prship(&tender), ip->i_name);
181 pr("%d total %s transferred %s %s\n",
182 total, ip->i_name, (amt > 0) ? "off of" : "to",
184 tender.shp_item[ip->i_vtype] = ontender;
185 tender.shp_mission = 0;
186 putship(tender.shp_uid, &tender);
192 expose_ship(struct shpstr *s1, struct shpstr *s2)
194 if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
195 s2->shp_pstage = PLG_EXPOSED;
196 if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
197 s1->shp_pstage = PLG_EXPOSED;
203 * get next item from list. Stolen from nxtitem to make 1 itsy-bitsy change
209 tend_nxtitem(struct nstr_item *np, void *ptr)
214 if (np->sel == NS_UNDEF)
216 gp = (struct genitem *)ptr;
218 if (np->sel == NS_LIST) {
220 if (np->index >= np->size)
222 np->cur = np->list[np->index];
226 if (!np->read(np->type, np->cur, ptr)) {
227 /* if read fails, fatal */
233 /* The change is to take the player->owner check out here */
236 /* XXX maybe combine NS_LIST and NS_ALL later */
239 if (!xyinrange(gp->x, gp->y, &np->range)) {
243 np->curdist = mapdist((int)gp->x, (int)gp->y,
244 (int)np->cx, (int)np->cy);
245 if (np->curdist > np->dist)
249 if (!xyinrange(gp->x, gp->y, &np->range))
251 if (gp->x == np->range.hx || gp->y == np->range.hy)
255 if (gp->x != np->cx || gp->y != np->cy)
259 if (np->group != gp->group)
263 CANT_HAPPEN("bad np->sel");
266 if (selected && np->ncond) {
267 /* nstr_exec is expensive, so we do it last */
268 if (!nstr_exec(np->cond, np->ncond, ptr))
276 tend_land(struct shpstr *tenderp, s_char *units)
278 struct nstr_item lni;
279 struct nstr_item targets;
280 struct shpstr target;
283 struct nstr_item pni;
286 if (!snxtitem(&lni, EF_LAND, units))
289 while (nxtitem(&lni, (s_char *)&land)) {
292 if (land.lnd_ship != tenderp->shp_uid) {
293 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
296 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
297 pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
300 if (!snxtitem(&targets, EF_SHIP,
301 getstarg(player->argp[4], "Ship to be tended? ",
304 if (!check_land_ok(&land))
306 while (tend_nxtitem(&targets, (s_char *)&target)) {
307 if (!player->owner &&
308 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
310 if (target.shp_uid == tenderp->shp_uid)
312 if (tenderp->shp_x != target.shp_x ||
313 tenderp->shp_y != target.shp_y)
316 /* Fit unit on ship */
317 count_units(&target);
318 getship(target.shp_uid, &target);
320 if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
321 if (mchr[(int)target.shp_type].m_nland)
322 pr("%s doesn't have room for any more land units!\n",
325 pr("%s doesn't carry land units!\n", prship(&target));
328 pr("%s transferred from %s to %s\n",
329 prland(&land), prship(tenderp), prship(&target));
330 sprintf(buf, "loaded on your %s at %s",
331 prship(&target), xyas(target.shp_x, target.shp_y,
333 gift(target.shp_own, player->cnum, (s_char *)&land,
335 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
337 land.lnd_own = target.shp_own;
338 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
340 land.lnd_ship = target.shp_uid;
342 land.lnd_mission = 0;
344 putland(land.lnd_uid, &land);
345 expose_ship(tenderp, &target);
346 putship(target.shp_uid, &target);
347 count_units(tenderp);
348 putship(tenderp->shp_uid, tenderp);
349 snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
350 while (nxtitem(&pni, (s_char *)&plane)) {
351 if (plane.pln_flags & PLN_LAUNCHED)
353 if (plane.pln_land != land.lnd_uid)
355 sprintf(buf, "loaded on %s", prship(&target));
356 gift(target.shp_own, player->cnum, (s_char *)&plane,
358 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
359 plane.pln_x, plane.pln_y);
360 plane.pln_own = target.shp_own;
361 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
362 plane.pln_x, plane.pln_y);
363 plane.pln_mission = 0;
364 putplane(plane.pln_uid, &plane);