| 1 |
# Source: https://github.com/nushell/nu_scripts/blob/e82487722bce88c11f6b31788142b29e4c40bda4/games/paperclips/game.nu |
| 2 |
|
| 3 |
# nu-niversal paperclips - a (somewhat) faithful clone of Universal Paperclips |
| 4 |
# play the original at: https://www.decisionproblem.com/paperclips/index2.html |
| 5 |
|
| 6 |
# NOTE: scalars are off cuz i'm counting by pennies instead of dollars and cents |
| 7 |
|
| 8 |
const carriage_return = "\n\r" |
| 9 |
|
| 10 |
# Resets cursor to home position and hides it to prevent visual distraction |
| 11 |
def reset-cursor [] { |
| 12 |
print -n "\e[H" # Move cursor to home position (top-left) |
| 13 |
print -n "\e[?25l" # Hide cursor |
| 14 |
} |
| 15 |
|
| 16 |
# Clears all content from cursor position to end of screen |
| 17 |
def clear-to-end [] { |
| 18 |
print -n "\e[J" # Clear from cursor to end of screen |
| 19 |
} |
| 20 |
|
| 21 |
# Updates the terminal display with current game state |
| 22 |
def refresh-game-view [state] { |
| 23 |
reset-cursor |
| 24 |
render-game-display $state |
| 25 |
} |
| 26 |
|
| 27 |
def make-paperclip [state, amount: int] { |
| 28 |
mut new_state = $state |
| 29 |
$new_state.paperclips.total += $amount |
| 30 |
$new_state.paperclips.stock += $amount |
| 31 |
$new_state.wire.length -= $amount |
| 32 |
$new_state.paperclips.current_second_clips += $amount # Add this line |
| 33 |
$new_state |
| 34 |
} |
| 35 |
|
| 36 |
def sell-paperclips [state] { |
| 37 |
if $state.paperclips.stock == 0 { return $state } |
| 38 |
# numbers picked by the OG, i haven't sussed out exactly why. |
| 39 |
if (random float 0.0..1.0) > ($state.market.demand / 100) { return $state } |
| 40 |
mut amount = 0.7 * ($state.market.demand ** 1.15) | math floor |
| 41 |
if $amount > $state.paperclips.stock { $amount = $state.paperclips.stock } |
| 42 |
mut new_state = $state |
| 43 |
let transaction = $amount * $state.paperclips.price |
| 44 |
$new_state.money += $transaction |
| 45 |
$new_state.paperclips.stock -= $amount |
| 46 |
$new_state |
| 47 |
} |
| 48 |
|
| 49 |
def update-demand [state] { |
| 50 |
let marketing = 1.1 ** ($state.market.level - 1) |
| 51 |
mut new_state = $state |
| 52 |
$new_state.market.demand = ((.8 / $state.paperclips.price) * $marketing) * 1000 |
| 53 |
$new_state |
| 54 |
} |
| 55 |
|
| 56 |
def get-user-input [] { |
| 57 |
let key = (input listen --types [key]) |
| 58 |
if ($key.code == 'c') and ($key.modifiers == ['keymodifiers(control)']) { |
| 59 |
print -n "\e[?25h" # Show cursor before exiting |
| 60 |
exit |
| 61 |
} |
| 62 |
$key.code | job send 0 |
| 63 |
} |
| 64 |
|
| 65 |
def format-text [prefix: string, value: number, --money --round] { |
| 66 |
mut value = $value |
| 67 |
if $money { |
| 68 |
$value = $value / 100.0 | math round --precision 2 |
| 69 |
} |
| 70 |
if $round { |
| 71 |
$value = $value | math round --precision 2 |
| 72 |
} |
| 73 |
# e.g. "Paperclips: XXX\n\r" or "Unsold Inventory: $X.XX\n\r" |
| 74 |
$"($prefix): (if $money { "$" } else { })($value | into string --group-digits)(if $round { "%" } else { })($carriage_return)" |
| 75 |
} |
| 76 |
|
| 77 |
# Prints text after clearing the current line with ANSI escape code |
| 78 |
def clear-line [text] { |
| 79 |
print -n $"\e[2K($text)" |
| 80 |
} |
| 81 |
|
| 82 |
def render-game-display [state] { |
| 83 |
# Game title and stats |
| 84 |
clear-line (format-text "Paperclips" $state.paperclips.total) |
| 85 |
clear-line $carriage_return |
| 86 |
|
| 87 |
# Business section |
| 88 |
clear-line "Business:\n\r" |
| 89 |
clear-line "---------------\n\r" |
| 90 |
clear-line (format-text 'Available Funds' $state.money --money) |
| 91 |
clear-line (format-text 'Unsold Inventory' $state.paperclips.stock) |
| 92 |
clear-line (format-text 'Price Per Clip' $state.paperclips.price --money) |
| 93 |
clear-line (format-text 'Public Demand' $state.market.demand --round) |
| 94 |
clear-line $carriage_return |
| 95 |
clear-line (format-text 'Marketing Level' $state.market.level) |
| 96 |
clear-line (format-text 'Marketing cost' $state.market.cost --money) |
| 97 |
clear-line $carriage_return |
| 98 |
|
| 99 |
# Manufacturing section |
| 100 |
clear-line "Manufacturing:\n\r" |
| 101 |
clear-line "---------------\n\r" |
| 102 |
clear-line (format-text 'Clips per Second' $state.paperclips.rate) |
| 103 |
clear-line $carriage_return |
| 104 |
|
| 105 |
# Wire section |
| 106 |
if $state.wire.length == 1 { "inch" } else { "inches" } |
| 107 |
clear-line $"Wire: ($state.wire.length | into string -g) ( |
| 108 |
if $state.wire.length == 1 { "inch" } else { "inches" } |
| 109 |
)\n\r" |
| 110 |
clear-line (format-text 'Wire cost' $state.wire.cost --money) |
| 111 |
clear-line $carriage_return |
| 112 |
|
| 113 |
# Autoclippers (conditional) |
| 114 |
if $state.autoclippers.unlocked { |
| 115 |
clear-line (format-text "Autoclippers" $state.autoclippers.count) |
| 116 |
clear-line (format-text 'Autoclipper cost' $state.autoclippers.cost --money) |
| 117 |
clear-line $carriage_return |
| 118 |
} |
| 119 |
|
| 120 |
# Controls |
| 121 |
clear-line $"($state.control_line)\n\r" |
| 122 |
|
| 123 |
# Clean up any trailing content from previous longer displays |
| 124 |
clear-to-end |
| 125 |
} |
| 126 |
|
| 127 |
def main [] { |
| 128 |
reset-cursor |
| 129 |
print "Welcome to nu-niversal paperclips!" |
| 130 |
clear-to-end |
| 131 |
sleep 2sec |
| 132 |
reset-cursor |
| 133 |
let table_name = "clip_message_queue" |
| 134 |
|
| 135 |
mut state = { |
| 136 |
delta_time: 0sec |
| 137 |
paperclips: { |
| 138 |
total: 0 |
| 139 |
stock: 0 |
| 140 |
price: 25 |
| 141 |
rate: 0 # Changed from -1 to 0: This will show last second's rate |
| 142 |
current_second_clips: 0 # Add: Accumulator for current second |
| 143 |
} |
| 144 |
last_second_timestamp: (date now) # Add: Track when current second started |
| 145 |
market: { |
| 146 |
demand: 0 |
| 147 |
level: 1 |
| 148 |
cost: 10000 |
| 149 |
} |
| 150 |
money: 0 |
| 151 |
control_line: "controls: [a]dd paperclip; buy [w]ire; price [u]p; price [d]own; [q]uit" |
| 152 |
wire: { |
| 153 |
length: 1000 |
| 154 |
cost: 2000 |
| 155 |
} |
| 156 |
autoclippers: { |
| 157 |
count: 0 |
| 158 |
unlocked: false |
| 159 |
cost: 1000 |
| 160 |
multiplier: 1.25 |
| 161 |
} |
| 162 |
} |
| 163 |
let save_exists = ('./gamestate.nuon' | path exists) |
| 164 |
if $save_exists { |
| 165 |
$state = open './gamestate.nuon' |
| 166 |
} |
| 167 |
# initial setup |
| 168 |
$state = update-demand $state |
| 169 |
refresh-game-view $state |
| 170 |
get-user-input |
| 171 |
|
| 172 |
loop { |
| 173 |
let seconds = $state.delta_time |
| 174 |
if ($seconds) > (1sec) { |
| 175 |
$state.delta_time -= $seconds |
| 176 |
# TODO: tweak numbers |
| 177 |
if (random bool --bias 0.5) { |
| 178 |
let deviation = (0.75 + (random float 0.0..0.50)) |
| 179 |
$state.wire.cost *= $deviation |
| 180 |
if $state.wire.cost < 1200 { |
| 181 |
$state.wire.cost = 1200 |
| 182 |
} else if $state.wire.cost > 3000 { |
| 183 |
$state.wire.cost = 3000 |
| 184 |
} |
| 185 |
} |
| 186 |
$state = update-demand $state |
| 187 |
|
| 188 |
# Calculate total autoclipper production for this time period |
| 189 |
let amount = (($seconds | into int | $in / 1000000000) * $state.autoclippers.count) | math round |
| 190 |
mut index = 0 |
| 191 |
while $index < $amount { |
| 192 |
$state = make-paperclip $state 1 |
| 193 |
$index += 1 |
| 194 |
} |
| 195 |
|
| 196 |
$state = sell-paperclips $state |
| 197 |
} |
| 198 |
let prev = date now |
| 199 |
|
| 200 |
# Check if a second has passed and roll over counters |
| 201 |
let now = date now |
| 202 |
if ($now - $state.last_second_timestamp) >= 1sec { |
| 203 |
$state.paperclips.rate = $state.paperclips.current_second_clips |
| 204 |
$state.paperclips.current_second_clips = 0 |
| 205 |
$state.last_second_timestamp = $now |
| 206 |
} |
| 207 |
|
| 208 |
if (($state.paperclips.total > 9) and ($state.autoclippers.count < 1)) and not $state.autoclippers.unlocked { |
| 209 |
$state.autoclippers.unlocked = true |
| 210 |
$state.control_line += "; [b]uy autoclipper" |
| 211 |
} |
| 212 |
|
| 213 |
# Update display with current game state |
| 214 |
refresh-game-view $state |
| 215 |
|
| 216 |
try { |
| 217 |
let key = job recv --timeout 0sec |
| 218 |
match $key { |
| 219 |
"a" => { |
| 220 |
# TODO: add some way to grow amount? |
| 221 |
$state = make-paperclip $state 1 |
| 222 |
} |
| 223 |
"b" => { |
| 224 |
if $state.money >= $state.autoclippers.cost { |
| 225 |
$state.money -= $state.autoclippers.cost |
| 226 |
$state.autoclippers.count += 1 |
| 227 |
$state.autoclippers.cost = ($state.autoclippers.cost * 1.25 | math round) |
| 228 |
} |
| 229 |
} |
| 230 |
"d" => { |
| 231 |
if $state.paperclips.price > 1 { |
| 232 |
$state.paperclips.price -= 1 |
| 233 |
$state = update-demand $state |
| 234 |
} |
| 235 |
} |
| 236 |
"q" => { |
| 237 |
if not $save_exists { |
| 238 |
$state | save gamestate.nuon |
| 239 |
} else { |
| 240 |
$state | save gamestate.nuon --force |
| 241 |
} |
| 242 |
print -n "\e[?25h" # Show cursor before exiting |
| 243 |
break |
| 244 |
} |
| 245 |
"u" => { |
| 246 |
$state.paperclips.price += 1 |
| 247 |
$state = update-demand $state |
| 248 |
} |
| 249 |
"w" => { |
| 250 |
if $state.money >= $state.wire.cost { |
| 251 |
$state.wire.length += 1000 |
| 252 |
$state.money -= $state.wire.cost |
| 253 |
} |
| 254 |
} |
| 255 |
_ => { |
| 256 |
"" |
| 257 |
} |
| 258 |
} |
| 259 |
job spawn { get-user-input } |
| 260 |
} |
| 261 |
|
| 262 |
# for 60fps framerate lock |
| 263 |
sleep 16.666ms |
| 264 |
let now = date now |
| 265 |
let delta = $now - $prev |
| 266 |
$state.delta_time += $delta |
| 267 |
} |
| 268 |
} |