]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sell.c
Update copyright notice.
[empserver] / src / lib / commands / sell.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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     time_t now;
77     int ii = 0;
78     coord x, y;
79     s_char buf[1024];
80
81     if (!opt_MARKET) {
82         pr("The market is disabled.\n");
83         return RET_FAIL;
84     }
85     check_market();
86     check_trade();
87     if (!(ip = whatitem(player->argp[1], "Commodity you want to sell: ")))
88         return RET_SYN;
89     if (ip->i_sell == 0) {
90         pr("You can't sell %s\n", ip->i_name);
91         return RET_FAIL;
92     }
93     if (!(p = getstarg(player->argp[2], "Sector to sell from: ", buf)))
94         return RET_SYN;
95     if (!sarg_xy(p, &x, &y))
96         return RET_SYN;
97     if (!getsect(x, y, &sect))
98         pr("Could not access that sector.\n");
99     if ((sect.sct_type != SCT_HARBR && sect.sct_type != SCT_WAREH) ||
100         !player->owner) {
101         pr("That sector cannot sell goods.\n");
102         return RET_FAIL;
103     }
104     if (sect.sct_effic < 60) {
105         pr("Sectors need to be >= 60%% efficient to sell goods.\n");
106         return RET_FAIL;
107     }
108     if (sect.sct_mobil <= 0) {
109         pr("Sectors need at least 1 mobility to sell goods.\n");
110         return RET_FAIL;
111     }
112     number_sub = 0;
113     if ((p = getstarg(player->argp[3], "Amount:  ", buf)) == 0 || *p == 0)
114         return RET_SYN;
115     if (!check_sect_ok(&sect))
116         return RET_FAIL;
117     number_set = atoi(p);
118     if ((p = getstarg(player->argp[4], "Price per unit: ", buf)) == 0 ||
119         *p == 0)
120         return RET_SYN;
121     if (!check_sect_ok(&sect))
122         return RET_FAIL;
123     price = atof(p);
124     if (price <= 0.0) {
125         pr("No sale.\n");
126         return RET_FAIL;
127     }
128     if (price > 1000.0)         /* Inf can cause overflow */
129         price = 1000.0;         /* bailey@math-cs.kent.edu */
130     totalcom = 0;
131     if (!military_control(&sect)) {
132         pr("Military control required to sell goods.\n");
133         return RET_FAIL;
134     }
135     if ((amt = sect.sct_item[ip->i_vtype]) == 0) {
136         pr("You don't have any %s to sell there.\n", ip->i_name);
137         return RET_FAIL;
138     }
139     if (number_set >= 0)
140         com = min(number_set, amt);
141     else
142         com = amt + number_set;
143     if (com <= 0)
144         return RET_SYN;
145     totalcom += com;
146     amt -= com;
147     pr("Sold %d %s at %s (%d left)\n", com, ip->i_name,
148        xyas(sect.sct_x, sect.sct_y, player->cnum), amt);
149     sect.sct_item[ip->i_vtype] = amt;
150     putsect(&sect);
151     if (totalcom > 0) {
152         for (ii = 0; getcomm(ii, &comm); ii++) {
153             if (comm.com_owner == 0)
154                 break;
155         }
156         if (getcomm(ii, &comm) == 0)
157             ef_extend(EF_COMM, 1);
158         (void)time(&now);
159         comm.com_type = ip->i_vtype;
160         comm.com_owner = player->cnum;
161         comm.com_price = price;
162         comm.com_maxbidder = player->cnum;
163         comm.com_markettime = now;
164         comm.com_amount = totalcom;
165         comm.com_x = 0;
166         comm.com_y = 0;
167         comm.sell_x = sect.sct_x;
168         comm.sell_y = sect.sct_y;
169         comm.com_uid = ii;
170         if (!putcomm(ii, &comm)) {
171             pr("Problems with the commodities file, call the Deity\n");
172             return RET_FAIL;
173         }
174     } else {
175         pr("No eligible %s for sale\n", ip->i_name);
176         return RET_FAIL;
177     }
178     return RET_OK;
179 }