]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tend.c
66aca256ee6aca70f0f25f28a03a4ea17af0af71
[empserver] / src / lib / commands / tend.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  tend.c: Transfer goodies from one ship to another.
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Thomas Ruschak, 1992
32  *     Steve McClure, 2000
33  *     Markus Armbruster, 2004-2017
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 "ship.h"
43
44 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
45 static int tend_land(struct shpstr *tenderp, char *units);
46
47 int
48 tend(void)
49 {
50     struct nstr_item targets;
51     struct nstr_item tenders;
52     struct shpstr tender;
53     struct shpstr target;
54     struct ichrstr *ip;
55     struct mchrstr *vbase;
56     int amt;
57     int retval;
58     int ontender;
59     int ontarget;
60     int maxtender;
61     int maxtarget;
62     int transfer;
63     int total;
64     int type;
65     char *p;
66     char prompt[512];
67     char buf[1024];
68
69     p = getstarg(player->argp[1], "Tend what commodity (or 'land')? ",
70                  buf);
71     if (!p || !*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, player->argp[2], "Tender(s)? "))
84         return RET_SYN;
85
86     while (nxtitem(&tenders, &tender)) {
87         if (!player->owner) {
88             if (tenders.sel == NS_LIST)
89                 pr("You don't own ship #%d!\n", tender.shp_uid);
90             continue;
91         }
92         if (type == EF_LAND) {
93             sprintf(prompt, "Land unit(s) to tend from %s? ",
94                     prship(&tender));
95             p = getstarg(player->argp[3], prompt, buf);
96             if (!p)
97                 return RET_FAIL;
98             if (!*p)
99                 continue;
100             if (!check_ship_ok(&tender))
101                 return RET_SYN;
102             if (0 != (retval = tend_land(&tender, p)))
103                 return retval;
104             continue;
105         }
106         sprintf(prompt, "Number of %s to tend from %s? ",
107                 ip->i_name, prship(&tender));
108         p = getstarg(player->argp[3], prompt, buf);
109         if (!p)
110             return RET_FAIL;
111         if (!*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_uid];
120         if (ontender == 0 && amt > 0) {
121             pr("No %s on %s\n", ip->i_name, prship(&tender));
122             continue;
123         }
124         vbase = &mchr[(int)tender.shp_type];
125         maxtender = vbase->m_item[ip->i_uid];
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             continue;
130         }
131         if (!snxtitem(&targets, EF_SHIP,
132                       player->argp[4], "Ships to be tended? "))
133             return RET_FAIL;
134         if (!check_ship_ok(&tender))
135             return RET_SYN;
136         total = 0;
137         while (nxtitem(&targets, &target)) {
138             if (!player->owner
139                 && relations_with(target.shp_own, player->cnum) < FRIENDLY)
140                 continue;
141             if (target.shp_uid == tender.shp_uid)
142                 continue;
143             if (tender.shp_x != target.shp_x ||
144                 tender.shp_y != target.shp_y)
145                 continue;
146             if (ip->i_uid == I_CIVIL && tender.shp_own != target.shp_own)
147                 continue;
148             ontarget = target.shp_item[ip->i_uid];
149             vbase = &mchr[(int)target.shp_type];
150             maxtarget = vbase->m_item[ip->i_uid];
151             if (amt < 0) {
152                 /* take from target and give to tender */
153                 if (!player->owner)
154                     continue;
155                 if (ontarget == 0) {
156                     pr("No %s on %s\n", ip->i_name, prship(&target));
157                     continue;
158                 }
159                 transfer = MIN(ontarget, -amt);
160                 transfer = MIN(maxtender - ontender, transfer);
161                 if (transfer == 0)
162                     continue;
163                 target.shp_item[ip->i_uid] = 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_uid] = ontarget + transfer;
173                 ontender -= transfer;
174                 total += transfer;
175                 if (transfer && target.shp_own != player->cnum) {
176                     wu(0, target.shp_own, "%s tended %d %s to %s\n",
177                        cname(player->cnum), total, ip->i_name,
178                        prship(&target));
179                 }
180             }
181             expose_ship(&tender, &target);
182             putship(target.shp_uid, &target);
183             if (amt > 0 && ontender == 0) {
184                 pr("%s out of %s\n", prship(&tender), ip->i_name);
185                 break;
186             }
187         }
188         pr("%d total %s transferred %s %s\n",
189            total, ip->i_name, (amt > 0) ? "off of" : "to",
190            prship(&tender));
191         tender.shp_item[ip->i_uid] = ontender;
192         tender.shp_mission = 0;
193         putship(tender.shp_uid, &tender);
194     }
195     return RET_OK;
196 }
197
198 static void
199 expose_ship(struct shpstr *s1, struct shpstr *s2)
200 {
201     if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
202         s2->shp_pstage = PLG_EXPOSED;
203     if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
204         s1->shp_pstage = PLG_EXPOSED;
205 }
206
207 static int
208 tend_land(struct shpstr *tenderp, char *units)
209 {
210     struct nstr_item lni;
211     struct nstr_item targets;
212     struct shpstr target;
213     struct lndstr land;
214     char buf[1024];
215
216     if (!snxtitem(&lni, EF_LAND, units, NULL))
217         return RET_SYN;
218
219     while (nxtitem(&lni, &land)) {
220         if (!player->owner) {
221             if (lni.sel == NS_LIST)
222                 pr("You don't own land unit #%d!\n", land.lnd_uid);
223             continue;
224         }
225         if (land.lnd_ship != tenderp->shp_uid) {
226             if (lni.sel == NS_LIST)
227                 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
228             continue;
229         }
230         if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
231             pr("%s does not have \"assault\" capability and can't be tended\n",
232                prland(&land));
233             continue;
234         }
235         if (!snxtitem(&targets, EF_SHIP,
236                       player->argp[4], "Ship to be tended? "))
237             return RET_FAIL;
238         if (!check_ship_ok(tenderp) || !check_land_ok(&land))
239             return RET_SYN;
240         while (nxtitem(&targets, &target)) {
241             if (!player->owner
242                 && relations_with(target.shp_own, player->cnum) < FRIENDLY)
243                 continue;
244             if (target.shp_uid == tenderp->shp_uid)
245                 continue;
246             if (tenderp->shp_x != target.shp_x ||
247                 tenderp->shp_y != target.shp_y)
248                 continue;
249
250             /* Fit unit on ship */
251             getship(target.shp_uid, &target);
252
253             if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
254                 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
255                    (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
256                 pr("You can only load light units onto ships,\n"
257                    "unless the ship is a non-sub supply ship\n"
258                    "%s not tended\n", prland(&land));
259                 continue;
260             }
261
262             if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
263                 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
264                 !mchr[(int)target.shp_type].m_nland) {
265                 if (shp_nland(&target) > 1) {
266                     pr("%s doesn't have room for more than two spy units!\n",
267                        prship(&target));
268                     continue;
269                 }
270             } else if (shp_nland(&target) >= mchr[target.shp_type].m_nland) {
271                 if (mchr[(int)target.shp_type].m_nland)
272                     pr("%s doesn't have room for any more land units!\n",
273                        prship(&target));
274                 else
275                     pr("%s doesn't carry land units!\n", prship(&target));
276                 continue;
277             }
278             pr("%s transferred from %s to %s\n",
279                prland(&land), prship(tenderp), prship(&target));
280             sprintf(buf, "loaded on your %s at %s",
281                     prship(&target), xyas(target.shp_x, target.shp_y,
282                                           target.shp_own));
283             gift(target.shp_own, player->cnum, &land, buf);
284             land.lnd_ship = target.shp_uid;
285             land.lnd_harden = 0;
286             putland(land.lnd_uid, &land);
287             expose_ship(tenderp, &target);
288             putship(target.shp_uid, &target);
289             putship(tenderp->shp_uid, tenderp);
290             break;
291         }
292     }
293     return 0;
294 }