Skip to content

Commit

Permalink
Better signs
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrw committed Oct 10, 2023
1 parent b0cb947 commit 52bf56a
Show file tree
Hide file tree
Showing 26 changed files with 259 additions and 177 deletions.
15 changes: 14 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"initCommands": ["command source ${workspaceRoot}/.lldbinit"],
"presentation": {
"clear": true // <-- this line
}
},
"console": "internalConsole"
},
{
"type": "lldb",
Expand All @@ -44,6 +45,18 @@
"cwd": "${workspaceFolder}",
"preLaunchTask": "Build Current File",
"initCommands": ["command source ${workspaceRoot}/.lldbinit"],
},
{
"type": "lldb",
"request": "attach",
"name": "Attatch to Enu",
"program": "${workspaceFolder}/vendor/godot/bin/godot.osx.opt.tools.arm64",
"windows": {
"program": "${workspaceFolder}/vendor/godot/bin/godot.windows.opt.tools.64"
},
"args": [],
"cwd": "${workspaceFolder}/app",
"initCommands": ["command source ${workspaceRoot}/.lldbinit"],
"presentation": {
"clear": true // <-- this line
}
Expand Down
3 changes: 2 additions & 1 deletion enu.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ else:
requires "https://github.com/dsrw/Nim#b7cf2e6"

requires "nim >= 1.6.10",
"https://github.com/arnetheduck/nim-results#f3c666a",
"https://github.com/dsrw/godot-nim#892c482",
"https://github.com/dsrw/model_citizen 0.18.3",
"https://github.com/dsrw/model_citizen 0.18.5",
"https://github.com/dsrw/nanoid.nim 0.2.1",
"cligen 1.6.0",
"https://github.com/treeform/pretty",
Expand Down
53 changes: 34 additions & 19 deletions src/controllers/script_controllers/host_bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,6 @@ proc drop_transform(unit: Unit): Transform =
else:
raise ObjectConversionDefect.init("Unknown unit type")

proc new_markdown_sign(self: Worker,
unit: Unit, pnode: PNode, markdown: string, title: string, width: float,
height: float, size: int, zoomable: bool, billboard: bool): Unit =

result = Sign.init(
markdown, title = title, owner = self.active_unit,
transform = drop_transform(unit), width = width, height = height,
size = size, zoomable = zoomable, billboard = billboard)

info "creating sign", id = result.id
self.map_unit(result, pnode)
unit.units.add(result)

proc reset(self: Unit, clear: bool) =
if clear:
if self of Build: Build(self).reset()
Expand Down Expand Up @@ -418,17 +405,44 @@ proc `tool=`(self: Unit, value: int) =
state.tool = Tools(value)

# Sign bindings

proc new_markdown_sign(self: Worker,
unit: Unit, pnode: PNode, message: string, more: string, width: float,
height: float, size: int, billboard: bool): Unit =

result = Sign.init(
message, more = more, owner = self.active_unit,
transform = drop_transform(unit), width = width, height = height,
size = size, billboard = billboard)

info "creating sign", id = result.id
self.map_unit(result, pnode)
unit.units.add(result)

proc update_markdown_sign(self: Worker, unit: Sign, message: string,
more: string, width: float, height: float, size: int, billboard: bool) =

unit.width = width
unit.height = height
unit.size = size
unit.billboard = billboard
unit.more = more
unit.message = message

proc `width=`(self: Sign, value: float) =
types.`width=`(self, value)
self.title_value.touch self.title
self.message_value.touch self.message

proc `height=`(self: Sign, value: float) =
types.`height=`(self, value)
self.title_value.touch self.title
self.message_value.touch self.message

proc `size=`(self: Sign, value: int) =
types.`size=`(self, value)
self.title_value.touch self.title
self.message_value.touch self.message

proc message(self: Sign): string =
self.message_value.value

proc open(self: Sign): bool =
state.open_sign == self
Expand Down Expand Up @@ -464,7 +478,8 @@ proc bridge_to_vm*(worker: Worker) =

result.bridged_from_vm "base_bridge_private",
link_dependency, action_running, `action_running=`, yield_script,
begin_turn, begin_move, sleep_impl, position_set, new_markdown_sign
begin_turn, begin_move, sleep_impl, position_set, new_markdown_sign,
update_markdown_sign

result.bridged_from_vm "bots",
play, all_bots
Expand All @@ -473,8 +488,8 @@ proc bridge_to_vm*(worker: Worker) =
drawing, `drawing=`, initial_position, save, restore, all_builds

result.bridged_from_vm "signs",
markdown, `markdown=`, title, `title=`, height, `height=`, width, `width=`,
size, `size=`, open, `open=`
message, `message=`, more, `more=`, height, `height=`, width, `width=`,
size, `size=`, open, `open=`, billboard, `billboard=`

result.bridged_from_vm "players",
playing, `playing=`, god, `god=`, flying, `flying=`, tool, `tool=`,
Expand Down
7 changes: 3 additions & 4 deletions src/controllers/script_controllers/scripting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ proc init_interpreter*[T](self: Worker, _: T) {.gcsafe.} =

if file_name.get_file_info != ctx.file_name.get_file_info:
(file_name, info) = extract_file_info msg
msg = msg.replace(re"unhandled exception:.*\) Error\: ", "")
else:
msg = msg.replace(re"(?ms);.*", "")
# msg = msg.replace(re"unhandled exception:.*\) Error\: ", "")
#else:
# msg = msg.replace(re"(?ms);.*", "")

var loc = \"{file_name}({int info.line},{int info.col})"
error "vm error", msg, file = ctx.file_name
Expand Down Expand Up @@ -211,4 +211,3 @@ proc eval*(self: Worker, unit: Unit, code: string) =
unit.script_ctx.timeout_at = get_mono_time() + script_timeout
{.gcsafe.}:
discard unit.script_ctx.eval(code)

5 changes: 4 additions & 1 deletion src/controllers/script_controllers/worker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ proc worker_thread(params: (ZenContext, GameState)) {.gcsafe.} =
if world_dir != "":
save_world(world_dir)
worker.unload_world()
if ResettingVM in state.local_flags:
worker.init_interpreter("")
worker.bridge_to_vm
world_dir = change.item.world_dir
if world_dir != "":
worker.load_world(world_dir)
Expand All @@ -229,7 +232,7 @@ proc worker_thread(params: (ZenContext, GameState)) {.gcsafe.} =
worker.load_script_and_dependents(player)

var sign = Sign.init("", "", width = 3, height = 2.05, owner = state.player,
size = 244, zoomable = true, billboard = true, text_only = true,
size = 244, billboard = true, text_only = true,
transform = Transform.init(origin = vec3(0, 3, 0)))

state.player.units += sign
Expand Down
10 changes: 2 additions & 8 deletions src/core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var state* {.threadvar.}: GameState
### Sugar ###

from sugar import dup, dump, collect
import std / [with, sets, times, monotimes, tables]
import std / [with, sets, monotimes, tables]
import std / times except seconds
import pkg / [pretty, flatty]

export with, sets, tables, pretty, flatty
Expand All @@ -27,13 +28,6 @@ export chronicles

template nim_filename*: string = instantiation_info(full_paths = true).filename

### times ###

export monotimes

proc seconds*(s: float): Duration {.inline.} =
init_duration(milliseconds = int(s * 1000))

### options ###

import options
Expand Down
5 changes: 4 additions & 1 deletion src/game.nim
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,12 @@ world: {state.world_name}
elif event.is_action_released("command_mode"):
state.pop_flag CommandMode
elif event.is_action_pressed("save_and_reload"):
state.pop_flag Playing
state.push_flag ResettingVM
self.switch_world(0)
state.pop_flag ResettingVM
self.get_tree().set_input_as_handled()
state.pop_flag Playing

elif event.is_action_pressed("pause"):
state.paused = not state.paused
elif event.is_action_pressed("clear_console"):
Expand Down
4 changes: 2 additions & 2 deletions src/models/bot_code_template.nim.nimf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#? stdtmpl
#proc bot_code_template(file_name, imports: string): string =
import system except echo, quit
import std / [strutils]
import types, class_macros, players, state_machine, base_api, bots, builds,
import types, class_macros, players, state_machine, base_api, bots, builds,
signs, bots_private
include overrides

let instance_global_by_default = true
var move_mode = 1
Expand Down
4 changes: 2 additions & 2 deletions src/models/build_code_template.nim.nimf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#? stdtmpl
#proc build_code_template(file_name, imports: string): string =
import system except echo, quit
import std / [strutils]
import types, class_macros, players, state_machine, base_api, bots, builds,
import types, class_macros, players, state_machine, base_api, bots, builds,
signs, builds_private
include overrides

let instance_global_by_default = false
var move_mode = 1
Expand Down
4 changes: 2 additions & 2 deletions src/models/players.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ proc open_code*(self: Player): string =
for unit in self.units:
if unit of Sign:
let unit = Sign(unit)
return unit.markdown
return unit.message

proc `open_code=`*(self: Player, code: string) =
for unit in self.units:
Expand All @@ -37,6 +37,6 @@ proc `open_code=`*(self: Player, code: string) =
if code == "":
unit.global_flags -= Visible
else:
unit.markdown = code
unit.message = code
unit.global_flags += Visible
return
2 changes: 1 addition & 1 deletion src/models/serializers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ proc `$`(self: Unit): string =
"basis": [
{elements.indent(6)}
],
"origin": {self.start_transform.origin}
"origin": {$self.start_transform.origin}
}},
"start_color": {self.start_color},
"edits": {{
Expand Down
13 changes: 5 additions & 8 deletions src/models/signs.nim
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import godotapi / spatial
import core, states, bots, builds, models / [colors, units]

proc init*(_: type Sign,
markdown: string, title = "", owner: Unit, transform = Transform.init,
width = 1.0, height = 1.0, size = 32, billboard = false,
zoomable = true, text_only = false): Sign =
proc init*(_: type Sign, message: string, more = "", owner: Unit,
transform = Transform.init, width = 1.0, height = 1.0, size = 32,
billboard = false, text_only = false): Sign =

let title = if title == "": markdown else: title
var self = Sign(
id: "sign_" & generate_id(),
markdown_value: ~markdown,
title_value: ~title,
message_value: ~message,
more_value: ~more,
glow_value: ~0.0,
width_value: ~width,
height_value: ~height,
size_value: ~size,
billboard_value: ~billboard,
zoomable_value: ~zoomable,
frame_created: state.frame_count,
color_value: ~action_colors[black],
start_transform: transform,
Expand Down
7 changes: 5 additions & 2 deletions src/models/units.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std / [os, sugar, with, tables]
import std / [os, with, tables]
import godotapi / spatial
from pkg / core / godotcoretypes import Basis
import core, models / [states, colors], libs / interpreters
Expand Down Expand Up @@ -125,10 +125,12 @@ method off_collision*(self: Model, partner: Model) {.base, gcsafe.} =

proc destroy*[T: Unit](self: T) =
ensure ?self

if self of Sign:
# :( Sign(self) will fail to compile if T is a Build or a Bot.
Sign(Unit(self)).owner = nil
for unit in self.units:
if unit of Sign:
Sign(Unit(unit)).owner = nil

# :( Parent isn't set properly for instances on the main thread
if self.parent == nil and "instance" notin self.id:
Expand All @@ -148,6 +150,7 @@ proc destroy*[T: Unit](self: T) =

if state.open_unit == self:
state.open_unit = nil

if Unit(state.open_sign) == self:
state.open_sign = nil

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/aim_target.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ gdobj AimTarget of Sprite3D:
state.pop_flag BlockTargetVisible
self.target_model = unit
# :(
if not (unit == nil or (unit of Sign and not Sign(unit).zoomable) or
if not (unit == nil or (unit of Sign and Sign(unit).more == "") or
(God notin state.local_flags and (unit of Bot or unit of Build) and
Lock in Unit(unit).find_root.global_flags)):

Expand Down
Loading

0 comments on commit 52bf56a

Please sign in to comment.