]> git.pond.sub.org Git - empserver/blob - src/lib/commands/set.c
3c5bd539ccc0ae8b9059055557fba1e8f1730004
[empserver] / src / lib / commands / set.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  set.c: Place units/ships/planes/nukes up for sale.
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Pat Loney, 1992
32  *     Steve McClure, 1996
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "empobj.h"
39 #include "land.h"
40 #include "optlist.h"
41 #include "plane.h"
42 #include "ship.h"
43 #include "trade.h"
44
45 /*
46  * format: set <type> <SHIP/NUKE> <PRICE>
47  */
48 int
49 set(void)
50 {
51     static int ef_saleable[] = { EF_SHIP, EF_PLANE, EF_LAND, EF_NUKE, EF_BAD };
52     char *p;
53     int type;
54     int price;
55     char prompt[80];
56     struct trdstr trade;
57     struct nstr_item ni;
58     struct nstr_item ni_trade;
59     union empobj_storage item;
60     struct sctstr sect;
61     int freeslot;
62     int foundslot;
63     int id;
64     time_t now;
65     char buf[1024];
66
67     if (!opt_MARKET) {
68         pr("The market is disabled.\n");
69         return RET_FAIL;
70     }
71     check_market();
72     check_trade();
73
74     p = getstarg(player->argp[1], "Ship, plane, land unit or nuke? ", buf);
75     if (!p)
76         return RET_SYN;
77     if ((type = ef_byname_from(p, ef_saleable)) < 0) {
78         pr("You can sell only ships, planes, land units or nukes\n");
79         return RET_SYN;
80     }
81     if (!snxtitem(&ni, type, player->argp[2], NULL))
82         return RET_SYN;
83     while (nxtitem(&ni, &item)) {
84         if (!player->owner && !player->god)
85             continue;
86         getsect(item.gen.x, item.gen.y, &sect);
87         if (!military_control(&sect)) {
88             pr("Military control required to sell goods.\n");
89             return RET_FAIL;
90         }
91         trade.trd_type = type;
92         sprintf(prompt, "%s #%d; price? ",
93                 trade_nameof(&trade, &item.gen), ni.cur);
94         if (!(p = getstarg(player->argp[3], prompt, buf)))
95             return RET_FAIL;
96         if (!check_obj_ok(&item.gen))
97             return RET_FAIL;
98         if ((price = atoi(p)) < 0)
99             continue;
100         foundslot = -1;
101         freeslot = -1;
102         snxtitem_all(&ni_trade, EF_TRADE);
103         while (nxtitem(&ni_trade, &trade)) {
104             if (trade.trd_owner == 0)
105                 freeslot = ni_trade.cur;
106             if (trade.trd_unitid == ni.cur && trade.trd_type == type) {
107                 foundslot = ni_trade.cur;
108                 break;
109             }
110         }
111         if (price <= 0) {
112             if (foundslot >= 0) {
113                 pr("%s #%d (lot #%d) removed from trading\n",
114                    trade_nameof(&trade, &item.gen), ni.cur, foundslot);
115                 trade.trd_owner = 0;
116                 puttrade(ni_trade.cur, &trade);
117             }
118         } else {
119             if (trade_has_unsalable_cargo(&item.gen, 1))
120                 return RET_FAIL;
121             if (foundslot >= 0)
122                 id = foundslot;
123             else if (freeslot >= 0)
124                 id = freeslot;
125             else
126                 id = ni_trade.cur;
127             ef_blank(EF_TRADE, id, &trade);
128             trade.trd_x = 1;
129             trade.trd_y = 0;
130             trade.trd_type = type;
131             trade.trd_owner = player->cnum;
132             trade.trd_unitid = ni.cur;
133             trade.trd_price = price;
134             (void)time(&now);
135             trade.trd_markettime = now;
136             trade.trd_maxbidder = player->cnum;
137             puttrade(id, &trade);
138             pr("%s #%d (lot #%d) price %s to $%d\n",
139                trade_nameof(&trade, &item.gen), ni.cur,
140                id, foundslot >= 0 ? "reset" : "set", price);
141         }
142     }
143     return RET_OK;
144 }