]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sell.c
Clean up dead stores
[empserver] / src / lib / commands / sell.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *  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 <config.h>
38
39 #include "commands.h"
40 #include "commodity.h"
41 #include "item.h"
42 #include "land.h"
43 #include "optlist.h"
44 #include "plane.h"
45 #include "ship.h"
46
47 /*
48  * format: sell <COMMODITY> <SECTS> <NUMBER> <PRICE>
49  *   where NUMBER represents either the number to reach
50  *   or, if negative, the abs number to try and get from
51  *   each sector.
52  */
53 int
54 sell(void)
55 {
56     struct sctstr sect;
57     struct ichrstr *ip;
58     struct comstr comm;
59     int number_set;
60     int totalcom;
61     int amt;
62     int com;
63     char *p;
64     float price;
65     time_t now;
66     int ii = 0;
67     coord x, y;
68     char buf[1024];
69
70     if (!opt_MARKET) {
71         pr("The market is disabled.\n");
72         return RET_FAIL;
73     }
74     check_market();
75     check_trade();
76     if (!(ip = whatitem(player->argp[1], "Commodity you want to sell: ")))
77         return RET_SYN;
78     if (ip->i_sell == 0) {
79         pr("You can't sell %s\n", ip->i_name);
80         return RET_FAIL;
81     }
82     if (!(p = getstarg(player->argp[2], "Sector to sell from: ", buf)))
83         return RET_SYN;
84     if (!sarg_xy(p, &x, &y))
85         return RET_SYN;
86     if (!getsect(x, y, &sect))
87         pr("Could not access that sector.\n");
88     if ((sect.sct_type != SCT_HARBR && sect.sct_type != SCT_WAREH) ||
89         !player->owner) {
90         pr("That sector cannot sell goods.\n");
91         return RET_FAIL;
92     }
93     if (sect.sct_effic < 60) {
94         pr("Sectors need to be >= 60%% efficient to sell goods.\n");
95         return RET_FAIL;
96     }
97     if (sect.sct_mobil <= 0) {
98         pr("Sectors need at least 1 mobility to sell goods.\n");
99         return RET_FAIL;
100     }
101     p = getstarg(player->argp[3], "Quantity: ", buf);
102     if (!p || !*p)
103         return RET_SYN;
104     if (!check_sect_ok(&sect))
105         return RET_FAIL;
106     number_set = atoi(p);
107     p = getstarg(player->argp[4], "Price per unit: ", buf);
108     if (!p || !*p)
109         return RET_SYN;
110     if (!check_sect_ok(&sect))
111         return RET_FAIL;
112     price = atof(p);
113     if (price <= 0.0) {
114         pr("No sale.\n");
115         return RET_FAIL;
116     }
117     if (price > 1000.0)         /* Inf can cause overflow */
118         price = 1000.0;         /* bailey@math-cs.kent.edu */
119     totalcom = 0;
120     if (!military_control(&sect)) {
121         pr("Military control required to sell goods.\n");
122         return RET_FAIL;
123     }
124     if ((amt = sect.sct_item[ip->i_uid]) == 0) {
125         pr("You don't have any %s to sell there.\n", ip->i_name);
126         return RET_FAIL;
127     }
128     if (number_set >= 0)
129         com = MIN(number_set, amt);
130     else
131         com = amt + number_set;
132     if (com <= 0)
133         return RET_SYN;
134     totalcom += com;
135     amt -= com;
136     pr("Sold %d %s at %s (%d left)\n", com, ip->i_name,
137        xyas(sect.sct_x, sect.sct_y, player->cnum), amt);
138     sect.sct_item[ip->i_uid] = amt;
139     putsect(&sect);
140     if (totalcom > 0) {
141         for (ii = 0; getcomm(ii, &comm); ii++) {
142             if (comm.com_owner == 0)
143                 break;
144         }
145         (void)time(&now);
146         ef_blank(EF_COMM, ii, &comm);
147         comm.com_type = ip->i_uid;
148         comm.com_owner = player->cnum;
149         comm.com_price = price;
150         comm.com_maxbidder = player->cnum;
151         comm.com_markettime = now;
152         comm.com_amount = totalcom;
153         comm.com_x = 0;
154         comm.com_y = 0;
155         comm.sell_x = sect.sct_x;
156         comm.sell_y = sect.sct_y;
157         if (!putcomm(ii, &comm)) {
158             pr("Problems with the commodities file, call the Deity\n");
159             return RET_FAIL;
160         }
161     } else {
162         pr("No eligible %s for sale\n", ip->i_name);
163         return RET_FAIL;
164     }
165     return RET_OK;
166 }