make script_hook behave as a POD object, and fix some parameter passing

git-svn-id: svn://svn.icculus.org/fs2open/trunk/fs2_open@10020 387891d4-d844-0410-90c0-e4c51a9137d3
This commit is contained in:
Goober5000 authored and niffiwan committed 2013-11-10 18:28:57 +10:00
1 parent 90e7709da5
commit f883a4bc09
6 files changed
+71 -43

No files matched your search

+45 -27
View File
@@ -82,6 +82,22 @@ int scripting_state_inited = 0;
//*************************Scripting init and handling*************************
// the prototype for this is in pstypes.h, below the script_hook struct
void script_hook_init(script_hook *hook)
{
hook->o_language = 0;
hook->h_language = 0;
hook->o_index = -1;
hook->h_index = -1;
}
// ditto
bool script_hook_valid(script_hook *hook)
{
return hook->h_index >= 0;
}
void script_parse_table(const char *filename)
{
script_state *st = &Script_system;
@@ -100,23 +116,23 @@ void script_parse_table(const char *filename)
//int num = 42;
//Script_system.SetHookVar("Version", 'i', &num);
if(optional_string("$Global:")) {
Script_globalhook = st->ParseChunk("Global");
st->ParseChunk(&Script_globalhook, "Global");
}
if(optional_string("$Splash:")) {
Script_splashhook = st->ParseChunk("Splash");
st->ParseChunk(&Script_splashhook, "Splash");
}
if(optional_string("$GameInit:")) {
Script_gameinithook = st->ParseChunk("GameInit");
st->ParseChunk(&Script_gameinithook, "GameInit");
}
if(optional_string("$Simulation:")) {
Script_simulationhook = st->ParseChunk("Simulation");
st->ParseChunk(&Script_simulationhook, "Simulation");
}
if(optional_string("$HUD:")) {
Script_hudhook = st->ParseChunk("HUD");
st->ParseChunk(&Script_hudhook, "HUD");
}
required_string("#End");
@@ -156,10 +172,17 @@ void script_parse_table(const char *filename)
//Initializes the (global) scripting system, as well as any subsystems.
//script_close is handled by destructors
void script_init (void)
void script_init()
{
mprintf(("SCRIPTING: Beginning initialization sequence...\n"));
// first things first: init all script hooks, since they are PODs now, not classes...
script_hook_init(&Script_splashhook);
script_hook_init(&Script_simulationhook);
script_hook_init(&Script_hudhook);
script_hook_init(&Script_globalhook);
script_hook_init(&Script_gameinithook);
mprintf(("SCRIPTING: Beginning Lua initialization...\n"));
Script_system.CreateLuaState();
@@ -193,13 +216,13 @@ DCF(script, "Evaluates a line of scripting")
//*************************CLASS: ConditionedScript*************************
extern char Game_current_mission_filename[];
bool ConditionedHook::AddCondition(script_condition sc)
bool ConditionedHook::AddCondition(script_condition *sc)
{
for(int i = 0; i < MAX_HOOK_CONDITIONS; i++)
{
if(Conditions[i].condition_type == CHC_NONE)
{
Conditions[i] = sc;
Conditions[i] = *sc;
return true;
}
}
@@ -207,12 +230,12 @@ bool ConditionedHook::AddCondition(script_condition sc)
return false;
}
bool ConditionedHook::AddAction(script_action sa)
bool ConditionedHook::AddAction(script_action *sa)
{
if(!sa.hook.IsValid())
if(!script_hook_valid(&sa->hook))
return false;
Actions.push_back(sa);
Actions.push_back(*sa);
return true;
}
@@ -1225,11 +1248,10 @@ void script_state::ParseChunkSub(int *out_lang, int *out_index, char* debug_str)
}
}
script_hook script_state::ParseChunk(char* debug_str)
void script_state::ParseChunk(script_hook *dest, char *debug_str)
{
static int total_parse_calls = 0;
char debug_buf[128];
script_hook rval;
total_parse_calls++;
@@ -1240,7 +1262,7 @@ script_hook script_state::ParseChunk(char* debug_str)
sprintf(debug_str, "script_parse() count %d", total_parse_calls);
}
ParseChunkSub(&rval.h_language, &rval.h_index, debug_str);
ParseChunkSub(&dest->h_language, &dest->h_index, debug_str);
if(optional_string("+Override:"))
{
@@ -1248,11 +1270,9 @@ script_hook script_state::ParseChunk(char* debug_str)
char *debug_str_over = (char*)vm_malloc(bufSize);
strcpy_s(debug_str_over, bufSize, debug_str);
strcat_s(debug_str_over, bufSize, " override");
ParseChunkSub(&rval.o_language, &rval.o_index, debug_str_over);
ParseChunkSub(&dest->o_language, &dest->o_index, debug_str_over);
vm_free(debug_str_over);
}
return rval;
}
int script_parse_condition()
@@ -1282,14 +1302,13 @@ flag_def_list* script_parse_action()
bool script_state::ParseCondition(const char *filename)
{
ConditionedHook *chp = NULL;
script_condition sct;
int condition;
for(condition = script_parse_condition(); condition != CHC_NONE; condition = script_parse_condition())
{
//Clear it
sct = script_condition();
script_condition sct;
sct.condition_type = condition;
switch(condition)
{
case CHC_STATE:
@@ -1313,7 +1332,7 @@ bool script_state::ParseCondition(const char *filename)
chp = &ConditionalHooks[ConditionalHooks.size()-1];
}
if(!chp->AddCondition(sct))
if(!chp->AddCondition(&sct))
{
Warning(LOCATION, "Could not add condition to conditional hook in file '%s'; you may have more than %d", filename, MAX_HOOK_CONDITIONS);
}
@@ -1324,25 +1343,24 @@ bool script_state::ParseCondition(const char *filename)
return false;
}
script_action sat;
flag_def_list *action;
bool actions_added = false;
for(action = script_parse_action(); action != NULL; action = script_parse_action())
{
sat = script_action();
script_action sat;
sat.action_type = action->def;
//WMC - build error string
char *buf = (char *)vm_malloc(strlen(filename) + strlen(action->name) + 4);
sprintf(buf, "%s - %s", filename, action->name);
sat.hook = ParseChunk(buf);
ParseChunk(&sat.hook, buf);
//Free error string
vm_free(buf);
//Add the action
if(chp->AddAction(sat))
if(chp->AddAction(&sat))
actions_added = true;
}
@@ -1356,7 +1374,7 @@ bool script_state::ParseCondition(const char *filename)
return true;
}
//*************************CLASS: script_hook*************************
//*************************CLASS: script_state*************************
bool script_state::IsOverride(script_hook &hd)
{
if(hd.h_index < 0)