]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tend.c
a1535a103bb1e0d6d5f539f22e6ce0929035148b
[empserver] / src / lib / commands / tend.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 "commands.h"
39 #include "item.h"
40 #include "land.h"
41 #include "plague.h"
42 #include "plane.h"
43 #include "ship.h"
44
45 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
46 static int tend_land(struct shpstr *tenderp, char *units);
47
48 int
49 tend(void)
50 {
51     struct nstr_item targets;
52     struct nstr_item tenders;
53     struct shpstr tender;
54     struct shpstr target;
55     struct ichrstr *ip;
56     struct mchrstr *vbase;
57     int amt;
58     int retval;
59     int ontender;
60     int ontarget;
61     int maxtender;
62     int maxtarget;
63     int transfer;
64     int total;
65     int type;
66     char *p;
67     char prompt[512];
68     char buf[1024];
69
70     if (!(p = getstarg(player->argp[1],
71                        "Tend what commodity (or 'land')? ", buf)) || !*p)
72         return RET_SYN;
73
74     if (!strncmp(p, "land", 4))
75         type = EF_LAND;
76     else if (NULL != (ip = item_by_name(p)))
77         type = EF_SECTOR;
78     else {
79         pr("Can't tend '%s'\n", p);
80         return RET_SYN;
81     }
82
83     if (!snxtitem(&tenders, EF_SHIP,
84                   getstarg(player->argp[2], "Tender(s)? ", buf)))
85         return RET_SYN;
86
87     while (nxtitem(&tenders, &tender)) {
88         if (!player->owner)
89             continue;
90         if (type == EF_LAND) {
91             sprintf(prompt, "Land unit(s) to tend from %s? ",
92                     prship(&tender));
93             if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
94                 continue;
95             if (!check_ship_ok(&tender))
96                 return RET_SYN;
97             if (0 != (retval = tend_land(&tender, p)))
98                 return retval;
99             continue;
100         }
101         sprintf(prompt, "Number of %s to tend from %s? ",
102                 ip->i_name, prship(&tender));
103         if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
104             continue;
105         if (!check_ship_ok(&tender))
106             return RET_SYN;
107         if (!(amt = atoi(p))) {
108             pr("Amount must be non-zero!\n");
109             return RET_SYN;
110         }
111         ontender = tender.shp_item[ip->i_uid];
112         if (ontender == 0 && amt > 0) {
113             pr("No %s on %s\n", ip->i_name, prship(&tender));
114             return RET_FAIL;
115         }
116         vbase = &mchr[(int)tender.shp_type];
117         maxtender = vbase->m_item[ip->i_uid];
118         if (maxtender == 0) {
119             pr("A %s cannot hold any %s\n",
120                mchr[(int)tender.shp_type].m_name, ip->i_name);
121             break;
122         }
123         if (!snxtitem(&targets, EF_SHIP,
124                       getstarg(player->argp[4], "Ships to be tended? ",
125                                buf)))
126             break;
127         if (!check_ship_ok(&tender))
128             return RET_SYN;
129         total = 0;
130         while (nxtitem(&targets, &target)) {
131             if (!player->owner &&
132                 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
133                 continue;
134             if (target.shp_uid == tender.shp_uid)
135                 continue;
136             if (tender.shp_x != target.shp_x ||
137                 tender.shp_y != target.shp_y)
138                 continue;
139             ontarget = target.shp_item[ip->i_uid];
140             if (ontarget == 0 && amt < 0) {
141                 pr("No %s on %s\n", ip->i_name, prship(&target));
142                 continue;
143             }
144             vbase = &mchr[(int)target.shp_type];
145             maxtarget = vbase->m_item[ip->i_uid];
146             if (amt < 0) {
147                 if (!player->owner)
148                     amt = 0;
149
150                 /* take from target and give to tender */
151                 transfer = MIN(ontarget, -amt);
152                 transfer = MIN(maxtender - ontender, transfer);
153                 if (transfer == 0)
154                     continue;
155                 target.shp_item[ip->i_uid] = ontarget - transfer;
156                 ontender += transfer;
157                 total += transfer;
158             } else {
159                 /* give to target from tender */
160                 transfer = MIN(ontender, amt);
161                 transfer = MIN(transfer, maxtarget - ontarget);
162                 if (transfer == 0)
163                     continue;
164                 target.shp_item[ip->i_uid] = ontarget + transfer;
165                 ontender -= transfer;
166                 total += transfer;
167                 if (transfer && target.shp_own != player->cnum) {
168                     wu(0, target.shp_own, "%s tended %d %s to %s\n",
169                        cname(player->cnum), total, ip->i_name,
170                        prship(&target));
171                 }
172             }
173             expose_ship(&tender, &target);
174             putship(target.shp_uid, &target);
175             if (amt > 0 && ontender == 0) {
176                 pr("%s out of %s\n", prship(&tender), ip->i_name);
177                 break;
178             }
179         }
180         pr("%d total %s transferred %s %s\n",
181            total, ip->i_name, (amt > 0) ? "off of" : "to",
182            prship(&tender));
183         tender.shp_item[ip->i_uid] = ontender;
184         tender.shp_mission = 0;
185         putship(tender.shp_uid, &tender);
186     }
187     return RET_OK;
188 }
189
190 static void
191 expose_ship(struct shpstr *s1, struct shpstr *s2)
192 {
193     if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
194         s2->shp_pstage = PLG_EXPOSED;
195     if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
196         s1->shp_pstage = PLG_EXPOSED;
197 }
198
199 static int
200 tend_land(struct shpstr *tenderp, char *units)
201 {
202     struct nstr_item lni;
203     struct nstr_item targets;
204     struct shpstr target;
205     struct lndstr land;
206     struct plnstr plane;
207     struct nstr_item pni;
208     char buf[1024];
209
210     if (!snxtitem(&lni, EF_LAND, units))
211         return RET_SYN;
212
213     while (nxtitem(&lni, &land)) {
214         if (!player->owner)
215             continue;
216         if (land.lnd_ship != tenderp->shp_uid) {
217             pr("%s is not on %s!\n", prland(&land), prship(tenderp));
218             continue;
219         }
220         if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
221             pr("%s does not have \"assault\" capability and can't be tended\n",
222                prland(&land));
223             continue;
224         }
225         if (!snxtitem(&targets, EF_SHIP,
226                       getstarg(player->argp[4], "Ship to be tended? ",
227                                buf)))
228             break;
229         if (!check_land_ok(&land))
230             return RET_SYN;
231         while (nxtitem(&targets, &target)) {
232             if (!player->owner &&
233                 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
234                 continue;
235             if (target.shp_uid == tenderp->shp_uid)
236                 continue;
237             if (tenderp->shp_x != target.shp_x ||
238                 tenderp->shp_y != target.shp_y)
239                 continue;
240
241             /* Fit unit on ship */
242             count_units(&target);
243             getship(target.shp_uid, &target);
244
245             if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
246                 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
247                    (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
248                 pr("You can only load light units onto ships,\n"
249                    "unless the ship is a non-sub supply ship\n"
250                    "%s not tended\n", prland(&land));
251                 continue;
252             }
253
254             if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
255                 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
256                 !mchr[(int)target.shp_type].m_nland) {
257                 if (target.shp_nland > 1) {
258                     pr("%s doesn't have room for more than two spy units!\n",
259                        prship(&target));
260                     continue;
261                 }
262             } else if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
263                 if (mchr[(int)target.shp_type].m_nland)
264                     pr("%s doesn't have room for any more land units!\n",
265                        prship(&target));
266                 else
267                     pr("%s doesn't carry land units!\n", prship(&target));
268                 continue;
269             }
270             pr("%s transferred from %s to %s\n",
271                prland(&land), prship(tenderp), prship(&target));
272             sprintf(buf, "loaded on your %s at %s",
273                     prship(&target), xyas(target.shp_x, target.shp_y,
274                                           target.shp_own));
275             gift(target.shp_own, player->cnum, &land, buf);
276             land.lnd_ship = target.shp_uid;
277             land.lnd_harden = 0;
278             land.lnd_mission = 0;
279             target.shp_nland++;
280             putland(land.lnd_uid, &land);
281             expose_ship(tenderp, &target);
282             putship(target.shp_uid, &target);
283             count_units(tenderp);
284             putship(tenderp->shp_uid, tenderp);
285             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
286             while (nxtitem(&pni, &plane)) {
287                 if (plane.pln_flags & PLN_LAUNCHED)
288                     continue;
289                 if (plane.pln_land != land.lnd_uid)
290                     continue;
291                 sprintf(buf, "loaded on %s", prship(&target));
292                 gift(target.shp_own, player->cnum, &plane, buf);
293                 plane.pln_mission = 0;
294                 putplane(plane.pln_uid, &plane);
295             }
296         }
297     }
298     return 0;
299 }