@@ -154,21 +154,35 @@ proc reset_level(self: Worker) =
154
154
state.config_value.value:
155
155
level_dir = current_level
156
156
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
+
157
170
proc ensure_unit (self: Worker , unit: Unit ) =
158
171
if unit notin self.node_map:
159
172
var node = self.node_map[self.active_unit].copy_tree
160
173
self.map_unit (unit, node)
161
174
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
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
172
186
173
187
proc world_name (): string =
174
188
state.config.world
@@ -219,11 +233,57 @@ proc sleep_impl(self: Worker, ctx: ScriptCtx, seconds: float) =
219
233
ctx.last_ran = MonoTime .default
220
234
self.pause_script ()
221
235
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
+
222
247
proc hit (unit_a: Unit , unit_b: Unit ): Vector3 =
223
248
for collision in unit_a.collisions:
224
249
if collision.id == unit_b.id:
225
250
return collision.normal.snapped (vec3 (1 , 1 , 1 ))
226
251
252
+ proc find_all [T: Unit ](worker: Worker , _: type T): seq [T] =
253
+ var units: seq [T]
254
+ state.units.value.walk_tree proc (unit: Unit ) =
255
+ if unit of T:
256
+ worker.ensure_unit (unit)
257
+ units.add T (unit)
258
+ units
259
+
260
+ proc all_players (ctx: ScriptCtx , worker: Worker ): Player =
261
+ Player .query (" all-players" , ctx):
262
+ worker.find_all (Player )
263
+
264
+ proc all_bots (ctx: ScriptCtx , worker: Worker ): Bot =
265
+ Bot .query (" all-bots" , ctx):
266
+ worker.find_all (Bot )
267
+
268
+ proc all_builds (ctx: ScriptCtx , worker: Worker ): Build =
269
+ Build .query (" all-builds" , ctx):
270
+ worker.find_all (Build )
271
+
272
+ proc all_signs (ctx: ScriptCtx , worker: Worker ): Sign =
273
+ Sign .query (" all-builds" , ctx):
274
+ worker.find_all (Sign )
275
+
276
+ proc all_units (ctx: ScriptCtx , worker: Worker ): Unit =
277
+ Unit .query (" all-builds" , ctx):
278
+ worker.find_all (Unit )
279
+
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
286
+
227
287
proc echo_console (msg: string ) =
228
288
logger (" info" , msg & " \n " )
229
289
state.push_flag ConsoleVisible
@@ -596,7 +656,8 @@ proc bridge_to_vm*(worker: Worker) =
596
656
active_unit, color, `color=`, sees, start_position, wake, frame_count,
597
657
write_stack_trace, show, `show=`, frame_created, lock, `lock=`, reset,
598
658
press_action, load_level, level_name, world_name, reset_level,
599
- current_collider
659
+ current_collider, added, all_players, all_builds, all_bots, all_signs,
660
+ all_units, loop_finished
600
661
601
662
result .bridged_from_vm " base_bridge_private" ,
602
663
link_dependency, action_running, `action_running=`, yield_script,
0 commit comments