2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future 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
53 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
54 static int tend_land(struct shpstr *tenderp, s_char *units);
59 struct nstr_item targets;
60 struct nstr_item tenders;
64 struct mchrstr *vbase;
78 if (!(p = getstarg(player->argp[1],
79 "Tend what commodity (or 'land')? ", buf)) || !*p)
82 if (!strncmp(p, "land", 4))
84 else if (NULL != (ip = item_by_name(p)))
87 pr("Can't tend '%s'\n", p);
91 if (!snxtitem(&tenders, EF_SHIP,
92 getstarg(player->argp[2], "Tender(s)? ", buf)))
95 while (nxtitem(&tenders, &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 = tender.shp_item[ip->i_vtype];
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 = vbase->m_item[ip->i_vtype];
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 (nxtitem(&targets, &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 = vbase->m_item[ip->i_vtype];
158 /* take from target and give to tender */
159 transfer = MIN(ontarget, -amt);
160 transfer = MIN(maxtender - ontender, transfer);
163 target.shp_item[ip->i_vtype] = ontarget - transfer;
164 ontender += transfer;
167 /* give to target from tender */
168 transfer = MIN(ontender, amt);
169 transfer = MIN(transfer, maxtarget - ontarget);
172 target.shp_item[ip->i_vtype] = ontarget + transfer;
173 ontender -= transfer;
176 expose_ship(&tender, &target);
177 putship(target.shp_uid, &target);
178 if (amt > 0 && ontender == 0) {
179 pr("%s out of %s\n", prship(&tender), ip->i_name);
183 pr("%d total %s transferred %s %s\n",
184 total, ip->i_name, (amt > 0) ? "off of" : "to",
186 if (target.shp_own != player->cnum) {
187 wu(0, target.shp_own, "%s tended %d %s to %s\n",
188 cname(player->cnum), total, ip->i_name, prship(&target));
190 tender.shp_item[ip->i_vtype] = ontender;
191 tender.shp_mission = 0;
192 putship(tender.shp_uid, &tender);
198 expose_ship(struct shpstr *s1, struct shpstr *s2)
200 if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
201 s2->shp_pstage = PLG_EXPOSED;
202 if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
203 s1->shp_pstage = PLG_EXPOSED;
207 tend_land(struct shpstr *tenderp, s_char *units)
209 struct nstr_item lni;
210 struct nstr_item targets;
211 struct shpstr target;
214 struct nstr_item pni;
217 if (!snxtitem(&lni, EF_LAND, units))
220 while (nxtitem(&lni, &land)) {
223 if (land.lnd_ship != tenderp->shp_uid) {
224 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
227 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
228 pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
231 if (!snxtitem(&targets, EF_SHIP,
232 getstarg(player->argp[4], "Ship to be tended? ",
235 if (!check_land_ok(&land))
237 while (nxtitem(&targets, &target)) {
238 if (!player->owner &&
239 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
241 if (target.shp_uid == tenderp->shp_uid)
243 if (tenderp->shp_x != target.shp_x ||
244 tenderp->shp_y != target.shp_y)
247 /* Fit unit on ship */
248 count_units(&target);
249 getship(target.shp_uid, &target);
251 if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
252 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
253 !mchr[(int)target.shp_type].m_nland) {
254 if (target.shp_nland > 1) {
255 pr("%s doesn't have room for more than two spy units!\n",
259 } else if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
260 if (mchr[(int)target.shp_type].m_nland)
261 pr("%s doesn't have room for any more land units!\n",
264 pr("%s doesn't carry land units!\n", prship(&target));
267 pr("%s transferred from %s to %s\n",
268 prland(&land), prship(tenderp), prship(&target));
269 sprintf(buf, "loaded on your %s at %s",
270 prship(&target), xyas(target.shp_x, target.shp_y,
272 gift(target.shp_own, player->cnum, (s_char *)&land,
274 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
276 land.lnd_own = target.shp_own;
277 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
279 land.lnd_ship = target.shp_uid;
281 land.lnd_mission = 0;
283 putland(land.lnd_uid, &land);
284 expose_ship(tenderp, &target);
285 putship(target.shp_uid, &target);
286 count_units(tenderp);
287 putship(tenderp->shp_uid, tenderp);
288 snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
289 while (nxtitem(&pni, &plane)) {
290 if (plane.pln_flags & PLN_LAUNCHED)
292 if (plane.pln_land != land.lnd_uid)
294 sprintf(buf, "loaded on %s", prship(&target));
295 gift(target.shp_own, player->cnum, (s_char *)&plane,
297 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
298 plane.pln_x, plane.pln_y);
299 plane.pln_own = target.shp_own;
300 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
301 plane.pln_x, plane.pln_y);
302 plane.pln_mission = 0;
303 putplane(plane.pln_uid, &plane);