Skip to content

Instantly share code, notes, and snippets.

@LVMBDV
Last active November 3, 2019 22:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LVMBDV/bb1449c87fab689a476d4dd7c949d111 to your computer and use it in GitHub Desktop.
Save LVMBDV/bb1449c87fab689a476d4dd7c949d111 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Copyright 2018 Ata Can Kuyumcu
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
FUEL_COST=5
FOOD_COST=2
START_FUEL=50
START_FOOD=80
START_PARTS=2
START_CREW_SIZE=5
RANDOM_EVENTS=(ILLNESS BREAKDOWN FLUX RAIDERS BLACKHOLE)
LANDMARKS=(
"Pluto, long since occupied with long-range sensors and scanners, stands ready to, and indeed continues to probe the far reaches of the galaxy."
"At the edge of the Sol system lies a treacherous asteroid belt. Many have been crushed by stray asteroids and misguided judgement."
"The nearest star system to Sol, in ages past it stood as a reminder of the boundaries of sub-light travel, now a low-population sanctuary for adventurers and traders."
"This region of space is particularly devoid of matter. Such low-density pockets are known to exist, but the vastness of it is astounding."
"Rigel Prime, the center of the Rigel system, burns hot, basking its planetary bodies in warmth and radiation."
"Tau Ceti Beta has recently become a waypoint for colonists headed towards Orion. There are many ships and makeshift stations in the vicinity."
"You have come into range of the first man-made structure in this region of space. It has been constructed not by travellers from Sol, but by colonists from Orion. It stands as a monument to the colonists' success."
"๐ŸŽŠ You have made it to Orion! Congratulations! Your crew is one of the few to start a new foothold for mankind!"
)
function initialize {
NAMES=($(who | awk '{print $1}' | sort | uniq) carmen charles sam nick isabelle jean emilio ian pat theodore)
CREW=($(whoami))
while [ ${#CREW[@]} -lt $START_CREW_SIZE ]; do
name=${NAMES[$[$RANDOM % ${#NAMES[@]}]]}
unique=true
for index in $(seq 0 $[${#CREW[@]} - 1]); do
if [ ${CREW[$index]} = $name ]; then
unique=false
break
fi
done
if [ $unique = true ]; then
CREW+=($name)
fi
done
CREW=($(printf "${CREW[*]}" | sed 's/[^ ]\+/\L\u&/g'))
STATE=START
TURN=0
FUEL=$START_FUEL
FOOD=$START_FOOD
SPARE_PARTS=$START_PARTS
}
function consume_fuel {
((FUEL-=FUEL_COST))
}
function consume_food {
((FOOD-=FOOD_COST*${#CREW[@]}))
}
function spend_day {
consume_fuel
consume_food
check_inventory
return $?
}
function check_inventory {
if [ $TURN -ge $((${#LANDMARKS[@]} - 1)) ]; then
STATE=WIN
elif [ ${#CREW[@]} -le 0 ]; then
printf '๐Ÿ’€ Your entire crew died, and your ship joins the fleet of ghost-ships littering the galaxy.\n'
STATE=LOSE
elif [ $FUEL -le 0 ]; then
printf '๐Ÿ’€ You ran out of fuel, and drift, slowly, into a star.\n'
STATE=LOSE
elif [ $FOOD -le 0 ]; then
printf '๐Ÿ’€ You ran out of food and starved.\n'
STATE=LOSE
fi
game_in_progress
return $?
}
function game_in_progress {
[ $STATE != WIN ] && [ $STATE != LOSE ]
}
function get_choice {
while true; do
read -s -n 1 CHOICE
[[ "$1" =~ "$CHOICE" ]] && [[ -n "$CHOICE" ]] && break
done
}
function start_turn {
printf "${LANDMARKS[$[$TURN]]}\n"
if check_inventory; then
printf "Your crew consists of ${CREW[*]}.\nYou have ${SPARE_PARTS} spare parts.\nYou have ${FOOD} units of food and ${FUEL} units of fuel.\nYou can kill (a) crew member to conserve food supplies or (s)pare them?\n"
get_choice 'as'
if [ $CHOICE = 'a' ]; then
printf "โ˜ ๏ธ You have chosen to kill ${CREW[0]}.\n"
CREW=("${CREW[@]:1}")
fi
check_inventory
return $?
fi
return 1
}
function random_event {
if [ $TURN -eq 2 ] && [ $[$RANDOM % 100] -lt 30 ]; then
STATE=COLLISION
elif [ $[$RANDOM % 100] -lt 75 ]; then
STATE=${RANDOM_EVENTS[$[$RANDOM % ${#RANDOM_EVENTS[@]}]]}
else
STATE=NOTHING
fi
process_event
return $?
}
function process_event {
case $STATE in
ILLNESS)
printf "โ˜ฃ๏ธ A deadly illness has been contracted!\n${CREW[0]} was killed by the disease.\n"
CREW=("${CREW[@]:1}")
;;
BREAKDOWN)
if [ $SPARE_PARTS -gt 0 ]; then
printf "๐Ÿ”ง Oh no! The engine has broken down!\nYou can repair it with an engine p(a)rt, or you can make repair(s) for 3 days.\n"
get_choice 'as'
if [ $CHOICE = 'a' ]; then
((SPARE_PARTS--))
else
consume_food && consume_food
fi
else
printf "๐Ÿ”ง Oh no! The engine has broken down!\nYou have to make repairs for 3 days.\n"
consume_food && consume_food
fi
;;
FLUX)
printf "๐ŸŒŠ This region of space is highly turbulent.\nIf we go (s)lowly we may avoid more damage, but if we keep our spee(d) we won't waste supplies.\n"
get_choice 'sd'
if [ $CHOICE = 's' ]; then
consume_food && consume_food
else
if [ $[$RANDOM % 100] -lt 75 ]; then
STATE=BREAKDOWN
process_event
fi
fi
;;
RAIDERS)
if [ $[$RANDOM % 100] -lt 50 ]; then
STOLEN_FOOD=$((($RANDOM % 10) + 1))
STOLEN_FOOD=$(($STOLEN_FOOD <= $FOOD ? $STOLEN_FOOD : $FOOD))
((FOOD-=STOLEN_FOOD))
STOLEN_FUEL=$((($RANDOM % 10) + 1))
STOLEN_FUEL=$(($STOLEN_FUEL <= $FUEL ? $STOLEN_FUEL : $FUEL))
((FUEL-=STOLEN_FUEL))
printf "๐Ÿ˜ˆ Raiders have come aboard your ship!\nThey have stolen $STOLEN_FOOD food and $STOLEN_FUEL fuel.\n"
elif [ $[$RANDOM % 100] -lt 10 ]; then
printf "โ˜ ๏ธ Raiders have come aboard your ship!\n${CREW[0]} tried to fight back, but was killed.\n"
CREW=("${CREW[@]:1}")
else
printf "โš”๏ธ Raiders have come aboard your ship!\nFortunately, you fended them off without any trouble.\n"
fi
;;
BLACKHOLE)
printf "โš ๏ธ Sensors indicate that a black hole's gravitational field is affecting the region of space we were headed through.\nWe could (s)tay of course, but risk of being overcome by its gravity, or we could change course to go (a)round, which will take longer.\n"
get_choice 'as'
if [ $CHOICE = 'a' ]; then
spend_day && spend_day
else
if [ $[$RANDOM % 100] -lt 50 ]; then
printf "You were swept away into the black hole.\n"
STATE=LOSE
fi
fi
;;
COLLISION)
LOST_FOOD=$((($RANDOM % 10) + 1))
LOST_FOOD=$(($LOST_FOOD <= $FOOD ? $LOST_FOOD : $FOOD))
((FOOD-=LOST_FOOD))
LOST_FUEL=$((($RANDOM % 10) + 1))
LOST_FUEL=$(($LOST_FUEL <= $FUEL ? $LOST_FUEL : $FUEL))
((FUEL-=LOST_FUEL))
if [ $SPARE_PARTS -gt 0 ]; then
printf "๐Ÿ’ฅ Something hit us! Looks like there's some hull damage.\n$LOST_FOOD food and $LOST_FUEL fuel was vented out into space.\nYou can rep(a)ir the damage with hull plates, or you can (s)pend the next 3 days welding scrap together.\n"
get_choice 'as'
if [ $CHOICE = 'a' ]; then
((SPARE_PARTS--))
else
consume_food && consume_food
fi
else
printf "๐Ÿ’ฅ Something hit us! We've sustained some hull damage! \nYou have to make repairs for 3 days.\n"
consume_food && consume_food
fi
;;
esac
check_inventory
return $?
}
printf "\
___ _ _ ____ ____ ____ _ ____ _ _ ___ ____ ____ _ _
| |__| |___ | | |__/ | | | |\ | | |__/ |__| | |
| | | |___ |__| | \ | |__| | \| | | \ | | | |___
[options are between parentheses e.g. press s to go (s)lowly]\n\n"
initialize
while game_in_progress; do
start_turn && random_event && spend_day && ((TURN++))
done
if [ $STATE = WIN ]; then
CREW_ALIVE=${#CREW[@]}
printf "Your score is $(((FUEL*FUEL_COST)+(FOOD*FOOD_COST)+(CREW_ALIVE*100)))!\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment