2 //**************************************************************************
\r
6 //**************************************************************************
\r
8 // HEADER FILES ------------------------------------------------------------
\r
16 #include "strlist.h"
\r
21 // MACROS ------------------------------------------------------------------
\r
23 // TYPES -------------------------------------------------------------------
\r
25 typedef struct scriptInfo_s
\r
37 typedef struct functionInfo_s
\r
39 U_BYTE hasReturnValue;
\r
46 typedef struct mapVarInfo_s
\r
54 // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
\r
56 // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
\r
58 // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
\r
60 static void GrowBuffer(void);
\r
61 static void Append(void *buffer, size_t size);
\r
62 static void Write(void *buffer, size_t size, int address);
\r
63 static void Skip(size_t size);
\r
64 static void CloseOld(void);
\r
65 static void CloseNew(void);
\r
66 static void CreateDummyScripts(void);
\r
67 static void RecordDummyScripts(void);
\r
69 // EXTERNAL DATA DECLARATIONS ----------------------------------------------
\r
71 // PUBLIC DATA DEFINITIONS -------------------------------------------------
\r
77 int pc_FunctionCount;
\r
78 boolean pc_NoShrink;
\r
79 boolean pc_HexenCase;
\r
80 boolean pc_WadAuthor = TRUE;
\r
81 boolean pc_EncryptStrings;
\r
82 int pc_LastAppendedCommand;
\r
83 int pc_DummyAddress;
\r
85 // PRIVATE DATA DEFINITIONS ------------------------------------------------
\r
87 static size_t BufferSize;
\r
88 static boolean ObjectOpened = NO;
\r
89 static scriptInfo_t ScriptInfo[MAX_SCRIPT_COUNT];
\r
90 static functionInfo_t FunctionInfo[MAX_FUNCTION_COUNT];
\r
91 static int ArraySizes[MAX_MAP_VARIABLES];
\r
92 static int *ArrayInits[MAX_MAP_VARIABLES];
\r
93 static boolean ArrayOfStrings[MAX_MAP_VARIABLES];
\r
94 static int NumArrays;
\r
95 static mapVarInfo_t MapVariables[MAX_MAP_VARIABLES];
\r
96 static boolean MapVariablesInit = NO;
\r
97 static char ObjectName[MAX_FILE_NAME_LENGTH];
\r
98 static int ObjectFlags;
\r
99 static int PushByteAddr;
\r
100 static char Imports[MAX_IMPORTS][9];
\r
101 static int NumImports;
\r
102 static boolean HaveScriptFlags;
\r
104 static char *PCDNames[PCODE_COMMAND_COUNT] =
\r
115 "PCD_LSPEC1DIRECT",
\r
116 "PCD_LSPEC2DIRECT",
\r
117 "PCD_LSPEC3DIRECT",
\r
118 "PCD_LSPEC4DIRECT",
\r
119 "PCD_LSPEC5DIRECT",
\r
131 "PCD_ASSIGNSCRIPTVAR",
\r
132 "PCD_ASSIGNMAPVAR",
\r
133 "PCD_ASSIGNWORLDVAR",
\r
134 "PCD_PUSHSCRIPTVAR",
\r
136 "PCD_PUSHWORLDVAR",
\r
137 "PCD_ADDSCRIPTVAR",
\r
140 "PCD_SUBSCRIPTVAR",
\r
143 "PCD_MULSCRIPTVAR",
\r
146 "PCD_DIVSCRIPTVAR",
\r
149 "PCD_MODSCRIPTVAR",
\r
152 "PCD_INCSCRIPTVAR",
\r
155 "PCD_DECSCRIPTVAR",
\r
164 "PCD_RANDOMDIRECT",
\r
166 "PCD_THINGCOUNTDIRECT",
\r
168 "PCD_TAGWAITDIRECT",
\r
170 "PCD_POLYWAITDIRECT",
\r
172 "PCD_CHANGEFLOORDIRECT",
\r
173 "PCD_CHANGECEILING",
\r
174 "PCD_CHANGECEILINGDIRECT",
\r
181 "PCD_NEGATELOGICAL",
\r
188 "PCD_SCRIPTWAITDIRECT",
\r
189 "PCD_CLEARLINESPECIAL",
\r
195 "PCD_PRINTCHARACTER",
\r
201 "PCD_AMBIENTSOUND",
\r
202 "PCD_SOUNDSEQUENCE",
\r
203 "PCD_SETLINETEXTURE",
\r
204 "PCD_SETLINEBLOCKING",
\r
205 "PCD_SETLINESPECIAL",
\r
207 "PCD_ENDPRINTBOLD",
\r
208 // [RH] End of Hexen p-codes
\r
209 "PCD_ACTIVATORSOUND",
\r
210 "PCD_LOCALAMBIENTSOUND",
\r
211 "PCD_SETLINEMONSTERBLOCKING",
\r
212 // [BC] Start of new pcodes
\r
213 "PCD_PLAYERBLUESKULL",
\r
214 "PCD_PLAYERREDSKULL",
\r
215 "PCD_PLAYERYELLOWSKULL",
\r
216 "PCD_PLAYERMASTERSKULL",
\r
217 "PCD_PLAYERBLUECARD",
\r
218 "PCD_PLAYERREDCARD",
\r
219 "PCD_PLAYERYELLOWCARD",
\r
220 "PCD_PLAYERMASTERCARD",
\r
221 "PCD_PLAYERBLACKSKULL",
\r
222 "PCD_PLAYERSILVERSKULL",
\r
223 "PCD_PLAYERGOLDSKULL",
\r
224 "PCD_PLAYERBLACKCARD",
\r
225 "PCD_PLAYERSILVERCARD",
\r
226 "PCD_PLAYERONTEAM",
\r
228 "PCD_PLAYERHEALTH",
\r
229 "PCD_PLAYERARMORPOINTS",
\r
231 "PCD_PLAYEREXPERT",
\r
232 "PCD_BLUETEAMCOUNT",
\r
233 "PCD_REDTEAMCOUNT",
\r
234 "PCD_BLUETEAMSCORE",
\r
235 "PCD_REDTEAMSCORE",
\r
236 "PCD_ISONEFLAGCTF",
\r
238 "PCD_LSPEC6DIRECT",
\r
241 "PCD_CONSOLECOMMANDDIRECT",
\r
242 "PCD_CONSOLECOMMAND",
\r
243 "PCD_SINGLEPLAYER",
\r
244 // [RH] End of Skull Tag p-codes
\r
248 "PCD_SETGRAVITYDIRECT",
\r
249 "PCD_SETAIRCONTROL",
\r
250 "PCD_SETAIRCONTROLDIRECT",
\r
251 "PCD_CLEARINVENTORY",
\r
252 "PCD_GIVEINVENTORY",
\r
253 "PCD_GIVEINVENTORYDIRECT",
\r
254 "PCD_TAKEINVENTORY",
\r
255 "PCD_TAKEINVENTORYDIRECT",
\r
256 "PCD_CHECKINVENTORY",
\r
257 "PCD_CHECKINVENTORYDIRECT",
\r
261 "PCD_SPAWNSPOTDIRECT",
\r
263 "PCD_SETMUSICDIRECT",
\r
264 "PCD_LOCALSETMUSIC",
\r
265 "PCD_LOCALSETMUSICDIRECT",
\r
267 "PCD_PRINTLOCALIZED",
\r
268 "PCD_MOREHUDMESSAGE",
\r
269 "PCD_OPTHUDMESSAGE",
\r
270 "PCD_ENDHUDMESSAGE",
\r
271 "PCD_ENDHUDMESSAGEBOLD",
\r
273 "PCD_SETSTYLEDIRECT",
\r
275 "PCD_SETFONTDIRECT",
\r
277 "PCD_LSPEC1DIRECTB",
\r
278 "PCD_LSPEC2DIRECTB",
\r
279 "PCD_LSPEC3DIRECTB",
\r
280 "PCD_LSPEC4DIRECTB",
\r
281 "PCD_LSPEC5DIRECTB",
\r
282 "PCD_DELAYDIRECTB",
\r
283 "PCD_RANDOMDIRECTB",
\r
289 "PCD_SETTHINGSPECIAL",
\r
290 "PCD_ASSIGNGLOBALVAR",
\r
291 "PCD_PUSHGLOBALVAR",
\r
292 "PCD_ADDGLOBALVAR",
\r
293 "PCD_SUBGLOBALVAR",
\r
294 "PCD_MULGLOBALVAR",
\r
295 "PCD_DIVGLOBALVAR",
\r
296 "PCD_MODGLOBALVAR",
\r
297 "PCD_INCGLOBALVAR",
\r
298 "PCD_DECGLOBALVAR",
\r
303 "PCD_SETFLOORTRIGGER",
\r
304 "PCD_SETCEILINGTRIGGER",
\r
308 "PCD_STARTTRANSLATION",
\r
309 "PCD_TRANSLATIONRANGE1",
\r
310 "PCD_TRANSLATIONRANGE2",
\r
311 "PCD_ENDTRANSLATION",
\r
316 "PCD_PUSHMAPARRAY",
\r
317 "PCD_ASSIGNMAPARRAY",
\r
335 "PCD_PUSHWORLDARRAY",
\r
336 "PCD_ASSIGNWORLDARRAY",
\r
337 "PCD_ADDWORLDARRAY",
\r
338 "PCD_SUBWORLDARRAY",
\r
339 "PCD_MULWORLDARRAY",
\r
340 "PCD_DIVWORLDARRAY",
\r
341 "PCD_MODWORLDARRAY",
\r
342 "PCD_INCWORLDARRAY",
\r
343 "PCD_DECWORLDARRAY",
\r
344 "PCD_PUSHGLOBALARRAY",
\r
345 "PCD_ASSIGNGLOBALARRAY",
\r
346 "PCD_ADDGLOBALARRAY",
\r
347 "PCD_SUBGLOBALARRAY",
\r
348 "PCD_MULGLOBALARRAY",
\r
349 "PCD_DIVGLOBALARRAY",
\r
350 "PCD_MODGLOBALARRAY",
\r
351 "PCD_INCGLOBALARRAY",
\r
352 "PCD_DECGLOBALARRAY",
\r
353 "PCD_SETMARINEWEAPON",
\r
354 "PCD_SETACTORPROPERTY",
\r
355 "PCD_GETACTORPROPERTY",
\r
356 "PCD_PLAYERNUMBER",
\r
357 "PCD_ACTIVATORTID",
\r
358 "PCD_SETMARINESPRITE",
\r
359 "PCD_GETSCREENWIDTH",
\r
360 "PCD_GETSCREENHEIGHT",
\r
361 "PCD_THING_PROJECTILE2",
\r
365 "PCD_CASEGOTOSORTED",
\r
366 "PCD_SETRESULTVALUE",
\r
367 "PCD_GETLINEROWOFFSET",
\r
368 "PCD_GETACTORFLOORZ",
\r
369 "PCD_GETACTORANGLE",
\r
370 "PCD_GETSECTORFLOORZ",
\r
371 "PCD_GETSECTORCEILINGZ",
\r
372 "PCD_LSPEC5RESULT",
\r
373 "PCD_GETSIGILPIECES",
\r
376 "PCD_PLAYERINGAME",
\r
378 "PCD_SETCAMERATOTEXTURE",
\r
380 "PCD_GETAMMOCAPACITY",
\r
381 "PCD_SETAMMOCAPACITY",
\r
382 // [JB] start of new pcodes
\r
383 "PCD_PRINTMAPCHARARRAY",
\r
384 "PCD_PRINTWORLDCHARARRAY",
\r
385 "PCD_PRINTGLOBALCHARARRAY",
\r
386 // [JB] end of new pcodes
\r
387 "PCD_SETACTORANGLE",
\r
389 "PCD_SETMOUSEPOINTER",
\r
390 "PCD_MOVEMOUSEPOINTER"
\r
391 "PCD_SPAWNPROJECTILE",
\r
392 "PCD_GETSECTORLIGHTLEVEL",
\r
393 "PCD_GETACTORCEILINGZ",
\r
394 "PCD_SETACTORPOSITION",
\r
395 "PCD_CLEARACTORINVENTORY",
\r
396 "PCD_GIVEACTORINVENTORY",
\r
397 "PCD_TAKEACTORINVENTORY",
\r
398 "PCD_CHECKACTORINVENTORY",
\r
399 "PCD_THINGCOUNTNAME",
\r
400 "PCD_SPAWNSPOTFACING",
\r
405 // CODE --------------------------------------------------------------------
\r
407 //==========================================================================
\r
411 //==========================================================================
\r
413 void PC_OpenObject(char *name, size_t size, int flags)
\r
415 if(ObjectOpened == YES)
\r
419 if(strlen(name) >= MAX_FILE_NAME_LENGTH)
\r
421 ERR_Exit(ERR_FILE_NAME_TOO_LONG, NO, name);
\r
423 strcpy(ObjectName, name);
\r
424 pc_Buffer = MS_Alloc(size, ERR_ALLOC_PCODE_BUFFER);
\r
425 pc_BufferPtr = pc_Buffer;
\r
427 ObjectFlags = flags;
\r
429 pc_ScriptCount = 0;
\r
430 ObjectOpened = YES;
\r
431 PC_AppendString("ACS");
\r
432 PC_SkipLong(); // Script table offset
\r
435 //==========================================================================
\r
439 //==========================================================================
\r
441 void PC_CloseObject(void)
\r
443 MS_Message(MSG_DEBUG, "---- PC_CloseObject ----\n");
\r
444 while (pc_Address & 3)
\r
448 if(!pc_NoShrink || (NumLanguages > 1) || (NumStringLists > 0) ||
\r
449 (pc_FunctionCount > 0) || MapVariablesInit || NumArrays != 0 ||
\r
450 pc_EncryptStrings || NumImports != 0 || HaveScriptFlags)
\r
458 if(MS_SaveFile(ObjectName, pc_Buffer, pc_Address) == FALSE)
\r
460 ERR_Exit(ERR_SAVE_OBJECT_FAILED, NO);
\r
464 //==========================================================================
\r
468 //==========================================================================
\r
470 static void CloseOld(void)
\r
474 STR_WriteStrings();
\r
475 PC_WriteLong((U_LONG)pc_Address, 4);
\r
476 PC_AppendLong((U_LONG)pc_ScriptCount);
\r
477 for(i = 0; i < pc_ScriptCount; ++i)
\r
479 scriptInfo_t *info = &ScriptInfo[i];
\r
480 MS_Message(MSG_DEBUG, "Script %d, address = %d, arg count = %d\n",
\r
481 info->number, info->address, info->argCount);
\r
482 PC_AppendLong((U_LONG)(info->number + info->type * 1000));
\r
483 PC_AppendLong((U_LONG)info->address);
\r
484 PC_AppendLong((U_LONG)info->argCount);
\r
489 //==========================================================================
\r
493 // Creates a new-format ACS file. For programs that don't know any better,
\r
494 // this will look just like an old ACS file with no scripts or strings but
\r
495 // with some extra junk in the middle. Both WadAuthor and DeePsea will not
\r
496 // accept ACS files that do not have the ACS\0 header. Worse, WadAuthor
\r
497 // will hang if the file begins with ACS\0 but does not look like something
\r
498 // that might have been created with Raven's ACC. Thus, the chunks live in
\r
499 // the string block, and there are two 0 dwords at the end of the file.
\r
501 //==========================================================================
\r
503 static void CloseNew(void)
\r
510 CreateDummyScripts();
\r
513 chunkStart = pc_Address;
\r
515 // Only write out those scripts that this acs file actually provides.
\r
516 for(i = j = 0; i < pc_ScriptCount; ++i)
\r
518 if(!ScriptInfo[i].imported)
\r
525 PC_Append("SPTR", 4);
\r
526 PC_AppendLong(j * 8);
\r
527 for(i = 0; i < pc_ScriptCount; i++)
\r
529 scriptInfo_t *info = &ScriptInfo[i];
\r
530 if(!info->imported)
\r
532 MS_Message(MSG_DEBUG, "Script %d, address = %d, arg count = %d\n",
\r
533 info->number, info->address, info->argCount);
\r
534 PC_AppendWord(info->number);
\r
535 PC_AppendByte(info->type);
\r
536 PC_AppendByte(info->argCount);
\r
537 PC_AppendLong((U_LONG)info->address);
\r
542 // If any scripts have more than the maximum number of arguments, output them.
\r
543 for(i = j = 0; i < pc_ScriptCount; ++i)
\r
545 if(!ScriptInfo[i].imported && ScriptInfo[i].varCount > MAX_SCRIPT_VARIABLES)
\r
552 PC_Append("SVCT", 4);
\r
553 PC_AppendLong(j * 4);
\r
554 for(i = 0; i < pc_ScriptCount; ++i)
\r
556 scriptInfo_t *info = &ScriptInfo[i];
\r
557 if(!info->imported && info->varCount > MAX_SCRIPT_VARIABLES)
\r
559 MS_Message(MSG_DEBUG, "Script %d, var count = %d\n",
\r
560 info->number, info->varCount);
\r
561 PC_AppendWord(info->number);
\r
562 PC_AppendWord(info->varCount);
\r
567 // Write script flags in a separate chunk, so older ZDooms don't get confused
\r
568 for(i = j = 0; i < pc_ScriptCount; ++i)
\r
570 if(!ScriptInfo[i].imported && ScriptInfo[i].flags != 0)
\r
577 PC_Append("SFLG", 4);
\r
578 PC_AppendLong(j * 4);
\r
579 for(i = 0; i < pc_ScriptCount; ++i)
\r
581 scriptInfo_t *info = &ScriptInfo[i];
\r
582 if(!info->imported && info->flags != 0)
\r
584 PC_AppendWord(info->number);
\r
585 PC_AppendWord(info->flags);
\r
590 if(pc_FunctionCount > 0)
\r
592 PC_Append("FUNC", 4);
\r
593 PC_AppendLong(pc_FunctionCount * 8);
\r
594 for(i = 0; i < pc_FunctionCount; ++i)
\r
596 functionInfo_t *info = &FunctionInfo[i];
\r
597 MS_Message(MSG_DEBUG, "Function %d:%s, address = %d, arg count = %d, var count = %d\n",
\r
598 i, STR_GetString(STRLIST_FUNCTIONS, info->name),
\r
599 info->address, info->argCount, info->localCount);
\r
600 PC_AppendByte(info->argCount);
\r
601 PC_AppendByte(info->localCount);
\r
602 PC_AppendByte((U_BYTE)(info->hasReturnValue?1:0));
\r
604 PC_AppendLong((U_LONG)info->address);
\r
606 STR_WriteListChunk(STRLIST_FUNCTIONS, MAKE4CC('F','N','A','M'), NO);
\r
609 if(NumLanguages > 1)
\r
611 for(i = 0; i < NumLanguages; i++)
\r
613 STR_WriteChunk(i, pc_EncryptStrings);
\r
616 else if(STR_ListSize(0) > 0)
\r
618 STR_WriteChunk(0, pc_EncryptStrings);
\r
621 STR_WriteListChunk(STRLIST_PICS, MAKE4CC('P','I','C','S'), NO);
\r
622 if(MapVariablesInit)
\r
626 for(i = 0; i < pa_MapVarCount; ++i)
\r
628 if(MapVariables[i].initializer != 0)
\r
631 for(j = pa_MapVarCount-1; j > i; --j)
\r
633 if(MapVariables[j].initializer != 0)
\r
640 PC_Append("MINI", 4);
\r
641 PC_AppendLong((j-i)*4+4);
\r
642 PC_AppendLong(i); // First map var defined
\r
645 PC_AppendLong(MapVariables[i].initializer);
\r
650 // If this is a library, record which map variables are
\r
651 // initialized with strings.
\r
652 if(ImportMode == IMPORT_Exporting)
\r
656 for(i = 0; i < pa_MapVarCount; ++i)
\r
658 if(MapVariables[i].isString)
\r
665 PC_Append("MSTR", 4);
\r
666 PC_AppendLong(count*4);
\r
667 for(i = 0; i < pa_MapVarCount; ++i)
\r
669 if(MapVariables[i].isString)
\r
676 // Now do the same thing for arrays.
\r
677 for(count = 0, i = 0; i < pa_MapVarCount; ++i)
\r
679 if(ArrayOfStrings[i])
\r
686 PC_Append("ASTR", 4);
\r
687 PC_AppendLong(count*4);
\r
688 for(i = 0; i < pa_MapVarCount; ++i)
\r
690 if(ArrayOfStrings[i])
\r
698 // Publicize the names of map variables in a library.
\r
699 if(ImportMode == IMPORT_Exporting)
\r
701 for(i = 0; i < pa_MapVarCount; ++i)
\r
703 if(!MapVariables[i].imported)
\r
705 STR_AppendToList(STRLIST_MAPVARS, MapVariables[i].name);
\r
709 STR_AppendToList(STRLIST_MAPVARS, NULL);
\r
712 STR_WriteListChunk(STRLIST_MAPVARS, MAKE4CC('M','E','X','P'), NO);
\r
715 // Record the names of imported map variables
\r
717 for(i = 0; i < pa_MapVarCount; ++i)
\r
719 if(MapVariables[i].imported && !ArraySizes[i])
\r
721 count += 5 + strlen(MapVariables[i].name);
\r
726 PC_Append("MIMP", 4);
\r
727 PC_AppendLong(count);
\r
728 for(i = 0; i < pa_MapVarCount; ++i)
\r
730 if(MapVariables[i].imported && !ArraySizes[i])
\r
733 PC_AppendString(MapVariables[i].name);
\r
742 // Arrays defined here
\r
743 for(count = 0, i = 0; i < pa_MapVarCount; ++i)
\r
745 if(ArraySizes[i] && !MapVariables[i].imported)
\r
752 PC_Append("ARAY", 4);
\r
753 PC_AppendLong(count*8);
\r
754 for(i = 0; i < pa_MapVarCount; ++i)
\r
756 if(ArraySizes[i] && !MapVariables[i].imported)
\r
759 PC_AppendLong(ArraySizes[i]);
\r
762 for(i = 0; i < pa_MapVarCount; ++i)
\r
768 PC_Append("AINI", 4);
\r
769 PC_AppendLong(ArraySizes[i]*4+4);
\r
770 PC_AppendLong((U_LONG)i);
\r
771 for(j = 0; j < ArraySizes[i]; ++j)
\r
773 PC_AppendLong((U_LONG)ArrayInits[i][j]);
\r
779 // Arrays imported from elsewhere
\r
780 for(count = 0, j = i = 0; i < pa_MapVarCount; ++i)
\r
782 if(ArraySizes[i] && MapVariables[i].imported)
\r
784 count += 9 + strlen(MapVariables[i].name);
\r
790 PC_Append("AIMP", 4);
\r
791 PC_AppendLong(count+4);
\r
793 for(i = 0; i < pa_MapVarCount; ++i)
\r
795 if(ArraySizes[i] && MapVariables[i].imported)
\r
798 PC_AppendLong(ArraySizes[i]);
\r
799 PC_AppendString(MapVariables[i].name);
\r
805 // Add a dummy chunk to indicate if this object is a library.
\r
806 if(ImportMode == IMPORT_Exporting)
\r
808 PC_Append("ALIB", 4);
\r
812 // Record libraries imported by this object.
\r
816 for(i = 0; i < NumImports; ++i)
\r
818 count += strlen(Imports[i]) + 1;
\r
822 PC_Append("LOAD", 4);
\r
823 PC_AppendLong(count);
\r
824 for(i = 0; i < NumImports; ++i)
\r
826 PC_AppendString(Imports[i]);
\r
831 PC_AppendLong((U_LONG)chunkStart);
\r
834 PC_Append("ACSE", 4);
\r
838 PC_Append("ACSe", 4);
\r
840 PC_WriteLong((U_LONG)pc_Address, 4);
\r
842 // WadAuthor compatibility when creating a library is pointless, because
\r
843 // that editor does not know anything about libraries and will never
\r
844 // find their scripts ever.
\r
845 if(pc_WadAuthor && ImportMode != IMPORT_Exporting)
\r
847 RecordDummyScripts();
\r
856 //==========================================================================
\r
858 // CreateDummyScripts
\r
860 //==========================================================================
\r
862 static void CreateDummyScripts(void)
\r
866 MS_Message(MSG_DEBUG, "Creating dummy scripts to make WadAuthor happy.\n");
\r
867 if(pc_Address%4 != 0)
\r
870 PC_Append((void *)&pad, 4-(pc_Address%4));
\r
872 pc_DummyAddress = pc_Address;
\r
873 for(i = 0; i < pc_ScriptCount; ++i)
\r
875 if(!ScriptInfo[i].imported)
\r
877 PC_AppendCmd(PCD_TERMINATE);
\r
880 PC_AppendCmd(PCD_NOP);
\r
881 PC_AppendCmd(PCD_NOP);
\r
882 PC_AppendCmd(PCD_NOP);
\r
888 //==========================================================================
\r
890 // RecordDummyScripts
\r
892 //==========================================================================
\r
894 static void RecordDummyScripts(void)
\r
898 for(i = count = 0; i < pc_ScriptCount; ++i)
\r
900 if(!ScriptInfo[i].imported)
\r
905 PC_AppendLong((U_LONG)count);
\r
906 for(i = 0; i < pc_ScriptCount; ++i)
\r
908 scriptInfo_t *info = &ScriptInfo[i];
\r
909 if(!info->imported)
\r
911 MS_Message(MSG_DEBUG, "Dummy script %d, address = %d, arg count = %d\n",
\r
912 info->number, info->address, info->argCount);
\r
913 PC_AppendLong((U_LONG)info->number);
\r
914 PC_AppendLong((U_LONG)pc_DummyAddress + i*4);
\r
915 PC_AppendLong((U_LONG)info->argCount);
\r
920 //==========================================================================
\r
924 //==========================================================================
\r
926 void GrowBuffer(void)
\r
928 ptrdiff_t buffpos = pc_BufferPtr - pc_Buffer;
\r
931 pc_Buffer = MS_Realloc(pc_Buffer, BufferSize, ERR_PCODE_BUFFER_OVERFLOW);
\r
932 pc_BufferPtr = pc_Buffer + buffpos;
\r
935 //==========================================================================
\r
937 // PC_Append functions
\r
939 //==========================================================================
\r
941 static void Append(void *buffer, size_t size)
\r
943 if (ImportMode != IMPORT_Importing)
\r
945 if(pc_Address+size > BufferSize)
\r
949 memcpy(pc_BufferPtr, buffer, size);
\r
950 pc_BufferPtr += size;
\r
951 pc_Address += size;
\r
955 void PC_Append(void *buffer, size_t size)
\r
957 if (ImportMode != IMPORT_Importing)
\r
959 MS_Message(MSG_DEBUG, "AD> %06d = (%d bytes)\n", pc_Address, size);
\r
960 Append(buffer, size);
\r
964 void PC_AppendByte(U_BYTE val)
\r
966 if (ImportMode != IMPORT_Importing)
\r
968 MS_Message(MSG_DEBUG, "AB> %06d = %d\n", pc_Address, val);
\r
969 Append(&val, sizeof(U_BYTE));
\r
973 void PC_AppendWord(U_WORD val)
\r
975 if (ImportMode != IMPORT_Importing)
\r
977 MS_Message(MSG_DEBUG, "AW> %06d = %d\n", pc_Address, val);
\r
978 val = MS_LittleUWORD(val);
\r
979 Append(&val, sizeof(U_WORD));
\r
983 void PC_AppendLong(U_LONG val)
\r
985 if (ImportMode != IMPORT_Importing)
\r
987 MS_Message(MSG_DEBUG, "AL> %06d = %d\n", pc_Address, val);
\r
988 val = MS_LittleULONG(val);
\r
989 Append(&val, sizeof(U_LONG));
\r
993 void PC_AppendString(char *string)
\r
995 if (ImportMode != IMPORT_Importing)
\r
999 length = strlen(string)+1;
\r
1000 MS_Message(MSG_DEBUG, "AS> %06d = \"%s\" (%d bytes)\n",
\r
1001 pc_Address, string, length);
\r
1002 Append(string, length);
\r
1006 void PC_AppendCmd(pcd_t command)
\r
1008 if (ImportMode != IMPORT_Importing)
\r
1010 pc_LastAppendedCommand = command;
\r
1013 MS_Message(MSG_DEBUG, "AC> %06d = #%d:%s\n", pc_Address,
\r
1014 command, PCDNames[command]);
\r
1015 command = MS_LittleULONG(command);
\r
1016 Append(&command, sizeof(U_LONG));
\r
1021 if (command != PCD_PUSHBYTE && PushByteAddr)
\r
1022 { // Maybe shrink a PCD_PUSHBYTE sequence into PCD_PUSHBYTES
\r
1023 int runlen = (pc_Address - PushByteAddr) / 2;
\r
1028 pc_Buffer[PushByteAddr] = PCD_PUSHBYTES;
\r
1029 for (i = 0; i < runlen; i++)
\r
1031 pc_Buffer[PushByteAddr+i+2] = pc_Buffer[PushByteAddr+i*2+1];
\r
1033 pc_Buffer[PushByteAddr+1] = runlen;
\r
1034 pc_Address = PushByteAddr + runlen + 2;
\r
1035 pc_BufferPtr = pc_Buffer + pc_Address;
\r
1036 MS_Message (MSG_DEBUG, "AC> Last %d PCD_PUSHBYTEs changed to #%d:PCD_PUSHBYTES\n",
\r
1037 runlen, PCD_PUSHBYTES);
\r
1039 else if (runlen > 1)
\r
1041 pc_Buffer[PushByteAddr] = PCD_PUSH2BYTES + runlen - 2;
\r
1042 for (i = 1; i < runlen; i++)
\r
1044 pc_Buffer[PushByteAddr+1+i] = pc_Buffer[PushByteAddr+1+i*2];
\r
1046 pc_Address = PushByteAddr + runlen + 1;
\r
1047 pc_BufferPtr = pc_Buffer + pc_Address;
\r
1048 MS_Message (MSG_DEBUG, "AC> Last %d PCD_PUSHBYTEs changed to #%d:PCD_PUSH%dBYTES\n",
\r
1049 runlen, PCD_PUSH2BYTES+runlen-2, runlen);
\r
1053 else if (command == PCD_PUSHBYTE && PushByteAddr == 0)
\r
1054 { // Remember the first PCD_PUSHBYTE, in case there are more
\r
1055 PushByteAddr = pc_Address;
\r
1057 MS_Message(MSG_DEBUG, "AC> %06d = #%d:%s\n", pc_Address,
\r
1058 command, PCDNames[command]);
\r
1060 if (command < 256-16)
\r
1063 Append(&cmd, sizeof(U_BYTE));
\r
1067 // Room for expansion: The top 16 pcodes in the [0,255]
\r
1068 // range select a set of pcodes, and the next byte is
\r
1069 // the pcode in that set.
\r
1070 cmd = ((command - (256-16)) >> 8) + (256-16);
\r
1071 Append(&cmd, sizeof(U_BYTE));
\r
1072 cmd = (command - (256-16)) & 255;
\r
1073 Append(&cmd, sizeof(U_BYTE));
\r
1079 //==========================================================================
\r
1081 // PC_AppendShrink
\r
1083 //==========================================================================
\r
1085 void PC_AppendShrink(U_BYTE val)
\r
1089 PC_AppendLong(val);
\r
1093 PC_AppendByte(val);
\r
1097 //==========================================================================
\r
1099 // PC_AppendPushVal
\r
1101 //==========================================================================
\r
1103 void PC_AppendPushVal(U_LONG val)
\r
1105 if(pc_NoShrink || val > 255)
\r
1107 PC_AppendCmd(PCD_PUSHNUMBER);
\r
1108 PC_AppendLong(val);
\r
1112 PC_AppendCmd(PCD_PUSHBYTE);
\r
1113 PC_AppendByte((U_BYTE)val);
\r
1117 //==========================================================================
\r
1119 // PC_Write functions
\r
1121 //==========================================================================
\r
1123 static void Write(void *buffer, size_t size, int address)
\r
1125 if (ImportMode != IMPORT_Importing)
\r
1127 if(address+size > BufferSize)
\r
1131 memcpy(pc_Buffer+address, buffer, size);
\r
1135 void PC_Write(void *buffer, size_t size, int address)
\r
1137 if (ImportMode != IMPORT_Importing)
\r
1139 MS_Message(MSG_DEBUG, "WD> %06d = (%d bytes)\n", address, size);
\r
1140 Write(buffer, size, address);
\r
1144 void PC_WriteByte(U_BYTE val, int address)
\r
1146 if (ImportMode != IMPORT_Importing)
\r
1148 MS_Message(MSG_DEBUG, "WB> %06d = %d\n", address, val);
\r
1149 Write(&val, sizeof(U_BYTE), address);
\r
1154 void PC_WriteWord(U_WORD val, int address)
\r
1156 MS_Message(MSG_DEBUG, "WW> %06d = %d\n", address, val);
\r
1157 val = MS_LittleUWORD(val);
\r
1158 Write(&val, sizeof(U_WORD), address);
\r
1162 void PC_WriteLong(U_LONG val, int address)
\r
1164 if (ImportMode != IMPORT_Importing)
\r
1166 MS_Message(MSG_DEBUG, "WL> %06d = %d\n", address, val);
\r
1167 val = MS_LittleULONG(val);
\r
1168 Write(&val, sizeof(U_LONG), address);
\r
1170 pc_LastAppendedCommand = PCD_NOP;
\r
1173 void PC_WriteString(char *string, int address)
\r
1175 if (ImportMode != IMPORT_Importing)
\r
1179 length = strlen(string)+1;
\r
1180 MS_Message(MSG_DEBUG, "WS> %06d = \"%s\" (%d bytes)\n",
\r
1181 address, string, length);
\r
1182 Write(string, length, address);
\r
1186 void PC_WriteCmd(pcd_t command, int address)
\r
1188 if (ImportMode != IMPORT_Importing)
\r
1190 MS_Message(MSG_DEBUG, "WC> %06d = #%d:%s\n", address,
\r
1191 command, PCDNames[command]);
\r
1192 command = MS_LittleULONG(command);
\r
1193 Write(&command, sizeof(U_LONG), address);
\r
1197 //==========================================================================
\r
1199 // PC_Skip functions
\r
1201 //==========================================================================
\r
1203 static void Skip(size_t size)
\r
1205 if (ImportMode != IMPORT_Importing)
\r
1207 if(pc_Address+size > BufferSize)
\r
1211 pc_BufferPtr += size;
\r
1212 pc_Address += size;
\r
1216 void PC_Skip(size_t size)
\r
1218 if (ImportMode != IMPORT_Importing)
\r
1220 MS_Message(MSG_DEBUG, "SD> %06d (skip %d bytes)\n", pc_Address, size);
\r
1226 void PC_SkipByte(void)
\r
1228 MS_Message(MSG_DEBUG, "SB> %06d (skip byte)\n", pc_Address);
\r
1229 Skip(sizeof(U_BYTE));
\r
1234 void PC_SkipWord(void)
\r
1236 MS_Message(MSG_DEBUG, "SW> %06d (skip word)\n", pc_Address);
\r
1237 Skip(sizeof(U_WORD));
\r
1241 void PC_SkipLong(void)
\r
1243 if (ImportMode != IMPORT_Importing)
\r
1245 MS_Message(MSG_DEBUG, "SL> %06d (skip long)\n", pc_Address);
\r
1246 Skip(sizeof(U_LONG));
\r
1250 //==========================================================================
\r
1252 // PC_PutMapVariable
\r
1254 //==========================================================================
\r
1256 void PC_PutMapVariable(int index, int value)
\r
1258 if(index < MAX_MAP_VARIABLES)
\r
1260 MapVariables[index].isString = pa_ConstExprIsString;
\r
1261 MapVariables[index].initializer = value;
\r
1262 MapVariablesInit = YES;
\r
1266 //==========================================================================
\r
1268 // PC_NameMapVariable
\r
1270 //==========================================================================
\r
1272 void PC_NameMapVariable(int index, symbolNode_t *sym)
\r
1274 if(index < MAX_MAP_VARIABLES)
\r
1276 MapVariables[index].name = sym->name;
\r
1277 MapVariables[index].imported = sym->imported;
\r
1281 //==========================================================================
\r
1285 //==========================================================================
\r
1287 void PC_AddScript(int inNumber, int argCount)
\r
1289 scriptInfo_t *script;
\r
1295 type = (inNumber & 65535) / 1000;
\r
1296 number = (inNumber & 65535) % 1000;
\r
1297 flags = (inNumber >> 16) & 65535;
\r
1301 HaveScriptFlags = YES;
\r
1304 for (i = 0; i < pc_ScriptCount; i++)
\r
1306 if (ScriptInfo[i].number == number)
\r
1308 ERR_Error(ERR_SCRIPT_ALREADY_DEFINED, YES);
\r
1311 if(pc_ScriptCount == MAX_SCRIPT_COUNT)
\r
1313 ERR_Error(ERR_TOO_MANY_SCRIPTS, YES);
\r
1317 script = &ScriptInfo[pc_ScriptCount];
\r
1318 script->number = number;
\r
1319 script->type = type;
\r
1320 script->address = (ImportMode == IMPORT_Importing) ? 0 : pc_Address;
\r
1321 script->argCount = argCount;
\r
1322 script->flags = flags;
\r
1323 script->srcLine = tk_Line;
\r
1324 script->imported = (ImportMode == IMPORT_Importing) ? YES : NO;
\r
1329 //==========================================================================
\r
1331 // PC_SetScriptVarCount
\r
1333 // Sets the number of local variables used by a script, including
\r
1336 //==========================================================================
\r
1338 void PC_SetScriptVarCount(int inNumber, int varCount)
\r
1344 type = (inNumber & 65535) / 1000;
\r
1345 number = (inNumber & 65535) % 1000;
\r
1347 for(i = 0; i < pc_ScriptCount; i++)
\r
1349 if(ScriptInfo[i].number == number)
\r
1351 ScriptInfo[i].varCount = varCount;
\r
1357 //==========================================================================
\r
1361 //==========================================================================
\r
1363 void PC_AddFunction(symbolNode_t *sym)
\r
1365 functionInfo_t *function;
\r
1367 if(pc_FunctionCount == MAX_FUNCTION_COUNT)
\r
1369 ERR_Error(ERR_TOO_MANY_FUNCTIONS, YES, NULL);
\r
1371 function = &FunctionInfo[pc_FunctionCount];
\r
1372 function->hasReturnValue = (U_BYTE)sym->info.scriptFunc.hasReturnValue;
\r
1373 function->argCount = (U_BYTE)sym->info.scriptFunc.argCount;
\r
1374 function->localCount = (U_BYTE)sym->info.scriptFunc.varCount;
\r
1375 function->name = STR_AppendToList (STRLIST_FUNCTIONS, sym->name);
\r
1376 function->address = sym->info.scriptFunc.address;
\r
1377 sym->info.scriptFunc.funcNumber = pc_FunctionCount;
\r
1378 pc_FunctionCount++;
\r
1381 //==========================================================================
\r
1385 //==========================================================================
\r
1387 void PC_AddArray(int index, int size)
\r
1390 ArraySizes[index] = size;
\r
1393 //==========================================================================
\r
1397 //==========================================================================
\r
1399 void PC_InitArray(int index, int *entries, boolean hasStrings)
\r
1403 // If the array is just initialized to zeros, then we don't need to
\r
1404 // remember the initializer.
\r
1405 for(i = 0; i < ArraySizes[index]; ++i)
\r
1407 if(entries[i] != 0)
\r
1412 if(i < ArraySizes[index])
\r
1414 ArrayInits[index] = MS_Alloc(ArraySizes[index]*sizeof(int), ERR_OUT_OF_MEMORY);
\r
1415 memcpy(ArrayInits[index], entries, ArraySizes[index]*sizeof(int));
\r
1417 ArrayOfStrings[index] = hasStrings;
\r
1420 //==========================================================================
\r
1424 //==========================================================================
\r
1426 int PC_AddImport(char *name)
\r
1428 if (NumImports >= MAX_IMPORTS)
\r
1430 ERR_Exit(ERR_TOO_MANY_IMPORTS, YES);
\r
1432 strncpy(Imports[NumImports], name, 8);
\r
1433 return NumImports++;
\r