-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEngine.elm
More file actions
943 lines (790 loc) · 20.7 KB
/
Engine.elm
File metadata and controls
943 lines (790 loc) · 20.7 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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
module Engine exposing (Engine, init, view, enter, speak, clickAt, hoverAt, tick, handleKeypress, resetHover, autorogue)
import Point exposing (Point, slide)
import Direction exposing (Direction(..))
import Path
import World
import Dungeon exposing (Dungeon)
import Entity exposing (Entity)
import Configuration
import Util
import Graphics
import Warrior
import Quest exposing (Quest)
import Journal
import Log
import Status
import Item exposing (Item, ItemKind(..))
import Spell exposing (Spell(..))
import Action exposing (Action(..))
import Palette
import Inventory
import Optics
import Language exposing (Language)
import Level
import Set exposing (Set)
import Time
import Mouse
import Svg exposing (svg, rect, text')
import Svg.Attributes exposing (viewBox, width, height, x, y, fontSize, fontFamily)
import Svg.Events
type alias Engine =
{ world : World.Model
, hover : Maybe Entity
, hoverPath : List Point
, followPath : Maybe (List Point)
, auto : Bool
, telepathy : Bool
, quests : List Quest
, action : Maybe Action
, selectPosition : Bool
, throwPath : Maybe (List Point)
, animatingThrow : Bool
, thrownItem : Maybe Item
}
init : Engine
init =
{ world = World.init
, hover = Nothing
, hoverPath = []
, followPath = Nothing
, auto = False
, telepathy = False
, quests = Quest.coreCampaign
, action = Nothing
, selectPosition = False
, throwPath = Nothing
, animatingThrow = False
, thrownItem = Nothing
}
isPerformingAnimation : Engine -> Bool
isPerformingAnimation model =
model.animatingThrow
enter : Dungeon -> Engine -> Engine
enter dungeon model =
let
dungeon' =
dungeon |> Dungeon.prepare Configuration.levelCount
world =
model.world
player =
world.player
startPos =
case (Dungeon.levelAt 0 dungeon').entrance of
Just (pt,_) -> pt
Nothing -> (10,10)
player' =
{ player | position = startPos }
world' =
{ world | dungeon = dungeon'
, depth = 0
, player = player'
}
in
({ model | world = world' |> World.playerViewsField
})
speak : Language -> Engine -> Engine
speak language model =
let
world =
model.world
world' =
{ world | language = language }
in
{ model | world = world' }
illuminate : Engine -> Engine
illuminate model =
{ model | world = World.playerViewsField model.world }
handleKeypress : Char -> Engine -> Engine
handleKeypress keyChar model =
if model |> isPerformingAnimation then
model -- ignore it until we're done animating
else
let
reset = (
resetThrow <<
resetAction <<
resetFollow <<
resetAuto <<
illuminate <<
moveCreatures
)
in
case model.action of
Just action ->
model |> case keyChar of
'd' ->
let act' = (if action == Action.drop then Action.default else Action.drop) in
waitForSelection act'
'i' ->
if action == Action.drop then
waitForSelection Action.default
else
resetAction
_ ->
let alpha = Util.fromAlpha keyChar in
if alpha == -1 then
resetAction
else
playerActs (Util.fromAlpha keyChar)
Nothing ->
model |> case keyChar of
'a' -> autorogue
'h' -> reset << playerSteps West
'j' -> reset << playerSteps South
'k' -> reset << playerSteps North
'l' -> reset << playerSteps East
't' -> telepath
'x' -> playerExplores
'd' -> waitForSelection Action.drop
'i' -> waitForSelection Action.default
_ -> reset
waitForSelection : Action -> Engine -> Engine
waitForSelection action model =
{ model | action = Just action }
waitForPosition : Action -> Engine -> Engine
waitForPosition action model =
{ model | action = Just action
, selectPosition = True
}
isEquipped : Item -> Engine -> Bool
isEquipped item model =
let
player =
model.world.player
isArmor =
case player.armor of
Nothing ->
False
Just armor ->
item == (Item.simple (Item.armor armor))
isWeapon =
case player.weapon of
Nothing ->
False
Just weapon ->
item == (Item.simple (Item.weapon weapon))
isHelm =
case player.helm of
Nothing ->
False
Just helm ->
item == (Item.simple (Item.helm helm))
isRing =
case player.ring of
Nothing ->
False
Just ring ->
item == (Item.simple (Item.ring ring))
in
isArmor
|| isWeapon
|| isHelm
|| isRing
playerActs : Int -> Engine -> Engine
playerActs idx model =
let
maybeItem =
model.world.player
|> Inventory.itemAtIndex idx
in
case maybeItem of
Nothing ->
model
|> resetAction
Just item ->
case model.action of
Nothing ->
model
Just act ->
if Action.canPerform (isEquipped item model) item act then
model |> playerActsOnItem item act
else
model
playerActsOnItem : Item -> Action -> Engine -> Engine
playerActsOnItem item act model =
case act of
Drop ->
{ model | world = model.world |> World.playerDropsItem item }
Wear ->
{ model | world = model.world |> World.playerWears item }
|> playerLosesItem item
TakeOff ->
{ model | world = model.world |> World.playerTakesOff item }
Wield ->
{ model | world = model.world |> World.playerWields item }
|> playerLosesItem item
Sheathe ->
{ model | world = model.world |> World.playerSheathesWeapon }
Drink ->
{ model | world = model.world |> World.playerDrinks item }
|> playerLosesItem item
Read ->
case item.kind of
Scroll spell ->
model
|> castSpell item spell
_ ->
model
Use item' act' ->
-- todo this could be refined?
if Item.canApply item' item then
model
|> playerApplies item' item
|> waitForSelection Action.default
else
model
Default ->
let
equipped =
isEquipped item model
action' =
Action.defaultForItem equipped item
in
model
|> playerActsOnItem item action'
Throw ->
model
|> resetAuto
|> waitForPosition (Action.hurl item)
Hurl it ->
model
Identify ->
model
Look ->
model
Enchant ->
model
playerApplies : Item -> Item -> Engine -> Engine
playerApplies item' item model =
case item'.kind of
Scroll spell ->
case spell of
Infuse ->
model
|> playerLosesItem item'
|> playerEnchants item
Lux ->
model
_ ->
model
resetAction : Engine -> Engine
resetAction model =
{ model | action = Nothing
, selectPosition = False
}
playerLosesItem : Item -> Engine -> Engine
playerLosesItem item model =
let
world =
model.world
player =
world.player
inventory =
player.inventory
inventory' =
inventory
|> List.filter (\it -> not (it == item))
player' =
{ player | inventory = inventory' }
in
{ model | world = { world | player = player' } }
castSpell : Item -> Spell -> Engine -> Engine
castSpell item spell model =
let
world' =
model.world
|> World.playerLearnsWord (Language.wordFor (Spell.idea spell) model.world.language)
model' =
{ model | world = world' }
in
case spell of
Lux ->
model'
|> playerLosesItem item
|> enhancePlayerVision
Infuse ->
model'
|> waitForSelection (Action.use item (Action.enchant))
enhancePlayerVision : Engine -> Engine
enhancePlayerVision model =
{ model | world = model.world
|> World.augmentVision
|> World.playerViewsField
}
playerEnchants : Item -> Engine -> Engine
playerEnchants item model =
{ model | world = model.world
|> World.enchantItem item
}
tick : Time.Time -> Engine -> Engine
tick time model =
if model |> isPerformingAnimation then
Debug.log "ANIMATE MODEL"
model
|> animate
else
model
|> followPaths
|> updateQuests
animate : Engine -> Engine
animate model =
if model.animatingThrow then
model
|> animateThrow
else
Debug.log "animate called but nothing being animated...?"
model
animateThrow : Engine -> Engine
animateThrow model =
case model.thrownItem of
Nothing ->
Debug.log "no thrown item, reset throw"
model |> resetThrow
Just item ->
case model.throwPath of
Nothing ->
Debug.log "no throw path, reset throw"
model |> resetThrow
Just path ->
case path |> List.head of
Nothing ->
Debug.log "throw path empty, reset throw"
model |> resetThrow
Just pt ->
let
item' =
{ item | position = pt }
model' =
{ model | thrownItem = Just item'
, throwPath = List.tail path }
in
if List.length path > 1 then
model'
else
{ model' | world = model.world
|> World.hitCreatureAt pt item' --thrownItem
}
resetThrow : Engine -> Engine
resetThrow model =
let
model' =
{ model | throwPath = Nothing -- Just model.hoverPath
, animatingThrow = False
, action = Nothing --Just Action.default
, thrownItem = Nothing
--, auto = True
, selectPosition = False
}
in
case model.thrownItem of
Nothing -> -- but how did we get here?
model'
Just item ->
Debug.log "ADD ITEM BACK TO DUNGEON"
model'
|> addItem item
addItem : Item -> Engine -> Engine
addItem item model =
let
world =
model.world
dungeon =
world.dungeon
|> Dungeon.apply (Level.addItem item) world.depth
in
{ model | world = { world | dungeon = dungeon }}
updateQuests : Engine -> Engine
updateQuests model =
let
quests' =
model.quests
|> Quest.unlocked model.world
in
{ model | quests = quests' ++ model.quests }
followPaths : Engine -> Engine
followPaths model =
case model.followPath of
Nothing ->
if model.auto then
model
|> playerExplores
else
model
Just path ->
model
|> playerFollowsPath
autorogue model =
{ model | auto = True }
telepath model =
if model.telepathy then
{ model | telepathy = False }
else
{ model | telepathy = True }
moveCreatures model =
let
world =
model.world
(dungeon', events, player') =
world.dungeon
|> Dungeon.moveCreatures world.player world.depth
world' =
{ world | dungeon = dungeon'
, events = world.events ++ List.reverse events
, player = player'
}
in
{ model | world = world' }
playerSteps direction model =
{ model | world = model.world |> World.playerSteps direction }
resetHover : Engine -> Engine
resetHover model =
{ model | hoverPath = []
, hover = Nothing }
resetFollow : Engine -> Engine
resetFollow model =
{ model | followPath = Nothing }
resetAuto : Engine -> Engine
resetAuto model =
{ model | auto = False }
hoverAt : Point -> Engine -> Engine
hoverAt pt model =
let
point =
pt
--pointFromMouse position
isLit =
Set.member point (model.world.illuminated)
wasLit =
Set.member point (World.viewed model.world)
in
if model.selectPosition then
model |> targetEntityAt point
else
if isLit then
model |> seeEntityAt point
else
if wasLit then
model |> rememberEntityAt point
else
if model.telepathy then
model |> imagineEntityAt point
else
model
targetEntityAt point model =
let
entity' =
World.entityAt point model.world
entity =
if entity' == Just (Entity.wall point) then
Nothing
else
entity'
path' =
case entity of
Nothing ->
[]
Just entity' ->
model
|> lineToEntity entity'
in
{ model | hover = entity
, hoverPath = path'
}
seeEntityAt point model =
let
entity =
World.entityAt point model.world
path' =
case entity of
Nothing ->
[]
Just entity' ->
model |> pathToEntity entity'
in
{ model | hover = entity
, hoverPath = path'
}
rememberEntityAt point model =
let
entity =
World.entityAt point model.world
maybeEntity =
case entity of
Just entity' ->
if (Entity.isCreature entity') then
Nothing
else
Just (Entity.memory entity')
Nothing ->
Nothing
path' =
case maybeEntity of
Nothing ->
[]
Just entity ->
model |> pathToEntity entity
in
{ model | hover = maybeEntity
, hoverPath = path'
}
imagineEntityAt point model =
let
entity =
World.entityAt point model.world
maybeEntity =
case entity of
Just entity' ->
Just (Entity.imaginary entity')
Nothing ->
Nothing
path' =
case maybeEntity of
Nothing ->
[]
Just entity ->
model |> pathToEntity entity
in
{ model | hover = maybeEntity
, hoverPath = path'
}
pathToEntity entity model =
let
entityPos =
Entity.position entity
playerPos =
model.world.player.position
alreadyHovering =
case model.hover of
Just entity' ->
entity' == entity
Nothing -> False
in
if alreadyHovering || not (model.followPath == Nothing) then
model.hoverPath
else
Path.seek entityPos playerPos (\pt -> Set.member pt (World.walls model.world))
lineToEntity entity model =
let
entityPos =
Entity.position entity
playerPos =
model.world.player.position
alreadyHovering =
case model.hover of
Just entity' ->
entity' == entity
Nothing ->
False
in
if alreadyHovering || not (model.throwPath == Nothing) then
model.hoverPath
else
let ray = Optics.castRay (Warrior.vision model.world.player) (World.walls model.world) playerPos entityPos in
ray
|> List.filter (\pt -> not (Set.member pt (World.walls model.world)))
clickAt : Mouse.Position -> Engine -> Engine
clickAt _ model =
case model.followPath of
Nothing ->
if model.selectPosition then
case model.action of
Just action ->
case action of
Hurl item ->
model
|> throwItem item
_ ->
Debug.log "clicked for position, but some action was associated besides hurl?"
model
|> waitForSelection Action.default
Nothing ->
Debug.log "clicked for position, but no action was associated?"
model
|> waitForSelection Action.default
else
{ model | followPath = Just model.hoverPath }
Just path ->
model
throwItem : Item -> Engine -> Engine
throwItem item model =
Debug.log ("THROW ITEM: " ++ (Item.name item))
{ model | throwPath = Just model.hoverPath
, animatingThrow = True
, selectPosition = False
, thrownItem = Just item
, auto = False
}
--|> resetAuto
playerFollowsPath : Engine -> Engine
playerFollowsPath model =
case model.followPath of
Nothing ->
model
Just path ->
case (List.head path) of
Nothing ->
model
|> resetHover
|> resetFollow
Just nextStep ->
let
playerPos =
model.world.player.position
direction =
(Point.towards nextStep playerPos)
onPath =
nextStep == (playerPos |> slide direction)
followPath' =
List.tail path
in
if onPath then
({model | followPath = followPath' })
|> playerSteps direction
|> moveCreatures
|> illuminate
else
model
|> resetFollow
|> resetHover
gatherTargets : Engine -> List Point
gatherTargets model =
let
viewed =
World.viewed model.world
explored =
World.floors model.world
|> Set.intersect viewed
visibleCreatures =
(World.creatures model.world)
|> List.map .position
|> List.filter (\pt -> Set.member pt explored)
visibleCoins =
(World.coins model.world)
|> List.filter (\pt -> Set.member pt explored)
visibleItems =
if Inventory.size model.world.player < Configuration.inventoryLimit then
(World.items model.world)
|> List.map .position
|> List.filter (\pt -> Set.member pt explored)
else
[]
in
visibleCreatures ++ visibleItems ++ visibleCoins
exploreTargets : Engine -> List Point
exploreTargets model =
if model.world |> World.doesPlayerHaveCrystal then
World.upstairs model.world ++ World.entrances model.world
else
let frontier = World.viewFrontier model.world in
if Set.size frontier == 0 then
World.downstairs model.world ++ World.crystals model.world
else
frontier |> Set.toList
autorogueDestination : Engine -> Maybe Point
autorogueDestination model =
let
gather =
gatherTargets model
byDistanceFromPlayer = \pt ->
Point.distance model.world.player.position pt
in
if List.length gather > 0 then
gather
|> List.sortBy byDistanceFromPlayer
|> List.head
else
exploreTargets model
|> List.sortBy byDistanceFromPlayer
|> List.head
playerExplores : Engine -> Engine
playerExplores model =
let
path =
case autorogueDestination model of
Nothing ->
Nothing
Just dest ->
let
walls =
World.walls model.world
blocked = \pt ->
Set.member pt walls
path' =
Path.seek dest model.world.player.position blocked
in
if List.length path' == 0 then
Nothing
else
Just path'
in
{ model | followPath = path }
-- VIEW
view : Engine -> List (Svg.Svg a)
view model =
let
world =
model.world
lang =
world.language
vocab =
world.player.vocabulary
path =
case model.followPath of
Nothing ->
if model |> isPerformingAnimation then
[]
else
model.hoverPath
Just path ->
path
worldView =
World.view { world | debugPath = path
, showMap = model.telepathy
, animateEntities = [ model.thrownItem ]
|> List.filterMap identity
|> List.map Entity.item
}
debugMsg =
case model.action of
Just action' ->
Action.question vocab lang action'
Nothing ->
model |> hoverMessage vocab lang
note =
Graphics.render debugMsg (25,1) Palette.accentLighter
rightBarY =
Configuration.viewWidth - 15
quests =
Journal.view (rightBarY,2) model.world model.quests
character =
model.world.player
|> Warrior.cardView (rightBarY, 5+(List.length model.quests)) (model.action)
inventory =
Inventory.view (rightBarY, 10+(List.length model.quests)) vocab lang model.action model.world.player
log =
Log.view (2, (Configuration.viewHeight - 6)) vocab lang model.world.events
status =
Status.view (1,1) model.world
rightBar =
quests
++ character
++ inventory
in
worldView
++ status
++ [note]
++ rightBar
++ log
hoverMessage : Language -> Language -> Engine -> String
hoverMessage vocab lang model =
case model.hover of
Nothing ->
"You aren't looking at anything in particular."
Just entity ->
case entity of
Entity.Memory e ->
"You remember seeing " ++ (Entity.describe vocab lang e) ++ " here."
Entity.Imaginary e ->
"You imagine there is " ++ (Entity.describe vocab lang e) ++ " here."
_ ->
"You see " ++ (Entity.describe vocab lang entity) ++ "."