]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tend.c
(tend): Fix rev. 1.20: used to send bulletin to last tendee's owner,
[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_uid];
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_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             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_uid];
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_uid];
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_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, s_char *units)
209 {
210     struct nstr_item lni;
211     struct nstr_item targets;
212     struct shpstr target;
213     struct lndstr land;
214     struct plnstr plane;
215     struct nstr_item pni;
216     s_char buf[1024];
217
218     if (!snxtitem(&lni, EF_LAND, units))
219         return RET_SYN;
220
221     while (nxtitem(&lni, &land)) {
222         if (!player->owner)
223             continue;
224         if (land.lnd_ship != tenderp->shp_uid) {
225             pr("%s is not on %s!\n", prland(&land), prship(tenderp));
226             continue;
227         }
228         if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
229             pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
230             continue;
231         }
232         if (!snxtitem(&targets, EF_SHIP,
233                       getstarg(player->argp[4], "Ship to be tended? ",
234                                buf)))
235             break;
236         if (!check_land_ok(&land))
237             return RET_SYN;
238         while (nxtitem(&targets, &target)) {
239             if (!player->owner &&
240                 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
241                 continue;
242             if (target.shp_uid == tenderp->shp_uid)
243                 continue;
244             if (tenderp->shp_x != target.shp_x ||
245                 tenderp->shp_y != target.shp_y)
246                 continue;
247
248             /* Fit unit on ship */
249             count_units(&target);
250             getship(target.shp_uid, &target);
251
252             if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
253                 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
254                 !mchr[(int)target.shp_type].m_nland) {
255                 if (target.shp_nland > 1) {
256                     pr("%s doesn't have room for more than two spy units!\n",
257                        prship(&target));
258                     continue;
259                 }
260             } else if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
261                 if (mchr[(int)target.shp_type].m_nland)
262                     pr("%s doesn't have room for any more land units!\n",
263                        prship(&target));
264                 else
265                     pr("%s doesn't carry land units!\n", prship(&target));
266                 continue;
267             }
268             pr("%s transferred from %s to %s\n",
269                prland(&land), prship(tenderp), prship(&target));
270             sprintf(buf, "loaded on your %s at %s",
271                     prship(&target), xyas(target.shp_x, target.shp_y,
272                                           target.shp_own));
273             gift(target.shp_own, player->cnum, &land, 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, &plane, EF_PLANE, buf);
296                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
297                          plane.pln_x, plane.pln_y);
298                 plane.pln_own = target.shp_own;
299                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
300                             plane.pln_x, plane.pln_y);
301                 plane.pln_mission = 0;
302                 putplane(plane.pln_uid, &plane);
303             }
304         }
305     }
306     return 0;
307 }