]> git.pond.sub.org Git - empserver/blob - src/lib/commands/lten.c
Update copyright notice
[empserver] / src / lib / commands / lten.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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  *  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 <config.h>
39
40 #include "commands.h"
41 #include "item.h"
42 #include "land.h"
43 #include "plague.h"
44 #include "plane.h"
45 #include "ship.h"
46
47 static void expose_land(struct shpstr *s1, struct lndstr *l1);
48
49 int
50 ltend(void)
51 {
52     struct nstr_item targets;
53     struct nstr_item tenders;
54     struct shpstr tender;
55     struct lndstr target;
56     struct ichrstr *ip;
57     struct mchrstr *vbase;
58     struct lchrstr *lbase;
59     int amt;
60     int ontender;
61     int ontarget;
62     int maxtender;
63     int maxtarget;
64     int transfer;
65     int total;
66     char *p;
67     char buf[1024];
68
69     if (!(ip = whatitem(player->argp[1], "Transfer what commodity? ")))
70         return RET_SYN;
71
72     if (!snxtitem(&tenders, EF_SHIP,
73                   getstarg(player->argp[2], "Tender(s)? ", buf)))
74         return RET_SYN;
75     while (nxtitem(&tenders, &tender)) {
76         if (!player->owner)
77             continue;
78         if ((p =
79              getstarg(player->argp[3], "Amount to transfer? ", buf)) == 0)
80             break;
81         if (!check_ship_ok(&tender))
82             return RET_FAIL;
83         if ((amt = atoi(p)) == 0)
84             break;
85         ontender = tender.shp_item[ip->i_uid];
86         if (ontender == 0 && amt > 0) {
87             pr("No %s on %s\n", ip->i_name, prship(&tender));
88             return RET_FAIL;
89         }
90         vbase = &mchr[(int)tender.shp_type];
91         maxtender = vbase->m_item[ip->i_uid];
92         if (maxtender == 0) {
93             pr("A %s cannot hold any %s\n",
94                mchr[(int)tender.shp_type].m_name, ip->i_name);
95             break;
96         }
97         if (!snxtitem(&targets, EF_LAND,
98                       getstarg(player->argp[4], "Units to be tended? ",
99                                buf)))
100             break;
101         if (!check_ship_ok(&tender))
102             return RET_FAIL;
103         total = 0;
104         while (nxtitem(&targets, &target)) {
105             if (!player->owner)
106                 continue;
107
108             if (target.lnd_ship != tender.shp_uid)
109                 continue;
110             ontarget = target.lnd_item[ip->i_uid];
111             if (ontarget == 0 && amt < 0) {
112                 pr("No %s on %s\n",
113                    ip->i_name, prland(&target));
114                 continue;
115             }
116             lbase = &lchr[(int)target.lnd_type];
117             maxtarget = lbase->l_item[ip->i_uid];
118             if (amt < 0) {
119                 if (!player->owner)
120                     amt = 0;
121
122                 /* take from target and give to tender */
123                 transfer = MIN(ontarget, -amt);
124                 transfer = MIN(maxtender - ontender, transfer);
125                 if (transfer == 0)
126                     continue;
127                 target.lnd_item[ip->i_uid] = ontarget - transfer;
128                 ontender += transfer;
129                 total += transfer;
130             } else {
131                 /* give to target from tender */
132                 transfer = MIN(ontender, amt);
133                 transfer = MIN(transfer, maxtarget - ontarget);
134                 if (transfer == 0)
135                     continue;
136                 target.lnd_item[ip->i_uid] = ontarget + transfer;
137                 ontender -= transfer;
138                 total += transfer;
139             }
140             expose_land(&tender, &target);
141             putland(target.lnd_uid, &target);
142             if (amt > 0 && ontender == 0) {
143                 pr("%s out of %s\n", prship(&tender), ip->i_name);
144                 break;
145             }
146         }
147         pr("%d total %s transferred %s %s\n",
148            total, ip->i_name, (amt > 0) ? "off of" : "to",
149            prship(&tender));
150         tender.shp_item[ip->i_uid] = ontender;
151         tender.shp_mission = 0;
152         putship(tender.shp_uid, &tender);
153     }
154     return RET_OK;
155 }
156
157 static void
158 expose_land(struct shpstr *s1, struct lndstr *l1)
159 {
160     if (s1->shp_pstage == PLG_INFECT && l1->lnd_pstage == PLG_HEALTHY)
161         l1->lnd_pstage = PLG_EXPOSED;
162     if (l1->lnd_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
163         s1->shp_pstage = PLG_EXPOSED;
164 }