Repository navigation
Expand file tree
/
Copy pathgame_state.rs
More file actions
860 lines (796 loc) · 29.3 KB
/
Copy pathgame_state.rs
File metadata and controls
860 lines (796 loc) · 29.3 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
use crate::state::{location_personalities::LocationConfig, Inventory, Location, LocationInfos};
use std::{borrow::BorrowMut, num::Saturating};
use chrono::Month;
use rand::rngs::StdRng;
use tracing::debug;
use super::{rng::MerchantRng, Good, StateError};
#[derive(PartialEq, Clone, Debug)]
pub struct Transaction {
pub good: Good,
pub amount: Option<u32>,
}
pub const CANNON_COST: u16 = 5000;
pub const SHIP_HEALTH: u8 = 5;
#[derive(PartialEq, Clone, Debug, Copy)]
pub struct PirateEncounterInfo {
pub health: u8,
pub total_pirates: u8,
pub cur_pirates: u8,
}
impl PirateEncounterInfo {
pub fn new(pirates: u8) -> PirateEncounterInfo {
PirateEncounterInfo {
health: SHIP_HEALTH,
total_pirates: pirates,
cur_pirates: pirates,
}
}
}
#[derive(PartialEq, Clone, Debug, Copy)]
pub enum PirateEncounterState {
Initial,
Prompt {
info: PirateEncounterInfo,
},
AttackResult {
info: PirateEncounterInfo,
did_kill_a_pirate: bool,
},
RunSuccess,
RunFailure {
info: PirateEncounterInfo,
},
PiratesAttack {
info: PirateEncounterInfo,
damage_this_attack: u8,
},
Destroyed,
Victory {
gold_recovered: u32,
},
}
impl PirateEncounterState {
pub fn pirates_attack(
info: PirateEncounterInfo,
rng: &mut Box<dyn MerchantRng>,
) -> Result<PirateEncounterState, StateError> {
let damage_this_attack = rng.gen_damage_from_pirates(info.cur_pirates);
Ok(PirateEncounterState::PiratesAttack {
info,
damage_this_attack,
})
}
}
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum NoEffectEvent {
SunnyDay,
StormOnHorizon,
}
#[derive(PartialEq, Clone, Debug)]
pub enum LocationEvent {
CheapGood(Good),
ExpensiveGood(Good),
FindGoods(Good, u32),
GoodsStolen(Option<GoodsStolenResult>),
CanBuyCannon,
PirateEncounter(PirateEncounterState),
CanBuyHoldSpace { price: u32, more_hold: u32 },
NoEffect(NoEffectEvent),
}
#[derive(PartialEq, Clone, Debug)]
pub enum Mode {
ViewingInventory,
Buying(Option<Transaction>),
Selling(Option<Transaction>),
Sailing,
StashDeposit(Option<Transaction>),
StashWithdraw(Option<Transaction>),
PayDebt(Option<u32>),
BankDeposit(Option<u32>),
BankWithdraw(Option<u32>),
GameEvent(LocationEvent),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Initialization {
SplashScreen,
Introduction,
Game,
}
pub struct GameState {
pub rng: Box<dyn MerchantRng>,
pub initialization: Initialization,
pub starting_date: (u16, Month),
pub date: (u16, Month),
pub hold_size: Saturating<u32>,
pub cannons: Saturating<u8>,
pub starting_gold: Saturating<u32>,
pub gold: Saturating<u32>,
pub bank: Saturating<u32>,
pub location: Location,
pub stash: Inventory,
pub inventory: Inventory,
pub location_config: LocationConfig,
pub locations: LocationInfos,
pub starting_debt: Saturating<u32>,
pub debt: Saturating<u32>,
pub mode: Mode,
pub game_end: bool,
}
impl GameState {
pub fn new(mut rng: Box<dyn MerchantRng>) -> GameState {
let starting_gold = Saturating(500);
let starting_debt = starting_gold * Saturating(3u32);
let location_config = rng.gen_location_config(starting_gold.0);
debug!("location_config: {:#?}", location_config);
let locations = LocationInfos::new(
&mut rng,
&location_config,
starting_gold.0 as i32 - starting_debt.0 as i32,
);
let starting_date = (1782, Month::March);
GameState {
rng,
initialization: Initialization::SplashScreen,
date: starting_date,
starting_date,
hold_size: Saturating(100),
cannons: Saturating(1),
gold: starting_gold,
starting_gold,
bank: Saturating(0),
location: location_config.home_port,
stash: Inventory::default(),
inventory: Inventory::default(),
location_config,
locations,
debt: starting_debt,
starting_debt,
mode: Mode::ViewingInventory,
game_end: false,
}
}
pub fn new_std_rng(rng: StdRng) -> GameState {
GameState::new(Box::new(rng))
}
pub fn splash_to_introduction(&mut self) {
self.initialization = Initialization::Introduction;
}
pub fn introduction_to_game(&mut self) {
self.initialization = Initialization::Game;
}
/// compute the net worth the player currently has
/// based on inventory, bank, stash, and debt
pub fn net_worth(&self) -> i32 {
(self.gold.0 as i32)
+ (self
.inventory
.net_worth(&self.location_config.overall_price_ranges))
+ (self.bank.0 as i32)
- (self.debt.0 as i32)
}
fn require_viewing_inventory(&self) -> Result<(), StateError> {
if self.mode != Mode::ViewingInventory {
Err(StateError::InvalidMode(self.mode.clone()))
} else {
Ok(())
}
}
fn require_location_home_base(&self) -> Result<(), StateError> {
if self.location != self.location_config.home_port {
Err(StateError::LocationNotHomeBase(self.location))
} else {
Ok(())
}
}
pub fn begin_buying(&mut self) -> Result<&mut GameState, StateError> {
self.require_viewing_inventory()?;
self.mode = Mode::Buying(None);
Ok(self)
}
pub fn begin_selling(&mut self) -> Result<&mut GameState, StateError> {
self.require_viewing_inventory()?;
self.mode = Mode::Selling(None);
Ok(self)
}
pub fn begin_sailing(&mut self) -> Result<&mut GameState, StateError> {
self.require_viewing_inventory()?;
self.mode = Mode::Sailing;
Ok(self)
}
pub fn begin_stash_deposit(&mut self) -> Result<&mut GameState, StateError> {
self.require_viewing_inventory()?;
self.require_location_home_base()?;
self.mode = Mode::StashDeposit(None);
Ok(self)
}
pub fn begin_stash_withdraw(&mut self) -> Result<&mut GameState, StateError> {
self.require_viewing_inventory()?;
self.require_location_home_base()?;
self.mode = Mode::StashWithdraw(None);
Ok(self)
}
pub fn begin_pay_debt(&mut self) -> Result<&mut GameState, StateError> {
self.require_viewing_inventory()?;
self.require_location_home_base()?;
self.mode = Mode::PayDebt(None);
Ok(self)
}
pub fn begin_bank_deposit(&mut self) -> Result<&mut GameState, StateError> {
self.require_viewing_inventory()?;
self.require_location_home_base()?;
self.mode = Mode::BankDeposit(None);
Ok(self)
}
pub fn begin_bank_withdraw(&mut self) -> Result<&mut GameState, StateError> {
self.require_viewing_inventory()?;
self.require_location_home_base()?;
self.mode = Mode::BankWithdraw(None);
Ok(self)
}
pub fn choose_buy_good(&mut self, good: Good) -> Result<&mut GameState, StateError> {
if let Mode::Buying(None) = &self.mode {
self.mode = Mode::Buying(Some(Transaction { good, amount: None }));
return Ok(self);
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn choose_sell_good(&mut self, good: Good) -> Result<&mut GameState, StateError> {
if let Mode::Selling(None) = &self.mode {
self.mode = Mode::Selling(Some(Transaction { good, amount: None }));
return Ok(self);
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn choose_stash_deposit_good(&mut self, good: Good) -> Result<&mut GameState, StateError> {
if let Mode::StashDeposit(None) = &self.mode {
self.mode = Mode::StashDeposit(Some(Transaction { good, amount: None }));
return Ok(self);
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn choose_stash_withdraw_good(&mut self, good: Good) -> Result<&mut GameState, StateError> {
if let Mode::StashWithdraw(None) = &self.mode {
self.mode = Mode::StashWithdraw(Some(Transaction { good, amount: None }));
return Ok(self);
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn cancel_buy(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::Buying(None) = &self.mode {
self.mode = Mode::ViewingInventory;
return Ok(self);
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn cancel_sell(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::Selling(None) = &self.mode {
self.mode = Mode::ViewingInventory;
return Ok(self);
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn cancel_stash_deposit(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::StashDeposit(None) = &self.mode {
self.mode = Mode::ViewingInventory;
return Ok(self);
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn cancel_stash_withdraw(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::StashWithdraw(None) = &self.mode {
self.mode = Mode::ViewingInventory;
return Ok(self);
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn user_typed_digit(&mut self, digit: u32) -> Result<&mut GameState, StateError> {
let mut binding = None;
let info = match &mut self.mode {
Mode::Buying(info) => info,
Mode::Selling(info) => info,
Mode::StashDeposit(info) => info,
Mode::StashWithdraw(info) => info,
_ => &mut binding,
};
if let Some(info) = info {
info.amount = Some(info.amount.map_or(digit, |amount| amount * 10 + digit));
Ok(self)
} else {
let binding: Option<&mut Option<u32>> = None;
let amount = match &mut self.mode {
Mode::PayDebt(amount) => Some(amount),
Mode::BankDeposit(amount) => Some(amount),
Mode::BankWithdraw(amount) => Some(amount),
_ => binding,
};
if let Some(amount) = amount {
*amount = amount.map_or(Some(digit), |amount| Some(amount * 10 + digit));
Ok(self)
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
}
pub fn user_typed_backspace(&mut self) -> Result<&mut GameState, StateError> {
let mut binding = None;
let info = match &mut self.mode {
Mode::Buying(info) => info,
Mode::Selling(info) => info,
Mode::StashDeposit(info) => info,
Mode::StashWithdraw(info) => info,
_ => &mut binding,
};
if let Some(info) = info {
info.amount = info
.amount
.and_then(|amount| if amount <= 9 { None } else { Some(amount / 10) });
Ok(self)
} else {
let binding: Option<&mut Option<u32>> = None;
let amount = match &mut self.mode {
Mode::PayDebt(amount) => Some(amount),
Mode::BankDeposit(amount) => Some(amount),
Mode::BankWithdraw(amount) => Some(amount),
_ => binding,
};
if let Some(amount) = amount {
*amount =
amount.and_then(|amount| if amount <= 9 { None } else { Some(amount / 10) });
Ok(self)
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
}
pub fn commit_buy(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::Buying(Some(info)) = &self.mode {
let amount = info.amount.unwrap_or(0);
let good_price = self
.locations
.location_info(&self.location)
.prices
.get_good(&info.good);
let can_afford = self.gold / Saturating(*good_price);
if amount > can_afford.0 {
return Err(StateError::CannotAfford);
} else {
let hold_size = self.hold_size;
let current_hold = self.inventory.total_amount();
if current_hold.saturating_add(amount) > hold_size.0 {
return Err(StateError::InsufficientHold);
} else {
self.inventory.add_good(&info.good, amount);
self.gold -= good_price * amount;
self.mode = Mode::ViewingInventory;
return Ok(self);
}
}
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn commit_sell(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::Selling(Some(info)) = &self.mode {
let amount = info.amount.unwrap_or(0);
let good_price = self
.locations
.location_info(&self.location)
.prices
.get_good(&info.good);
let user_amount = self.inventory.get_good(&info.good);
if &amount > user_amount {
return Err(StateError::InsufficientInventory);
} else {
self.inventory.remove_good(&info.good, amount);
self.gold += good_price * amount;
self.mode = Mode::ViewingInventory;
return Ok(self);
}
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn commit_stash_deposit(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::StashDeposit(Some(info)) = &self.mode {
let amount = info.amount.unwrap_or(0);
let user_amount = self.inventory.get_good(&info.good);
if &amount > user_amount {
return Err(StateError::InsufficientInventory);
} else {
self.inventory.remove_good(&info.good, amount);
self.stash.add_good(&info.good, amount);
self.mode = Mode::ViewingInventory;
return Ok(self);
}
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn commit_stash_withdraw(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::StashWithdraw(Some(info)) = &self.mode {
let amount = info.amount.unwrap_or(0);
let stash_amount = self.stash.get_good(&info.good);
if &amount > stash_amount {
return Err(StateError::InsufficientStash);
} else {
self.stash.remove_good(&info.good, amount);
self.inventory.add_good(&info.good, amount);
self.mode = Mode::ViewingInventory;
return Ok(self);
}
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn commit_pay_debt(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::PayDebt(amount) = &self.mode {
let amount = amount.unwrap_or(0);
if amount > self.debt.0 {
return Err(StateError::PayDownAmountHigherThanDebt);
}
if amount > self.gold.0 {
return Err(StateError::CannotAfford);
}
self.debt -= amount;
self.gold -= amount;
self.mode = Mode::ViewingInventory;
return Ok(self);
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn commit_bank_deposit(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::BankDeposit(amount) = &self.mode {
let amount = amount.unwrap_or(0);
if amount > self.gold.0 {
return Err(StateError::CannotAfford);
}
self.gold -= amount;
self.bank += amount;
self.mode = Mode::ViewingInventory;
return Ok(self);
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn commit_bank_withdraw(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::BankWithdraw(amount) = &self.mode {
let amount = amount.unwrap_or(0);
if amount > self.bank.0 {
return Err(StateError::InsufficientBank);
}
self.gold += amount;
self.bank -= amount;
self.mode = Mode::ViewingInventory;
return Ok(self);
}
Err(StateError::InvalidMode(self.mode.clone()))
}
pub fn sail_to(&mut self, destination: &Location) -> Result<&mut GameState, StateError> {
if let Mode::Sailing = self.mode {
if destination == &self.location {
Err(StateError::AlreadyInLocation)
} else {
self.mode = Mode::ViewingInventory;
// increment the month
self.date.1 = self.date.1.succ();
if self.date.1 == Month::January {
self.date.0 += 1;
}
if self.date.0 == 1785 && self.date.1 == Month::March {
// 3 years have elapsed
// end the game
self.game_end = true
}
// update location info for location we just left
let player_net_worth = self.net_worth();
let new_location_info = self.locations.generate_location(
&mut self.rng,
destination,
self.location_config.personalities.get(destination),
true,
player_net_worth,
);
// set current location
self.location = *destination;
// increment debt, if any
let new_debt = f64::from(self.debt.0) * 1.1;
self.debt = Saturating(new_debt.floor() as u32);
// determine if we've encountered an event
if let Some(event) = &new_location_info.event {
self.mode = Mode::GameEvent(event.clone());
}
Ok(self)
}
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub fn cancel_sail_to(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::Sailing = self.mode {
self.mode = Mode::ViewingInventory;
Ok(self)
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub fn acknowledge_event(&mut self) -> Result<&mut GameState, StateError> {
if let Mode::GameEvent(_) = self.mode {
self.mode = Mode::ViewingInventory;
Ok(self)
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub fn remaining_hold(&self) -> u32 {
self.hold_size
.0
.saturating_sub(self.inventory.total_amount())
}
pub(crate) fn compute_goods_stolen(&mut self) -> GoodsStolenResult {
if let Mode::GameEvent(event) = &mut self.mode {
if let LocationEvent::GoodsStolen(info) = event.borrow_mut() {
if let Some(info) = info {
return *info;
} else {
// randomly select a good that we have inventory of
let goods_with_inventory = self
.inventory
.clone()
.into_iter()
.filter(|x| x.1 > 0)
.collect::<Vec<(Good, u32)>>();
let computed_info = if goods_with_inventory.is_empty() {
GoodsStolenResult::NothingStolen
} else {
let (good_stolen, amount_stolen) =
self.rng.gen_good_stolen(&goods_with_inventory);
GoodsStolenResult::WasStolen {
good: good_stolen,
amount: amount_stolen,
}
};
*info = Some(computed_info);
return computed_info;
}
}
}
GoodsStolenResult::NothingStolen
}
pub(crate) fn remove_stolen_goods(&mut self, goods_stolen_info: GoodsStolenResult) {
if let GoodsStolenResult::WasStolen { good, amount } = goods_stolen_info {
self.inventory.remove_good(&good, amount);
}
}
pub(crate) fn confirm_buy_cannon(&mut self) -> Result<(), StateError> {
if self.gold.0 >= CANNON_COST.into() {
self.gold -= Saturating(CANNON_COST as u32);
self.cannons += 1;
self.acknowledge_event()?;
}
Ok(())
}
pub(crate) fn proceed_pirate_encounter(&mut self) -> Result<(), StateError> {
if let Mode::GameEvent(LocationEvent::PirateEncounter(PirateEncounterState::Initial)) =
self.mode
{
let pirates = self.rng.gen_num_pirates_encountered();
self.mode = Mode::GameEvent(LocationEvent::PirateEncounter(
PirateEncounterState::Prompt {
info: PirateEncounterInfo::new(pirates),
},
));
Ok(())
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub(crate) fn pirate_run(&mut self) -> Result<(), StateError> {
if let Mode::GameEvent(LocationEvent::PirateEncounter(PirateEncounterState::Prompt {
info,
})) = self.mode
{
if self.rng.gen_run_success(info.cur_pirates) {
// success
self.mode = Mode::GameEvent(LocationEvent::PirateEncounter(
PirateEncounterState::RunSuccess,
));
} else {
// failure
self.mode = Mode::GameEvent(LocationEvent::PirateEncounter(
PirateEncounterState::RunFailure { info },
));
}
Ok(())
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub(crate) fn pirate_fight(&mut self) -> Result<(), StateError> {
if let Mode::GameEvent(LocationEvent::PirateEncounter(PirateEncounterState::Prompt {
info,
})) = self.mode
{
let did_kill_a_pirate = self.rng.gen_did_kill_a_pirate(self.cannons.0);
self.mode = Mode::GameEvent(LocationEvent::PirateEncounter(
PirateEncounterState::AttackResult {
info,
did_kill_a_pirate,
},
));
Ok(())
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub(crate) fn proceed_pirate_run_failure(&mut self) -> Result<(), StateError> {
if let Mode::GameEvent(LocationEvent::PirateEncounter(PirateEncounterState::RunFailure {
info,
})) = self.mode
{
self.mode = Mode::GameEvent(LocationEvent::PirateEncounter(
PirateEncounterState::pirates_attack(info, &mut self.rng)?,
));
Ok(())
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub(crate) fn proceed_pirates_attack(&mut self) -> Result<(), StateError> {
if let Mode::GameEvent(LocationEvent::PirateEncounter(
PirateEncounterState::PiratesAttack {
info:
PirateEncounterInfo {
health,
cur_pirates,
total_pirates,
},
damage_this_attack,
},
)) = self.mode
{
let health = health.saturating_sub(damage_this_attack);
if health == 0 {
self.mode = Mode::GameEvent(LocationEvent::PirateEncounter(
PirateEncounterState::Destroyed,
));
} else {
self.mode = Mode::GameEvent(LocationEvent::PirateEncounter(
PirateEncounterState::Prompt {
info: PirateEncounterInfo {
health,
cur_pirates,
total_pirates,
},
},
));
}
Ok(())
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub(crate) fn proceed_destroyed(&mut self) -> Result<(), StateError> {
if let Mode::GameEvent(LocationEvent::PirateEncounter(PirateEncounterState::Destroyed)) =
self.mode
{
// player loses their inventory and half of their gold to the pirates
self.inventory = Inventory::default();
self.gold = Saturating(self.gold.0.div_ceil(2));
self.mode = Mode::ViewingInventory;
Ok(())
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub(crate) fn proceed_pirate_run_success(&mut self) -> Result<(), StateError> {
if let Mode::GameEvent(LocationEvent::PirateEncounter(PirateEncounterState::RunSuccess)) =
self.mode
{
self.mode = Mode::ViewingInventory;
Ok(())
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub(crate) fn proceed_attack_result(&mut self) -> Result<(), StateError> {
if let Mode::GameEvent(LocationEvent::PirateEncounter(
PirateEncounterState::AttackResult {
info,
did_kill_a_pirate,
},
)) = self.mode
{
let cur_pirates =
info.cur_pirates
.saturating_sub(if did_kill_a_pirate { 1 } else { 0 });
if cur_pirates == 0 {
// player recovers some gold from wreckage
let gold_recovered = self
.rng
.gen_gold_recovered_from_pirate_encounter(info.total_pirates);
self.mode = Mode::GameEvent(LocationEvent::PirateEncounter(
PirateEncounterState::Victory { gold_recovered },
));
} else {
self.mode = Mode::GameEvent(LocationEvent::PirateEncounter(
PirateEncounterState::pirates_attack(
PirateEncounterInfo {
health: info.health,
cur_pirates,
total_pirates: info.cur_pirates,
},
&mut self.rng,
)?,
))
}
Ok(())
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub(crate) fn proceed_pirate_encounter_victory(&mut self) -> Result<(), StateError> {
if let Mode::GameEvent(LocationEvent::PirateEncounter(PirateEncounterState::Victory {
gold_recovered,
})) = self.mode
{
self.gold += gold_recovered;
self.mode = Mode::ViewingInventory;
Ok(())
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub(crate) fn back(&mut self) -> Result<(), StateError> {
if let Mode::Buying(Some(_)) = &self.mode {
self.mode = Mode::Buying(None);
Ok(())
} else if let Mode::Buying(None) = &self.mode {
self.mode = Mode::ViewingInventory;
Ok(())
} else if let Mode::Selling(Some(_)) = &self.mode {
self.mode = Mode::Selling(None);
Ok(())
} else if let Mode::Selling(None) = &self.mode {
self.mode = Mode::ViewingInventory;
Ok(())
} else if let Mode::Sailing = &self.mode {
self.mode = Mode::ViewingInventory;
Ok(())
} else if let Mode::StashDeposit(Some(_)) = &self.mode {
self.mode = Mode::StashDeposit(None);
Ok(())
} else if let Mode::StashDeposit(None) = &self.mode {
self.mode = Mode::ViewingInventory;
Ok(())
} else if let Mode::StashWithdraw(Some(_)) = &self.mode {
self.mode = Mode::StashWithdraw(None);
Ok(())
} else if let Mode::StashWithdraw(None) = &self.mode {
self.mode = Mode::ViewingInventory;
Ok(())
} else if let Mode::PayDebt(None) = &self.mode {
self.mode = Mode::ViewingInventory;
Ok(())
} else if let Mode::BankDeposit(None) = &self.mode {
self.mode = Mode::ViewingInventory;
Ok(())
} else if let Mode::BankWithdraw(None) = &self.mode {
self.mode = Mode::ViewingInventory;
Ok(())
} else {
Err(StateError::InvalidMode(self.mode.clone()))
}
}
pub(crate) fn confirm_buy_hold_space(
&mut self,
price: u32,
more_hold: u32,
) -> Result<(), StateError> {
if self.gold.0 >= price {
self.gold -= Saturating(price);
self.hold_size += more_hold;
self.acknowledge_event()?;
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GoodsStolenResult {
NothingStolen,
WasStolen { good: Good, amount: u32 },
}