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