mt-lang 0.3.1 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/milk_tea/base.rb +1 -1
- data/lib/milk_tea/lsp/diagnostics.rb +1 -1
- data/lib/milk_tea/tooling/linter/imports_platform.rb +14 -3
- data/lib/milk_tea/tooling/linter/release_rules.rb +24 -3
- data/lib/milk_tea/tooling/linter/visitors.rb +1 -0
- data/std/blackboard.mt +100 -0
- data/std/cmd.mt +132 -0
- data/std/color.mt +245 -0
- data/std/graph.mt +237 -0
- data/std/htn.mt +254 -0
- data/std/input.mt +132 -0
- data/std/iter.mt +304 -0
- data/std/mem/ptr.mt +12 -0
- data/std/noise.mt +201 -0
- data/std/pool.mt +82 -0
- data/std/signal.mt +66 -0
- data/std/simd.mt +100 -0
- data/std/steering.mt +132 -0
- data/std/timer.mt +81 -0
- data/std/tween.mt +374 -0
- data/std/utility.mt +208 -0
- metadata +17 -2
data/std/utility.mt
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
## Utility AI — score-based action selection.
|
|
2
|
+
##
|
|
3
|
+
## Agents evaluate candidate actions by scoring them against
|
|
4
|
+
## the current context. The action with the highest score wins.
|
|
5
|
+
## Considerations return 0..1 values, combined via geometric
|
|
6
|
+
## mean (default), average, min, max, or multiplicative.
|
|
7
|
+
##
|
|
8
|
+
## import std.utility as util
|
|
9
|
+
## var selector = util.Selector[MyContext].create()
|
|
10
|
+
## selector.add_action(util.Action[MyContext].create("fight", util.CombineMode.geometric))
|
|
11
|
+
## let chosen = selector.select(ptr_of(ctx))
|
|
12
|
+
## if chosen.selected: execute(chosen.name)
|
|
13
|
+
|
|
14
|
+
import std.vec as vec
|
|
15
|
+
import std.math as math
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
public enum CombineMode: ubyte
|
|
19
|
+
geometric = 0
|
|
20
|
+
average = 1
|
|
21
|
+
minimum = 2
|
|
22
|
+
maximum = 3
|
|
23
|
+
multiplicative = 4
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
public struct Consideration[Context]:
|
|
27
|
+
name: str
|
|
28
|
+
score: fn(context: ptr[Context]) -> float
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
public struct Action[Context]:
|
|
32
|
+
name: str
|
|
33
|
+
considerations: vec.Vec[Consideration[Context]]
|
|
34
|
+
combine: CombineMode
|
|
35
|
+
weight: float
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
public struct Selector[Context]:
|
|
39
|
+
actions: vec.Vec[Action[Context]]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
public struct Selection:
|
|
43
|
+
index: ptr_uint
|
|
44
|
+
name: str
|
|
45
|
+
score: float
|
|
46
|
+
selected: bool
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# ── internal helpers ──
|
|
50
|
+
|
|
51
|
+
function combine_scores(mode: CombineMode, scores: ref[vec.Vec[float]]) -> float:
|
|
52
|
+
let n = scores.len()
|
|
53
|
+
if n == 0:
|
|
54
|
+
return 0.0
|
|
55
|
+
|
|
56
|
+
if mode == CombineMode.geometric:
|
|
57
|
+
var product: float = 1.0
|
|
58
|
+
var i: ptr_uint = 0
|
|
59
|
+
while i < n:
|
|
60
|
+
let s_ptr = scores.get(i) else:
|
|
61
|
+
return 0.0
|
|
62
|
+
let s = unsafe: read(s_ptr)
|
|
63
|
+
if s <= 0.0:
|
|
64
|
+
return 0.0
|
|
65
|
+
product = product * s
|
|
66
|
+
i += 1
|
|
67
|
+
let exponent = 1.0 / float<-(n)
|
|
68
|
+
return float<-(math.pow(double<-(product), double<-(exponent)))
|
|
69
|
+
|
|
70
|
+
if mode == CombineMode.multiplicative:
|
|
71
|
+
var product: float = 1.0
|
|
72
|
+
var i: ptr_uint = 0
|
|
73
|
+
while i < n:
|
|
74
|
+
let s_ptr = scores.get(i) else:
|
|
75
|
+
return 0.0
|
|
76
|
+
let s = unsafe: read(s_ptr)
|
|
77
|
+
if s <= 0.0:
|
|
78
|
+
return 0.0
|
|
79
|
+
product = product * s
|
|
80
|
+
i += 1
|
|
81
|
+
return product
|
|
82
|
+
|
|
83
|
+
if mode == CombineMode.average:
|
|
84
|
+
var sum: float = 0.0
|
|
85
|
+
var i: ptr_uint = 0
|
|
86
|
+
while i < n:
|
|
87
|
+
let s_ptr = scores.get(i) else:
|
|
88
|
+
break
|
|
89
|
+
sum = sum + unsafe: read(s_ptr)
|
|
90
|
+
i += 1
|
|
91
|
+
return sum / float<-(n)
|
|
92
|
+
|
|
93
|
+
if mode == CombineMode.minimum:
|
|
94
|
+
var best: float = 1.0
|
|
95
|
+
var i: ptr_uint = 0
|
|
96
|
+
while i < n:
|
|
97
|
+
let s_ptr = scores.get(i) else:
|
|
98
|
+
break
|
|
99
|
+
let s = unsafe: read(s_ptr)
|
|
100
|
+
if s < best:
|
|
101
|
+
best = s
|
|
102
|
+
i += 1
|
|
103
|
+
return best
|
|
104
|
+
|
|
105
|
+
# CombineMode.maximum
|
|
106
|
+
var best: float = 0.0
|
|
107
|
+
var i: ptr_uint = 0
|
|
108
|
+
while i < n:
|
|
109
|
+
let s_ptr = scores.get(i) else:
|
|
110
|
+
break
|
|
111
|
+
let s = unsafe: read(s_ptr)
|
|
112
|
+
if s > best:
|
|
113
|
+
best = s
|
|
114
|
+
i += 1
|
|
115
|
+
return best
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# ── public types: extensions ──
|
|
119
|
+
|
|
120
|
+
extending Consideration[Context]:
|
|
121
|
+
public static function create(
|
|
122
|
+
name: str,
|
|
123
|
+
score: fn(context: ptr[Context]) -> float
|
|
124
|
+
) -> Consideration[Context]:
|
|
125
|
+
return Consideration[Context](name = name, score = score)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
extending Action[Context]:
|
|
129
|
+
public static function create(
|
|
130
|
+
name: str,
|
|
131
|
+
combine: CombineMode
|
|
132
|
+
) -> Action[Context]:
|
|
133
|
+
return Action[Context](
|
|
134
|
+
name = name,
|
|
135
|
+
considerations = vec.Vec[Consideration[Context]].create(),
|
|
136
|
+
combine = combine,
|
|
137
|
+
weight = 1.0
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
public editable function set_weight(w: float) -> void:
|
|
142
|
+
this.weight = w
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
public editable function add_consideration(c: Consideration[Context]) -> void:
|
|
146
|
+
this.considerations.push(c)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
public function evaluate(context: ptr[Context]) -> float:
|
|
150
|
+
let n = this.considerations.len()
|
|
151
|
+
if n == 0:
|
|
152
|
+
return 0.0
|
|
153
|
+
|
|
154
|
+
var scores = vec.Vec[float].with_capacity(n)
|
|
155
|
+
defer scores.release()
|
|
156
|
+
|
|
157
|
+
for entry in this.considerations:
|
|
158
|
+
unsafe:
|
|
159
|
+
scores.push(read(entry).score(context))
|
|
160
|
+
|
|
161
|
+
return combine_scores(this.combine, ref_of(scores)) * this.weight
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
public editable function release() -> void:
|
|
165
|
+
this.considerations.release()
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
extending Selector[Context]:
|
|
169
|
+
public static function create() -> Selector[Context]:
|
|
170
|
+
return Selector[Context](
|
|
171
|
+
actions = vec.Vec[Action[Context]].create()
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
public editable function add_action(action: Action[Context]) -> void:
|
|
176
|
+
this.actions.push(action)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
public function action_count() -> ptr_uint:
|
|
180
|
+
return this.actions.len()
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
public function select(context: ptr[Context]) -> Selection:
|
|
184
|
+
var best_index: ptr_uint = 0
|
|
185
|
+
var best_name: str = ""
|
|
186
|
+
var best_score: float = -1.0
|
|
187
|
+
var found: bool = false
|
|
188
|
+
|
|
189
|
+
var i: ptr_uint = 0
|
|
190
|
+
for entry in this.actions:
|
|
191
|
+
unsafe:
|
|
192
|
+
let action = read(entry)
|
|
193
|
+
let score = action.evaluate(context)
|
|
194
|
+
if score > 0.0 and score > best_score:
|
|
195
|
+
best_score = score
|
|
196
|
+
best_index = i
|
|
197
|
+
best_name = action.name
|
|
198
|
+
found = true
|
|
199
|
+
i += 1
|
|
200
|
+
|
|
201
|
+
return Selection(index = best_index, name = best_name, score = best_score, selected = found)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
public editable function release() -> void:
|
|
205
|
+
for entry in this.actions:
|
|
206
|
+
unsafe:
|
|
207
|
+
read(entry).release()
|
|
208
|
+
this.actions.release()
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mt-lang
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Long (Teefan) Tran
|
|
@@ -401,6 +401,7 @@ files:
|
|
|
401
401
|
- std/binary.mt
|
|
402
402
|
- std/binary_heap.mt
|
|
403
403
|
- std/bitset.mt
|
|
404
|
+
- std/blackboard.mt
|
|
404
405
|
- std/box2d.mt
|
|
405
406
|
- std/bytes.mt
|
|
406
407
|
- std/c/box2d.mt
|
|
@@ -463,6 +464,8 @@ files:
|
|
|
463
464
|
- std/cgltf.mt
|
|
464
465
|
- std/cjson.mt
|
|
465
466
|
- std/cli.mt
|
|
467
|
+
- std/cmd.mt
|
|
468
|
+
- std/color.mt
|
|
466
469
|
- std/cookie.mt
|
|
467
470
|
- std/counter.mt
|
|
468
471
|
- std/crypto.mt
|
|
@@ -486,9 +489,12 @@ files:
|
|
|
486
489
|
- std/graph.mt
|
|
487
490
|
- std/gzip.mt
|
|
488
491
|
- std/hash.mt
|
|
492
|
+
- std/htn.mt
|
|
489
493
|
- std/http.mt
|
|
490
494
|
- std/http/server.mt
|
|
495
|
+
- std/input.mt
|
|
491
496
|
- std/intern.mt
|
|
497
|
+
- std/iter.mt
|
|
492
498
|
- std/jobs.mt
|
|
493
499
|
- std/json.mt
|
|
494
500
|
- std/libc.mt
|
|
@@ -505,6 +511,7 @@ files:
|
|
|
505
511
|
- std/mem/endian.mt
|
|
506
512
|
- std/mem/heap.mt
|
|
507
513
|
- std/mem/pool.mt
|
|
514
|
+
- std/mem/ptr.mt
|
|
508
515
|
- std/mem/stack.mt
|
|
509
516
|
- std/mem/tracking.mt
|
|
510
517
|
- std/miniaudio.mt
|
|
@@ -524,6 +531,7 @@ files:
|
|
|
524
531
|
- std/net/stun.mt
|
|
525
532
|
- std/net/sync.mt
|
|
526
533
|
- std/net/turn.mt
|
|
534
|
+
- std/noise.mt
|
|
527
535
|
- std/oauth2.mt
|
|
528
536
|
- std/option.mt
|
|
529
537
|
- std/ordered_map.mt
|
|
@@ -531,6 +539,7 @@ files:
|
|
|
531
539
|
- std/path.mt
|
|
532
540
|
- std/pcre2.mt
|
|
533
541
|
- std/pcre2/runtime.mt
|
|
542
|
+
- std/pool.mt
|
|
534
543
|
- std/priority_queue.mt
|
|
535
544
|
- std/process.mt
|
|
536
545
|
- std/queue.mt
|
|
@@ -552,6 +561,8 @@ files:
|
|
|
552
561
|
- std/sdl3/runtime.mt
|
|
553
562
|
- std/serialize.mt
|
|
554
563
|
- std/set.mt
|
|
564
|
+
- std/signal.mt
|
|
565
|
+
- std/simd.mt
|
|
555
566
|
- std/sparse_set.mt
|
|
556
567
|
- std/spatial.mt
|
|
557
568
|
- std/sqlite3.mt
|
|
@@ -564,6 +575,7 @@ files:
|
|
|
564
575
|
- std/stb_vorbis.mt
|
|
565
576
|
- std/stdio.mt
|
|
566
577
|
- std/steamworks.mt
|
|
578
|
+
- std/steering.mt
|
|
567
579
|
- std/str.mt
|
|
568
580
|
- std/string.mt
|
|
569
581
|
- std/sync.mt
|
|
@@ -572,11 +584,14 @@ files:
|
|
|
572
584
|
- std/testing.mt
|
|
573
585
|
- std/thread.mt
|
|
574
586
|
- std/time.mt
|
|
587
|
+
- std/timer.mt
|
|
575
588
|
- std/tls.mt
|
|
576
589
|
- std/toml.mt
|
|
577
590
|
- std/tracy.mt
|
|
591
|
+
- std/tween.mt
|
|
578
592
|
- std/uri.mt
|
|
579
593
|
- std/url.mt
|
|
594
|
+
- std/utility.mt
|
|
580
595
|
- std/vec.mt
|
|
581
596
|
- std/zstd.mt
|
|
582
597
|
homepage: https://teefan.github.io/mt-lang/
|
|
@@ -586,7 +601,7 @@ metadata:
|
|
|
586
601
|
homepage_uri: https://teefan.github.io/mt-lang/
|
|
587
602
|
source_code_uri: https://github.com/teefan/mt-lang
|
|
588
603
|
post_install_message: |
|
|
589
|
-
Milk Tea 0.3.
|
|
604
|
+
Milk Tea 0.3.4 installed!
|
|
590
605
|
|
|
591
606
|
System requirements:
|
|
592
607
|
- A C compiler (gcc or clang) must be available on PATH
|