]> git.pond.sub.org Git - empserver/blob - src/lib/commands/desi.c
Update copyright notice.
[empserver] / src / lib / commands / desi.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  *  desi.c: Redesignate sectors
29  * 
30  *  Known contributors to this file:
31  *      Steve McClure, 1998-2000
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 static long do_desi(struct natstr *natp, char *sects, char *deschar,
42                     long cash, int for_real);
43
44 int
45 desi(void)
46 {
47     long cash;
48     long cost;
49     struct natstr *natp;
50
51     natp = getnatp(player->cnum);
52     cash = natp->nat_money;
53     if (player->argp[2]) {
54         cost = do_desi(natp, player->argp[1], player->argp[2], cash, 0);
55         if (cost < 0)
56             return (int)-cost;
57         if (chkmoney(cost, cash, player->argp[3]))
58             return RET_SYN;
59     }
60     return (int)do_desi(natp, player->argp[1], player->argp[2], cash, 1);
61 }
62
63 static long
64 do_desi(struct natstr *natp, char *sects, char *deschar, long cash,
65         int for_real)
66 {
67     int n;
68     char *p;
69     int breaksanct;
70     int cap_x;
71     int cap_y;
72     int des;
73     struct nstr_sect nstr;
74     struct sctstr sect;
75     struct sctstr check;
76     char prompt[128];
77     char buf[1024];
78     long cost = 0;
79     int changed = 0;
80     int warned = 0;
81
82     breaksanct = 0;
83     if (!snxtsct(&nstr, sects)) {
84         if (for_real)
85             return (long)RET_SYN;
86         else
87             return (long)-RET_SYN;
88     }
89     cap_x = natp->nat_xcap;
90     cap_y = natp->nat_ycap;
91     while (!player->aborted && nxtsct(&nstr, &sect)) {
92         if (!player->owner)
93             continue;
94         if (!player->god && dchr[sect.sct_type].d_cost < 0)
95             continue;
96         sprintf(prompt, "%s %d%% %s  desig? ",
97                 xyas(sect.sct_x, sect.sct_y, player->cnum),
98                 sect.sct_effic, dchr[sect.sct_type].d_name);
99         if ((p = getstarg(deschar, prompt, buf)) == 0)
100             continue;
101
102         if (!check_sect_ok(&sect))
103             continue;
104
105         des = sct_typematch(p);
106         if (des < 0 || (((des == SCT_BSPAN) || (des == SCT_BTOWER)) &&
107                         !player->god)) {
108             pr("No such designation\n"
109                "See \"info Sector-types\" for possible designations\n");
110             if (for_real)
111                 return (long)RET_FAIL;
112             else
113                 return (long)-RET_FAIL;
114         }
115         if (!player->god) {
116             if (des == SCT_WASTE) {
117                 if (for_real)
118                     pr("Only a nuclear device (or %s) can make a %s!\n",
119                        cname(0), dchr[des].d_name);
120                 continue;
121             }
122             if (dchr[des].d_cost < 0) {
123                 if (for_real)
124                     pr("Only %s can make a %s!\n",
125                        cname(0), dchr[des].d_name);
126                 continue;
127             }
128         }
129         if (sect.sct_type == des && sect.sct_newtype == des)
130             continue;
131         if (sect.sct_type == SCT_SANCT)
132             breaksanct++;
133         if ((des == SCT_HARBR) || (des == SCT_BHEAD)) {
134             for (n = 1; n <= 6; n++) {
135                 getsect(nstr.x + diroff[n][0],
136                         nstr.y + diroff[n][1], &check);
137                 if (check.sct_type == SCT_WATER)
138                     break;
139                 if (check.sct_type == SCT_BSPAN)
140                     break;
141                 if (check.sct_type == SCT_BTOWER)
142                     break;
143             }
144             if (n > 6) {
145                 if (for_real)
146                     pr("%s does not border on water.\n",
147                        xyas(nstr.x, nstr.y, player->cnum));
148                 if (player->god) {
149                     if (for_real)
150                         pr("But if it's what you want ...\n");
151                 } else
152                     continue;
153             }
154         }
155         if (sect.sct_type == SCT_SANCT && !player->god)
156             continue;
157         n = sect.sct_type;
158         if ((sect.sct_newtype != des) && (sect.sct_type != des)
159             && dchr[des].d_cost > 0) {
160             if (for_real) {
161                 if (check_cost(!deschar, dchr[des].d_cost, cash, &warned,
162                                player->argp[3]))
163                     break;
164             } else {
165                 cost += dchr[des].d_cost;
166                 continue;
167             }
168         }
169         if (sect.sct_type != des && (sect.sct_effic < 5 || player->god)) {
170             if (player->god)
171                 set_coastal(&sect, sect.sct_type, des);
172             sect.sct_type = des;
173             sect.sct_effic = 0;
174             changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
175                                dchr[des].d_mnem, 0);
176         }
177         sect.sct_newtype = des;
178         putsect(&sect);
179         if (sect.sct_x == cap_x && sect.sct_y == cap_y
180             && des != SCT_CAPIT && des != SCT_SANCT && des != SCT_MOUNT
181             && for_real)
182             pr("You have redesignated your capital!\n");
183         if (opt_EASY_BRIDGES == 0) {    /* may cause a bridge fall */
184             if (n != SCT_BHEAD)
185                 continue;
186             bridgefall(&sect, 0);
187         }
188     }
189     if (for_real) {
190         if (changed)
191             writemap(player->cnum);
192         if (breaksanct)
193             bsanct();
194         return (long)RET_OK;
195     } else {
196         return cost;
197     }
198 }