-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
639 lines (628 loc) · 18.6 KB
/
map.js
File metadata and controls
639 lines (628 loc) · 18.6 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
(function (App) {
let movementModule = App.RequireModule("helllibjs/map/movement.js")
let hmm = App.Include("helllibjs/lib/hmm/hmm.js")
let module = {}
module.DefaultStepTimeout = 3000
module.DefaultResendDelay = 500
module.HMM = hmm
module.Database = new hmm.MapDatabase()
module.DefaultOnModeChange = (map, oldmode, newmode) => {
}
class Room {
ID = ""
Name = ""
#nameRaw = null
Zone = ""
Exits = []
Data = {}
Keep = false//设为true,不更新room,一般用于look更新当前房间信息
Keeping = false
WithName(name) {
this.Name = name
return this
}
WithZone(zone) {
this.Zone = zone
return this
}
WithExits(exits) {
this.Exits = exits
return this
}
WithNameRaw(raw) {
this.#nameRaw = raw
return this
}
GetNameRaw() {
return this.#nameRaw
}
WithData(name, value) {
this.Data[name] = value
return this
}
WithID(id) {
this.ID = id
return this
}
}
let DefaultCheckEnterMaze = function (map, move, step) {
let maze = map.Mazes[map.Room.Name]
if (maze && maze.CheckEnter(maze, move, this, step)) {
return maze
}
return null
}
let DefaultTrace = function (map, fr, cmd) {
if (fr != "") {
return hmm.APITrackExit(fr, cmd, this.Context, hmm.MapperOptions.New())
}
return ""
}
class Snap {
Move = null
Term = null
}
class Map {
constructor(position, moveposition) {
this.Position = position
this.MovePosition = moveposition
this.Movement = movementModule
this.StepTimeout = module.DefaultStepTimeout
this.ResendDelay = module.DefaultResendDelay
}
Context = new hmm.Context()
CheckEnterMaze = DefaultCheckEnterMaze
Position = null
Room = new Room()
#initiator = []
Move = null
#tags = {}
#blocked = []
#temporaryPaths = []
#temporaryRooms = []
Data = {}
Movement = null
StepPlan = null
StepTimeout = 0
ResendDelay = 0
Mode = ""
ChangeMode(mode) {
if (mode != this.Mode) {
let om = this.Mode
this.Mode = mode
this.OnModeChange(this, om, mode)
}
}
OnModeChange = module.DefaultOnModeChange
Mazes = {}
Trace = DefaultTrace
AppendInitiator(fn) {
this.#initiator.push(fn)
}
TracePath(fr, ...commands) {
let result = []
if (!fr) {
return null
}
let current = fr
for (let cmd of commands) {
let to = this.Trace(this, current, cmd)
if (!to) {
PrintSystem("路径解析错误:" + current + "," + cmd)
return null
}
result.push(new Step(cmd, to))
current = to
}
return result
}
TraceRooms(fr, ...commands) {
let result = [fr]
let current = fr
for (let cmd of commands) {
let to = this.Trace(this, current, cmd)
if (!to) {
PrintSystem("路径解析错误:" + current + "," + cmd)
return null
}
result.push(to)
current = to
}
return result
}
NewRoom() {
return new Room()
}
EnterNewRoom(room) {
if (!room) {
room = new Room()
}
let oroom = this.Room
this.Room = room
if (oroom.Keep) {
if (oroom.ID) {
this.Room.ID = oroom.ID
}
this.Room.Keeping = true
}
this.Room.Keep = false
return this.Room
}
FlashTags() {
this.#tags = {}
this.Context = hmm.Context.New();
//Mapper.flashtags()
//Mapper.ResetTemporary()
this.#blocked = []
this.#temporaryPaths = []
this.#temporaryRooms = []
}
AddTemporaryRooms(rooms) {
this.#temporaryRooms = this.#temporaryRooms.concat(rooms)
}
AddTemporaryPath(path) {
this.#temporaryPaths.push(path)
}
BlockPath(from, to) {
this.#blocked.push([from, to])
}
SetTag(name, value, force) {
let old = this.#tags[name]
if (value === true || value === null || value === undefined) {
value = 1
} else if (value === false) {
value = 0
}
if (old == null || force) {
this.#tags[name] = value
}
}
InitTags() {
this.FlashTags()
if (this.Move != null) {
this.Move.InitTags(this)
}
this.#initiator.forEach(fn => {
fn(this)
})
for (var key in this.#tags) {
if (this.#tags[key]) {
this.Context.WithTags([hmm.ValueTag.New(key, this.#tags[key])])
}
}
this.#blocked.forEach(val => {
this.Context.WithBlockedLinks([hmm.Link.New(val[0], val[1])])
})
this.Context.WithRooms(this.#temporaryRooms)
this.Context.WithPaths(this.#temporaryPaths)
}
UpdateMapperOption(option) {
if (option.blockedpath == null) {
option.blockedpath = []
}
this.#blocked.forEach(val => {
option.blockedpath.push(val)
})
}
filterpath(path) {
let result = []
path.forEach(step => {
if (step.Command != "#skip") {
result.push(step)
}
})
return result
}
GetMapperPath(from, fly, to) {
if (typeof (to) != "object") {
to = [to]
}
if (to.length == 0) {
return []
}
let result = module.Database.APIQueryPathAny([from], to, this.Context, this.GetMapperOptions(!fly))
if (result == null) {
return null
}
let path = []
result.Steps.forEach(step => {
path.push(new Step(step.Command, step.Target))
})
return this.filterpath(path)
}
GetMapperWalkAll(rooms, fly, distance) {
let result = module.Database.APIQueryPathAll(rooms[0], rooms, this.Context, this.GetMapperOptions(!fly).WithMaxTotalCost(distance))
if (result == null) {
return null
}
let path = []
result.Steps.forEach(step => {
path.push(new Step(step.Command, step.Target))
})
return this.filterpath(path)
}
GetMapperWalkOrdered(from, rooms, fly) {
let path = []
let result = module.Database.APIQueryPathOrdered(from, rooms, this.Context, this.GetMapperOptions(!fly))
if (result == null) {
return null
}
result.Steps.forEach(step => {
path.push(new Step(step.Command, step.Target))
})
return this.filterpath(path)
}
OnWalking() {
if (!this.Room.Keeping) {
this.Position.StartNewTerm()
}
if (this.Move != null) {
this.Move.OnWalking(this)
}
}
Retry() {
if (this.Move != null) {
this.Move.Retry(this.Move, this)
}
}
TrySteps(steps) {
if (this.Move != null) {
this.Move.TrySteps(this, steps)
}
}
OnStepTimeout() {
if (this.Move != null) {
return this.Move.StepTimeout(this)
}
return true
}
Resend(delay, offset) {
if (delay == null) {
delay = this.ResendDelay
}
if (delay <= 0) {
if (this.Move) {
this.Move.Resend(this)
}
return
}
this.Position.Wait(delay, offset, () => {
if (this.Move) {
this.Move.Resend(this)
}
})
}
FinishMove() {
if (this.Move != null) {
let move = this.Move
this.Move = null
move.OnFinish(this.Move, this)
this.MovePosition.StartNewTerm()
}
}
CancelMove() {
if (this.Move != null) {
let move = this.Move
this.Move = null
move.OnCancel(this.Move, this)
this.MovePosition.StartNewTerm()
}
}
DiscardMove() {
if (this.Move) {
this.Move = null
this.MovePosition.StartNewTerm()
}
}
Snap() {
if (this.Move != null) {
let snap = new Snap()
snap.Move = this.Move
this.Move = null
snap.Term = this.MovePosition.Snap()
return snap
}
return null
}
Rollback(snap) {
this.Move = snap.Move
this.MovePosition.Rollback(snap.Term)
}
StartMove(move) {
this.Move = move
this.MovePosition.StartNewTerm()
this.ChangeMode("")
move.Walk(this)
}
NewRoute(...initiators) {
return new Route(this, ...initiators)
}
NewStep(command, target) {
return new Step(command, target)
}
NewTag(key, value) {
return new Tag(key, value)
}
NewMaze() {
return new Maze()
}
RegisterMaze(name, maze) {
this.Mazes[name] = maze
}
GetRoomExits(rid, withouttemp = false, withoutfly = false) {
return module.Database.APIGetRoomExits(rid, !withouttemp ? this.Context : hmm.Context.New(), this.GetMapperOptions(withoutfly))
}
GetMapperOptions(withoutfly) {
return hmm.MapperOptions.New().WithDisableShortcuts(withoutfly)
}
}
class Step {
constructor(command, target) {
this.Command = command
this.Target = target
}
Command = null
Target = null
}
module.DefaultOnFinish = function (move, map) {
}
module.DefaultOnCancel = function (move, map) {
}
let DefaultMoveRetry = function (move, map) {
move.Walk(map)
}
let DefaultMoveNext = function (move, map) {
return []
}
let DefaultMoveOnStepFinish = function (move, map, step) {
}
let DefaultMoveOnRoom = function (move, map, step) {
}
let DefaultMoveOnArrive = function (move, map) {
move.Walk(map)
}
let DefaultOnInitTags = function (move, map) {
}
let DefaultOnStepTimeout = function (move, map) {
return true
}
let DefaultMapperOptionCreator = function (move, map) {
return null
}
class Move {
StartCommand = ""
Data = {}
Retry = DefaultMoveRetry
Next = DefaultMoveNext
OnRoom = DefaultMoveOnRoom
OnArrive = DefaultMoveOnArrive
Vehicle = DefaultVehicle
OnFinish = module.DefaultOnFinish
OnCancel = module.DefaultOnCancel
OnInitTags = DefaultOnInitTags
OnStepTimeout = DefaultOnStepTimeout
OnStepFinsih = DefaultMoveOnStepFinish
MapperOptionCreator = DefaultMapperOptionCreator
Option = new Option()
#walking = []
Pending = null
#maze = null
Walk(map) {
let steps
if (this.#maze && this.#maze.CheckEscaped(this.#maze, this, map)) {
this.#maze = null
}
if (this.Pending && this.Pending.length) {
steps = this.Pending
this.Pending = null
} else if (this.#maze) {
this.#maze.Walk(this.#maze, this, map)
return
} else {
steps = this.Next(this, map)
}
if (typeof steps == "function") {
steps(this, map)
return
}
if (steps == null || steps.length == 0) {
map.FinishMove()
return
}
if (steps.length == 1 && this.#maze == null && map.Room.ID) {
let maze = map.CheckEnterMaze(map, this, steps[0])
if (maze != null) {
map.ChangeMode("maze")
this.#maze = maze
maze.NextRoom = steps[0].Next
maze.Walk(maze, this, map)
return
}
}
this.TrySteps(map, steps)
}
GetPath(map, from, to, skipinit) {
if (!skipinit) {
map.InitTags()
}
return map.GetMapperPath(from, this.Option.Fly, to, this.GetMapperOptions(map))
}
WalkAll(map, rooms, distance, skipinit) {
if (!skipinit) {
map.InitTags()
}
return map.GetMapperWalkAll(rooms, this.Option.Fly, distance, this.GetMapperOptions(map))
}
GetWalkOrdered(map, from, rooms, skipinit) {
if (!skipinit) {
map.InitTags()
}
return map.GetMapperWalkOrdered(from, rooms, this.Option.Fly, this.GetMapperOptions(map))
}
GetMapperOptions(map) {
let opt = this.MapperOptionCreator(this, map)
if (opt == null) {
opt = {}
}
map.UpdateMapperOption(opt)
return opt
}
StepTimeout(map) {
return this.OnStepTimeout(this, map)
}
OnWalking(map) {
if (this.#walking.length == 0) {
this.OnArrive(this, map)
return
}
let step = this.#walking.shift()
if (step.Target) {
map.Room.ID = step.Target
}
if (this.#maze) {
this.#maze.OnStepFinsih(move,map, step)
} else {
this.OnStepFinsih(this, map, step)
}
this.OnRoom(this, map, step)
if (this.#walking.length == 0) {
this.OnArrive(this, map)
return
}
if (map.StepPlan) {
map.StepPlan.Execute()
}
}
TrySteps(map, steps) {
this.#walking = steps
this.Resend(map)
}
Resend(map) {
if (this.#walking && this.#walking.length) {
this.#walking.forEach(step => {
this.Vehicle.Send(step, map)
});
if (map.StepPlan) {
map.StepPlan.Execute()
}
}
}
GetLastStep() {
if (this.#walking && this.#walking.length) {
return this.#walking[0]
}
return null
}
InitTags(map) {
if (this.Option != null) {
for (var key in this.Option.Tags) {
let value = this.Option.Tags[key]
if (value != null) {
map.SetTag(key, value)
}
}
this.Vehicle.OnInitTags(this, map)
this.OnInitTags(this, map)
}
}
}
let DefaultVehicleSend = function (step, map) {
App.Send(step.Command, true)
}
class Vehicle {
Send = DefaultVehicleSend
OnInitTags = DefaultOnInitTags
}
let DefaultVehicle = new Vehicle()
module.MultipleStep = true
module.Fly = true
module.MultipleStepSplit = function (paths) {
return paths
}
class Option {
constructor() {
this.MultipleStep = module.MultipleStep
this.Fly = module.Fly
}
MultipleStep = false
Fly = false
Tags = {}
ApplyTo(move, map) {
move.Option = this
}
}
class Tag {
constructor(key = "", value = 1) {
this.Key = key;
this.Value = value;
}
Key = ""
Value = 1
ApplyTo(move, map) {
move.Option.Tags[this.Key] = this.Value
}
}
class Route {
constructor(map, ...initiators) {
this.Map = map
this.Initers = initiators
}
Map = null
Initers = []
Execute() {
let move = new Move()
this.Initers.forEach(initiator => {
if (typeof (initiator) == "function") {
initiator(move, this.Map)
} else {
initiator.ApplyTo(move, this.Map)
}
})
this.Map.StartMove(move)
}
}
let DefaultMazeCheckEnter = function (maze, move, map, steps) {
return false
}
let DefaultMazeEscaped = function (maze, move, map) {
return true
}
let DefaultMazeWalk = function (maze, move, map) {
return
}
let DefaultMazeMoveOnStepFinish = function (move, map,step) {
move.OnStepFinsih(move, map, step)
return
}
class Maze {
Data = null
CheckEnter = DefaultMazeCheckEnter
CheckEscaped = DefaultMazeEscaped
Walk = DefaultMazeWalk
OnStepFinsih = DefaultMazeMoveOnStepFinish
NextRoom = ""
WithCheckEnter(fn) {
this.CheckEnter = fn
return this
}
WithCheckEscaped(fn) {
this.CheckEscaped = fn
return this
}
WithOnStepFinsih(fn) {
this.OnStepFinsih = fn
}
WithWalk(fn) {
this.Walk = fn
return this
}
}
module.Map = Map
module.Room = Room
module.Vehicle = Vehicle
module.Move = Move
module.Tag = Tag
module.Step = Step
module.Option = Option
return module
})