]> git.pond.sub.org Git - empserver/blob - src/lib/commands/set.c
Update copyright notice.
[empserver] / src / lib / commands / set.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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  *  set.c: Place units/ships/planes/nukes up for sale.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Pat Loney, 1992
33  *     Steve McClure, 1996
34  */
35
36 #include "misc.h"
37 #include "sect.h"
38 #include "ship.h"
39 #include "land.h"
40 #include "plane.h"
41 #include "xy.h"
42 #include "nsc.h"
43 #include "nat.h"
44 #include "trade.h"
45 #include "file.h"
46 #include "player.h"
47 #include "commands.h"
48 #include "optlist.h"
49
50 /*
51  * format: set <type> <SHIP/NUKE> <PRICE>
52  */
53 int
54 set(void)
55 {
56     char *p;
57     int type;
58     int price;
59     char prompt[80];
60     struct trdstr trade;
61     struct nstr_item ni;
62     struct nstr_item ni_trade;
63     union trdgenstr item;
64     union trdgenstr check;
65     struct sctstr sect;
66     int freeslot;
67     int foundslot;
68     int id;
69     time_t now;
70     s_char buf[1024];
71
72     if (!opt_MARKET) {
73         pr("The market is disabled.\n");
74         return RET_FAIL;
75     }
76     check_market();
77     check_trade();
78
79     if ((p = getstarg(player->argp[1], "Item type? ", buf)) == 0)
80         return RET_SYN;
81     if ((type = ef_byname(p)) < 0) {
82         pr("%s: not an item type\n", p);
83         return RET_SYN;
84     }
85     if (type == EF_SECTOR)
86         type = EF_SHIP;
87     if (type == EF_NEWS)
88         type = EF_NUKE;
89     if (type == EF_LOAN)
90         type = EF_LAND;
91     if (!snxtitem(&ni, type, player->argp[2]))
92         return RET_SYN;
93     while (nxtitem(&ni, (char *)&item)) {
94         if (!player->owner && !player->god)
95             continue;
96         getsect(item.gen.trg_x, item.gen.trg_y, &sect);
97         if (!military_control(&sect)) {
98             pr("Military control required to sell goods.\n");
99             return RET_FAIL;
100         }
101         trade.trd_type = type;
102         sprintf(prompt, "%s #%d; Price? ",
103                 trade_nameof(&trade, &item), ni.cur);
104         memcpy(&check, &item, sizeof(union trdgenstr));
105         if ((p = getstarg(player->argp[3], prompt, buf)) == 0)
106             break;
107         if (memcmp(&check, &item, sizeof(union trdgenstr))) {
108             pr("That item has changed!\n");
109             return RET_FAIL;
110         }
111         if ((price = atoi(p)) < 0)
112             continue;
113         foundslot = -1;
114         freeslot = -1;
115         snxtitem_all(&ni_trade, EF_TRADE);
116         while (nxtitem(&ni_trade, (char *)&trade)) {
117             if (trade.trd_owner == 0)
118                 freeslot = ni_trade.cur;
119             if (trade.trd_unitid == ni.cur && trade.trd_type == type) {
120                 foundslot = ni_trade.cur;
121                 break;
122             }
123         }
124         if (price <= 0) {
125             if (foundslot >= 0) {
126                 pr("%s #%d (lot #%d) removed from trading\n",
127                    trade_nameof(&trade, &item), ni.cur, foundslot);
128                 memset(&trade, 0, sizeof(trade));
129                 puttrade(ni_trade.cur, &trade);
130             }
131         } else {
132             if (foundslot >= 0)
133                 id = foundslot;
134             else if (freeslot >= 0)
135                 id = freeslot;
136             else {
137                 ef_extend(EF_TRADE, 1);
138                 id = ni_trade.cur;
139             }
140             trade.trd_x = item.gen.trg_x;
141             trade.trd_y = item.gen.trg_x;
142             trade.trd_type = type;
143             trade.trd_owner = player->cnum;
144             trade.trd_uid = id;
145             trade.trd_unitid = ni.cur;
146             trade.trd_price = price;
147             (void)time(&now);
148             trade.trd_markettime = now;
149             trade.trd_maxbidder = player->cnum;
150             puttrade(id, &trade);
151             pr("%s #%d (lot #%d) price %s to $%d\n",
152                trade_nameof(&trade, &item), ni.cur,
153                id, foundslot >= 0 ? "reset" : "set", price);
154         }
155     }
156     return RET_OK;
157 }