Skip to content

Instantly share code, notes, and snippets.

@lcanady
Created July 29, 2024 11:59
Show Gist options
  • Save lcanady/54c612b306ce338c38d4fbde3d960f68 to your computer and use it in GitHub Desktop.
Save lcanady/54c612b306ce338c38d4fbde3d960f68 to your computer and use it in GitHub Desktop.
Damage system shell script
#!/bin/bash
# World of Darkness Precise Damage and Healing Calculator
# Writtren by Diablerie @ Dies Irae MUSH
# Read input from environment variables
bashing=${MUSHQ_0:-0}
lethal=${MUSHQ_1:-0}
agg=${MUSHQ_2:-0}
damage=${MUSHQ_3:-0}
type=${MUSHQ_4:-"bashing"}
health_levels=${MUSHQ_5:-7}
char_type=${MUSHQ_6:-"mortal"}
# Function to apply damage or healing
apply_damage_or_healing() {
local current_bashing=$1
local current_lethal=$2
local current_agg=$3
local change=$4
local damage_type=$5
local health_levels=$6
local new_bashing=$current_bashing
local new_lethal=$current_lethal
local new_agg=$current_agg
# Handle healing (negative damage)
if (( change < 0 )); then
local heal_amount=$((0 - change)) # Convert to positive for easier calculation
case $damage_type in
"bashing")
new_bashing=$((new_bashing - heal_amount))
if (( new_bashing < 0 )); then
new_bashing=0
fi
;;
"lethal")
if (( heal_amount > new_lethal )); then
local excess=$((heal_amount - new_lethal))
new_lethal=0
new_bashing=$((new_bashing - excess))
if (( new_bashing < 0 )); then
new_bashing=0
fi
else
new_lethal=$((new_lethal - heal_amount))
fi
;;
"aggravated")
if (( heal_amount > new_agg )); then
local excess=$((heal_amount - new_agg))
new_agg=0
if (( excess > new_lethal )); then
local bashing_heal=$((excess - new_lethal))
new_lethal=0
new_bashing=$((new_bashing - bashing_heal))
if (( new_bashing < 0 )); then
new_bashing=0
fi
else
new_lethal=$((new_lethal - excess))
fi
else
new_agg=$((new_agg - heal_amount))
fi
;;
esac
else
# Handle damage (positive change)
for ((i=1; i<=change; i++)); do
local total_damage=$((new_bashing + new_lethal + new_agg))
if [[ $damage_type == "bashing" ]]; then
if (( total_damage < health_levels )); then
new_bashing=$((new_bashing + 1))
elif (( new_bashing > 0 )); then
new_lethal=$((new_lethal + 1))
new_bashing=$((new_bashing - 1))
elif (( new_lethal > 0 )); then
new_agg=$((new_agg + 1))
new_lethal=$((new_lethal - 1))
else
break
fi
elif [[ $damage_type == "lethal" ]]; then
if (( total_damage < health_levels )); then
new_lethal=$((new_lethal + 1))
elif (( new_lethal > 0 || new_bashing > 0 )); then
new_agg=$((new_agg + 1))
if (( new_lethal > 0 )); then
new_lethal=$((new_lethal - 1))
else
new_bashing=$((new_bashing - 1))
fi
else
break
fi
elif [[ $damage_type == "aggravated" ]]; then
if (( new_agg < health_levels )); then
new_agg=$((new_agg + 1))
if (( new_lethal > 0 )); then
new_lethal=$((new_lethal - 1))
elif (( new_bashing > 0 )); then
new_bashing=$((new_bashing - 1))
fi
else
break
fi
fi
done
fi
# Ensure no negative values
if (( new_bashing < 0 )); then new_bashing=0; fi
if (( new_lethal < 0 )); then new_lethal=0; fi
if (( new_agg < 0 )); then new_agg=0; fi
echo "$new_bashing $new_lethal $new_agg"
}
# Function to calculate penalty and determine injury level
calculate_penalty_and_injury() {
local total_damage=$1
local health_levels=$2
local new_bashing=$3
local new_lethal=$4
local new_agg=$5
local penalty=0
local injury_level="Healthy"
local standard_levels=7
local bruised_levels=$((health_levels - standard_levels))
if (( total_damage > bruised_levels )); then
local effective_damage=$((total_damage - bruised_levels))
if (( effective_damage >= 7 )); then
penalty=5
if (( new_agg > 0 )); then
injury_level="Dead"
elif (( new_lethal > 0 )); then
injury_level="Incapacitated"
else
injury_level="Unconscious"
fi
elif (( effective_damage == 6 )); then
penalty=5
injury_level="Crippled"
elif (( effective_damage == 5 )); then
penalty=2
injury_level="Mauled"
elif (( effective_damage == 4 )); then
penalty=1
injury_level="Wounded"
elif (( effective_damage == 3 )); then
penalty=1
injury_level="Injured"
elif (( effective_damage == 2 )); then
penalty=1
injury_level="Hurt"
elif (( effective_damage == 1 )); then
injury_level="Bruised"
fi
elif (( total_damage > 0 )); then
injury_level="Bruised"
fi
echo "$penalty $injury_level"
}
# Apply damage or healing and get updated values
new_damages=$(apply_damage_or_healing $bashing $lethal $agg $damage $type $health_levels)
read new_bashing new_lethal new_agg <<< $new_damages
# Calculate total damage, penalty, and injury level
total_damage=$((new_bashing + new_lethal + new_agg))
penalty_and_injury=$(calculate_penalty_and_injury $total_damage $health_levels $new_bashing $new_lethal $new_agg)
read penalty injury_level <<< $penalty_and_injury
# Output the result in the specified format
echo "$new_bashing|$new_lethal|$new_agg|$penalty|$injury_level"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment