]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sell.c
Import of Empire 4.2.12
[empserver] / src / lib / commands / sell.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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  *  sell.c: Sell commodities to other nations.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Jeff Bailey
33  *     Pat Loney, 1992
34  *     Steve McClure, 1996
35  */
36
37 #include "misc.h"
38 #include "xy.h"
39 #include "file.h"
40 #include "var.h"
41 #include "sect.h"
42 #include "item.h"
43 #include "nsc.h"
44 #include "nat.h"
45 #include "nuke.h"
46 #include "plane.h"
47 #include "ship.h"
48 #include <math.h>               /* bailey@math-cs.kent.edu */
49 #include "commodity.h"
50 #include "land.h"
51 #include "trade.h"
52 #include "player.h"
53 #include "commands.h"
54 #include "optlist.h"
55 /*#define EF_COMM 10*/
56
57 /*
58  * format: sell <COMMODITY> <SECTS> <NUMBER> <PRICE>
59  *   where NUMBER represents either the number to reach
60  *   or, if negative, the abs number to try and get from
61  *   each sector.
62  */
63 int
64 sell(void)
65 {
66         struct sctstr   sect;
67         struct ichrstr  *ip;
68         struct comstr comm;
69         int     number_set;
70         int     number_sub;
71         int     totalcom;
72         int     amt;
73         int     com;
74         char    *p;
75         float   price;
76         char    cc;
77         time_t  now;
78         int     ii = 0;
79         coord   x, y;
80         s_char  buf[1024];
81
82         if (!opt_MARKET) {
83             pr("The market is disabled.\n");
84             return RET_FAIL;
85         }       
86         check_market();
87         check_trade();
88         if ((ip = whatitem(player->argp[1], "Commodity you want to sell: ")) == 0)
89                 return RET_SYN;
90         if (ip->i_sell == 0) {
91                 pr("You can't sell %s\n", ip->i_name);
92                 return RET_FAIL;
93         }
94         if (!(p = getstarg(player->argp[2], "Sector to sell from: ",buf)))
95             return RET_SYN;
96         if (!sarg_xy(p, &x, &y))
97             return RET_SYN;
98         if (!getsect(x, y, &sect))
99             pr("Could not access that sector.\n");
100         if ((sect.sct_type != SCT_HARBR && sect.sct_type != SCT_WAREH) ||
101             !player->owner) {
102             pr("That sector cannot sell goods.\n");
103             return RET_FAIL;
104         }
105         if(sect.sct_effic < 60){
106             pr("Sectors need to be >= 60%% efficient to sell goods.\n");
107             return RET_FAIL;
108         }
109         if (sect.sct_mobil <= 0) {
110             pr("Sectors need at least 1 mobility to sell goods.\n");
111             return RET_FAIL;
112         }
113         number_set = 0;
114         number_sub = 0;
115         if ((p = getstarg(player->argp[3], "Amount:  ",buf)) == 0 || *p == 0)
116                 return RET_SYN;
117         if (!check_sect_ok(&sect))
118             return RET_FAIL;
119         number_set = atoi(p);
120         if ((p = getstarg(player->argp[4], "Price per unit: ", buf)) == 0 ||
121             *p == 0)
122                 return RET_SYN;
123         if (!check_sect_ok(&sect))
124             return RET_FAIL;
125         price = atof(p);
126         if (price <= 0.0) {
127                 pr("No sale.\n");
128                 return RET_FAIL;
129         }
130         if (price > 1000.0) /* Inf can cause overflow */
131                 price = 1000.0;           /* bailey@math-cs.kent.edu */
132         totalcom = 0;
133         /*
134          * military control necessary to sell
135          * goodies in occupied territory.
136          */
137         if (sect.sct_oldown != player->cnum) {
138             int     tot_mil=0;
139             struct  nstr_item ni;
140             struct  lndstr land;
141
142             snxtitem_xy(&ni, EF_LAND, sect.sct_x, sect.sct_y);
143             while (nxtitem(&ni, (s_char *)&land)){
144                 if (land.lnd_own == player->cnum)
145                     tot_mil  += total_mil(&land);
146             }
147             if (((tot_mil+(getvar(V_MILIT, (char *)&sect, EF_SECTOR))) * 10)
148                 < getvar(V_CIVIL, (char *)&sect, EF_SECTOR)) {
149                 pr("Military control required to sell goods.\n");
150                 return RET_FAIL;
151             }
152         }
153         if (((amt = getvar(ip->i_vtype, (char *)&sect,EF_SECTOR))) == 0) {
154             pr("You don't have any %s to sell there.\n", ip->i_name);
155             return RET_FAIL;
156         }
157         if (number_set > 0)
158             com = min(number_set, amt);
159         else if (number_set < 0)
160             com = amt + number_set;
161         else
162             com = 0;
163         if (com <= 0)
164             return RET_SYN;
165         totalcom += com;
166         amt -= com;
167         pr("Sold %d %s at %s (%d left)\n", com, ip->i_name,
168            xyas(sect.sct_x, sect.sct_y, player->cnum), amt);
169         putvar(ip->i_vtype, amt, (char *)&sect, EF_SECTOR);
170         cc = ip->i_mnem;
171         putsect(&sect);
172         if (totalcom > 0) {
173             for (ii = 0; getcomm(ii, &comm); ii++) {
174                 if (comm.com_owner == 0)
175                     break;
176             }
177             if (getcomm(ii, &comm) == 0)
178                 ef_extend(EF_COMM, 1);
179             (void) time(&now);
180             comm.com_type = ip->i_mnem;
181             comm.com_owner = player->cnum;
182             comm.com_price = price;
183             comm.com_maxbidder = player->cnum;
184             comm.com_maxprice = price;
185             comm.com_markettime = now;
186             comm.com_amount = totalcom;
187             comm.com_x = 0;
188             comm.com_y = 0;
189             comm.sell_x = sect.sct_x;
190             comm.sell_y = sect.sct_y;
191             comm.com_uid = ii;
192             if(!putcomm(ii, &comm)) {
193                 pr("Problems with the commodities file, call the Deity\n");
194                 return RET_FAIL;
195             }
196         } else {
197             pr("No eligible %s for sale\n", ip->i_name);
198             return RET_FAIL;
199         }
200         return RET_OK;
201 }
202
203