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