Make all items have an id an associated location

This commit is contained in:
2022-08-01 23:50:26 +01:00
parent 237cd3e020
commit 82aedc3830
11 changed files with 123 additions and 84 deletions
+4 -3
View File
@@ -11,7 +11,7 @@ setHeldItemLoc cr = fst . getHeldItemLoc cr
getHeldItemLoc :: Creature -> World -> (World, Int)
getHeldItemLoc cr w = case maybeitid of
Nothing ->
( w & cWorld . creatures . ix cid . crInv . ix j . itID ?~ newitid
( w & cWorld . creatures . ix cid . crInv . ix j . itID .~ newitid
& cWorld . itemLocations %~ IM.insert newitid (InInv cid j)
, itid
)
@@ -20,7 +20,7 @@ getHeldItemLoc cr w = case maybeitid of
cid = _crID cr
j = crSel cr
newitid = IM.newKey $ _itemLocations (_cWorld w)
maybeitid = cr ^? crInv . ix j . itID . _Just
maybeitid = cr ^? crInv . ix j . itID
itid = fromMaybe newitid maybeitid
getItem :: Int -> World -> Maybe Item
@@ -29,7 +29,8 @@ getItem itid w = do
case itpos of
OnFloor flitid -> w ^? cWorld . floorItems . ix flitid . flIt
InInv cid invid -> w ^? cWorld . creatures . ix cid . crInv . ix invid
VoidItm -> Nothing
OnTurret mcid -> w ^? cWorld . machines . ix mcid . mcType . _McTurret . tuWeapon
InVoid -> Nothing
pointerToItemLocation ::
Applicative f =>
+63
View File
@@ -0,0 +1,63 @@
module Dodge.Item.Location.Initialize
where
import Dodge.Data.CWorld
import Control.Lens
import qualified IntMapHelp as IM
import Data.Traversable
initItemLocations :: CWorld -> CWorld
initItemLocations = initCrsItemLocations . initFlItemsLocations . initTusItemLocations
initCrsItemLocations :: CWorld -> CWorld
initCrsItemLocations w = w' & creatures .~ newcreatures
where
(w', newcreatures) = mapAccumR initCrItemLocations w (_creatures w)
initFlItemsLocations :: CWorld -> CWorld
initFlItemsLocations w = w' & floorItems .~ newfloorItems
where
(w', newfloorItems) = mapAccumR initFlItemLocation w (_floorItems w)
initTusItemLocations :: CWorld -> CWorld
initTusItemLocations w = w' & machines .~ newmachines
where
(w', newmachines) = mapAccumR initTuItemLocation w (_machines w)
initSpecificCrItemLocations :: Int -> CWorld -> CWorld
initSpecificCrItemLocations crid w = w' & creatures . ix crid .~ newcr
where
(w',newcr) = initCrItemLocations w (w ^?! creatures . ix crid)
initCrItemLocations :: CWorld -> Creature -> (CWorld, Creature)
initCrItemLocations w cr = (w', cr & crInv .~ newinv)
where
(w',newinv) = imapAccumR (initCrItemLocation cr) w (_crInv cr)
initCrItemLocation :: Creature -> Int -> CWorld -> Item -> (CWorld,Item)
initCrItemLocation cr invid w it = (w & itemLocations . at locid ?~ loc
,it & itID .~ locid
& itLocation .~ loc)
where
locid = IM.newKey (_itemLocations w)
loc = InInv (_crID cr) invid
initFlItemLocation :: CWorld -> FloorItem -> (CWorld, FloorItem)
initFlItemLocation w flit = (w & itemLocations . at locid ?~ loc
, flit & flIt . itID .~ locid
& flIt . itLocation .~ loc
)
where
locid = IM.newKey (_itemLocations w)
loc = OnFloor (_flItID flit)
initTuItemLocation :: CWorld -> Machine -> (CWorld, Machine)
initTuItemLocation w mc = case mc ^? mcType . _McTurret . tuWeapon of
Nothing -> (w, mc)
Just _ ->
let locid = IM.newKey (_itemLocations w)
loc = OnTurret (_mcID mc)
in ( w & itemLocations . at locid ?~ loc
, mc & mcType . _McTurret . tuWeapon . itID .~ locid
& mcType . _McTurret . tuWeapon . itLocation .~ loc
)