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