static void ifel_eval (NCDCall call) { size_t count = NCDCall_ArgCount(&call); if (count % 2 == 0) { return FunctionLog(&call, BLOG_ERROR, "ifel: need an odd number of arguments"); } NCDValRef value; size_t j = 0; while (1) { NCDValRef arg = NCDCall_EvalArg(&call, j, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { return; } if (j == count - 1) { value = arg; break; } NCDValRef arg2 = NCDCall_EvalArg(&call, j + 1, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg2)) { return; } int arg_val; if (!ncd_read_boolean(arg, &arg_val)) { return FunctionLog(&call, BLOG_ERROR, "ifel: bad condition"); } if (arg_val) { value = arg2; break; } j += 2; } NCDCall_SetResult(&call, value); }
static void decode_value_eval (NCDCall call) { if (NCDCall_ArgCount(&call) != 1) { return FunctionLog(&call, BLOG_ERROR, "decode_value: need one argument"); } // Evaluate the string to a temporary mem, not ResMem. // Otherwise the ResMem could get resized while we're // parsing a string within it, and boom. NCDValMem temp_mem; NCDValMem_Init(&temp_mem, NCDCall_Iparams(&call)->string_index); NCDValRef arg = NCDCall_EvalArg(&call, 0, &temp_mem); if (NCDVal_IsInvalid(arg)) { goto fail1; } if (!NCDVal_IsString(arg)) { FunctionLog(&call, BLOG_ERROR, "decode_value: argument not a string"); goto fail1; } NCDValRef value; int res = NCDValParser_Parse(NCDVal_StringMemRef(arg), NCDCall_ResMem(&call), &value); if (!res) { FunctionLog(&call, BLOG_ERROR, "decode_value: NCDValParser_Parse failed"); goto fail1; } NCDCall_SetResult(&call, value); fail1: NCDValMem_Free(&temp_mem); }
/** * Constructor, also prepares external files. */ GAMECONTROL::GAMECONTROL(){ scene_accesible = game_finished = save_exists = new_game_started = false; work_scene = nullptr; game_states = nullptr; script = new_game = save_game = nullptr; next_chapter = next_scene = L""; volume = globals->GetGameSettings()->GetValue<int>(L"sound_volume"); script = new CMarkup; if (script->Load(globals->GetGameSettings()->GetValue<std::wstring>(L"script_file")) == false) { std::wstring fail_msg; fail_msg.append(L"Loding of the file ").append(globals->GetGameSettings()->GetValue<std::wstring>(L"script_file")).append(L" failed with error ").append(script->GetError()); TermLog(fail_msg.c_str()); } new_game = new CMarkup; FunctionLog(L"Loading newgame.xml failed. File is probably missing." , new_game->Load(L"newgame.xml") , true); // TO BE CHANGED (UNNESCESSARY TO FAIL save_game = new CMarkup; FunctionLog(L"Loading savegame.xml failed. File is probably missing.", save_game->Load(L"savegame.xml"), true); game_states = new STATES(); FunctionLog(L"game_states allocation failed, probably out of memory", (game_states != nullptr), true); FunctionLog(L"Application couldn't find GAME tag in script.xml, file is probably damaged", script->FindElem(L"GAME"), true); script->IntoElem(); }
static void bool_not_eval (NCDCall call, int negate, char const *name) { if (NCDCall_ArgCount(&call) != 1) { return FunctionLog(&call, BLOG_ERROR, "%s: need one argument", name); } NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { return; } int arg_val; if (!ncd_read_boolean(arg, &arg_val)) { return FunctionLog(&call, BLOG_ERROR, "%s: bad argument", name); } int res = (arg_val != negate); NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); }
static void encode_value_eval (NCDCall call) { if (NCDCall_ArgCount(&call) != 1) { return FunctionLog(&call, BLOG_ERROR, "encode_value: need one argument"); } NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { return; } char *str = NCDValGenerator_Generate(arg); if (!str) { return FunctionLog(&call, BLOG_ERROR, "encode_value: NCDValGenerator_Generate failed"); } NCDCall_SetResult(&call, NCDVal_NewString(NCDCall_ResMem(&call), str)); free(str); }
static void identity_eval (NCDCall call) { if (NCDCall_ArgCount(&call) != 1) { return FunctionLog(&call, BLOG_ERROR, "identity: need one argument"); } NCDCall_SetResult(&call, NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call))); }
static void if_eval (NCDCall call) { if (NCDCall_ArgCount(&call) != 3) { return FunctionLog(&call, BLOG_ERROR, "if: need three arguments"); } NCDValRef cond = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(cond)) { return; } int cond_val; if (!ncd_read_boolean(cond, &cond_val)) { return FunctionLog(&call, BLOG_ERROR, "if: bad condition"); } int eval_arg = 2 - cond_val; NCDCall_SetResult(&call, NCDCall_EvalArg(&call, eval_arg, NCDCall_ResMem(&call))); }
/* Building the scene at the position. */ bool GAMECONTROL::CreateScene(){ SCR->Clear(Gdiplus::Color(0,0,0)); // Covers what was left from previous session; if(work_scene != 0){ delete work_scene; work_scene = 0; } FunctionLog("script->FindElem(L\"TYPE\")", "Scene type couldn't be resolved, <TYPE> tag is missing.", script->FindElem(L"TYPE"), true); if(script->GetData() == L"video"){ Log("Creating VIDEO scene"); work_scene = new VIDEO(script); } else if(script->GetData() == L"dialogue"){ Log("Creating DIALOGUE scene"); work_scene = new DIALOGUE(script); } else if(script->GetData() == L"interactive"){ Log("Creating INTERACTIVE scene"); work_scene = new INTERACTIVE(script); } if(work_scene == 0){ TermLog("Application couldn't create next scene, file script.xml is probably damaged"); return false; } return true; }
/* Constructor, also prepares external files. */ GAMECONTROL::GAMECONTROL(){ scene_accesible = game_finished = new_game_started = false; total_game_time = 0; work_scene = 0; script = 0; video_paused = true; script = new CMarkup; FunctionLog("Loading rough.xml failed. File is probably missing." , script->Load(L"rough.xml") , true); work_scene = new SCENE(); // Cannot be used FunctionLog("work_scene allocation failed, probably out of memory", (work_scene != 0), true); FunctionLog("Application couldn't find SCENE tag in rough.xml, file is probably damaged", script->FindElem(L"SCENE"), true); script->IntoElem(); video_paused = !CreateScene(); }
static void integer_compare_eval (NCDCall call, integer_compare_func func) { if (NCDCall_ArgCount(&call) != 2) { return FunctionLog(&call, BLOG_ERROR, "integer_compare: need two arguments"); } uintmax_t ints[2]; for (int i = 0; i < 2; i++) { NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { return; } if (!ncd_read_uintmax(arg, &ints[i])) { return FunctionLog(&call, BLOG_ERROR, "integer_compare: wrong value"); } } int res = func(ints[0], ints[1]); NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); }
static void perchar_eval (NCDCall call, perchar_func func) { if (NCDCall_ArgCount(&call) != 1) { return FunctionLog(&call, BLOG_ERROR, "tolower: need one argument"); } NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { return; } if (!NCDVal_IsString(arg)) { return FunctionLog(&call, BLOG_ERROR, "tolower: argument not a string"); } NCDValRef value = NCDVal_NewStringUninitialized(NCDCall_ResMem(&call), NCDVal_StringLength(arg)); if (NCDVal_IsInvalid(value)) { return; } char *out_data = (char *)NCDVal_StringData(value); MEMREF_LOOP_CHARS(NCDVal_StringMemRef(arg), i, ch, { out_data[i] = func(ch); })
static int concat_recurser (ExpString *estr, NCDValRef arg, NCDCall const *call) { if (NCDVal_IsString(arg)) { if (!ExpString_AppendBinaryMr(estr, NCDVal_StringMemRef(arg))) { FunctionLog(call, BLOG_ERROR, "ExpString_AppendBinaryMr failed"); return 0; } } else if (NCDVal_IsList(arg)) { size_t count = NCDVal_ListCount(arg); for (size_t i = 0; i < count; i++) { if (!concat_recurser(estr, NCDVal_ListGet(arg, i), call)) { return 0; } } } else { FunctionLog(call, BLOG_ERROR, "concat: value is not a string or list"); return 0; } return 1; }
static void imp_eval (NCDCall call) { if (NCDCall_ArgCount(&call) != 2) { return FunctionLog(&call, BLOG_ERROR, "imp: need two arguments"); } int res = 0; for (size_t i = 0; i < 2; i++) { NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { return; } int arg_val; if (!ncd_read_boolean(arg, &arg_val)) { return FunctionLog(&call, BLOG_ERROR, "imp: bad argument"); } if (arg_val == i) { res = 1; break; } } NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); }
static void concatlist_eval (NCDCall call) { NCDValRef args_list; if (!ncd_eval_func_args(&call, NCDCall_ResMem(&call), &args_list)) { return; } size_t arg_count = NCDVal_ListCount(args_list); bsize_t elem_count = bsize_fromsize(0); for (size_t i = 0; i < arg_count; i++) { NCDValRef arg = NCDVal_ListGet(args_list, i); if (!NCDVal_IsList(arg)) { return FunctionLog(&call, BLOG_ERROR, "concatlist: argument is not a list"); } elem_count = bsize_add(elem_count, bsize_fromsize(NCDVal_ListCount(arg))); } if (elem_count.is_overflow) { return FunctionLog(&call, BLOG_ERROR, "concatlist: count overflow"); } NCDValRef res = NCDVal_NewList(NCDCall_ResMem(&call), elem_count.value); if (NCDVal_IsInvalid(res)) { return; } for (size_t i = 0; i < arg_count; i++) { NCDValRef arg = NCDVal_ListGet(args_list, i); size_t arg_list_count = NCDVal_ListCount(arg); for (size_t j = 0; j < arg_list_count; j++) { NCDValRef copy = NCDVal_NewCopy(NCDCall_ResMem(&call), NCDVal_ListGet(arg, j)); if (NCDVal_IsInvalid(copy)) { return; } if (!NCDVal_ListAppend(res, copy)) { return; } } } NCDCall_SetResult(&call, res); }
static void value_compare_eval (NCDCall call, value_compare_func func) { if (NCDCall_ArgCount(&call) != 2) { return FunctionLog(&call, BLOG_ERROR, "value_compare: need two arguments"); } NCDValRef vals[2]; for (int i = 0; i < 2; i++) { vals[i] = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(vals[i])) { return; } } int res = func(NCDVal_Compare(vals[0], vals[1])); NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); }
/** * Creation of a new game - this function is called from the menu. */ bool GAMECONTROL::CreateNewGame(){ script->ResetPos(); FunctionLog(L"Application couldn't find GAME tag in script.xml, file is probably damaged", script->FindElem(L"GAME"), true); script->IntoElem(); game_finished = false; if(!LoadStarting()) return false; if(!SetPositionForNextScene()) return false; if(!CreateScene()) return false; new_game_started = true; return true; }
static void and_or_eval (NCDCall call, int is_and, char const *name) { size_t count = NCDCall_ArgCount(&call); int res = is_and; for (size_t i = 0; i < count; i++) { NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { return; } int arg_val; if (!ncd_read_boolean(arg, &arg_val)) { return FunctionLog(&call, BLOG_ERROR, "%s: bad argument", name); } if (arg_val != is_and) { res = !is_and; break; } } NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); }
/** * Building the scene at the position. */ bool GAMECONTROL::CreateScene(){ globals->GetScreenObject()->Clear(Gdiplus::Color(0,0,0)); // Covers what was left from previous session; if(work_scene != nullptr){ delete work_scene; work_scene = nullptr; } FunctionLog(L"script->FindElem(L\"TYPE\")", L"Scene type couldn't be resolved, <TYPE> tag is missing.", script->FindElem(L"TYPE"), true); if(script->GetData() == L"video"){ Log(L" Creating VIDEO scene"); work_scene = new VIDEO(script, game_states); } else if(script->GetData() == L"dialogue"){ Log(L" Creating DIALOGUE scene"); work_scene = new DIALOGUE(script, game_states); } else if(script->GetData() == L"interactive"){ Log(L" Creating INTERACTIVE scene"); work_scene = new INTERACTIVE(script, game_states); } else if(script->GetData() == L"finish"){ Log(L" Creating LAST VIDEO scene"); work_scene = new VIDEO(script, game_states); game_finished = true; } if(work_scene == 0){ TermLog(L"Application couldn't create next scene, file script.xml is probably damaged"); return false; } // Save the game SaveToFile(next_chapter, next_scene); work_scene->SetVolume(volume); return true; }
/** * Finds out what the next scene should be. */ bool GAMECONTROL::DetermineNext(){ /* Function gets data from file and tests state variables against file tests*/ FunctionLog(L"script->FindElem(L\"NEXT\")", L"Script file is damaged", script->FindElem(L"NEXT"), true); script->IntoElem(); bool found = false; while(script->FindElem(L"CASE") && found != true){ // goes through all the cases script->IntoElem(); if(CheckTests()){ // All tests passed if(!script->FindElem(L"NEXTCHAPTER")) TermLog(L"Application couldn't find next chapter in script.xml, file is probably damaged"); next_chapter = script->GetData(); if(!script->FindElem(L"NEXTSCENE")) TermLog(L"Application couldn't find next chapter in script.xml, file is probably damaged"); next_scene = script->GetData(); found = true; } script->OutOfElem(); } if(found == false){ // No path passed - using deafault continue if(!script->FindElem(L"DEFAULT")) TermLog(L"Application couldn't find default continuing scene in script.xml, file is probably damaged"); script->IntoElem(); if(!script->FindElem(L"NEXTCHAPTER")) TermLog(L"Application couldn't find next chapter in script.xml, file is probably damaged"); next_chapter = script->GetData(); if(!script->FindElem(L"NEXTSCENE")) TermLog(L"Application couldn't find next scene in script.xml, file is probably damaged"); next_scene = script->GetData(); script->OutOfElem(); } script->OutOfElem(); return true; }
static void concat_eval (NCDCall call) { ExpString estr; if (!ExpString_Init(&estr)) { FunctionLog(&call, BLOG_ERROR, "ExpString_Init failed"); goto fail0; } size_t count = NCDCall_ArgCount(&call); for (size_t i = 0; i < count; i++) { NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { goto fail1; } if (!concat_recurser(&estr, arg, &call)) { goto fail1; } } NCDCall_SetResult(&call, NCDVal_NewStringBinMr(NCDCall_ResMem(&call), ExpString_GetMr(&estr))); fail1: ExpString_Free(&estr); fail0: return; }
static void error_eval (NCDCall call) { FunctionLog(&call, BLOG_ERROR, "error: failing"); }