summaryrefslogtreecommitdiff
path: root/town.tcl
blob: 1b71f19b8fb27796b31b36c608506918472fc375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
    }
  }
}