@@ -3,17 +3,17 @@ extern crate dirs;
33use crate :: character;
44use crate :: character:: Character ;
55use crate :: event:: Event ;
6- use crate :: item:: { Item , Potion } ;
6+ use crate :: item:: Item ;
77use crate :: location:: Location ;
88use crate :: quest:: QuestList ;
99use crate :: randomizer:: random;
1010use crate :: randomizer:: Randomizer ;
11+ use chest:: Chest ;
1112use serde:: { Deserialize , Serialize } ;
1213use std:: collections:: { HashMap , HashSet } ;
13- use tombstone:: Tombstone ;
1414
1515pub mod battle;
16- pub mod tombstone ;
16+ pub mod chest ;
1717
1818#[ derive( Serialize , Deserialize ) ]
1919#[ serde( default ) ]
@@ -23,7 +23,7 @@ pub struct Game {
2323 pub gold : i32 ,
2424 pub quests : QuestList ,
2525 pub inventory : HashMap < String , Vec < Box < dyn Item > > > ,
26- pub tombstones : HashMap < String , Tombstone > ,
26+ pub tombstones : HashMap < String , Chest > ,
2727 inspected : HashSet < Location > ,
2828}
2929
@@ -77,35 +77,28 @@ impl Game {
7777 }
7878
7979 /// Look for chests and tombstones at the current location.
80- /// Remembers previous checks for consistency.
80+ /// Remembers previously visited locations for consistency.
8181 pub fn inspect ( & mut self ) {
82- self . pick_up_tombstone ( ) ;
82+ let maybe_tomb = self . tombstones . remove ( & self . location . to_string ( ) ) ;
83+ self . pick_up_chest ( maybe_tomb, true ) ;
8384
8485 if !self . inspected . contains ( & self . location ) {
8586 self . inspected . insert ( self . location . clone ( ) ) ;
87+ self . pick_up_chest ( Chest :: generate ( self ) , false ) ;
88+ }
89+ }
8690
87- // this could be extended to find better items, with a non uniform
88- // probability, and to change according to the distance from home
89- // it's likely better to extract to an item generator module at that point
90- match random ( ) . range ( 6 ) {
91- 0 => {
92- let gold = random ( ) . gold_gained ( self . player . level * 200 ) ;
93- Event :: emit ( self , Event :: ChestFound { items : & [ ] , gold } ) ;
94- self . gold += gold;
95- }
96- 1 => {
97- let potion = Potion :: new ( self . player . level ) ;
98- Event :: emit (
99- self ,
100- Event :: ChestFound {
101- items : & [ "potion" . to_string ( ) ] ,
102- gold : 0 ,
103- } ,
104- ) ;
105- self . add_item ( "potion" , Box :: new ( potion) ) ;
106- }
107- _ => { }
108- }
91+ fn pick_up_chest ( & mut self , maybe_chest : Option < Chest > , is_tombstone : bool ) {
92+ if let Some ( mut chest) = maybe_chest {
93+ let ( items, gold) = chest. pick_up ( self ) ;
94+ Event :: emit (
95+ self ,
96+ Event :: ChestFound {
97+ items : & items,
98+ gold,
99+ is_tombstone,
100+ } ,
101+ ) ;
109102 }
110103 }
111104
@@ -177,20 +170,6 @@ impl Game {
177170 . collect :: < HashMap < & str , usize > > ( )
178171 }
179172
180- /// If there's a tombstone laying in the current location, pick up its items
181- fn pick_up_tombstone ( & mut self ) {
182- if let Some ( mut tombstone) = self . tombstones . remove ( & self . location . to_string ( ) ) {
183- let ( items, gold) = tombstone. pick_up ( self ) ;
184- Event :: emit (
185- self ,
186- Event :: TombstoneFound {
187- items : & items,
188- gold,
189- } ,
190- ) ;
191- }
192- }
193-
194173 pub fn maybe_spawn_enemy ( & mut self ) -> Option < Character > {
195174 let distance = self . location . distance_from_home ( ) ;
196175 if random ( ) . should_enemy_appear ( & distance) {
@@ -272,7 +251,7 @@ impl Game {
272251 }
273252 Err ( character:: Dead ) => {
274253 // leave hero items in the location
275- let tombstone = Tombstone :: drop ( self ) ;
254+ let tombstone = Chest :: drop ( self ) ;
276255 self . tombstones . insert ( self . location . to_string ( ) , tombstone) ;
277256
278257 Event :: emit ( self , Event :: BattleLost ) ;
0 commit comments