| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <ctype.h> |
| 10 |
|
| 11 |
#define global |
| 12 |
#include "agility.h" |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
static long cntvar, cntflag, cntcnt, cntquest, cntstr, cntsub; |
| 17 |
|
| 18 |
|
| 19 |
#define m(cnt) do {if (arg>cnt) cnt=arg; return;} while(0) |
| 20 |
|
| 21 |
void check_arg(integer argtype, integer arg) |
| 22 |
{ |
| 23 |
if (argtype & AGT_VAR) argtype=AGT_VAR; |
| 24 |
switch(argtype) |
| 25 |
{ |
| 26 |
case AGT_VAR:m(cntvar); |
| 27 |
case AGT_FLAG:m(cntflag); |
| 28 |
case AGT_CNT:m(cntcnt); |
| 29 |
case AGT_QUEST:m(cntquest); |
| 30 |
case AGT_STR:m(cntstr); |
| 31 |
case AGT_SUB:m(cntsub); |
| 32 |
#if 0 |
| 33 |
case AGT_PIC:m(cntpic); |
| 34 |
case AGT_PIX:m(cntpix); |
| 35 |
case AGT_FONT:m(cntfont); |
| 36 |
case AGT_SONG:m(cntsong); |
| 37 |
case AGT_ROOMFLAG:m(cntroomflag); |
| 38 |
#endif |
| 39 |
default:break; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
#undef m |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
void truncate_cmd(void) |
| 51 |
{ |
| 52 |
int i, j; |
| 53 |
const opdef *opdata; |
| 54 |
|
| 55 |
cntvar=cntflag=cntcnt=cntquest=cntstr=cntsub=0; |
| 56 |
|
| 57 |
for(i=0;i<last_cmd;i++) { |
| 58 |
j=0; |
| 59 |
while(j<command[i].cmdsize) { |
| 60 |
if (command[i].data[j]>=END_ACT) break; |
| 61 |
opdata=get_opdef(command[i].data[j++]); |
| 62 |
if (opdata==&illegal_def) |
| 63 |
agtwarn("Illegal opcode found.",1); |
| 64 |
|
| 65 |
if (j+opdata->argnum>command[i].cmdsize || |
| 66 |
opdata==&illegal_def) { |
| 67 |
j=command[i].cmdsize; |
| 68 |
if (!(opdata==&illegal_def)) |
| 69 |
agtwarn("Command block overrun",0); |
| 70 |
break; |
| 71 |
} |
| 72 |
|
| 73 |
if (opdata->argnum>=1) { |
| 74 |
check_arg(opdata->arg1,command[i].data[j++]); |
| 75 |
if (opdata->argnum>=2) |
| 76 |
check_arg(opdata->arg2,command[i].data[j++]); |
| 77 |
} |
| 78 |
} |
| 79 |
if (j<command[i].cmdsize) |
| 80 |
command[i].cmdsize=j+1; |
| 81 |
} |
| 82 |
if (VAR_NUM>cntvar) VAR_NUM=cntvar; |
| 83 |
if (FLAG_NUM>cntflag) FLAG_NUM=cntflag; |
| 84 |
if (CNT_NUM>cntcnt) CNT_NUM=cntcnt; |
| 85 |
if (MAX_USTR>cntstr) MAX_USTR=cntstr; |
| 86 |
if (MAX_SUB>cntsub) MAX_SUB=cntsub; |
| 87 |
if (MaxQuestion>cntquest) MaxQuestion=cntquest; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
void copydesc(descr_ptr *descp,int num) |
| 94 |
{ |
| 95 |
int i; |
| 96 |
descr_line *txt; |
| 97 |
|
| 98 |
if (num<=0 || descp==NULL) return; |
| 99 |
for(i=0;i<num;i++) { |
| 100 |
txt=read_descr(descp[i].start,descp[i].size); |
| 101 |
write_descr(&descp[i],txt); |
| 102 |
free_descr(txt); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
descr_ptr *convert_qa(tline *qa) |
| 109 |
{ |
| 110 |
int i; |
| 111 |
descr_ptr *qa_ptr; |
| 112 |
descr_line txt[2]; |
| 113 |
|
| 114 |
qa_ptr=rmalloc(sizeof(descr_ptr)*MaxQuestion); |
| 115 |
txt[1]=NULL; |
| 116 |
if (qa!=NULL) |
| 117 |
for(i=0;i<MaxQuestion;i++) { |
| 118 |
txt[0]=qa[i]; |
| 119 |
write_descr(&qa_ptr[i],txt); |
| 120 |
} |
| 121 |
else |
| 122 |
for(i=0;i<MaxQuestion;i++) { |
| 123 |
qa_ptr[i].start=0; |
| 124 |
qa_ptr[i].size=0; |
| 125 |
} |
| 126 |
return qa_ptr; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static void copy_misc(fc_type fc) |
| 132 |
{ |
| 133 |
descr_line *txt; |
| 134 |
|
| 135 |
txt=read_ttl(fc); |
| 136 |
write_descr(&title_ptr,txt); |
| 137 |
free_descr(txt); |
| 138 |
|
| 139 |
txt=read_ins(fc); |
| 140 |
write_descr(&ins_ptr,txt); |
| 141 |
free_ins(txt); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
void descr_out(fc_type fc) |
| 147 |
{ |
| 148 |
open_descr(fc); |
| 149 |
copydesc(&intro_ptr,1); |
| 150 |
copydesc(err_ptr,NUM_ERR); |
| 151 |
copydesc(msg_ptr,last_message); |
| 152 |
copydesc(help_ptr,maxroom-first_room+1); |
| 153 |
copydesc(room_ptr,maxroom-first_room+1); |
| 154 |
copydesc(special_ptr,maxroom-first_room+1); |
| 155 |
copydesc(noun_ptr,maxnoun-first_noun+1); |
| 156 |
copydesc(text_ptr,maxnoun-first_noun+1); |
| 157 |
copydesc(turn_ptr,maxnoun-first_noun+1); |
| 158 |
copydesc(push_ptr,maxnoun-first_noun+1); |
| 159 |
copydesc(pull_ptr,maxnoun-first_noun+1); |
| 160 |
copydesc(play_ptr,maxnoun-first_noun+1); |
| 161 |
copydesc(talk_ptr,maxcreat-first_creat+1); |
| 162 |
copydesc(ask_ptr,maxcreat-first_creat+1); |
| 163 |
copydesc(creat_ptr,maxcreat-first_creat+1); |
| 164 |
|
| 165 |
if (quest_ptr!=NULL) copydesc(quest_ptr,MaxQuestion); |
| 166 |
else quest_ptr=convert_qa(question); |
| 167 |
|
| 168 |
if (ans_ptr!=NULL) copydesc(ans_ptr,MaxQuestion); |
| 169 |
else ans_ptr=convert_qa(answer); |
| 170 |
|
| 171 |
close_descr(); |
| 172 |
|
| 173 |
copy_misc(fc); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
rbool end_cmd_options; |
| 179 |
|
| 180 |
void helpmsg(void) |
| 181 |
{ |
| 182 |
printf("Syntax: agt2agx <options> <game name>\n"); |
| 183 |
printf("Options:\n"); |
| 184 |
printf("(all of these can be turned off by putting a - after the option)\n"); |
| 185 |
printf("-h Print this help message.\n"); |
| 186 |
printf("-o <filename> Specify output file root.\n"); |
| 187 |
#ifdef OPEN_AS_TEXT |
| 188 |
printf(" -b Open data files as binary files.\n"); |
| 189 |
#endif |
| 190 |
} |
| 191 |
|
| 192 |
char *outname; |
| 193 |
|
| 194 |
int parse_options(char *opt,char *next) |
| 195 |
{ |
| 196 |
if (opt[0]=='-' && opt[1]==0) |
| 197 |
{end_cmd_options=1;return 0;} |
| 198 |
for(;*opt!=0;opt++) |
| 199 |
switch(tolower(*opt)) |
| 200 |
{ |
| 201 |
case '?': case 'h': |
| 202 |
helpmsg(); |
| 203 |
exit(EXIT_SUCCESS); |
| 204 |
case 'i': |
| 205 |
if (opt[1]=='-') {DIAG=0;opt++;} |
| 206 |
else if (opt[1]=='+') {DIAG=1;opt++;} |
| 207 |
else DIAG=1; |
| 208 |
break; |
| 209 |
#ifdef OPEN_FILE_AS_TEXT |
| 210 |
case 'b': |
| 211 |
if (opt[1]=='-') {open_as_binary=0;opt++;} |
| 212 |
else if (opt[1]=='+') {open_as_binary=1;opt++;} |
| 213 |
else open_as_binary=1; |
| 214 |
break; |
| 215 |
#endif |
| 216 |
case 'o': |
| 217 |
if (opt[1]!=0 || next!=NULL) { |
| 218 |
if (opt[1]!=0) outname=opt+1; |
| 219 |
else outname=next; |
| 220 |
return (opt[1]==0); |
| 221 |
} else { |
| 222 |
printf("-o requires a file name\n"); |
| 223 |
exit(EXIT_FAILURE); |
| 224 |
} |
| 225 |
default:printf("Do not recognize option %c\n",*opt); |
| 226 |
helpmsg(); |
| 227 |
exit(EXIT_FAILURE); |
| 228 |
} |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
int main(int argc,char *argv[]) |
| 234 |
{ |
| 235 |
int i; |
| 236 |
char *gamefile; |
| 237 |
fc_type fc, fc_out; |
| 238 |
|
| 239 |
end_cmd_options=0; |
| 240 |
|
| 241 |
init_flags(); |
| 242 |
|
| 243 |
no_auxsyn=1; |
| 244 |
fix_ascii_flag=0; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
PURE_ANSWER=PURE_TIME=PURE_ROOMTITLE=2; |
| 249 |
PURE_AND=PURE_METAVERB=PURE_SYN=PURE_NOUN=PURE_ADJ=2; |
| 250 |
PURE_DUMMY=PURE_SUBNAME=PURE_PROSUB=PURE_HOSTILE=2; |
| 251 |
PURE_GETHOSTILE=PURE_DISAMBIG=PURE_ALL=2; |
| 252 |
irun_mode=verboseflag=2; |
| 253 |
|
| 254 |
build_trans_ascii(); |
| 255 |
|
| 256 |
printf("agt2agx: Convert AGT game files into AGX files\n"); |
| 257 |
printf("%s\n",version_str); |
| 258 |
printf(" Copyright (C) 1996-1999,2001 Robert Masenten\n"); |
| 259 |
printf("[%s]\n\n",portstr); |
| 260 |
|
| 261 |
outname=gamefile=NULL; |
| 262 |
for(i=1;i<argc;i++) |
| 263 |
if (argv[i][0]=='-' && !end_cmd_options) |
| 264 |
i+=parse_options(argv[i]+1,argv[i+1]); |
| 265 |
else if (gamefile==NULL) |
| 266 |
gamefile=argv[i]; |
| 267 |
else {helpmsg();exit(EXIT_FAILURE);} |
| 268 |
if (gamefile==NULL) |
| 269 |
{helpmsg();exit(EXIT_FAILURE);} |
| 270 |
if (outname==NULL) |
| 271 |
outname=gamefile; |
| 272 |
|
| 273 |
fc=init_file_context(gamefile,fDA1); |
| 274 |
fc_out=init_file_context(outname,fAGX); |
| 275 |
text_file=1; |
| 276 |
read_config(openfile(fc,fCFG,NULL,0),0); |
| 277 |
text_file=0; |
| 278 |
printf("Reading AGT file...\n"); |
| 279 |
if (!readagt(fc,0)) |
| 280 |
fatal("Unable to open info (DA1) or AGX file."); |
| 281 |
text_file=1; |
| 282 |
read_config(openfile(fc,fCFG,NULL,0),1); |
| 283 |
text_file=0; |
| 284 |
|
| 285 |
sort_cmd(); |
| 286 |
truncate_cmd(); |
| 287 |
|
| 288 |
printf("Opening AGX file and copying description text...\n"); |
| 289 |
agx_create(fc_out); |
| 290 |
descr_out(fc); |
| 291 |
printf("Writing AGX file...\n"); |
| 292 |
agx_write(); |
| 293 |
agx_wclose(); |
| 294 |
|
| 295 |
free_all_agtread(); |
| 296 |
|
| 297 |
return 0; |
| 298 |
} |