@@ -79,11 +79,23 @@ proc to_node(self: Worker, unit: Unit): PNode =
79
79
else :
80
80
ast.new_node (nkNilLit)
81
81
82
+ proc to_node [T: Unit ](self: Worker , units: seq [T]): PNode =
83
+ var node = ast.new_node (nkBracketExpr)
84
+ for unit in units:
85
+ if ? unit:
86
+ node.add self.to_node (unit)
87
+ else :
88
+ node.add ast.new_node (nk_nil_lit)
89
+ result = node
90
+
82
91
# Common bindings
83
92
84
93
proc press_action (self: Worker , name: string ) =
85
94
state.queued_action = name
86
95
96
+ proc register_template_node (self: Worker , pnode: PNode , name: string ) =
97
+ self.template_node_map[name] = pnode
98
+
87
99
proc register_active (self: Worker , pnode: PNode ) =
88
100
assert not self.active_unit.is_nil
89
101
self.map_unit (self.active_unit, pnode)
@@ -154,35 +166,36 @@ proc reset_level(self: Worker) =
154
166
state.config_value.value:
155
167
level_dir = current_level
156
168
157
- template query (
158
- T: type Unit , key: string , ctx: ScriptCtx , body: untyped
159
- ): untyped =
160
- var result : T
161
- if key in ctx.query_results:
162
- result = T (ctx.query_results[key][^ 1 ])
163
- else :
164
- let results = body
165
- if ? results:
166
- result = results[^ 1 ]
167
- ctx.query_results [key] = cast [seq [Unit ]](results)
168
- result
169
-
170
- proc ensure_unit (self: Worker , unit: Unit ) =
169
+ proc ensure_unit_impl [T: Unit ](self: Worker , unit: T) {.gcsafe .} =
171
170
if unit notin self.node_map:
172
- var node = self.node_map[self.active_unit ].copy_tree
171
+ var node = self.template_node_map[ $ T ].copy_tree
173
172
self.map_unit (unit, node)
174
173
175
- proc current_collider (self: Worker , unit: Unit , kind: string ): Unit =
176
- Unit .query (kind & " -collider" , unit.script_ctx):
177
- var colliders: seq [Unit ]
178
- state.units.value.walk_tree proc (other: Unit ) =
179
- if unit.collisions.value.any_it (it.id == other.id):
180
- if kind == " Unit" or kind == " Player" and other of Player or
181
- kind == " Bot" and other of Bot or kind == " Build" and other of Build or
182
- kind == " Sign" and other of Sign :
183
- colliders.add (other)
184
- self.ensure_unit (other)
185
- colliders
174
+ method ensure_exists (self: Unit , worker: Worker ) {.base , gcsafe .} =
175
+ raise_assert " ensure_unit not implemented for " & $ self.type
176
+
177
+ method ensure_exists (self: Player , worker: Worker ) =
178
+ worker.ensure_unit_impl (self)
179
+
180
+ method ensure_exists (self: Bot , worker: Worker ) =
181
+ worker.ensure_unit_impl (self)
182
+
183
+ method ensure_exists (self: Build , worker: Worker ) =
184
+ worker.ensure_unit_impl (self)
185
+
186
+ method ensure_exists (self: Sign , worker: Worker ) =
187
+ worker.ensure_unit_impl (self)
188
+
189
+ proc current_colliders * (self: Worker , unit: Unit , kind: string ): seq [Unit ] =
190
+ var colliders: seq [Unit ]
191
+ state.units.value.walk_tree proc (other: Unit ) =
192
+ if unit.collisions.value.any_it (it.id == other.id):
193
+ if kind == " Unit" or kind == " Player" and other of Player or
194
+ kind == " Bot" and other of Bot or kind == " Build" and other of Build or
195
+ kind == " Sign" and other of Sign :
196
+ colliders.add (other)
197
+ other.ensure_exists (self)
198
+ colliders
186
199
187
200
proc world_name (): string =
188
201
state.config.world
@@ -233,17 +246,6 @@ proc sleep_impl(self: Worker, ctx: ScriptCtx, seconds: float) =
233
246
ctx.last_ran = MonoTime .default
234
247
self.pause_script ()
235
248
236
- proc loop_finished (self: Worker , ctx: ScriptCtx ) =
237
- if ? ctx.query_results:
238
- let key = ctx.query_results.first_key
239
- let res = ctx.query_results[key].pop
240
- if not ? ctx.query_results[key]:
241
- ctx.query_results.del key
242
- if not ? ctx.query_results:
243
- self.sleep_impl (ctx, 0.0 )
244
- else :
245
- self.sleep_impl (ctx, 0.0 )
246
-
247
249
proc hit (unit_a: Unit , unit_b: Unit ): Vector3 =
248
250
for collision in unit_a.collisions:
249
251
if collision.id == unit_b.id:
@@ -253,38 +255,33 @@ proc find_all[T: Unit](worker: Worker, _: type T): seq[T] =
253
255
var units: seq [T]
254
256
state.units.value.walk_tree proc (unit: Unit ) =
255
257
if unit of T:
256
- worker. ensure_unit (unit )
258
+ unit. ensure_exists (worker )
257
259
units.add T (unit)
258
260
units
259
261
260
- proc all_players (ctx: ScriptCtx , worker: Worker ): Player =
261
- Player .query (" all-players" , ctx):
262
- worker.find_all (Player )
262
+ proc all_players (worker: Worker ): seq [Player ] =
263
+ worker.find_all (Player )
263
264
264
- proc all_bots (ctx: ScriptCtx , worker: Worker ): Bot =
265
- Bot .query (" all-bots" , ctx):
266
- worker.find_all (Bot )
265
+ proc all_bots (worker: Worker ): seq [Bot ] =
266
+ worker.find_all (Bot )
267
267
268
- proc all_builds (ctx: ScriptCtx , worker: Worker ): Build =
269
- Build .query (" all-builds" , ctx):
270
- worker.find_all (Build )
268
+ proc all_builds (worker: Worker ): seq [Build ] =
269
+ worker.find_all (Build )
271
270
272
- proc all_signs (ctx: ScriptCtx , worker: Worker ): Sign =
273
- Sign .query (" all-builds" , ctx):
274
- worker.find_all (Sign )
271
+ proc all_signs (worker: Worker ): seq [Sign ] =
272
+ worker.find_all (Sign )
275
273
276
- proc all_units (ctx: ScriptCtx , worker: Worker ): Unit =
277
- Unit .query (" all-builds" , ctx):
278
- worker.find_all (Unit )
274
+ proc all_units (worker: Worker ): seq [Unit ] =
275
+ worker.find_all (Unit )
279
276
280
- proc added * (_: type Player , ctx: ScriptCtx , worker: Worker ): Player =
281
- Player .query (" players-added" , ctx):
282
- collect:
283
- for player in worker.find_all (Player ):
284
- if player.frame_created == state.frame_count:
285
- player
277
+ proc added_units (worker: Worker ): seq [Unit ] =
278
+ collect:
279
+ for unit in worker.find_all (Unit ):
280
+ if unit.frame_created == state.frame_count:
281
+ unit
286
282
287
283
proc echo_console (msg: string ) =
284
+ echo (msg)
288
285
logger (" info" , msg & " \n " )
289
286
state.push_flag ConsoleVisible
290
287
@@ -461,24 +458,8 @@ proc reset(self: Unit, clear: bool) =
461
458
proc play (self: Bot , animation_name: string ) =
462
459
self.animation = animation_name
463
460
464
- proc all_units (T: type Unit , self: Worker ): PNode =
465
- var node = ast.new_node (nkBracketExpr)
466
- state.units.value.walk_tree proc (unit: Unit ) =
467
- if unit of T:
468
- # objects without scripts won't show up in the node map. Create
469
- # a new dummy object
470
- self.ensure_unit (unit)
471
- node.add self.to_node (unit)
472
- result = node
473
-
474
- proc all_bots (self: Worker ): PNode =
475
- Bot .all_units (self)
476
-
477
461
# Build bindings
478
462
479
- proc all_builds (self: Worker ): PNode =
480
- Build .all_units (self)
481
-
482
463
proc drawing (self: Build ): bool =
483
464
self.drawing
484
465
@@ -635,7 +616,7 @@ proc `open=`(self: Sign, value: bool) =
635
616
proc coding (self: Worker , unit: Unit ): Unit =
636
617
if unit == state.player:
637
618
if ? state.open_unit:
638
- self. ensure_unit ( state.open_unit)
619
+ state.open_unit. ensure_exists (self )
639
620
result = state.open_unit
640
621
641
622
proc `coding=` (self: Unit , value: Unit ) =
@@ -650,25 +631,25 @@ proc bridge_to_vm*(worker: Worker) =
650
631
result .bridged_from_vm " vm_bridge_utils" , get_last_error
651
632
652
633
result .bridged_from_vm " base_bridge" ,
653
- register_active, echo_console, new_instance, exec_instance, hit, exit ,
654
- global, `global=`, position, local_position, rotation, `rotation =`, id ,
655
- glow , `glow =`, speed, `speed =`, scale , `scale =`, velocity , `velocity =`,
656
- active_unit, color, `color=`, sees, start_position, wake, frame_count ,
657
- write_stack_trace, show, `show=`, frame_created, lock, `lock=`, reset ,
658
- press_action, load_level, level_name, world_name, reset_level ,
659
- current_collider, added, all_players, all_builds, all_bots, all_signs ,
660
- all_units, loop_finished
634
+ register_active, register_template_node, echo_console, new_instance ,
635
+ exec_instance, hit, exit, global, `global =`, position, local_position ,
636
+ rotation , `rotation =`, id, glow, `glow =`, speed , `speed =`, scale , `scale =`,
637
+ velocity, `velocity=`, active_unit, color, `color=`, sees, start_position,
638
+ wake, frame_count, write_stack_trace, show, `show=`, frame_created, lock,
639
+ `lock=`, reset, press_action, load_level, level_name, world_name,
640
+ reset_level, current_colliders, added_units, all_players, all_builds ,
641
+ all_bots, all_signs, all_units
661
642
662
643
result .bridged_from_vm " base_bridge_private" ,
663
644
link_dependency, action_running, `action_running=`, yield_script,
664
645
begin_turn, begin_move, sleep_impl, position_set, new_markdown_sign,
665
646
update_markdown_sign
666
647
667
- result .bridged_from_vm " bots" , play, all_bots
648
+ result .bridged_from_vm " bots" , play
668
649
669
650
result .bridged_from_vm " builds" ,
670
- drawing, `drawing=`, initial_position, save, restore, all_builds ,
671
- draw_position, draw_position_set
651
+ drawing, `drawing=`, initial_position, save, restore, draw_position ,
652
+ draw_position_set
672
653
673
654
result .bridged_from_vm " signs" ,
674
655
message, `message=`, more, `more=`, height, `height=`, width, `width=`,
0 commit comments