]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sell.c
Remove unreachable code in sell()
[empserver] / src / lib / commands / sell.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  sell.c: Sell commodities to other nations.
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Jeff Bailey
32  *     Pat Loney, 1992
33  *     Steve McClure, 1996
34  */
35
36 #include <config.h>
37
38 #include "commands.h"
39 #include "commodity.h"
40 #include "item.h"
41 #include "land.h"
42 #include "optlist.h"
43 #include "plane.h"
44 #include "ship.h"
45
46 /*
47  * format: sell <COMMODITY> <SECTS> <NUMBER> <PRICE>
48  *   where NUMBER represents either the number to reach
49  *   or, if negative, the abs number to try and get from
50  *   each sector.
51  */
52 int
53 sell(void)
54 {
55     struct sctstr sect;
56     struct ichrstr *ip;
57     struct comstr comm;
58     int number_set;
59     int totalcom;
60     int amt;
61     int com;
62     char *p;
63     float price;
64     time_t now;
65     int ii = 0;
66     coord x, y;
67     char buf[1024];
68
69     if (!opt_MARKET) {
70         pr("The market is disabled.\n");
71         return RET_FAIL;
72     }
73     check_market();
74     check_trade();
75     if (!(ip = whatitem(player->argp[1], "Commodity you want to sell: ")))
76         return RET_SYN;
77     if (ip->i_sell == 0) {
78         pr("You can't sell %s\n", ip->i_name);
79         return RET_FAIL;
80     }
81     if (!(p = getstarg(player->argp[2], "Sector to sell from: ", buf)))
82         return RET_SYN;
83     if (!sarg_xy(p, &x, &y))
84         return RET_SYN;
85     if (!getsect(x, y, &sect))
86         pr("Could not access that sector.\n");
87     if ((sect.sct_type != SCT_HARBR && sect.sct_type != SCT_WAREH) ||
88         !player->owner) {
89         pr("That sector cannot sell goods.\n");
90         return RET_FAIL;
91     }
92     if (sect.sct_effic < 60) {
93         pr("Sectors need to be >= 60%% efficient to sell goods.\n");
94         return RET_FAIL;
95     }
96     if (sect.sct_mobil <= 0) {
97         pr("Sectors need at least 1 mobility to sell goods.\n");
98         return RET_FAIL;
99     }
100     p = getstarg(player->argp[3], "Quantity: ", buf);
101     if (!p || !*p)
102         return RET_SYN;
103     if (!check_sect_ok(&sect))
104         return RET_FAIL;
105     number_set = atoi(p);
106     p = getstarg(player->argp[4], "Price per unit: ", buf);
107     if (!p || !*p)
108         return RET_SYN;
109     if (!check_sect_ok(&sect))
110         return RET_FAIL;
111     price = atof(p);
112     if (price <= 0.0) {
113         pr("No sale.\n");
114         return RET_FAIL;
115     }
116     if (price > 1000.0)         /* Inf can cause overflow */
117         price = 1000.0;         /* bailey@math-cs.kent.edu */
118     totalcom = 0;
119     if (!military_control(&sect)) {
120         pr("Military control required to sell goods.\n");
121         return RET_FAIL;
122     }
123     if ((amt = sect.sct_item[ip->i_uid]) == 0) {
124         pr("You don't have any %s to sell there.\n", ip->i_name);
125         return RET_FAIL;
126     }
127     if (number_set >= 0)
128         com = MIN(number_set, amt);
129     else
130         com = amt + number_set;
131     if (com <= 0)
132         return RET_SYN;
133     totalcom += com;
134     amt -= com;
135     pr("Sold %d %s at %s (%d left)\n", com, ip->i_name,
136        xyas(sect.sct_x, sect.sct_y, player->cnum), amt);
137     sect.sct_item[ip->i_uid] = amt;
138     putsect(&sect);
139
140     for (ii = 0; getcomm(ii, &comm); ii++) {
141         if (comm.com_owner == 0)
142             break;
143     }
144     (void)time(&now);
145     ef_blank(EF_COMM, ii, &comm);
146     comm.com_type = ip->i_uid;
147     comm.com_owner = player->cnum;
148     comm.com_price = price;
149     comm.com_maxbidder = player->cnum;
150     comm.com_markettime = now;
151     comm.com_amount = totalcom;
152     comm.com_x = 0;
153     comm.com_y = 0;
154     comm.sell_x = sect.sct_x;
155     comm.sell_y = sect.sct_y;
156     if (!putcomm(ii, &comm)) {
157         pr("Problems with the commodities file, call the Deity\n");
158         return RET_FAIL;
159     }
160     return RET_OK;
161 }