]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sell.c
Indented with src/scripts/indent-emp.
[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_set = 0;
115     number_sub = 0;
116     if ((p = getstarg(player->argp[3], "Amount:  ", buf)) == 0 || *p == 0)
117         return RET_SYN;
118     if (!check_sect_ok(&sect))
119         return RET_FAIL;
120     number_set = atoi(p);
121     if ((p = getstarg(player->argp[4], "Price per unit: ", buf)) == 0 ||
122         *p == 0)
123         return RET_SYN;
124     if (!check_sect_ok(&sect))
125         return RET_FAIL;
126     price = atof(p);
127     if (price <= 0.0) {
128         pr("No sale.\n");
129         return RET_FAIL;
130     }
131     if (price > 1000.0)         /* Inf can cause overflow */
132         price = 1000.0;         /* bailey@math-cs.kent.edu */
133     totalcom = 0;
134     /*
135      * military control necessary to sell
136      * goodies in occupied territory.
137      */
138     if (sect.sct_oldown != player->cnum) {
139         int tot_mil = 0;
140         struct nstr_item ni;
141         struct lndstr land;
142
143         snxtitem_xy(&ni, EF_LAND, sect.sct_x, sect.sct_y);
144         while (nxtitem(&ni, (s_char *)&land)) {
145             if (land.lnd_own == player->cnum)
146                 tot_mil += total_mil(&land);
147         }
148         if (((tot_mil + (getvar(V_MILIT, (char *)&sect, EF_SECTOR))) * 10)
149             < getvar(V_CIVIL, (char *)&sect, EF_SECTOR)) {
150             pr("Military control required to sell goods.\n");
151             return RET_FAIL;
152         }
153     }
154     if (((amt = getvar(ip->i_vtype, (char *)&sect, EF_SECTOR))) == 0) {
155         pr("You don't have any %s to sell there.\n", ip->i_name);
156         return RET_FAIL;
157     }
158     if (number_set > 0)
159         com = min(number_set, amt);
160     else if (number_set < 0)
161         com = amt + number_set;
162     else
163         com = 0;
164     if (com <= 0)
165         return RET_SYN;
166     totalcom += com;
167     amt -= com;
168     pr("Sold %d %s at %s (%d left)\n", com, ip->i_name,
169        xyas(sect.sct_x, sect.sct_y, player->cnum), amt);
170     putvar(ip->i_vtype, amt, (char *)&sect, EF_SECTOR);
171     cc = ip->i_mnem;
172     putsect(&sect);
173     if (totalcom > 0) {
174         for (ii = 0; getcomm(ii, &comm); ii++) {
175             if (comm.com_owner == 0)
176                 break;
177         }
178         if (getcomm(ii, &comm) == 0)
179             ef_extend(EF_COMM, 1);
180         (void)time(&now);
181         comm.com_type = ip->i_mnem;
182         comm.com_owner = player->cnum;
183         comm.com_price = price;
184         comm.com_maxbidder = player->cnum;
185         comm.com_maxprice = price;
186         comm.com_markettime = now;
187         comm.com_amount = totalcom;
188         comm.com_x = 0;
189         comm.com_y = 0;
190         comm.sell_x = sect.sct_x;
191         comm.sell_y = sect.sct_y;
192         comm.com_uid = ii;
193         if (!putcomm(ii, &comm)) {
194             pr("Problems with the commodities file, call the Deity\n");
195             return RET_FAIL;
196         }
197     } else {
198         pr("No eligible %s for sale\n", ip->i_name);
199         return RET_FAIL;
200     }
201     return RET_OK;
202 }