-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcexecute.c
327 lines (313 loc) · 8.42 KB
/
cexecute.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*conquer : Copyright (c) 1988 by Ed Barlow.
* I spent a long time writing this code & I hope that you respect this.
* I give permission to alter the code, but not to copy or redistribute
* it without my explicit permission. If you alter the code,
* please document changes and send me a copy, so all can have it.
* This code, to the best of my knowledge works well, but it is my first
* 'C' program and should be treated as such. I disclaim any
* responsibility for the codes actions (use at your own risk). I guess
* I am saying "Happy gaming", and am trying not to get sued in the process.
* Ed
*/
/*EXECUTE THE PROGRAM*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include "header.h"
#include "data.h"
extern long startgold;
extern short country;
extern FILE *fexe, *fnews;
extern char fison[];
#ifdef CONQUER
extern int roads_this_turn;
extern int terror_adj;
#endif
int
execute(isupdate)
int isupdate; /* 0 if not update, 1 if update */
{
FILE *fp, *fopen();
int cmd,savectry;
char comment[LINELTH];
char temp[LINELTH];
long longvar,long2var;
int armynum;
short int x,y;
int execed=0;
int done=FALSE;
char line[BIGLTH];
/* initialize startgold */
if( isupdate==0) startgold = curntn->tgold;
/* initialize i_people */
for(x=0;x<MAPX;x++)
for(y=0;y<MAPY;y++)
if(( sct[x][y].owner == country)&&
((sct[x][y].designation == DTOWN)
||( sct[x][y].designation == DCAPITOL)
||( sct[x][y].designation == DCITY)))
/* note: i_people is a short, so we must scale to allow for people >= 32K */
sct[x][y].i_people = sct[x][y].people/256;
else
sct[x][y].i_people = -1;
/*open exefile file*/
sprintf(line,"%s%d",exefile,country);
if ((fp=fopen(line,"r"))==NULL) {
/*THIS MEANS THAT THE NATION HAS NOT MOVED YET*/
return(0);
}
savectry=country;
/*read in file*/
if(fgets(line,80,fp)==NULL) done=TRUE;
while(done==FALSE) {
/*read and parse a new line*/
/*CODE IF YOU USE LONG VAR IS L_*/
if( line[0] == 'L' && line[1] == '_' ) {
sscanf(line,"%s %d %hd %ld %ld %hd %s",
temp,&cmd,&country,&longvar,&long2var,&y,comment);
} else {
sscanf(line,"%s %d %hd %d %hd %hd %s",
temp,&cmd,&country,&armynum,&x,&y,comment);
}
curntn = &ntn[country];
execed=1;
switch(cmd){
case XASTAT: /*Aadjstat*/
if(x>0) P_ASTAT=x;
break;
case XAMEN: /*Aadjmen*/
armynum= (int) longvar;
P_ASOLD= (int) long2var;
P_ATYPE= y;
break;
case XBRIBE: /* nation has been bribed */
if(!isupdate) break; /* only work on update */
ntn[y].tgold += longvar;
#ifdef DEBUG
fprintf(stderr,"DEBUG: BRIBE BY %s of %s\n",ntn[country].name,ntn[y].name);
#endif /* DEBUG */
/* x represents chance of successful bribe */
if(npctype(curntn->active)==npctype(ntn[y].active))
x = 50;
else if(isneutral(ntn[y].active)) x=30;
else if(npctype(ntn[y].active)==ISOLATIONIST) x=15;
else x = 20;
if(curntn->race==ntn[y].race) x+= 20;
if( rand()%100 < x){
#ifdef DEBUG
fprintf(stderr,"DEBUG: BRIBE IS SUCCESS\n");
#endif /* DEBUG */
ntn[y].dstatus[country]--;
}
break;
case XALOC: /*Aadjloc*/
P_AXLOC=x;
P_AYLOC=y;
break;
case MSETA: /*Aadjmerc*/
#ifdef CONQUER
mercgot+=armynum;
#endif
#ifdef ADMIN
MERCMEN-=armynum;
#endif
break;
case MSETB: /*Aadjdisb*/
/* only allow raising of merc bonus */
if (x>MERCATT)
MERCATT=(MERCMEN*MERCATT+armynum*x)/(MERCMEN+armynum);
if (y>MERCDEF)
MERCDEF=(MERCMEN*MERCDEF+armynum*y)/(MERCMEN+armynum);
MERCMEN+=armynum;
break;
case XNLOC: /*nadjloc*/
curntn->nvy[armynum].xloc=x;
curntn->nvy[armynum].yloc=y;
break;
case XNACREW:
curntn->nvy[armynum].crew=x;
curntn->nvy[armynum].armynum=y;
break;
case XNAMER: /*nadjmer*/
curntn->nvy[armynum].merchant=x;
break;
case XNAWAR: /*nadjwar*/
curntn->nvy[armynum].warships=x;
break;
case XNAGAL: /*nadjgal*/
curntn->nvy[armynum].galleys=x;
break;
case XNAHOLD: /*nadjhld*/
curntn->nvy[armynum].people=(unsigned char)y;
curntn->nvy[armynum].armynum=(unsigned char)x;
break;
case XECNAME: /*Nadjname*/
strcpy(curntn->name,comment);
break;
case XECPAS: /*Nadjpas*/
strncpy(curntn->passwd,comment,PASSLTH);
break;
case NPOP: /* set various nation attributes */
#ifdef CONQUER
terror_adj++;
#endif /* CONQUER */
curntn->popularity = armynum;
curntn->terror = x;
curntn->reputation = y;
break;
case NTAX: /* set nations tax rate */
#ifdef ADMIN
if (ispc(curntn->active) && !ispc(x)) {
fprintf(fnews, "1.\tNation %s is now being run by the computer.\n", curntn->name);
}
if (ispc(x) && !ispc(curntn->active)) {
fprintf(fnews, "1.\tNation %s is no longer being run by the computer.\n", curntn->name);
}
#endif /*ADMIN*/
curntn->tax_rate = armynum;
curntn->active = x;
curntn->charity = y;
break;
case EDSPL: /*Edecspl*/
curntn->spellpts-=armynum;
break;
case XSADES: /*Sadjdes*/
if((sct[x][y].owner!=country)&&(country!=0)) {
fprintf(stderr,"ERROR: <%s> redesignate sector %d,%d that is not owned\n",curntn->name,x,y);
break;
}
sct[x][y].designation=comment[0];
if(sct[x][y].designation==DCAPITOL){
curntn->capx=x;
curntn->capy=y;
}
#ifdef CONQUER
if (sct[x][y].designation==DROAD)
roads_this_turn++;
#endif
break;
case XSACIV: /*Sadjciv*/
/* if for some reason you dont own it, put people
back into your capitol */
if((sct[x][y].owner!=country)&&(country!=0)) {
sct[curntn->capx][curntn->capy].people+=armynum;
break;
}
sct[x][y].people=armynum;
break;
case XSACIV3: /*Sadjciv3 - incremental people adjust */
/* if you dont own it, put people in your capitol */
if((sct[x][y].owner!=country)&&(country!=0)) {
sct[curntn->capx][curntn->capy].people+=armynum;
}
else
{
sct[x][y].people+=armynum;
}
break;
case XSIFORT: /*Sincfort*/
sct[x][y].fortress++;
break;
case XNAGOLD: /*Nadjgold:*/
curntn->tgold = longvar;
break;
case XAMOV:
P_AMOVE=x;
break;
case XNMOV:
curntn->nvy[armynum].smove=x;
break;
case XSAOWN:
/* if not own it, and if people there, problem */
if((sct[x][y].owner!=country)
&&( country!=0)
&&( sct[x][y].owner!=0 )
&&( sct[x][y].people>0 )
&&( magic(country,SLAVER)==FALSE )
&&( ntn[sct[x][y].owner].race != curntn->race)){
sct[ntn[sct[x][y].owner].capx][ntn[sct[x][y].owner].capy].people+= sct[x][y].people;
sct[x][y].people=0;
fprintf(stderr,"ERROR: <%s> taking sector %d %d but civilians exist of other race - puting them in their capitol\n",curntn->name,x,y);
}
if(curntn->popularity<MAXTGVAL) curntn->popularity++;
sct[x][y].owner=country;
break;
case EDADJ:
curntn->dstatus[armynum]=x;
break;
case XNARGOLD:
curntn->jewels = longvar;
break;
case XNAMETAL:
curntn->metals = longvar;
break;
case INCAPLUS:
curntn->aplus++;
break;
case INCDPLUS:
curntn->dplus++;
break;
case DESTRY:
sct[ntn[armynum].capx][ntn[armynum].capy].owner=savectry;
country=armynum;
if (isupdate) destroy(country);
country=savectry;
break;
case CHG_MGK:
curntn->powers|=long2var;
if(curntn->powers!=longvar){
printf("\nERROR ON MAGIC READ country=%d %ld != %ld (or of %ld)",country,longvar,curntn->powers,long2var);
getchar();
}
for(armynum=0;armynum<=MAXPOWER;armynum++) {
longvar = 1L << armynum;
if (longvar & long2var)
exenewmgk(longvar);
}
long2var=0;
break;
}
if(fgets(line,80,fp)==NULL) done=TRUE;
}
fclose(fp);
/*return 1 if it did something*/
country=savectry;
curntn = &ntn[country];
if(execed==1) return(1);
else return(0);
}
#ifdef CONQUER
void
hangup()
{
char line[LINELTH];
if(country==0) writedata();
else {
fprintf(fexe,"L_NGOLD\t%d \t%d \t%ld \t0 \t0 \t%s\n",
XNAGOLD ,country,curntn->tgold,"null");
fprintf(fexe,"L_NMETAL\t%d \t%d \t%ld \t0 \t0 \t%s\n",
XNAMETAL ,country,curntn->metals,"null");
fprintf(fexe,"L_NJWLS\t%d \t%d \t%ld \t0 \t0 \t%s\n",
XNARGOLD ,country,curntn->jewels,"null");
}
/*close file*/
fclose(fexe);
/* remove any existing mail reading/writing file */
if (mailok!=DONEMAIL) {
extern char tmp_mail_name[];
unlink(tmp_mail_name);
}
/* remove the lock file */
unlink(fison);
sprintf(line,"%s%hd.tmp",msgfile,country);
unlink(line);
/*send a message to God*/
if(mailopen( 0 )!=(-1)) {
fprintf(fm,"WARNING: Nation %s hungup on me.\n",curntn->name);
mailclose(0);
}
/* exit program */
exit(FAIL);
}
#endif /* CONQUER */