]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tend.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / commands / tend.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  tend.c: Transfer goodies from one ship to another.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Thomas Ruschak, 1992
33  *     Steve McClure, 2000
34  */
35
36 #include <config.h>
37
38 #include <string.h>
39 #include "misc.h"
40 #include "player.h"
41 #include "plague.h"
42 #include "xy.h"
43 #include "file.h"
44 #include "ship.h"
45 #include "item.h"
46 #include "nsc.h"
47 #include "nat.h"
48 #include "land.h"
49 #include "plane.h"
50 #include "genitem.h"
51 #include "commands.h"
52
53 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
54 static int tend_land(struct shpstr *tenderp, s_char *units);
55
56 int
57 tend(void)
58 {
59     struct nstr_item targets;
60     struct nstr_item tenders;
61     struct shpstr tender;
62     struct shpstr target;
63     struct ichrstr *ip;
64     struct mchrstr *vbase;
65     int amt;
66     int retval;
67     int ontender;
68     int ontarget;
69     int maxtender;
70     int maxtarget;
71     int transfer;
72     int total;
73     int type;
74     s_char *p;
75     s_char prompt[512];
76     s_char buf[1024];
77
78     if (!(p = getstarg(player->argp[1],
79                        "Tend what commodity (or 'land')? ", buf)) || !*p)
80         return RET_SYN;
81
82     if (!strncmp(p, "land", 4))
83         type = EF_LAND;
84     else if (NULL != (ip = item_by_name(p)))
85         type = EF_SECTOR;
86     else {
87         pr("Can't tend '%s'\n", p);
88         return RET_SYN;
89     }
90
91     if (!snxtitem(&tenders, EF_SHIP,
92                   getstarg(player->argp[2], "Tender(s)? ", buf)))
93         return RET_SYN;
94
95     while (nxtitem(&tenders, &tender)) {
96         if (!player->owner)
97             continue;
98         if (type == EF_LAND) {
99             sprintf(prompt, "Land unit(s) to tend from %s? ",
100                     prship(&tender));
101             if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
102                 continue;
103             if (!check_ship_ok(&tender))
104                 return RET_SYN;
105             if (0 != (retval = tend_land(&tender, p)))
106                 return retval;
107             continue;
108         }
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)
112             continue;
113         if (!check_ship_ok(&tender))
114             return RET_SYN;
115         if (!(amt = atoi(p))) {
116             pr("Amount must be non-zero!\n");
117             return RET_SYN;
118         }
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));
122             return RET_FAIL;
123         }
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);
129             break;
130         }
131         if (!snxtitem(&targets, EF_SHIP,
132                       getstarg(player->argp[4], "Ships to be tended? ",
133                                buf)))
134             break;
135         if (!check_ship_ok(&tender))
136             return RET_SYN;
137         total = 0;
138         while (nxtitem(&targets, &target)) {
139             if (!player->owner &&
140                 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
141                 continue;
142             if (target.shp_uid == tender.shp_uid)
143                 continue;
144             if (tender.shp_x != target.shp_x ||
145                 tender.shp_y != target.shp_y)
146                 continue;
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));
150                 continue;
151             }
152             vbase = &mchr[(int)target.shp_type];
153             maxtarget = vbase->m_item[ip->i_vtype];
154             if (amt < 0) {
155                 if (!player->owner)
156                     amt = 0;
157
158                 /* take from target and give to tender */
159                 transfer = MIN(ontarget, -amt);
160                 transfer = MIN(maxtender - ontender, transfer);
161                 if (transfer == 0)
162                     continue;
163                 target.shp_item[ip->i_vtype] = ontarget - transfer;
164                 ontender += transfer;
165                 total += transfer;
166             } else {
167                 /* give to target from tender */
168                 transfer = MIN(ontender, amt);
169                 transfer = MIN(transfer, maxtarget - ontarget);
170                 if (transfer == 0)
171                     continue;
172                 target.shp_item[ip->i_vtype] = ontarget + transfer;
173                 ontender -= transfer;
174                 total += transfer;
175             }
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);
180                 break;
181             }
182         }
183         pr("%d total %s transferred %s %s\n",
184            total, ip->i_name, (amt > 0) ? "off of" : "to",
185            prship(&tender));
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));
189         }
190         tender.shp_item[ip->i_vtype] = ontender;
191         tender.shp_mission = 0;
192         putship(tender.shp_uid, &tender);
193     }
194     return RET_OK;
195 }
196
197 static void
198 expose_ship(struct shpstr *s1, struct shpstr *s2)
199 {
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;
204 }
205
206 static int
207 tend_land(struct shpstr *tenderp, s_char *units)
208 {
209     struct nstr_item lni;
210     struct nstr_item targets;
211     struct shpstr target;
212     struct lndstr land;
213     struct plnstr plane;
214     struct nstr_item pni;
215     s_char buf[1024];
216
217     if (!snxtitem(&lni, EF_LAND, units))
218         return RET_SYN;
219
220     while (nxtitem(&lni, &land)) {
221         if (!player->owner)
222             continue;
223         if (land.lnd_ship != tenderp->shp_uid) {
224             pr("%s is not on %s!\n", prland(&land), prship(tenderp));
225             continue;
226         }
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));
229             continue;
230         }
231         if (!snxtitem(&targets, EF_SHIP,
232                       getstarg(player->argp[4], "Ship to be tended? ",
233                                buf)))
234             break;
235         if (!check_land_ok(&land))
236             return RET_SYN;
237         while (nxtitem(&targets, &target)) {
238             if (!player->owner &&
239                 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
240                 continue;
241             if (target.shp_uid == tenderp->shp_uid)
242                 continue;
243             if (tenderp->shp_x != target.shp_x ||
244                 tenderp->shp_y != target.shp_y)
245                 continue;
246
247             /* Fit unit on ship */
248             count_units(&target);
249             getship(target.shp_uid, &target);
250
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",
256                        prship(&target));
257                     continue;
258                 }
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",
262                        prship(&target));
263                 else
264                     pr("%s doesn't carry land units!\n", prship(&target));
265                 continue;
266             }
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,
271                                           target.shp_own));
272             gift(target.shp_own, player->cnum, (s_char *)&land,
273                  EF_LAND, buf);
274             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
275                      land.lnd_y);
276             land.lnd_own = target.shp_own;
277             makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
278                         land.lnd_y);
279             land.lnd_ship = target.shp_uid;
280             land.lnd_harden = 0;
281             land.lnd_mission = 0;
282             target.shp_nland++;
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)
291                     continue;
292                 if (plane.pln_land != land.lnd_uid)
293                     continue;
294                 sprintf(buf, "loaded on %s", prship(&target));
295                 gift(target.shp_own, player->cnum, (s_char *)&plane,
296                      EF_PLANE, buf);
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);
304             }
305         }
306     }
307     return 0;
308 }