/* *** SCRIPT SYMBOL: [GUI] GUI::set_ZOrder *** */ void GUI_SetZOrder(ScriptGUI *tehgui, int z) { tehgui->gui->zorder = z; update_gui_zorder(); }
HGameInitError InitGameState(const LoadedGameEntities &ents, GameDataVersion data_ver) { const ScriptAPIVersion base_api = (ScriptAPIVersion)game.options[OPT_BASESCRIPTAPI]; const ScriptAPIVersion compat_api = (ScriptAPIVersion)game.options[OPT_SCRIPTCOMPATLEV]; if (data_ver >= kGameVersion_341) { const char * const scapi_names[] = {"v3.2.1", "v3.3.0", "v3.3.4", "v3.3.5", "v3.4.0", "v3.4.1", "v3.5.0"}; Debug::Printf(kDbgMsg_Init, "Requested script API: %s (%d), compat level: %s (%d)", base_api >= 0 && base_api <= kScriptAPI_Current ? scapi_names[base_api] : "unknown", base_api, compat_api >= 0 && compat_api <= kScriptAPI_Current ? scapi_names[compat_api] : "unknown", compat_api); } // If the game was compiled using unsupported version of the script API, // we warn about potential incompatibilities but proceed further. if (game.options[OPT_BASESCRIPTAPI] > kScriptAPI_Current) platform->DisplayAlert("Warning: this game requests a higher version of AGS script API, it may not run correctly or run at all."); // // 1. Check that the loaded data is valid and compatible with the current // engine capabilities. // if (game.numfonts == 0) return new GameInitError(kGameInitErr_NoFonts); if (game.audioClipTypeCount > MAX_AUDIO_TYPES) return new GameInitError(kGameInitErr_TooManyAudioTypes, String::FromFormat("Required: %d, max: %d", game.audioClipTypeCount, MAX_AUDIO_TYPES)); // // 2. Apply overriding config settings // // The earlier versions of AGS provided support for "upscaling" low-res // games (320x200 and 320x240) to hi-res (640x400 and 640x480 // respectively). The script API has means for detecting if the game is // running upscaled, and game developer could use this opportunity to setup // game accordingly (e.g. assign hi-res fonts, etc). // This feature is officially deprecated since 3.1.0, however the engine // itself still supports it, technically. // This overriding option re-enables "upscaling". It works ONLY for low-res // resolutions, such as 320x200 and 320x240. if (usetup.override_upscale) { if (game.GetDefaultResolution() == kGameResolution_320x200) game.SetDefaultResolution(kGameResolution_640x400); else if (game.GetDefaultResolution() == kGameResolution_320x240) game.SetDefaultResolution(kGameResolution_640x480); } // // 3. Allocate and init game objects // charextra = (CharacterExtras*)calloc(game.numcharacters, sizeof(CharacterExtras)); charcache = (CharacterCache*)calloc(1,sizeof(CharacterCache)*game.numcharacters+5); mls = (MoveList*)calloc(game.numcharacters + MAX_ROOM_OBJECTS + 1, sizeof(MoveList)); actSpsCount = game.numcharacters + MAX_ROOM_OBJECTS + 2; actsps = (Bitmap **)calloc(actSpsCount, sizeof(Bitmap *)); actspsbmp = (IDriverDependantBitmap**)calloc(actSpsCount, sizeof(IDriverDependantBitmap*)); actspswb = (Bitmap **)calloc(actSpsCount, sizeof(Bitmap *)); actspswbbmp = (IDriverDependantBitmap**)calloc(actSpsCount, sizeof(IDriverDependantBitmap*)); actspswbcache = (CachedActSpsData*)calloc(actSpsCount, sizeof(CachedActSpsData)); play.charProps.resize(game.numcharacters); HError err = InitAndRegisterGameEntities(); if (!err) return new GameInitError(kGameInitErr_EntityInitFail, err); LoadFonts(); // // 4. Initialize certain runtime variables // game_paused = 0; // reset the game paused flag ifacepopped = -1; if (game.saveGameFileExtension[0] != 0) saveGameSuffix.Format(".%s", game.saveGameFileExtension); else saveGameSuffix = ""; play.score_sound = game.scoreClipID; play.fade_effect = game.options[OPT_FADETYPE]; // // 5. Initialize runtime state of certain game objects // for (int i = 0; i < numguilabels; ++i) { // labels are not clickable by default guilabels[i].SetClickable(false); } play.gui_draw_order = (int*)calloc(game.numgui * sizeof(int), 1); update_gui_zorder(); calculate_reserved_channel_count(); // // 6. Register engine API exports // NOTE: we must do this before plugin start, because some plugins may // require access to script API at initialization time. // ccSetScriptAliveTimer(150000); ccSetStringClassImpl(&myScriptStringImpl); setup_script_exports(base_api, compat_api); // // 7. Start up plugins // pl_register_plugins(ents.PluginInfos); pl_startup_plugins(); // // 8. Create script modules // NOTE: we must do this after plugins, because some plugins may export // script symbols too. // gamescript = ents.GlobalScript; dialogScriptsScript = ents.DialogScript; numScriptModules = ents.ScriptModules.size(); scriptModules = ents.ScriptModules; AllocScriptModules(); if (create_global_script()) return new GameInitError(kGameInitErr_ScriptLinkFailed, ccErrorString); return HGameInitError::None(); }
// Final processing after successfully restoring from save HSaveError DoAfterRestore(const PreservedParams &pp, const RestoredData &r_data) { // Preserve whether the music vox is available play.separate_music_lib = pp.MusicVOX; // If they had the vox when they saved it, but they don't now if ((pp.SpeechVOX < 0) && (play.want_speech >= 0)) play.want_speech = (-play.want_speech) - 1; // If they didn't have the vox before, but now they do else if ((pp.SpeechVOX >= 0) && (play.want_speech < 0)) play.want_speech = (-play.want_speech) - 1; // recache queued clips for (int i = 0; i < play.new_music_queue_size; ++i) { play.new_music_queue[i].cachedClip = NULL; } // restore these to the ones retrieved from the save game const size_t dynsurf_num = Math::Min((size_t)MAX_DYNAMIC_SURFACES, r_data.DynamicSurfaces.size()); for (size_t i = 0; i < dynsurf_num; ++i) { dynamicallyCreatedSurfaces[i] = r_data.DynamicSurfaces[i]; } for (int i = 0; i < game.numgui; ++i) export_gui_controls(i); update_gui_zorder(); if (create_global_script()) { return new SavegameError(kSvgErr_GameObjectInitFailed, String::FromFormat("Unable to recreate global script: %s", ccErrorString.GetCStr())); } // read the global data into the newly created script if (r_data.GlobalScript.Data.get()) memcpy(gameinst->globaldata, r_data.GlobalScript.Data.get(), Math::Min((size_t)gameinst->globaldatasize, r_data.GlobalScript.Len)); // restore the script module data for (int i = 0; i < numScriptModules; ++i) { if (r_data.ScriptModules[i].Data.get()) memcpy(moduleInst[i]->globaldata, r_data.ScriptModules[i].Data.get(), Math::Min((size_t)moduleInst[i]->globaldatasize, r_data.ScriptModules[i].Len)); } setup_player_character(game.playercharacter); // Save some parameters to restore them after room load int gstimer=play.gscript_timer; int oldx1 = play.mboundx1, oldx2 = play.mboundx2; int oldy1 = play.mboundy1, oldy2 = play.mboundy2; // disable the queue momentarily int queuedMusicSize = play.music_queue_size; play.music_queue_size = 0; update_polled_stuff_if_runtime(); // load the room the game was saved in if (displayed_room >= 0) load_new_room(displayed_room, NULL); update_polled_stuff_if_runtime(); play.gscript_timer=gstimer; // restore the correct room volume (they might have modified // it with SetMusicVolume) thisroom.Options.MusicVolume = r_data.RoomVolume; Mouse::SetMoveLimit(Rect(oldx1, oldy1, oldx2, oldy2)); set_cursor_mode(r_data.CursorMode); set_mouse_cursor(r_data.CursorID); if (r_data.CursorMode == MODE_USE) SetActiveInventory(playerchar->activeinv); // ensure that the current cursor is locked spriteset.Precache(game.mcurs[r_data.CursorID].pic); #if (ALLEGRO_DATE > 19990103) set_window_title(play.game_name); #endif update_polled_stuff_if_runtime(); if (displayed_room >= 0) { for (int i = 0; i < MAX_ROOM_BGFRAMES; ++i) { if (r_data.RoomBkgScene[i]) { thisroom.BgFrames[i].Graphic = r_data.RoomBkgScene[i]; } } in_new_room=3; // don't run "enters screen" events // now that room has loaded, copy saved light levels in for (size_t i = 0; i < MAX_ROOM_REGIONS; ++i) { thisroom.Regions[i].Light = r_data.RoomLightLevels[i]; thisroom.Regions[i].Tint = r_data.RoomTintLevels[i]; } generate_light_table(); for (size_t i = 0; i < MAX_WALK_AREAS + 1; ++i) { thisroom.WalkAreas[i].ScalingFar = r_data.RoomZoomLevels1[i]; thisroom.WalkAreas[i].ScalingNear = r_data.RoomZoomLevels2[i]; } on_background_frame_change(); } gui_disabled_style = convert_gui_disabled_style(game.options[OPT_DISABLEOFF]); // restore the queue now that the music is playing play.music_queue_size = queuedMusicSize; if (play.digital_master_volume >= 0) System_SetVolume(play.digital_master_volume); // Run audio clips on channels // these two crossfading parameters have to be temporarily reset const int cf_in_chan = play.crossfading_in_channel; const int cf_out_chan = play.crossfading_out_channel; play.crossfading_in_channel = 0; play.crossfading_out_channel = 0; // NOTE: channels are array of MAX_SOUND_CHANNELS+1 size for (int i = 0; i <= MAX_SOUND_CHANNELS; ++i) { const RestoredData::ChannelInfo &chan_info = r_data.AudioChans[i]; if (chan_info.ClipID < 0) continue; if (chan_info.ClipID >= game.audioClipCount) { return new SavegameError(kSvgErr_GameObjectInitFailed, String::FromFormat("Invalid audio clip index: %d (clip count: %d).", chan_info.ClipID, game.audioClipCount)); } play_audio_clip_on_channel(i, &game.audioClips[chan_info.ClipID], chan_info.Priority, chan_info.Repeat, chan_info.Pos); if (channels[i] != NULL) { channels[i]->set_volume_direct(chan_info.VolAsPercent, chan_info.Vol); channels[i]->set_speed(chan_info.Speed); channels[i]->set_panning(chan_info.Pan); channels[i]->panningAsPercentage = chan_info.PanAsPercent; } } if ((cf_in_chan > 0) && (channels[cf_in_chan] != NULL)) play.crossfading_in_channel = cf_in_chan; if ((cf_out_chan > 0) && (channels[cf_out_chan] != NULL)) play.crossfading_out_channel = cf_out_chan; // If there were synced audio tracks, the time taken to load in the // different channels will have thrown them out of sync, so re-time it // NOTE: channels are array of MAX_SOUND_CHANNELS+1 size for (int i = 0; i <= MAX_SOUND_CHANNELS; ++i) { int pos = r_data.AudioChans[i].Pos; if ((pos > 0) && (channels[i] != NULL) && (channels[i]->done == 0)) { channels[i]->seek(pos); } } // TODO: investigate loop range for (int i = 1; i < MAX_SOUND_CHANNELS; ++i) { if (r_data.DoAmbient[i]) PlayAmbientSound(i, r_data.DoAmbient[i], ambient[i].vol, ambient[i].x, ambient[i].y); } for (int i = 0; i < game.numgui; ++i) { guibg[i] = BitmapHelper::CreateBitmap(guis[i].Width, guis[i].Height, game.GetColorDepth()); guibg[i] = ReplaceBitmapWithSupportedFormat(guibg[i]); } recreate_overlay_ddbs(); guis_need_update = 1; play.ignore_user_input_until_time = 0; update_polled_stuff_if_runtime(); pl_run_plugin_hooks(AGSE_POSTRESTOREGAME, 0); if (displayed_room < 0) { // the restart point, no room was loaded load_new_room(playerchar->room, playerchar); playerchar->prevroom = -1; first_room_initialization(); } if ((play.music_queue_size > 0) && (cachedQueuedMusic == NULL)) { cachedQueuedMusic = load_music_from_disk(play.music_queue[0], 0); } // test if the playing music was properly loaded if (current_music_type > 0) { if (crossFading > 0 && !channels[crossFading] || crossFading <= 0 && !channels[SCHAN_MUSIC]) { current_music_type = 0; } } set_game_speed(r_data.FPS); return HSaveError::None(); }