]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sell.c
(sell): Simplify.
[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 =
89          whatitem(player->argp[1], "Commodity you want to sell: ")) == 0)
90         return RET_SYN;
91     if (ip->i_sell == 0) {
92         pr("You can't sell %s\n", ip->i_name);
93         return RET_FAIL;
94     }
95     if (!(p = getstarg(player->argp[2], "Sector to sell from: ", buf)))
96         return RET_SYN;
97     if (!sarg_xy(p, &x, &y))
98         return RET_SYN;
99     if (!getsect(x, y, &sect))
100         pr("Could not access that sector.\n");
101     if ((sect.sct_type != SCT_HARBR && sect.sct_type != SCT_WAREH) ||
102         !player->owner) {
103         pr("That sector cannot sell goods.\n");
104         return RET_FAIL;
105     }
106     if (sect.sct_effic < 60) {
107         pr("Sectors need to be >= 60%% efficient to sell goods.\n");
108         return RET_FAIL;
109     }
110     if (sect.sct_mobil <= 0) {
111         pr("Sectors need at least 1 mobility to sell goods.\n");
112         return RET_FAIL;
113     }
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 + sect.sct_item[I_MILIT]) * 10)
148             < sect.sct_item[I_CIVIL]) {
149             pr("Military control required to sell goods.\n");
150             return RET_FAIL;
151         }
152     }
153     if ((amt = sect.sct_item[ip->i_vtype]) == 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
160         com = amt + number_set;
161     if (com <= 0)
162         return RET_SYN;
163     totalcom += com;
164     amt -= com;
165     pr("Sold %d %s at %s (%d left)\n", com, ip->i_name,
166        xyas(sect.sct_x, sect.sct_y, player->cnum), amt);
167     sect.sct_item[ip->i_vtype] = amt;
168     cc = ip->i_mnem;
169     putsect(&sect);
170     if (totalcom > 0) {
171         for (ii = 0; getcomm(ii, &comm); ii++) {
172             if (comm.com_owner == 0)
173                 break;
174         }
175         if (getcomm(ii, &comm) == 0)
176             ef_extend(EF_COMM, 1);
177         (void)time(&now);
178         comm.com_type = ip->i_mnem;
179         comm.com_owner = player->cnum;
180         comm.com_price = price;
181         comm.com_maxbidder = player->cnum;
182         comm.com_markettime = now;
183         comm.com_amount = totalcom;
184         comm.com_x = 0;
185         comm.com_y = 0;
186         comm.sell_x = sect.sct_x;
187         comm.sell_y = sect.sct_y;
188         comm.com_uid = ii;
189         if (!putcomm(ii, &comm)) {
190             pr("Problems with the commodities file, call the Deity\n");
191             return RET_FAIL;
192         }
193     } else {
194         pr("No eligible %s for sale\n", ip->i_name);
195         return RET_FAIL;
196     }
197     return RET_OK;
198 }