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