1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
package game
import (
"fmt"
"sort"
"strconv"
"strings"
"thehouseoficarus/internal/behavior"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/object"
"thehouseoficarus/internal/player"
"thehouseoficarus/internal/world"
)
var verbAliases = map[string]string{
"mine": "gather",
"chop": "gather",
"fish": "gather",
"cut": "gather",
"shear": "gather",
"talk": "talk",
"speak": "talk",
"ask": "talk",
"steal": "steal",
"thieve": "steal",
"hide": "safespot",
}
var verbSkill = map[string]string{
"mine": "mining",
"chop": "woodcutting",
"cut": "woodcutting",
"fish": "fishing",
"shear": "crafting",
"steal": "thieving",
"thieve": "thieving",
}
func normalizeVerb(v string) string {
if a, ok := verbAliases[v]; ok {
return a
}
return v
}
func (g *Game) startAction(sess *net.Session, verb, target string) {
p := sess.Player
verb = normalizeVerb(verb)
if g.Combat.Get(p.Name) != nil {
sess.WriteLine("You can't do that during combat!")
return
}
if p.Action != nil {
g.cancelAction(p)
}
g.cancelBgAction(p)
lower := strings.ToLower(target)
instanceIdx := -1
if dotPos := strings.Index(lower, "."); dotPos > 0 {
if n, err := strconv.Atoi(lower[:dotPos]); err == nil && n > 0 {
instanceIdx = n - 1
lower = lower[dotPos+1:]
}
}
instances := g.World.FindObjInstances(p.RoomID, lower)
sort.Slice(instances, func(i, j int) bool {
return instances[i].Index < instances[j].Index
})
var obj *object.ObjectDef
var mob *world.MobInstance
var chosen *world.ObjState
if len(instances) > 0 {
uniqueDefs := make(map[string]bool)
for _, ist := range instances {
uniqueDefs[ist.DefID] = true
}
if len(uniqueDefs) > 1 {
sess.WriteLine("That's ambiguous, which one?")
return
}
if instanceIdx >= 0 && instanceIdx < len(instances) {
chosen = &instances[instanceIdx]
} else {
for i := range instances {
if !instances[i].Depleted {
chosen = &instances[i]
break
}
}
if chosen == nil {
chosen = &instances[0]
}
}
if chosen == nil {
sess.WriteLine(fmt.Sprintf("There's nothing here to %s.", verb))
return
}
var err error
obj, _, err = g.resolveObjectDef(p.RoomID, chosen.DefID)
if err != nil {
sess.WriteLine(fmt.Sprintf("There's nothing here to %s.", verb))
return
}
if !obj.IsInteractable() {
sess.WriteLine(fmt.Sprintf("You can't %s the %s.", verb, obj.Name))
return
}
} else {
mobs := g.MobStore.MobsInRoom(p.RoomID)
var candidates []*world.MobInstance
for _, m := range mobs {
if !m.IsTalkable() {
continue
}
if q := m.MatchQuality(lower); q >= world.MatchPrefix {
candidates = append(candidates, m)
}
}
if len(candidates) == 0 {
sess.WriteLine(fmt.Sprintf("There's nothing here to %s.", verb))
return
}
sort.Slice(candidates, func(i, j int) bool {
return candidates[i].InstanceID < candidates[j].InstanceID
})
if instanceIdx >= 0 && instanceIdx < len(candidates) {
mob = candidates[instanceIdx]
} else if len(candidates) == 1 {
mob = candidates[0]
} else {
sess.WriteLine("Which one?")
return
}
}
if obj != nil && obj.ID == "estate_directory" {
g.startEstateDirectory(sess, verb)
return
}
bhType := ""
if obj != nil {
bhType = obj.BehaviorType()
} else if mob != nil {
if mob.IsTalkable() {
bhType = "talk"
}
}
if bhType != verb {
if obj != nil {
sess.WriteLine(fmt.Sprintf("You can't %s the %s.", verb, obj.Name))
} else {
sess.WriteLine(fmt.Sprintf("You can't %s that.", verb))
}
return
}
switch bhType {
case "gather":
if obj == nil || chosen == nil {
sess.WriteLine(fmt.Sprintf("You can't %s that.", verb))
return
}
g.startGather(sess, p, obj, chosen)
case "talk":
if mob != nil {
g.startMobTalk(sess, p, mob)
} else {
g.startTalk(sess, p, obj)
}
default:
sess.WriteLine(fmt.Sprintf("You can't %s that.", verb))
}
}
func (g *Game) cancelAction(p *player.Player) {
p.Action = nil
p.WalkSequence = nil
p.ClearMoveState()
}
func (g *Game) cancelBgAction(p *player.Player) {
p.BackgroundAction = nil
}
func (g *Game) AdvanceActions() {
if g.Hub == nil {
return
}
for _, sess := range g.Hub.AllSessions() {
p := sess.Player
if p == nil || p.Action == nil {
continue
}
if !p.Action.Advance() {
continue
}
switch p.Action.Type {
case "gather":
g.advanceGather(sess, p)
case "burn":
g.advanceBurn(sess, p)
case "stoke":
g.advanceStoke(sess, p)
case "search":
g.advanceSearch(sess, p)
case "identify":
g.advanceIdentify(sess, p)
case "steal":
g.advanceSteal(sess, p)
case "plant":
g.advancePlant(sess, p)
case "harvest":
g.advanceHarvest(sess, p)
case "rake":
g.advanceRake(sess, p)
case "water":
g.advanceWater(sess, p)
case "cure":
g.advanceCure(sess, p)
case "obstacle":
g.advanceObstacle(sess, p)
case behavior.TypeTalk:
g.advanceTalk(sess, p)
case behavior.TypeTriggerModule:
g.advanceTriggerModule(sess, p)
case behavior.TypeUseInteraction:
g.advanceUseInteraction(sess, p)
case behavior.TypeCombine:
g.advanceCombine(sess, p)
default:
if productionActionTypes[string(p.Action.Type)] {
g.advanceProduction(sess, p)
}
}
if p.Action == nil {
g.writePrompt(sess)
}
}
for _, sess := range g.Hub.AllSessions() {
p := sess.Player
if p == nil || p.BackgroundAction == nil {
continue
}
if !p.BackgroundAction.Advance() {
continue
}
switch p.BackgroundAction.Type {
case "fletch":
g.advanceFletch(sess, p)
case "cleaning":
g.advanceClean(sess, p)
}
if p.BackgroundAction == nil {
g.writePrompt(sess)
}
}
}
func (g *Game) checkCondition(sess *net.Session, c *behavior.Condition) bool {
p := sess.Player
if c.AllOf != nil {
for _, sub := range c.AllOf {
if !g.checkCondition(sess, &sub) {
return false
}
}
return true
}
if c.AnyOf != nil {
for _, sub := range c.AnyOf {
if g.checkCondition(sess, &sub) {
return true
}
}
return false
}
if c.PlayerFlag != "" {
var val any
present := false
if p != nil && p.Flags != nil {
val, present = p.Flags[c.PlayerFlag]
}
return flagMatches(present, val, c.Value, c.Not)
}
if c.Flag != "" {
val, present := g.Flags.Get(c.Flag)
return flagMatches(present, val, c.Value, c.Not)
}
if c.HasItem != "" {
has := p != nil && p.HasItem(c.HasItem)
if c.Not {
return !has
}
return has
}
if c.MinCredits > 0 {
has := p != nil && p.Credits >= c.MinCredits
if c.Not {
return !has
}
return has
}
return true
}
// flagMatches evaluates a flag/player_flag condition.
//
// If the condition specifies no `value`, the flag matches when it is present
// and truthy (so `{player_flag: x}` means "x is set" and
// `{player_flag: x, not: true}` means "x is not set"). If a `value` is given,
// the flag must be present and equal to it. `not` inverts the result. Numeric
// values are compared by coercing both sides through a common numeric type so
// that an int flag set in code matches an int64/float64 decoded from YAML.
func flagMatches(present bool, val, want any, not bool) bool {
var match bool
if want == nil {
match = present && isTruthy(val)
} else {
match = present && valuesEqual(val, want)
}
if not {
return !match
}
return match
}
// valuesEqual compares two any-typed values, normalizing numeric types (int,
// int64, float64) so that e.g. int(3) == int64(3) == float64(3). Non-numeric
// types fall back to ==.
func valuesEqual(a, b any) bool {
ai, aok := numericValue(a)
bi, bok := numericValue(b)
if aok && bok {
return ai == bi
}
return a == b
}
// numericValue returns the value as a float64 if it is a numeric type.
func numericValue(v any) (float64, bool) {
switch x := v.(type) {
case int:
return float64(x), true
case int64:
return float64(x), true
case float64:
return x, true
}
return 0, false
}
func isTruthy(v any) bool {
switch x := v.(type) {
case nil:
return false
case bool:
return x
case int:
return x != 0
case int64:
return x != 0
case float64:
return x != 0
case string:
return x != ""
default:
return true
}
}
|