Skip to content

Commit 8ec4013

Browse files
committed
Run all code on the server
1 parent a7f0420 commit 8ec4013

File tree

9 files changed

+64
-28
lines changed

9 files changed

+64
-28
lines changed

ios/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.a
22
Enu.pck
33
**/xcuserdata
4+
build/

src/controllers/script_controllers/host_bridge.nim

+24-11
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ proc reset_level(self: Worker) =
154154
state.config_value.value:
155155
level_dir = current_level
156156

157+
proc ensure_unit(self: Worker, unit: Unit) =
158+
if unit notin self.node_map:
159+
var node = self.node_map[self.active_unit].copy_tree
160+
self.map_unit(unit, node)
161+
162+
proc current_collider(self: Unit, kind: string): Unit =
163+
var collider: Unit
164+
state.units.value.walk_tree proc(unit: Unit) =
165+
if self.collisions.value.any_it(it.id == unit.id):
166+
if kind == "Unit" or kind == "Player" and unit of Player or
167+
kind == "Bot" and unit of Bot or kind == "Build" and unit of Build or
168+
kind == "Sign" and unit of Sign:
169+
collider = unit
170+
return
171+
result = collider
172+
157173
proc world_name(): string =
158174
state.config.world
159175

@@ -391,9 +407,7 @@ proc all_units(T: type Unit, self: Worker): PNode =
391407
if unit of T:
392408
# objects without scripts won't show up in the node map. Create
393409
# a new dummy object
394-
if unit notin self.node_map:
395-
var node = self.node_map[self.active_unit].copy_tree
396-
self.map_unit(unit, node)
410+
self.ensure_unit(unit)
397411
node.add self.to_node(unit)
398412
result = node
399413

@@ -425,9 +439,8 @@ proc draw_position_set(self: Build, position: Vector3) =
425439
(position - self.position).local_to(self.parent)
426440

427441
proc save(self: Build, name: string) =
428-
self.save_points[name] = (
429-
self.draw_transform, self.color_value.value, self.drawing
430-
)
442+
self.save_points[name] =
443+
(self.draw_transform, self.color_value.value, self.drawing)
431444

432445
proc restore(self: Build, name: string) =
433446
(self.draw_transform, self.color_value.value, self.drawing) =
@@ -561,10 +574,9 @@ proc `open=`(self: Sign, value: bool) =
561574

562575
proc coding(self: Worker, unit: Unit): Unit =
563576
if unit == state.player:
564-
if ?state.open_unit and state.open_unit notin self.node_map:
565-
var node = self.node_map[self.active_unit].copy_tree
566-
self.map_unit(state.open_unit, node)
567-
result = state.open_unit
577+
if ?state.open_unit:
578+
self.ensure_unit(state.open_unit)
579+
result = state.open_unit
568580

569581
proc `coding=`(self: Unit, value: Unit) =
570582
state.open_unit = value
@@ -583,7 +595,8 @@ proc bridge_to_vm*(worker: Worker) =
583595
glow, `glow=`, speed, `speed=`, scale, `scale=`, velocity, `velocity=`,
584596
active_unit, color, `color=`, sees, start_position, wake, frame_count,
585597
write_stack_trace, show, `show=`, frame_created, lock, `lock=`, reset,
586-
press_action, load_level, level_name, world_name, reset_level
598+
press_action, load_level, level_name, world_name, reset_level,
599+
current_collider
587600

588601
result.bridged_from_vm "base_bridge_private",
589602
link_dependency, action_running, `action_running=`, yield_script,

src/controllers/script_controllers/scripting.nim

+8-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ proc script_error*(self: Worker, unit: Unit, e: ref VMQuit) =
3636
msg = e.parent.msg
3737

3838
info "vm error", msg, file = unit.script_ctx.file_name
39-
state.err(\"[url=unit://{unit.id}]{msg}[/url]")
40-
unit.local_flags += HighlightError
39+
for i, error in unit.errors.value:
40+
var error = error
41+
error.log = true
42+
unit.errors[i] = error
43+
44+
unit.global_flags += HighlightError
4145
unit.global_flags -= ScriptInitializing
4246
unit.ensure_visible
43-
state.push_flags ConsoleVisible
4447

4548
proc init_interpreter*[T](self: Worker, _: T) {.gcsafe.} =
4649
private_access ScriptCtx
@@ -77,7 +80,7 @@ proc init_interpreter*[T](self: Worker, _: T) {.gcsafe.} =
7780
error "File not found handling error", file_name
7881

7982
var loc = \"{file_name}({int info.line},{int info.col})"
80-
errors.add (msg, info, loc)
83+
errors.add (msg, info, loc, false)
8184
ctx.exit_code = error_code
8285
raise (ref VMQuit)(info: info, msg: msg, location: loc)
8386

@@ -131,7 +134,7 @@ proc load_script*(self: Worker, unit: Unit, timeout = script_timeout) =
131134
try:
132135
self.active_unit = unit
133136
unit.errors.clear
134-
unit.local_flags -= HighlightError
137+
unit.global_flags -= HighlightError
135138

136139
if not state.paused:
137140
let module_name = ctx.script.split_file.name

src/controllers/script_controllers/worker.nim

+14-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ proc advance_unit(self: Worker, unit: Unit, timeout: MonoTime): bool =
6262
proc change_code(self: Worker, unit: Unit, code: Code) =
6363
debug "code changing", unit = unit.id
6464
unit.errors.clear
65-
unit.local_flags -= HighlightError
65+
unit.global_flags -= HighlightError
6666
if ?unit.script_ctx and unit.script_ctx.running and not ?unit.clone_of:
6767
unit.collect_garbage
6868

