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