]> git.pond.sub.org Git - empserver/blob - src/lib/commands/lten.c
(ltend): Fix printing of land unit in diagnostic message.
[empserver] / src / lib / commands / lten.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  lten.c: Transfer commodity from a ship to a land unit the ship is
29  *          carrying
30  * 
31  *  Known contributors to this file:
32  *     Dave Pare, 1986
33  *     Thomas Ruschak
34  *     Ken Stevens, 1995
35  *     Steve McClure, 1998
36  */
37
38 #include "misc.h"
39 #include "player.h"
40 #include "plague.h"
41 #include "file.h"
42 #include "ship.h"
43 #include "item.h"
44 #include "nsc.h"
45 #include "plane.h"
46 #include "land.h"
47 #include "commands.h"
48
49 static void expose_land(struct shpstr *s1, struct lndstr *l1);
50
51 int
52 ltend(void)
53 {
54     struct nstr_item targets;
55     struct nstr_item tenders;
56     struct shpstr tender;
57     struct lndstr target;
58     struct ichrstr *ip;
59     struct mchrstr *vbase;
60     struct lchrstr *lbase;
61     int amt;
62     int ontender;
63     int ontarget;
64     int maxtender;
65     int maxtarget;
66     int transfer;
67     int total;
68     s_char *p;
69     s_char buf[1024];
70
71     if (!(ip = whatitem(player->argp[1], "Transfer what commodity? ")))
72         return RET_SYN;
73
74     if (!snxtitem(&tenders, EF_SHIP,
75                   getstarg(player->argp[2], "Tender(s)? ", buf)))
76         return RET_SYN;
77     while (nxtitem(&tenders, &tender)) {
78         if (!player->owner)
79             continue;
80         if ((p =
81              getstarg(player->argp[3], "Amount to transfer? ", buf)) == 0)
82             break;
83         if (!check_ship_ok(&tender))
84             return RET_FAIL;
85         if ((amt = atoi(p)) == 0)
86             break;
87         ontender = tender.shp_item[ip->i_vtype];
88         if (ontender == 0 && amt > 0) {
89             pr("No %s on %s\n", ip->i_name, prship(&tender));
90             return RET_FAIL;
91         }
92         vbase = &mchr[(int)tender.shp_type];
93         maxtender = vbase->m_item[ip->i_vtype];
94         if (maxtender == 0) {
95             pr("A %s cannot hold any %s\n",
96                mchr[(int)tender.shp_type].m_name, ip->i_name);
97             break;
98         }
99         if (!snxtitem(&targets, EF_LAND,
100                       getstarg(player->argp[4], "Units to be tended? ",
101                                buf)))
102             break;
103         if (!check_ship_ok(&tender))
104             return RET_FAIL;
105         total = 0;
106         while (nxtitem(&targets, &target)) {
107             if (!player->owner)
108                 continue;
109
110             if (target.lnd_ship != tender.shp_uid)
111                 continue;
112             ontarget = target.lnd_item[ip->i_vtype];
113             if (ontarget == 0 && amt < 0) {
114                 pr("No %s on %s\n",
115                    ip->i_name, prland(&target));
116                 continue;
117             }
118             lbase = &lchr[(int)target.lnd_type];
119             maxtarget = lbase->l_item[ip->i_vtype];
120             if (amt < 0) {
121                 if (!player->owner)
122                     amt = 0;
123
124                 /* take from target and give to tender */
125                 transfer = min(ontarget, -amt);
126                 transfer = min(maxtender - ontender, transfer);
127                 if (transfer == 0)
128                     continue;
129                 target.lnd_item[ip->i_vtype] = ontarget - transfer;
130                 ontender += transfer;
131                 total += transfer;
132             } else {
133                 /* give to target from tender */
134                 transfer = min(ontender, amt);
135                 transfer = min(transfer, maxtarget - ontarget);
136                 if (transfer == 0)
137                     continue;
138                 target.lnd_item[ip->i_vtype] = ontarget + transfer;
139                 ontender -= transfer;
140                 total += transfer;
141             }
142             expose_land(&tender, &target);
143             putland(target.lnd_uid, &target);
144             if (amt > 0 && ontender == 0) {
145                 pr("%s out of %s\n", prship(&tender), ip->i_name);
146                 break;
147             }
148         }
149         pr("%d total %s transferred %s %s\n",
150            total, ip->i_name, (amt > 0) ? "off of" : "to",
151            prship(&tender));
152         tender.shp_item[ip->i_vtype] = ontender;
153         tender.shp_mission = 0;
154         putship(tender.shp_uid, &tender);
155     }
156     return RET_OK;
157 }
158
159 static void
160 expose_land(struct shpstr *s1, struct lndstr *l1)
161 {
162     if (s1->shp_pstage == PLG_INFECT && l1->lnd_pstage == PLG_HEALTHY)
163         l1->lnd_pstage = PLG_EXPOSED;
164     if (l1->lnd_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
165         s1->shp_pstage = PLG_EXPOSED;
166 }