From f1b11ee3581d682ea78fd3dfb05fcfc69c7351ca Mon Sep 17 00:00:00 2001 From: Scott Wadden Date: Fri, 13 Oct 2023 00:10:28 -0300 Subject: [PATCH] Fixed `press_action`. Added `load_world`. --- .../script_controllers/host_bridge.nim | 9 ++- src/controllers/script_controllers/worker.nim | 5 +- src/game.nim | 69 ++++--------------- src/models/serializers.nim | 25 +++++++ src/models/states.nim | 3 +- src/models/units.nim | 2 +- src/types.nim | 24 ++++++- src/ui/right_panel.nim | 3 +- vmlib/enu/base_bridge.nim | 1 + 9 files changed, 77 insertions(+), 64 deletions(-) diff --git a/src/controllers/script_controllers/host_bridge.nim b/src/controllers/script_controllers/host_bridge.nim index 5b2d27b5..cf0c2061 100644 --- a/src/controllers/script_controllers/host_bridge.nim +++ b/src/controllers/script_controllers/host_bridge.nim @@ -7,7 +7,7 @@ import pkg / compiler / ast except new_node import pkg / compiler / [vmdef, renderer, msgs] import godotapi / [spatial, ray_cast] -import core, models / [states, bots, builds, units, colors, signs] +import core, models / [states, bots, builds, units, colors, signs, serializers] import libs / [interpreters, eval] import shared / errors @@ -80,6 +80,9 @@ proc to_node(self: Worker, unit: Unit): PNode = proc press_action(self: Worker, name: string) = state.queued_action = name +proc load_world(name: string) = + change_loaded_world(name) + proc register_active(self: Worker, pnode: PNode) = assert not self.active_unit.is_nil self.map_unit(self.active_unit, pnode) @@ -455,7 +458,7 @@ proc `open=`(self: Sign, value: bool) = proc coding(self: Worker, unit: Unit): Unit = if unit == state.player: - if state.open_unit notin self.node_map: + if ?state.open_unit and state.open_unit notin self.node_map: var node = self.node_map[self.active_unit].copy_tree self.map_unit(state.open_unit, node) result = state.open_unit @@ -478,7 +481,7 @@ proc bridge_to_vm*(worker: Worker) = glow, `glow=`, speed, `speed=`, scale, `scale=`, velocity, `velocity=`, active_unit, color, `color=`, sees, start_position, wake, frame_count, write_stack_trace, show, `show=`, frame_created, lock, `lock=`, reset, - press_action + press_action, load_world result.bridged_from_vm "base_bridge_private", link_dependency, action_running, `action_running=`, yield_script, diff --git a/src/controllers/script_controllers/worker.nim b/src/controllers/script_controllers/worker.nim index 94bf17dd..85a3d3bc 100644 --- a/src/controllers/script_controllers/worker.nim +++ b/src/controllers/script_controllers/worker.nim @@ -106,8 +106,11 @@ proc watch_code(self: Worker, unit: Unit) = unit.eval_value.changes: if added or touched and change.item != "": - self.eval(unit, change.item) unit.eval = "" + try: + self.eval(unit, change.item) + except VMQuit as e: + self.script_error(unit, e) if unit.script_ctx.is_nil: unit.script_ctx = ScriptCtx.init(owner = unit, diff --git a/src/game.nim b/src/game.nim index d79ac3e3..2e9b531d 100644 --- a/src/game.nim +++ b/src/game.nim @@ -8,29 +8,6 @@ import godotapi / [input, input_event, gd_os, node, scene_tree, packed_scene, import core, types, globals, controllers, models / [serializers, units, colors] -type - UserConfig = object - font_size: Option[int] - dock_icon_size: Option[float] - world_prefix: Option[string] - world: Option[string] - show_stats: Option[bool] - god_mode: Option[bool] - mega_pixels: Option[float] - start_full_screen: Option[bool] - semicolon_as_colon: Option[bool] - listen_address: Option[string] - connect_address: Option[string] - player_color: Option[colortypes.Color] - channel_size: Option[int] - walk_speed*: Option[int] - fly_speed*: Option[int] - alt_walk_speed*: Option[int] - alt_fly_speed*: Option[int] - mouse_sensitivity*: Option[float] - gamepad_sensitivity*: Option[float] - invert_gamepad_y_axis*: Option[bool] - if file_exists(".env"): dotenv.overload() @@ -74,14 +51,6 @@ world: {state.world_name} self.rescale_at = MonoTime.high self.rescale() - if state.queued_action != "": - var ev = gdnew[InputEventAction]() - ev.action = state.queued_action - ev.pressed = true - state.queued_action = "" - - parse_input_event(ev) - proc rescale*() = let vp = self.get_viewport().size state.scale_factor = sqrt(state.config.mega_pixels * @@ -109,20 +78,6 @@ world: {state.world_name} action_add_event(name, event) erase_action(action) - proc load_user_config(): UserConfig = - let - work_dir = get_user_data_dir() - config_file = join_path(work_dir, "config.json") - if file_exists(config_file): - let opt = Joptions(allow_missing_keys: true, allow_extra_keys: true) - result.from_json(read_file(config_file).parse_json, opt) - - proc save_user_config(config: UserConfig) = - let - work_dir = get_user_data_dir() - config_file = join_path(work_dir, "config.json") - write_file(config_file, jsonutils.to_json(config).pretty) - proc init* = self.process_priority = -100 @@ -132,7 +87,7 @@ world: {state.world_name} else: get_screen_dpi(-1).float / 96.0 - var initial_user_config = self.load_user_config() + var initial_user_config = load_user_config(get_user_data_dir()) var chan_size = initial_user_config.channel_size || 5000 @@ -196,16 +151,16 @@ world: {state.world_name} self.node_controller = NodeController.init self.script_controller = ScriptController.init - self.save_user_config(uc) + save_user_config(uc) proc set_font_size(size: int) = if state.config.font_size != size: - var user_config = self.load_user_config() + var user_config = load_user_config() state.config_value.value: font_size = size user_config.font_size = some(size) - self.save_user_config(user_config) + save_user_config(user_config) let theme_holder = self.find_node("LeftPanel").as(Container) @@ -255,6 +210,14 @@ world: {state.world_name} state.push_flag MouseCaptured + state.queued_action_value.changes: + if added and change.item != "": + var ev = gdnew[InputEventAction]() + ev.action = state.queued_action + ev.pressed = true + state.queued_action = "" + parse_input_event(ev) + method on_size_changed() = self.rescale_at = get_mono_time() @@ -269,13 +232,7 @@ world: {state.world_name} except ValueError: 1 num += diff - var user_config = self.load_user_config() - config.world = prefix & $num - state.world_name = config.world - user_config.world = some(config.world) - self.save_user_config(user_config) - config.world_dir = join_path(config.work_dir, config.world) - state.config = config + change_loaded_world(prefix & $num) else: # force a reload of the current world let current_world = state.config.world_dir diff --git a/src/models/serializers.nim b/src/models/serializers.nim index 8f778a25..d27cc3e0 100644 --- a/src/models/serializers.nim +++ b/src/models/serializers.nim @@ -203,6 +203,31 @@ proc load_units(parent: Unit) = else: unit.global_flags -= ScriptInitializing +proc load_user_config*(dir = ""): UserConfig = + var work_dir = dir + if not ?dir: + work_dir = state.config.work_dir + let config_file = join_path(work_dir, "config.json") + if file_exists(config_file): + let opt = Joptions(allow_missing_keys: true, allow_extra_keys: true) + result.from_json(read_file(config_file).parse_json, opt) + +proc save_user_config*(config: UserConfig) = + let + work_dir = state.config.work_dir + config_file = join_path(work_dir, "config.json") + write_file(config_file, jsonutils.to_json(config).pretty) + +proc change_loaded_world*(world: string) = + var config = state.config + var user_config = load_user_config() + config.world = world + state.world_name = config.world + user_config.world = some(config.world) + save_user_config(user_config) + config.world_dir = join_path(config.work_dir, config.world) + state.config = config + proc unload_world*(worker: Worker) = state.global_flags += LoadingWorld state.push_flag LoadingScript diff --git a/src/models/states.nim b/src/models/states.nim index ea3fb442..8b20ff9c 100644 --- a/src/models/states.nim +++ b/src/models/states.nim @@ -126,7 +126,8 @@ proc init*(_: type GameState): GameState = console: ConsoleModel(log: ~(seq[string], flags)), open_sign_value: ~(Sign, flags), wants: ~(seq[LocalStateFlags], flags), - world_name_value: ~("", id = "world_name") + world_name_value: ~("", id = "world_name"), + queued_action_value: ~("", flags) ) result = self self.open_unit_value.changes: diff --git a/src/models/units.nim b/src/models/units.nim index 97062e06..37da0d63 100644 --- a/src/models/units.nim +++ b/src/models/units.nim @@ -167,7 +167,7 @@ proc destroy_impl*(self: Bot | Build | Sign) = state.open_unit = nil when self is Sign: - if state.open_sign == self: + if state.open_sign_value.valid and state.open_sign == self: state.open_sign = nil Zen.thread_ctx.free(self) diff --git a/src/types.nim b/src/types.nim index 1b8d1367..7776db51 100644 --- a/src/types.nim +++ b/src/types.nim @@ -63,7 +63,7 @@ type frame_count*: int skip_block_paint*: bool open_sign_value*: ZenValue[Sign] - queued_action*: string + queued_action_value*: ZenValue[string] scale_factor*: float worker_ctx_name*: string world_name_value*: ZenValue[string] @@ -181,6 +181,28 @@ type gamepad_sensitivity*: float invert_gamepad_y_axis*: bool + UserConfig* = object + font_size*: Option[int] + dock_icon_size*: Option[float] + world_prefix*: Option[string] + world*: Option[string] + show_stats*: Option[bool] + god_mode*: Option[bool] + mega_pixels*: Option[float] + start_full_screen*: Option[bool] + semicolon_as_colon*: Option[bool] + listen_address*: Option[string] + connect_address*: Option[string] + player_color*: Option[colortypes.Color] + channel_size*: Option[int] + walk_speed*: Option[int] + fly_speed*: Option[int] + alt_walk_speed*: Option[int] + alt_fly_speed*: Option[int] + mouse_sensitivity*: Option[float] + gamepad_sensitivity*: Option[float] + invert_gamepad_y_axis*: Option[bool] + Code* = object owner*: string nim*: string diff --git a/src/ui/right_panel.nim b/src/ui/right_panel.nim index 68a7c326..9d51b80a 100644 --- a/src/ui/right_panel.nim +++ b/src/ui/right_panel.nim @@ -34,7 +34,8 @@ gdobj RightPanel of MarginContainer: self.label.markdown = md(sign, change.item) self.label.update if removed and change.item != nil: - change.item.more_value.untrack(self.zid) + if change.item.more_value.valid: + change.item.more_value.untrack(self.zid) state.pop_flags DocsFocused, DocsVisible state.local_flags.changes: diff --git a/vmlib/enu/base_bridge.nim b/vmlib/enu/base_bridge.nim index c8583f69..fb15d64c 100644 --- a/vmlib/enu/base_bridge.nim +++ b/vmlib/enu/base_bridge.nim @@ -38,6 +38,7 @@ bridged_to_host: proc `lock=`*(self: Unit, value: bool) proc reset*(self: Unit, clear = false) proc press_action*(name: string) + proc load_world*(name: string) # TODO: These should be in base_bridge_private, but are currently needed outside of base_api. proc echo_console*(msg: string)