]> git.pond.sub.org Git - empserver/blob - src/lib/commands/new.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / commands / new.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  *  new.c: Create a new capital for a player
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  */
33
34 #include <config.h>
35
36 #include "misc.h"
37 #include "player.h"
38 #include "nat.h"
39 #include "sect.h"
40 #include "path.h"
41 #include "file.h"
42 #include "xy.h"
43 #include "tel.h"
44 #include "land.h"
45 #include "nsc.h"
46 #include "optlist.h"
47 #include "commands.h"
48
49 #include <fcntl.h>
50
51 static int isok(int x, int y);
52 static void ok(signed char *map, int x, int y);
53
54 static struct range defrealm = { -8, -5, 10, 5, 0, 0 };
55
56 #define MAXAVAIL        300
57
58 int
59 new(void)
60 {
61     struct sctstr sect;
62     struct natstr *natp;
63     struct realmstr newrealm;
64     struct range absrealm;
65     natid num;
66     coord x, y;
67     int i;
68     char *p;
69     char buf[1024];
70     time_t current_time = time(NULL);
71
72     natp = getnatp(player->cnum);
73     if (natp->nat_xorg != 0 || natp->nat_yorg != 0) {
74         pr("Must be at 0,0 to add a new country\n");
75         return RET_FAIL;
76     }
77     if (!(natp = natargp(player->argp[1], "Country? ")))
78         return RET_SYN;
79     num = natp->nat_cnum;
80     if (natp->nat_stat != STAT_NEW) {
81         pr("Country #%d (%s) isn't a new country!\n", num, cname(num));
82         return RET_SYN;
83     }
84     if (player->argp[2] != 0) {
85         if ((p = getstarg(player->argp[2], "sanctuary pair : ", buf)) == 0)
86             return RET_SYN;
87         if (!sarg_xy(p, &x, &y) || !getsect(x, y, &sect))
88             return RET_SYN;
89         if (sect.sct_type != SCT_RURAL) {
90             pr("%s is a %s; try again...\n",
91                xyas(x, y, player->cnum), dchr[sect.sct_type].d_name);
92             return RET_SYN;
93         }
94         getsect(x + 2, y, &sect);
95         if (sect.sct_type != SCT_RURAL) {
96             pr("%s is a %s; try again...\n",
97                xyas(x + 2, y, player->cnum), dchr[sect.sct_type].d_name);
98             return RET_SYN;
99         }
100     } else {
101         for (i = 0; i < 300 && !player->aborted; i++) {
102             /* Both x and y should be either odd or even */
103             x = (random() % WORLD_X) - (WORLD_X / 2);
104             y = (((random() % WORLD_Y) - (WORLD_Y / 2)) & ~1) | (x & 1);
105             /*
106              * If either of the two potential
107              * sanctuary sectors are already
108              * owned by someone else, pick
109              * another place on the map.
110              */
111             getsect(x, y, &sect);
112             if (sect.sct_type == SCT_WATER || sect.sct_own != 0)
113                 continue;
114             getsect(x + 2, y, &sect);
115             if (sect.sct_type == SCT_WATER || sect.sct_own != 0)
116                 continue;
117             if (isok(x, y))
118                 break;
119         }
120         if (i == 300) {
121             pr("couldn't find an empty slot!\n");
122             return RET_FAIL;
123         }
124     }
125
126     if (player->aborted)
127         return RET_FAIL;
128     pr("added country %d at %s\n", num, xyas(x, y, player->cnum));
129     getsect(x, y, &sect);
130     sect.sct_own = num;
131     sect.sct_type = SCT_SANCT;
132     sect.sct_newtype = SCT_SANCT;
133     sect.sct_effic = 100;
134     sect.sct_road = 0;
135     sect.sct_rail = 0;
136     sect.sct_defense = 0;
137     if (!opt_DEFENSE_INFRA)
138         sect.sct_defense = sect.sct_effic;
139     sect.sct_mobil = startmob;
140     sect.sct_work = 100;
141     sect.sct_oldown = num;
142     if (at_least_one_100) {
143         sect.sct_oil = 100;
144         sect.sct_fertil = 100;
145         sect.sct_uran = 100;
146         sect.sct_min = 100;
147         sect.sct_gmin = 100;
148     }
149     sect.sct_item[I_CIVIL] = opt_RES_POP ? 550 : 999;
150     sect.sct_item[I_MILIT] = 55;
151     sect.sct_item[I_FOOD] = 1000;
152     sect.sct_item[I_UW] = 75;
153     putsect(&sect);
154     getsect(x + 2, y, &sect);
155     sect.sct_own = num;
156     sect.sct_type = SCT_SANCT;
157     sect.sct_newtype = SCT_SANCT;
158     sect.sct_effic = 100;
159     sect.sct_road = 0;
160     sect.sct_rail = 0;
161     sect.sct_defense = 0;
162     if (!opt_DEFENSE_INFRA)
163         sect.sct_defense = sect.sct_effic;
164     sect.sct_work = 100;
165     sect.sct_oldown = num;
166     sect.sct_mobil = startmob;
167     if (at_least_one_100) {
168         sect.sct_oil = 100;
169         sect.sct_fertil = 100;
170         sect.sct_uran = 100;
171         sect.sct_min = 100;
172         sect.sct_gmin = 100;
173     }
174     sect.sct_item[I_CIVIL] = opt_RES_POP ? 550 : 999;
175     sect.sct_item[I_MILIT] = 55;
176     sect.sct_item[I_FOOD] = 100;
177     sect.sct_item[I_UW] = 75;
178     putsect(&sect);
179     natp->nat_btu = max_btus;
180     natp->nat_stat = STAT_SANCT;
181     natp->nat_xcap = x;
182     natp->nat_ycap = y;
183     natp->nat_xorg = x;
184     natp->nat_yorg = y;
185     xyabsrange(natp, &defrealm, &absrealm);
186     for (i = 0; i < MAXNOR; i++) {
187         getrealm(i, num, &newrealm);
188         newrealm.r_xl = absrealm.lx;
189         newrealm.r_xh = absrealm.hx;
190         newrealm.r_yl = absrealm.ly;
191         newrealm.r_yh = absrealm.hy;
192         newrealm.r_timestamp = current_time;
193         putrealm(&newrealm);
194     }
195     if (players_at_00) {
196         natp->nat_xorg = 0;
197         natp->nat_yorg = 0;
198     }
199     natp->nat_money = start_cash;
200     natp->nat_level[NAT_HLEV] = start_happiness;
201     natp->nat_level[NAT_RLEV] = start_research;
202     natp->nat_level[NAT_TLEV] = start_technology;
203     natp->nat_level[NAT_ELEV] = start_education;
204     natp->nat_tgms = 0;
205     (void)close(open(mailbox(buf, num), O_RDWR | O_TRUNC | O_CREAT, 0660));
206     putnat(natp);
207     return RET_OK;
208 }
209
210 static int nmin, ngold, noil, nur;
211 static int nfree, navail, nowned;
212
213 static int
214 isok(int x, int y)
215 {
216     signed char *map;
217     char *p;
218     char buf[1024];
219
220     nmin = ngold = noil = nur = 0;
221     navail = nfree = nowned = 0;
222     if ((map = malloc((WORLD_X * WORLD_Y) / 2)) == 0) {
223         logerror("malloc failed in isok\n");
224         pr("Memory error.  Tell the deity.\n");
225         return 0;
226     }
227     memset(map, 0, (WORLD_X * WORLD_Y) / 2);
228     ok(map, x, y);
229     free(map);
230     if (nfree < 5)
231         return 0;
232     pr("Cap at %s; owned sectors: %d, free sectors: %d, avail: %d\n",
233        xyas(x, y, player->cnum), nowned, nfree, navail);
234     pr("min: %d, oil: %d, gold: %d, uranium: %d\n",
235        nmin, noil, ngold, nur);
236     p = getstring("This setup ok? ", buf);
237     if (p == 0 || *p != 'y')
238         return 0;
239     return 1;
240 }
241
242 static void
243 ok(signed char *map, int x, int y)
244 {
245     struct sctstr sect;
246     int dir;
247     int id;
248
249     if (navail > MAXAVAIL)
250         return;
251     id = sctoff(x, y);
252     if (map[id])
253         return;
254     if (!ef_read(EF_SECTOR, id, &sect))
255         return;
256     if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_BSPAN)
257         return;
258     navail++;
259     if (navail >= MAXAVAIL) {
260         pr("At least %d...\n", MAXAVAIL);
261         return;
262     }
263     if (sect.sct_type != SCT_MOUNT && sect.sct_type != SCT_PLAINS) {
264         if (sect.sct_own == 0)
265             nfree++;
266         else
267             nowned++;
268         if (sect.sct_min > 9)
269             nmin++;
270         if (sect.sct_gmin > 9)
271             ngold++;
272         if (sect.sct_uran > 9)
273             nur++;
274         if (sect.sct_oil > 9)
275             noil++;
276     }
277     map[id] = 1;
278     for (dir = DIR_FIRST; dir <= DIR_LAST; dir++)
279         ok(map, diroff[dir][0] + x, diroff[dir][1] + y);
280 }