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");
92 if (!snxtitem(&tenders, EF_SHIP, getstarg(player->argp[2], "Tender(s)? ", buf)))
95 while (nxtitem(&tenders, (s_char *)&tender)) {
98 if (type == EF_LAND) {
99 sprintf(prompt, "Land unit(s) to tend from %s? ",
101 if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
103 if (!check_ship_ok(&tender))
105 if (0 != (retval=tend_land(&tender, p)))
109 sprintf(prompt, "Number of %s to tend from %s? ",
110 ip->i_name, prship(&tender));
111 if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
113 if (!check_ship_ok(&tender))
115 if (!(amt = atoi(p))) {
116 pr("Amount must be non-zero!\n");
119 ontender = getvar(ip->i_vtype, (s_char *)&tender, EF_SHIP);
120 if (ontender == 0 && amt > 0) {
121 pr("No %s on %s\n", ip->i_name, prship(&tender));
124 vbase = &mchr[(int)tender.shp_type];
125 maxtender = vl_find(ip->i_vtype, vbase->m_vtype,
126 vbase->m_vamt, (int)vbase->m_nv);
127 if (maxtender == 0) {
128 pr("A %s cannot hold any %s\n",
129 mchr[(int)tender.shp_type].m_name,
133 if (!snxtitem(&targets, EF_SHIP,
134 getstarg(player->argp[4], "Ships to be tended? ", buf)))
136 if (!check_ship_ok(&tender))
139 while (tend_nxtitem(&targets, (s_char *)&target)) {
140 if (!player->owner &&
141 (getrel(getnatp(target.shp_own),
142 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,
150 (s_char *)&target, EF_SHIP);
151 if (ontarget == 0 && amt < 0) {
153 ip->i_name, prship(&target));
156 vbase = &mchr[(int)target.shp_type];
157 maxtarget = vl_find(ip->i_vtype, vbase->m_vtype,
158 vbase->m_vamt, (int)vbase->m_nv);
163 /* take from target and give to tender */
164 transfer = min(ontarget, -amt);
165 transfer = min(maxtender - ontender, transfer);
168 putvar(ip->i_vtype, ontarget - transfer,
169 (s_char *)&target, EF_SHIP);
170 ontender += transfer;
173 /* give to target from tender */
174 transfer = min(ontender, amt);
175 transfer = min(transfer, maxtarget - ontarget);
178 putvar(ip->i_vtype, ontarget + transfer,
179 (s_char *)&target, EF_SHIP);
180 ontender -= transfer;
183 expose_ship(&tender, &target);
184 putship(target.shp_uid, &target);
185 if (amt > 0 && ontender == 0) {
192 pr("%d total %s transferred %s %s\n",
193 total, ip->i_name, (amt > 0) ? "off of" : "to",
195 putvar(ip->i_vtype, ontender, (s_char *)&tender, EF_SHIP);
196 tender.shp_mission = 0;
197 putship(tender.shp_uid, &tender);
203 expose_ship(struct shpstr *s1, struct shpstr *s2)
205 if (getvar(V_PSTAGE, (s_char *)s1, EF_SHIP) == PLG_INFECT &&
206 getvar(V_PSTAGE, (s_char *)s2, EF_SHIP) == PLG_HEALTHY)
207 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)s2, EF_SHIP);
208 if (getvar(V_PSTAGE, (s_char *)s2, EF_SHIP) == PLG_INFECT &&
209 getvar(V_PSTAGE, (s_char *)s1, EF_SHIP) == PLG_HEALTHY)
210 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)s1, EF_SHIP);
216 * get next item from list. Stolen from nxtitem to make 1 itsy-bitsy change
222 tend_nxtitem(struct nstr_item *np, caddr_t ptr)
227 if (np->sel == NS_UNDEF)
229 gp = (struct genitem *) ptr;
231 if (np->sel == NS_LIST) {
233 if (np->index >= np->size)
235 np->cur = np->list[np->index];
239 if (!np->read(np->type, np->cur, ptr)) {
240 /* if read fails, fatal */
246 /* The change is to take the player->owner check out here */
249 /* XXX maybe combine NS_LIST and NS_ALL later */
252 if (!xyinrange(gp->x, gp->y, &np->range)) {
256 np->curdist = mapdist((int)gp->x, (int)gp->y,
257 (int)np->cx, (int)np->cy);
258 if (np->curdist > np->dist)
262 if (!xyinrange(gp->x, gp->y, &np->range))
264 if (gp->x == np->range.hx || gp->y == np->range.hy)
268 if (gp->x != np->cx || gp->y != np->cy)
272 if (np->group != gp->group)
276 logerror("nxtitem: bad selector %d\n", np->sel);
279 if (selected && np->ncond) {
280 /* nstr_exec is expensive, so we do it last */
281 if (!nstr_exec(np->cond, np->ncond, ptr, np->type))
289 tend_land(struct shpstr *tenderp, s_char *units)
291 struct nstr_item lni;
292 struct nstr_item targets;
293 struct shpstr target;
296 struct nstr_item pni;
299 if (!snxtitem(&lni, EF_LAND, units))
302 while(nxtitem(&lni, (s_char *)&land)) {
305 if (land.lnd_ship != tenderp->shp_uid) {
306 pr("%s is not on %s!\n",
307 prland(&land), prship(tenderp));
310 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
311 pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
314 if (!snxtitem(&targets, EF_SHIP,
315 getstarg(player->argp[4], "Ship to be tended? ", buf)))
317 if (!check_land_ok(&land))
319 while (tend_nxtitem(&targets, (s_char *)&target)) {
320 if (!player->owner &&
321 (getrel(getnatp(target.shp_own),
322 player->cnum) < FRIENDLY))
324 if (target.shp_uid == tenderp->shp_uid)
326 if (tenderp->shp_x != target.shp_x ||
327 tenderp->shp_y != target.shp_y)
330 /* Fit unit on ship */
331 count_units(&target);
332 getship(target.shp_uid,&target);
334 if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
335 if (mchr[(int)target.shp_type].m_nland)
336 pr("%s doesn't have room for any more land units!\n",prship(&target));
338 pr("%s doesn't carry land units!\n",prship(&target));
341 pr("%s transferred from %s to %s\n",
342 prland(&land), prship(tenderp), prship(&target));
343 sprintf(buf, "loaded on your %s at %s",
344 prship(&target),xyas(target.shp_x,target.shp_y,
346 gift(target.shp_own,player->cnum,(s_char *)&land,
348 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x, land.lnd_y);
349 land.lnd_own = target.shp_own;
350 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x, land.lnd_y);
351 land.lnd_ship = target.shp_uid;
353 land.lnd_mission = 0;
355 putland(land.lnd_uid,&land);
356 expose_ship(tenderp, &target);
357 putship(target.shp_uid, &target);
358 count_units(tenderp);
359 putship(tenderp->shp_uid, tenderp);
360 snxtitem_xy(&pni,EF_PLANE,land.lnd_x,land.lnd_y);
361 while (nxtitem(&pni, (s_char *)&plane)) {
362 if (plane.pln_flags & PLN_LAUNCHED)
364 if (plane.pln_land != land.lnd_uid)
366 sprintf(buf, "loaded on %s", prship(&target));
367 gift(target.shp_own,player->cnum,(s_char *)&plane,
369 makelost(EF_PLANE, plane.pln_own, plane.pln_uid, plane.pln_x, plane.pln_y);
370 plane.pln_own = target.shp_own;
371 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid, plane.pln_x, plane.pln_y);
372 plane.pln_mission = 0;
373 putplane(plane.pln_uid,&plane);