Changeset 269
- Timestamp:
- 08/15/06 00:15:22 (2 years ago)
- Files:
-
- 1 modified
-
trunk/docs/smoke_and_mirrors.rb (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/docs/smoke_and_mirrors.rb
r268 r269 1 1 #!/usr/bin/ruby 2 3 require 'thread' 4 require 'readline' 2 5 3 6 # … … 53 56 }, 54 57 { 55 :delay => 7,58 :delay => 3, 56 59 :output => "You continue walking through the forest towards Barrowton. You can\n" + 57 60 "see the path to the town ahead of you.\n" … … 67 70 # remember Barrowton 68 71 "You have visited Barrowton only once before. You remember the folks as\n" + 69 "friendly and welcoming. You met Phaedrus there for the first time. Aodh n\n" +72 "friendly and welcoming. You met Phaedrus there for the first time. Aodhán\n" + 70 73 "once defeated a banshee not far from the town.", 71 74 … … 82 85 ### Feature: Meeting someone you don't know will only show their description 83 86 { 84 :delay => 3,87 :delay => 2, 85 88 :output => "A short humanoid with bluish-grey skin walks past you, away from \n" + 86 89 "Barrowton.\n", 87 90 }, 88 91 { 89 :delay => 7,92 :delay => 2, 90 93 :output => "You have reached the entrace of Barrowton.\n", 91 94 }, … … 94 97 ### Feature: Combat. 95 98 { 96 :delay => 11,99 :delay => 2, 97 100 :output => "Suddenly, a rabid squirrel-monkey leaps out onto the path in \n" + 98 101 "front of you.\n", … … 100 103 101 104 ### Feature: complex actions as recipes 102 # use senu-oti styledefense105 # use turning defense 103 106 { 104 107 :delay => 1, … … 106 109 }, 107 110 { 108 :delay => 2,109 :output => "You spin on your left foot in sen-paksha, and avoid the \n" +111 :delay => 1, 112 :output => "You spin on your left foot in a crouch, and avoid the \n" + 110 113 "monkey's attack.\n", 111 114 }, 112 115 { 113 :delay => 3,116 :delay => 1, 114 117 :output => "The monkey falls to the ground in a heap. It looks somewhat\n" + 115 118 "dazed.\n", 116 119 }, 117 120 { 118 :delay => 4,121 :delay => 1, 119 122 :output => "It shrieks in anger, and charges your leg.\n", 120 123 }, 121 124 { 122 :delay => 5,125 :delay => 1, 123 126 :output => "You crouch in the cat pose, and deflect the monkey's bite with\n" + 124 127 "your satchel with a loud thump. The monkey falls to the ground.\n", … … 133 136 ### Oaths? 134 137 ### Story elements 138 139 140 # quit 135 141 ] 136 142 137 143 138 144 def debug_msg( msg ) 139 $deferr.puts msg if $DEBUG145 $deferr.puts "DEBUG>>> " + msg if $DEBUG 140 146 end 141 147 … … 150 156 151 157 $defout.sync = true 152 pending = ThreadGroup.new153 done = ThreadGroup.new154 158 155 def handle_threads 156 pending.list.each do |thr| 157 debug_msg "Joining thread %p" % [ thr ] 158 begin 159 thr.join unless thr.alive? || thr == Thread.current 160 rescue Exception => err 161 $deferr.puts "*** Error: %s" % [err.message] 162 $deferr.puts " " + err.stacktrace.join( "\n " ) if $DEBUG 163 end 164 done.add( thr ) 159 pending_events = Queue.new 160 world_simulator = Thread.new do 161 puts "** World starting up **\n" 162 Thread.current.abort_on_exception = true 163 Thread.current[:run] = true 164 165 while Thread.current[:run] 166 debug_msg "Waiting on an event." 167 event = pending_events.pop 168 169 debug_msg "Got an event. Firing in %d seconds..." % event[:delay] 170 sleep event[:delay] 171 print "\n" + event[:output] + "\n>" 165 172 end 166 173 174 puts "** World shutting down **\n" 167 175 end 168 176 169 177 Output.each do |event| 170 handle_threads171 172 178 case event 173 179 when String … … 175 181 gets 176 182 when Hash 177 thr = Thread.new { 178 Thread.current.abort_on_exception = true 179 sleep event[:delay]; print "\n" + event[:output] + "\n>" 180 } 181 debug_msg "Started delayed event in thread: %p" % [thr] 182 pending.add( thr ) 183 debug_msg "Pending thread group now has %d events" % [pending.list.length] 183 debug_msg "Queueing event '%s...' for +%ds" % 184 event.values_at( :output, :delay ) 185 pending_events.push( event ) 184 186 end 185 187 end 186 188 187 until pending.list.empty? 188 handle_threads 189 sleep 0.25 190 end 191 192 193 __END__ 194 189 debug_msg "Done with main loop. Injecting final event into simulator." 190 world_simulator[:run] = false 191 pending_events.push({ :delay => 0, :output => "Quitting." }) 192 debug_msg "Joining world_simulator" 193 world_simulator.join 194 debug_msg "Done."
