]> git.pond.sub.org Git - empserver/blob - src/lib/commands/desi.c
386e869b45cda0f586b43f6cb2a271d5cf180a35
[empserver] / src / lib / commands / desi.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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  *  desi.c: Redesignate sectors
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 1998-2000
31  *     Markus Armbruster, 2004-2009
32  */
33
34 #include <config.h>
35
36 #include "commands.h"
37 #include "map.h"
38 #include "optlist.h"
39 #include "path.h"
40
41 int
42 desi(void)
43 {
44     int n;
45     char *p;
46     int des;
47     struct nstr_sect nstr;
48     struct sctstr sect;
49     struct natstr *natp;
50     char prompt[128];
51     char buf[1024];
52     int changed = 0;
53     int rc = RET_OK;
54
55     if (!snxtsct(&nstr, player->argp[1]))
56         return RET_SYN;
57     natp = getnatp(player->cnum);
58     while (nxtsct(&nstr, &sect)) {
59         if (!player->owner)
60             continue;
61         if (!player->god && dchr[sect.sct_type].d_cost < 0)
62             continue;
63         sprintf(prompt, "%s %d%% %s  desig? ",
64                 xyas(sect.sct_x, sect.sct_y, player->cnum),
65                 sect.sct_effic, dchr[sect.sct_type].d_name);
66         if (!(p = getstarg(player->argp[2], prompt, buf))) {
67             rc = RET_FAIL;
68             break;
69         }
70
71         if (!check_sect_ok(&sect))
72             continue;
73
74         des = sct_typematch(p);
75         if (des < 0) {
76             pr("No such designation\n"
77                "See \"info Sector-types\" for possible designations\n");
78             rc = RET_FAIL;
79             break;
80         }
81         if (!player->god) {
82             if (des == SCT_WASTE) {
83                 pr("Only a nuclear device (or %s) can make a %s!\n",
84                    cname(0), dchr[des].d_name);
85                 rc = RET_FAIL;
86                 break;
87             }
88             if (dchr[des].d_cost < 0) {
89                 pr("Only %s can designate a %s!\n",
90                    cname(0), dchr[des].d_name);
91                 rc = RET_FAIL;
92                 break;
93             }
94             if (dchr[des].d_terrain != dchr[sect.sct_type].d_terrain) {
95                 pr("You can't change a %s into a %s\n",
96                    dchr[sect.sct_type].d_name, dchr[des].d_name);
97                 continue;
98             }
99         }
100         if (sect.sct_type == des && sect.sct_newtype == des)
101             continue;
102         if ((des == SCT_HARBR || des == SCT_BHEAD) && !sect.sct_coastal) {
103             pr("%s does not border on water.\n",
104                xyas(nstr.x, nstr.y, player->cnum));
105             if (player->god)
106                 pr("But if it's what you want ...\n");
107             else
108                 continue;
109         }
110         if (sect.sct_type == SCT_SANCT && !player->god)
111             continue;
112         n = sect.sct_type;
113         if ((sect.sct_newtype != des) && (sect.sct_type != des)
114             && dchr[des].d_cost > 0) {
115             if (natp->nat_money < player->dolcost + dchr[des].d_cost) {
116                 pr("You can't afford a %s!\n", dchr[des].d_name);
117                 rc = RET_FAIL;
118                 break;
119             }
120             player->dolcost += dchr[des].d_cost;
121         }
122         if (sect.sct_type != des && (sect.sct_effic < 5 || player->god)) {
123             if (player->god)
124                 set_coastal(&sect, sect.sct_type, des);
125             sect.sct_type = des;
126             sect.sct_effic = 0;
127             changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
128                                dchr[des].d_mnem, 0);
129         }
130         sect.sct_newtype = des;
131         putsect(&sect);
132         if (!player->god
133             && sect.sct_x == natp->nat_xcap && sect.sct_y == natp->nat_ycap
134             && des != SCT_CAPIT && des != SCT_SANCT && des != SCT_MOUNT)
135             pr("You have redesignated your capital!\n");
136         if (opt_EASY_BRIDGES == 0) {    /* may cause a bridge fall */
137             if (n != SCT_BHEAD)
138                 continue;
139             bridgefall(&sect);
140         }
141     }
142     if (changed)
143         writemap(player->cnum);
144     return rc;
145 }