@@ -74,7 +74,6 @@ proc change_code(self: Worker, unit: Unit, code: Code) =
7474
edit.destroy
7575

7676
unit.reset()
77-
state.pop_flag ConsoleVisible
7877
if LoadingScript notin state.local_flags and code.nim.strip == "":
7978
self.interpreter.reset_module(unit.script_ctx.module_name)
8079
debug "reset module", module = unit.script_ctx.module_name
@@ -97,10 +96,9 @@ proc change_code(self: Worker, unit: Unit, code: Code) =
9796
proc watch_code(self: Worker, unit: Unit) =
9897
unit.code_value.changes:
9998
if added or touched:
100-
if change.item.owner == "" or change.item.owner == Zen.thread_ctx.id:
99+
if Server in state.local_flags:
101100
save_level(state.config.level_dir)
102101
self.change_code(unit, change.item)
103-
elif Server in state.local_flags:
104102
if change.item.nim == "":
105103
remove_file unit.script_ctx.script
106104
else:
@@ -114,6 +112,18 @@ proc watch_code(self: Worker, unit: Unit) =
114112
except VMQuit as e:
115113
self.script_error(unit, e)
116114

115+
unit.zids.add:
116+
unit.errors.changes:
117+
if unit.code.owner == Zen.thread_ctx.id:
118+
if added and change.item.log:
119+
state.err(
120+
\"[url=unit://{unit.id}]{change.item.msg} {unit.errors.len}[/url]"
121+
)
122+
state.push_flags ConsoleVisible
123+
124+
if removed:
125+
state.pop_flags ConsoleVisible
126+
117127
if unit.script_ctx.is_nil:
118128
unit.script_ctx =
119129
ScriptCtx.init(owner = unit, interpreter = self.interpreter)

src/nodes/build_node.nim

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import pkg/godot except print, Color
33
import
44
godotapi/[
55
node, voxel_terrain, voxel_mesher_blocky, voxel_tool, voxel_library,
6-
shader_material, resource_loader, packed_scene, ray_cast
6+
shader_material, resource_loader, packed_scene, ray_cast,
77
]
88
import core, models/[units, builds, colors], gdutils
99
import ./queries
@@ -80,7 +80,7 @@ gdobj BuildNode of VoxelTerrain:
8080

8181
if Highlight in self.model.local_flags or
8282
(
83-
HighlightError in self.model.local_flags and
83+
HighlightError in self.model.global_flags and
8484
self.error_highlight_on
8585
):
8686
m.set_shader_param("emission_energy", highlight_glow.to_variant)
@@ -168,15 +168,17 @@ gdobj BuildNode of VoxelTerrain:
168168
elif Resetting.removed:
169169
self.generator = gdnew[VoxelGeneratorFlat]()
170170
self.track_chunks()
171-
172-
self.model.local_flags.watch:
173-
if HighlightError.added:
171+
elif HighlightError.added:
174172
self.toggle_error_highlight_at = get_mono_time() + error_flash_time
175173
self.error_highlight_on = true
174+
self.set_highlight
176175
elif HighlightError.removed:
177176
self.toggle_error_highlight_at = MonoTime.high
178177
self.error_highlight_on = false
179-
if change.item in [Highlight, HighlightError]:
178+
self.set_highlight
179+
180+
self.model.local_flags.watch:
181+
if change.item == Highlight:
180182
self.set_highlight
181183

182184
state.local_flags.watch:

src/types.nim

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ type
5555
Hover
5656
TargetMoved
5757
Highlight
58-
HighlightError
5958
Hide
6059

6160
GlobalModelFlags* = enum
@@ -66,6 +65,7 @@ type
6665
ScriptInitializing
6766
Dirty
6867
Resetting
68+
HighlightError
6969

7070
Tools* = enum
7171
CodeMode
@@ -129,7 +129,8 @@ type
129129
emission_colors*: seq[godot.Color]
130130
edits*: ZenTable[string, ZenTable[Vector3, VoxelInfo]]
131131

132-
ScriptErrors* = ZenSeq[tuple[msg: string, info: TLineInfo, location: string]]
132+
ScriptErrors* =
133+
ZenSeq[tuple[msg: string, info: TLineInfo, location: string, log: bool]]
133134

134135
SightQuery* = object
135136
target*: Unit

vmlib/enu/base_api.nim

+3
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ template t*(enu_target: NegativeNode) =
371371
template hit*(node: Unit): Vector3 =
372372
enu_target.hit(node)
373373

374+
proc hit*[T: Unit](_: type T): T =
375+
T(active_unit().current_collider($T))
376+
374377
proc distance*(position: Vector3): float =
375378
position.distance_to(active_unit().position)
376379

vmlib/enu/base_bridge.nim

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ bridged_to_host:
4949
proc reset_level*()
5050
proc level_name*(): string
5151
proc world_name*(): string
52+
proc current_collider*(self: Unit, name: string): Unit
5253

5354
# TODO: These should be in base_bridge_private, but are currently needed outside of base_api.
5455
proc echo_console*(msg: string)

vmlib/enu/types.nim

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ type
4343

4444
Sign* = ref object of Unit
4545

46+
Player* = ref object of Unit
47+
4648
Colors* = enum
4749
eraser
4850
blue

0 commit comments

Comments
 (0)