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
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 = whatitem(p, (s_char *)0)))
86 pr("Bad commodity.\n");
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 = vl_find(ip->i_vtype, vbase->m_vtype,
125 vbase->m_vamt, (int)vbase->m_nv);
126 if (maxtender == 0) {
127 pr("A %s cannot hold any %s\n",
128 mchr[(int)tender.shp_type].m_name, ip->i_name);
131 if (!snxtitem(&targets, EF_SHIP,
132 getstarg(player->argp[4], "Ships to be tended? ",
135 if (!check_ship_ok(&tender))
138 while (tend_nxtitem(&targets, (s_char *)&target)) {
139 if (!player->owner &&
140 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
142 if (target.shp_uid == tender.shp_uid)
144 if (tender.shp_x != target.shp_x ||
145 tender.shp_y != target.shp_y)
147 ontarget = target.shp_item[ip->i_vtype];
148 if (ontarget == 0 && amt < 0) {
149 pr("No %s on %s\n", ip->i_name, prship(&target));
152 vbase = &mchr[(int)target.shp_type];
153 maxtarget = vl_find(ip->i_vtype, vbase->m_vtype,
154 vbase->m_vamt, (int)vbase->m_nv);
159 /* take from target and give to tender */
160 transfer = min(ontarget, -amt);
161 transfer = min(maxtender - ontender, transfer);
164 target.shp_item[ip->i_vtype] = ontarget - transfer;
165 ontender += transfer;
168 /* give to target from tender */
169 transfer = min(ontender, amt);
170 transfer = min(transfer, maxtarget - ontarget);
173 target.shp_item[ip->i_vtype] = ontarget + transfer;
174 ontender -= transfer;
177 expose_ship(&tender, &target);
178 putship(target.shp_uid, &target);
179 if (amt > 0 && ontender == 0) {
180 pr("%s out of %s\n", prship(&tender), ip->i_name);
184 pr("%d total %s transferred %s %s\n",
185 total, ip->i_name, (amt > 0) ? "off of" : "to",
187 tender.shp_item[ip->i_vtype] = ontender;
188 tender.shp_mission = 0;
189 putship(tender.shp_uid, &tender);
195 expose_ship(struct shpstr *s1, struct shpstr *s2)
197 if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
198 s2->shp_pstage = PLG_EXPOSED;
199 if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
200 s1->shp_pstage = PLG_EXPOSED;
206 * get next item from list. Stolen from nxtitem to make 1 itsy-bitsy change
212 tend_nxtitem(struct nstr_item *np, caddr_t ptr)
217 if (np->sel == NS_UNDEF)
219 gp = (struct genitem *)ptr;
221 if (np->sel == NS_LIST) {
223 if (np->index >= np->size)
225 np->cur = np->list[np->index];
229 if (!np->read(np->type, np->cur, ptr)) {
230 /* if read fails, fatal */
236 /* The change is to take the player->owner check out here */
239 /* XXX maybe combine NS_LIST and NS_ALL later */
242 if (!xyinrange(gp->x, gp->y, &np->range)) {
246 np->curdist = mapdist((int)gp->x, (int)gp->y,
247 (int)np->cx, (int)np->cy);
248 if (np->curdist > np->dist)
252 if (!xyinrange(gp->x, gp->y, &np->range))
254 if (gp->x == np->range.hx || gp->y == np->range.hy)
258 if (gp->x != np->cx || gp->y != np->cy)
262 if (np->group != gp->group)
266 logerror("nxtitem: bad selector %d\n", np->sel);
269 if (selected && np->ncond) {
270 /* nstr_exec is expensive, so we do it last */
271 if (!nstr_exec(np->cond, np->ncond, ptr, np->type))
279 tend_land(struct shpstr *tenderp, s_char *units)
281 struct nstr_item lni;
282 struct nstr_item targets;
283 struct shpstr target;
286 struct nstr_item pni;
289 if (!snxtitem(&lni, EF_LAND, units))
292 while (nxtitem(&lni, (s_char *)&land)) {
295 if (land.lnd_ship != tenderp->shp_uid) {
296 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
299 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
300 pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
303 if (!snxtitem(&targets, EF_SHIP,
304 getstarg(player->argp[4], "Ship to be tended? ",
307 if (!check_land_ok(&land))
309 while (tend_nxtitem(&targets, (s_char *)&target)) {
310 if (!player->owner &&
311 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
313 if (target.shp_uid == tenderp->shp_uid)
315 if (tenderp->shp_x != target.shp_x ||
316 tenderp->shp_y != target.shp_y)
319 /* Fit unit on ship */
320 count_units(&target);
321 getship(target.shp_uid, &target);
323 if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
324 if (mchr[(int)target.shp_type].m_nland)
325 pr("%s doesn't have room for any more land units!\n",
328 pr("%s doesn't carry land units!\n", prship(&target));
331 pr("%s transferred from %s to %s\n",
332 prland(&land), prship(tenderp), prship(&target));
333 sprintf(buf, "loaded on your %s at %s",
334 prship(&target), xyas(target.shp_x, target.shp_y,
336 gift(target.shp_own, player->cnum, (s_char *)&land,
338 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
340 land.lnd_own = target.shp_own;
341 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
343 land.lnd_ship = target.shp_uid;
345 land.lnd_mission = 0;
347 putland(land.lnd_uid, &land);
348 expose_ship(tenderp, &target);
349 putship(target.shp_uid, &target);
350 count_units(tenderp);
351 putship(tenderp->shp_uid, tenderp);
352 snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
353 while (nxtitem(&pni, (s_char *)&plane)) {
354 if (plane.pln_flags & PLN_LAUNCHED)
356 if (plane.pln_land != land.lnd_uid)
358 sprintf(buf, "loaded on %s", prship(&target));
359 gift(target.shp_own, player->cnum, (s_char *)&plane,
361 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
362 plane.pln_x, plane.pln_y);
363 plane.pln_own = target.shp_own;
364 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
365 plane.pln_x, plane.pln_y);
366 plane.pln_mission = 0;
367 putplane(plane.pln_uid, &plane);