@@ -24,7 +24,7 @@ pub struct TradeBehavior {
2424}
2525
2626impl TradeBehavior {
27- pub const BUY_THRESHOLD : f64 = 0.1 ;
27+ pub const BUY_THRESHOLD : f64 = 0.3 ;
2828 pub const SELL_THRESHOLD : f64 = 0.9 ;
2929}
3030
@@ -84,19 +84,26 @@ impl Behavior for TradeBehavior {
8484
8585 let in_vault = vault. get ( id) . as_u32 ( ) ;
8686
87- if in_vault > 0 && gold > 0 && amount < capacity * Self :: BUY_THRESHOLD {
87+ if in_vault > 0 && gold > 0 && amount < ( capacity * Self :: BUY_THRESHOLD ) {
8888 match id {
8989 ResourceId :: Food => push ! ( BuyResourcesBehavior , Food ) ,
9090 ResourceId :: Iron => push ! ( BuyResourcesBehavior , Iron ) ,
9191 ResourceId :: Stone => push ! ( BuyResourcesBehavior , Stone ) ,
9292 ResourceId :: Wood => push ! ( BuyResourcesBehavior , Wood ) ,
9393 }
94- } else if amount > capacity * Self :: SELL_THRESHOLD {
95- match id {
96- ResourceId :: Food => push ! ( SellResourcesBehavior , Food ) ,
97- ResourceId :: Iron => push ! ( SellResourcesBehavior , Iron ) ,
98- ResourceId :: Stone => push ! ( SellResourcesBehavior , Stone ) ,
99- ResourceId :: Wood => push ! ( SellResourcesBehavior , Wood ) ,
94+ } else {
95+ let threshold = capacity * Self :: SELL_THRESHOLD ;
96+ let surplus = ( amount - threshold) . floor ( ) . max ( 0.0 ) as u32 ;
97+ if surplus > 0
98+ && in_vault. saturating_add ( surplus) < u32:: MAX
99+ && gold. saturating_add ( surplus) < u32:: MAX
100+ {
101+ match id {
102+ ResourceId :: Food => push ! ( SellResourcesBehavior , Food ) ,
103+ ResourceId :: Iron => push ! ( SellResourcesBehavior , Iron ) ,
104+ ResourceId :: Stone => push ! ( SellResourcesBehavior , Stone ) ,
105+ ResourceId :: Wood => push ! ( SellResourcesBehavior , Wood ) ,
106+ }
100107 }
101108 }
102109 }
0 commit comments