From 8619785617eb73f8a4246c69b115b76769130d6f Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Tue, 6 Jul 2021 21:25:00 +0100 Subject: Initial Commit --- player.tcl | 37 +++++++++++++++++++++++++ resources.tcl | 40 +++++++++++++++++++++++++++ town | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ town.tcl | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 239 insertions(+) create mode 100644 player.tcl create mode 100644 resources.tcl create mode 100755 town create mode 100644 town.tcl diff --git a/player.tcl b/player.tcl new file mode 100644 index 0000000..156c1f8 --- /dev/null +++ b/player.tcl @@ -0,0 +1,37 @@ +oo::class create player { + constructor {} { + my variable satiation + set satiation 100 + } + + method status {} { + my variable satiation + return "Satiation: $satiation" + } + + method died {} { + my variable satiation + return [expr $satiation <= 0] + } + + method update {} { + my variable satiation + incr satiation -2 + } + + method eat {food} { + if {$food != ""} { + my variable satiation + set satiation [expr min($satiation + [$food value], 100)] + $food eat + } else { + puts "The town has no food." + } + } + + method work {amount} { + my variable satiation + set satiation [expr $satiation - $amount] + puts "You wipe the sweat from your brow." + } +} diff --git a/resources.tcl b/resources.tcl new file mode 100644 index 0000000..de0a730 --- /dev/null +++ b/resources.tcl @@ -0,0 +1,40 @@ +oo::class create food { + method title {} { + my variable name + return $name + } + + method value {} { + my variable satiation + return $satiation + } + + method eat {} { + my variable consumption + puts $consumption + } +} + +oo::class create water { + superclass food + + constructor {} { + my variable satiation consumption name + set satiation 5 + set name water + set consumption "You enjoy some fresh water." + puts "You gather some clean water from the nearby river." + } +} + +oo::class create apples { + superclass food + + constructor {} { + my variable satiation consumption name + set satiation [expr int(rand() * 5) + 8] + set name apples + set consumption "You enjoy a crisp, red apple." + puts "You gather some ripe apples from the forest." + } +} diff --git a/town b/town new file mode 100755 index 0000000..b817212 --- /dev/null +++ b/town @@ -0,0 +1,73 @@ +#!/usr/bin/env tclsh + +source player.tcl +source town.tcl +source resources.tcl + +proc statusline {player town} { + puts -nonewline [$player status] + puts -nonewline " -- " + puts [$town status] +} + +proc checkeog {player} { + if [$player died] { + puts "You lie, exhausted, as the light fades away." + puts "Game over." + exit + } +} + +proc help {} { + puts {Available commmands: + quit: quit game + help: show this help + + make power: make power for the town's supply + gather water: gather water for the town's supply + gather food: gather food for the town's supply + + build house: build a house in the town which can house people + + hire power maker: set an unassigned occupant to generate power + hire food gatherer: set an unassigned occupant to gather food + + drink: gather some water for yourself + eat: eat an item from the town's food supply} +} + +set p [player new] +set t [town new] + +while true { + statusline $p $t + checkeog $p + + set update true + + puts -nonewline ">: " + flush stdout + gets stdin ent + + switch -nocase $ent { + {} {set update false} + quit exit + help {help; set update false} + + "make power" {$t crank; $p work 5} + "gather water" {$t putfood [water new]} + "gather food" {$t putfood [apples new]; $p work 3} + + "build house" {$t build house; $p work 15} + + drink {$p eat [water new]} + eat {$p eat [$t takefood]} + + default {puts "You consider it deeply, but you're not sure how to $ent."; set update false} + } + + if $update { + $p update + $t update + } +} diff --git a/town.tcl b/town.tcl new file mode 100644 index 0000000..1b71f19 --- /dev/null +++ b/town.tcl @@ -0,0 +1,89 @@ +oo::class create town { + constructor {} { + my variable power foods buildings + set power 0 + set foods {} + set buildings {} + } + + method status {} { + my variable power foods buildings + set houses [lmap b $buildings { + expr {[$b is house] ? $b : [continue]} + }] + set popn 0 + foreach h $houses {incr popn [$h occupancy]} + return "Town power: $power, food: [llength $foods], houses: [llength $houses], popn: $popn/[expr [llength $houses] * 4]" + } + + method update {} { + my variable power buildings + set power [expr max($power - 2, 0)] + foreach b $buildings {$b update [self]} + } + + method crank {} { + my variable power + incr power 10 + puts "You turn the crank awhile, the lights flicker." + } + + method takefood {} { + my variable foods + if [expr [llength $foods] > 0] { + set choice [expr int(rand() * [llength $foods])] + set food [lindex $foods $choice] + set foods [lreplace $foods $choice $choice] + puts "You take [$food title] from the town's food supply." + return $food + } + } + + method putfood {food} { + my variable foods + puts "You add [$food title] to the town's food supply." + lappend foods $food + } + + method build {what} { + my variable buildings + + switch $what { + house {lappend buildings [house new]} + } + } +} + +oo::class create building { + method is {{what {}}} { + my variable type + if {$what != {}} { + return [expr {$type == $what}] + } else { + return $type + } + } +} + +oo::class create house { + superclass building + + constructor {} { + my variable type occupants + set type house + set occupants 0 + puts "You put up a wooden cabin." + } + + method occupancy {} { + my variable occupants + return $occupants + } + + method update {town} { + my variable occupants + if {$occupants < 4 && [expr int(rand() * 6) == 0]} { + incr occupants + } + } +} -- cgit v1.2.1