Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/enuPublic

Commitccb1b35

Browse files
committed
Reworked collection queries
1 parentf7794f9 commitccb1b35

File tree

5 files changed

+79
-46
lines changed

5 files changed

+79
-46
lines changed

‎src/controllers/script_controllers/host_bridge.nim‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,11 @@ proc sleep_impl(self: Worker, ctx: ScriptCtx, seconds: float) =
246246
ctx.last_ran=MonoTime.default
247247
self.pause_script()
248248

249-
prochit(unit_a:Unit, unit_b:Unit):Vector3=
250-
for collisionin unit_a.collisions:
249+
prochit(self:Unit, unit_b:Unit):Vector3=
250+
ifnot?unit_b:
251+
return
252+
253+
for collisionin self.collisions:
251254
if collision.id== unit_b.id:
252255
return collision.normal.snapped(vec3(1,1,1))
253256

‎vmlib/enu/base_api.nim‎

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -517,32 +517,42 @@ template `\`*(s: string): string =
517517
f.remove_suffix("\n\n")
518518
f
519519

520-
template`?`*(self:ref):bool=
521-
not self.is_nil
520+
procinit*[T](_:typeQueryAnswer, results: open_array[T]):QueryAnswer[T]=
521+
result.results= results.to_seq
522+
result.truthy= results.len>0
522523

523-
template`?`*(self:object):bool=
524-
self!= self.type.default
524+
template`?`*[T:ref](self:T):Answer[T]=
525+
Answer[T](truthy:not self.is_nil)
525526

526-
template`?`*[T](option:Option[T]):bool=
527-
option.is_some
527+
template`?`*[T:object](self: T):Answer[T]=
528+
Answer[T](truthy: self!= self.type.default)
528529

529-
template`?`*(self:SomeNumber):bool=
530-
self!=0
530+
template`?`*[T](option:Option[T]):Answer[T]=
531+
if option.is_some:
532+
Answer[T](truthy:true)
533+
else:
534+
Answer[T]()
535+
536+
template`?`*[T:SomeNumber](self:SomeNumber):Answer[T]=
537+
Answer[T](truthy: self!=0)
531538

532-
template`?`*(self:string):bool=
533-
self!=""
539+
template`?`*(self:string):Answer[string]=
540+
Answer[string](truthy:self!="")
534541

535-
template`?`*[T](self:open_array[T]):bool=
536-
self.len>0
542+
template`?`*[T: open_array](self:T):Answer[T]=
543+
Answer[T](truthy:self.len>0)
537544

538-
template`?`*(self:Table):bool=
539-
self.len>0
545+
template`?`*[T:Table](self:T):Answer[T]=
546+
Answer[T](truthy:self.len>0)
540547

541-
template`?`*[T](self:set[T]):bool=
542-
self.card>0
548+
template`?`*[T:set](self:T):Answer[T]=
549+
Answer[T](truthy:self.card>0)
543550

544-
template`?`*[T](self:HashSet[T]):bool=
545-
self.card>0
551+
template`?`*[T:HashSet](self: T):Answer[T]=
552+
Answer[T](truthy: self.card>0)
553+
554+
converterto_bool*(src:Answer):bool=
555+
src.truthy
546556

547557
proc`or`*[T:notbool](a, b: T): T=
548558
if?a:
@@ -581,37 +591,50 @@ template query(T: type Unit, key: string, body: untyped): untyped =
581591
unit.query_results[key]= results.map_it(Unit(it))
582592
result
583593

584-
procall*(_:typeBot):Bot=
585-
Bot.query("all-bots"):
586-
all_bots()
594+
iteratoritems*[T](self:QueryAnswer[T]): T=
595+
for itemin self.results:
596+
yield item
597+
598+
procall*(_:typeBot):QueryAnswer[Bot]=
599+
QueryAnswer.initall_bots()
587600

588-
procall*(T:typeBuild):Build=
589-
Build.query("all-builds"):
590-
all_builds()
601+
procall*(T:typeBuild):QueryAnswer[Build]=
602+
QueryAnswer.initall_builds()
591603

592-
procall*(_:typeSign):Sign=
593-
Sign.query("all-signs"):
594-
all_signs()
604+
procall*(_:typeSign):QueryAnswer[Sign]=
605+
QueryAnswer.initall_signs()
595606

596-
procall*(_:typePlayer):Player=
597-
Player.query("all-players"):
598-
all_players()
607+
procall*(_:typePlayer):QueryAnswer[Player]=
608+
QueryAnswer.initall_players()
599609

600-
procall*(_:typeUnit):Unit=
601-
Unit.query("all-units"):
602-
all_units()
610+
procall*(_:typeUnit):QueryAnswer[Unit]=
611+
QueryAnswer.initall_units()
603612

604-
procadded*(_:typePlayer):Player=
605-
Player.query("added-players"):
606-
added_units().filter_it(itofPlayer).map_it(Player(it))
613+
procadded*(_:typePlayer):QueryAnswer[Player]=
614+
QueryAnswer.initadded_units().filter_it(itofPlayer).map_it(Player(it))
607615

608-
prochit*(_:typePlayer):Player=
609-
Player.query("Player-hit"):
610-
active_unit().current_colliders("Player").map_it(Player(it))
616+
prochit*(_:typePlayer):QueryAnswer[Player]=
617+
QueryAnswer.initactive_unit().current_colliders("Player").map_it(Player(it))
611618

612619
procregister_type[T:Unit](unit: T)=
613620
register_template_node(unit,$T)
614621

622+
template`as`*[T](answer:QueryAnswer[T], name:untyped):bool=
623+
if loop_context.is_nil:
624+
raise
625+
ObjectConversionDefect.init("`as` can only be used inside an action loop")
626+
let ans= answer
627+
let key=$T&"-"&$instantiation_info()
628+
varresult=false
629+
let name {.inject.}: T=
630+
if ans.truthy:
631+
result=true
632+
T.query(key):
633+
ans.results
634+
else:
635+
T.default
636+
result
637+
615638
register_typePlayer()
616639
register_typeBot()
617640
register_typeBuild()

‎vmlib/enu/base_bridge.nim‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ bridged_to_host:
3333
proc`global=`*(self:Unit, global:bool)
3434
procrotation*(self:Unit):float
3535
proc`rotation=`*(self:Unit, degrees:float)
36+
prochit*(self:Unit, node:Unit):Vector3
3637
proc`velocity=`*(self:Unit, velocity:Vector3)
3738
procvelocity*(self:Unit):Vector3
3839
proccolor*(self:Unit):Colors

‎vmlib/enu/loops.nim‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
varcontext:Context
1+
varloop_context:Context
22

33
me.advance_state_machine=proc():bool=
44
result=
5-
ifnotcontext.is_nil:
6-
context.advance()
5+
ifnotloop_context.is_nil:
6+
loop_context.advance()
77
else:
88
true
99

1010
procloop_started(ctx:Context, main_loop:bool)=
1111
if main_loop:
12-
context= ctx
12+
loop_context= ctx
1313

1414
procloop_ended(ctx:Context, main_loop:bool)=
1515
if main_loop:
16-
context=nil
16+
loop_context=nil

‎vmlib/enu/types.nim‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ type
3131
sign*:Sign
3232
query_results*:Table[string,seq[Unit]]
3333

34+
Answer*[T]=objectofRootObj
35+
truthy*:bool
36+
37+
QueryAnswer*[T]=objectofAnswer[T]
38+
results*:seq[T]
39+
3440
World*=refobjectofRootObj
3541

3642
PositionOffset*=object

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp