about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/Xanthous/App.hs9
-rw-r--r--src/Xanthous/Random.hs15
-rw-r--r--src/Xanthous/messages.yaml3
3 files changed, 26 insertions, 1 deletions
diff --git a/src/Xanthous/App.hs b/src/Xanthous/App.hs
index 24073c51097a..ea1405e463fe 100644
--- a/src/Xanthous/App.hs
+++ b/src/Xanthous/App.hs
@@ -548,9 +548,16 @@ attackAt pos =
         msg <- uses character getAttackMessage
         message msg msgParams
         entities . ix creatureID . positioned .= SomeEntity creature'
+
+    whenM (uses character $ isNothing . weapon)
+      $ whenM (chance (0.08 :: Float)) $ do
+        say_ ["combat", "fistSelfDamage"]
+        character %= Character.damage 1
+
     stepGame -- TODO
+  weapon chr = chr ^? inventory . wielded . wieldedItems . wieldableItem
   getAttackMessage chr =
-    case chr ^? inventory . wielded . wieldedItems . wieldableItem of
+    case weapon chr of
       Just wi ->
         fromMaybe (Messages.lookup ["combat", "hit", "generic"])
         $ wi ^. attackMessage
diff --git a/src/Xanthous/Random.hs b/src/Xanthous/Random.hs
index 3cb0b068d3d7..41c80ab73c4c 100644
--- a/src/Xanthous/Random.hs
+++ b/src/Xanthous/Random.hs
@@ -9,6 +9,7 @@ module Xanthous.Random
   , evenlyWeighted
   , weightedBy
   , subRand
+  , chance
   ) where
 --------------------------------------------------------------------------------
 import Xanthous.Prelude
@@ -85,3 +86,17 @@ instance (Num w, Ord w, Distribution Uniform w, Excludable w) => Choose (Weighte
 
 subRand :: MonadRandom m => Rand StdGen a -> m a
 subRand sub = evalRand sub . mkStdGen <$> getRandom
+
+-- | Has a @n@ chance of returning 'True'
+--
+-- eg, chance 0.5 will return 'True' half the time
+chance
+  :: (Num w, Ord w, Distribution Uniform w, Excludable w, MonadRandom m)
+  => w
+  -> m Bool
+chance n = choose $ weightedBy (bool 1 (n * 2)) bools
+
+--------------------------------------------------------------------------------
+
+bools :: NonEmpty Bool
+bools = True :| [False]
diff --git a/src/Xanthous/messages.yaml b/src/Xanthous/messages.yaml
index ed592e26507e..40426423783c 100644
--- a/src/Xanthous/messages.yaml
+++ b/src/Xanthous/messages.yaml
@@ -57,6 +57,9 @@ character:
 combat:
   nothingToAttack: There's nothing to attack there.
   menu: Which creature would you like to attack?
+  fistSelfDamage:
+    - You hit so hard with your fists you hurt yourself!
+    - The punch leaves your knuckles bloody!
   hit:
     fists:
       - You punch the {{creature.creatureType.name}} with your bare fists! It hurts. A lot.