muby 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. data/LICENSE +339 -0
  2. data/bin/muby +37 -0
  3. data/contrib/aardmud.org_4000/README.txt +39 -0
  4. data/contrib/aardmud.org_4000/aard-config.rb +234 -0
  5. data/contrib/aardmud.org_4000/aard-helpers.rb +464 -0
  6. data/contrib/aardmud.org_4000/aliases/aard-aliases.rb +205 -0
  7. data/contrib/aardmud.org_4000/gags/aard-gags.rb +182 -0
  8. data/contrib/aardmud.org_4000/misc/aard-affects.rb +252 -0
  9. data/contrib/aardmud.org_4000/misc/aard-know.rb +147 -0
  10. data/contrib/aardmud.org_4000/misc/aard-poznai_sebia.rb +191 -0
  11. data/contrib/aardmud.org_4000/misc/aard-prompts.rb +65 -0
  12. data/contrib/aardmud.org_4000/misc/aard-status_toggling.rb +156 -0
  13. data/contrib/aardmud.org_4000/misc/aard_consider_substitutions.rb +319 -0
  14. data/contrib/aardmud.org_4000/speedwalks/aard-sw-areas-hero.rb +86 -0
  15. data/contrib/aardmud.org_4000/speedwalks/aard-sw-areas-newbie.rb +98 -0
  16. data/contrib/aardmud.org_4000/speedwalks/aard-sw-areas-noble.rb +170 -0
  17. data/contrib/aardmud.org_4000/speedwalks/aard-sw-areas-vidblain.rb +88 -0
  18. data/contrib/aardmud.org_4000/speedwalks/aard-sw-areas.rb +850 -0
  19. data/contrib/aardmud.org_4000/speedwalks/aard-sw-clans.rb +43 -0
  20. data/contrib/aardmud.org_4000/speedwalks/aard-sw-guilds.rb +13 -0
  21. data/contrib/aardmud.org_4000/speedwalks/aard-sw.rb +45 -0
  22. data/contrib/aardmud.org_4000/triggers/aard-triggers-items.rb +254 -0
  23. data/contrib/aardmud.org_4000/triggers/aard-triggers.rb +227 -0
  24. data/contrib/sy/cce.rb +120 -0
  25. data/lib/muby.rb +15 -0
  26. data/lib/muby/application.rb +66 -0
  27. data/lib/muby/completer.rb +62 -0
  28. data/lib/muby/configuration.rb +379 -0
  29. data/lib/muby/connection.rb +332 -0
  30. data/lib/muby/displayer.rb +60 -0
  31. data/lib/muby/help.rb +88 -0
  32. data/lib/muby/helper_methods.rb +46 -0
  33. data/lib/muby/inputwindow.rb +173 -0
  34. data/lib/muby/logger.rb +28 -0
  35. data/lib/muby/outputwindow.rb +189 -0
  36. data/lib/muby/style.rb +142 -0
  37. data/lib/muby/user_methods.rb +463 -0
  38. metadata +90 -0
@@ -0,0 +1,464 @@
1
+ #
2
+ # Variable helpers
3
+ #
4
+ # This is so that you don't have to constantly put your terms in quotes. So instead of run "n" you do run n.
5
+ # They could technically be made really smart, to return x or write x .. but even I'm not crazy enough to code that.
6
+
7
+ def n ; return "n" end
8
+ def s ; return "s" end
9
+ def e ; return "e" end
10
+ def w ; return "w" end
11
+ def u ; return "u" end
12
+ def d ; return "d" end
13
+ def cast(string) ; write "cast #{string}" end
14
+ def casting(spell, target)
15
+ if target == "" then
16
+ # Strangely, I can't specify "self" because some spells won't allow that parameter.
17
+ write "cast '#{spell}'"
18
+ else
19
+ write "cast '#{spell}' #{target}"
20
+ end
21
+ nil
22
+ end
23
+
24
+
25
+ #
26
+ # Procedure helpers
27
+ #
28
+ # These exist, so that your scripting can do things like a person at the keyboard, without you having to use 'write' every time. It's very handy for copying keyboard commands and pasting them into your scripting.
29
+ # examples: run n;open e;close s;write "echo The server echos this";echo "only you see this"
30
+ # It's not quite perfect, because you need double-quotes around your text.. but it's good enough.
31
+
32
+ # run is housed in speedwalks_common.rb
33
+ # Remember that you use these like: open "one", "two"
34
+ # and NOT like: open "one" "two"
35
+ #
36
+ # Yes, I am paranoid enough to ensure against everything, even when write does it again. =)
37
+ # Warning: 'open' is an internal Ruby command. If you want to open files, use: Kernel::open
38
+ def open(w) ; write "open #{ensure_w(w)}" end
39
+ def close(w) ; write "close #{ensure_c(w)}" end
40
+ def lock(w) ; write "lock #{ensure_c(w)}" end
41
+ def run(s) ; write "run #{ensure_s(s)}" end
42
+ # def get(s) ; write "get #{ensure_s(s)}" end
43
+ # I think something is strange with this one..
44
+ def put(s) ; write "put #{ensure_s(s)}" end
45
+ def get(s) ; write "get #{ensure_s(s)}" end
46
+ def take(s) ; write "take #{ensure_s(s)}" end
47
+ def buy(s) ; write "buy #{ensure_s(s)}" end
48
+ def say(s) ; write "say #{ensure_s(s)}" end
49
+ # Is this another magical Ruby keyword?
50
+ def scan(s) ; write "scan #{ensure_s(s)}" end
51
+
52
+ # Warning: 'enter' is reserved by Ruby. If you want to 'enter', use: Kernel::enter
53
+ # TODO: This needs testing.
54
+ def enter(*s)
55
+ s = ensure_s(s)
56
+ if s == "" then
57
+ write "enter"
58
+ else
59
+ write "enter '#{s}'"
60
+ end
61
+ end
62
+
63
+ # Aardwolf requires single quotes around multiple-word objects.
64
+ def wear(s) ; write "wear '#{ensure_s(s)}'" end
65
+ # This might be bugged somehow:
66
+ def remove(s); write "remove '#{ensure_s(s)}'" end
67
+ def wield(s) ; write "wield '#{ensure_s(s)}'" end
68
+ def hold(s) ; write "hold '#{ensure_s(s)}'" end
69
+ def keep(s) ; write "keep '#{ensure_s(s)}'" end
70
+ def unkeep(s); write "unkeep '#{ensure_s(s)}'" end
71
+ def quaff(s) ; write "quaff '#{ensure_s(s)}'" end
72
+ def dual(s)
73
+ s = ensure_s(s)
74
+ if s == "remove" then
75
+ write "dual remove"
76
+ else
77
+ write "dual '#{s}'"
78
+ # should really only set dual on success
79
+ $dual = s
80
+ end
81
+ end
82
+ def wield(s)
83
+ s = ensure_s(s)
84
+ if s == "remove" then
85
+ write "wield remove"
86
+ else
87
+ write "wield '#{s}'"
88
+ # should really only set wield on success
89
+ $wield = s
90
+ end
91
+ end
92
+
93
+ def getfrom(s, s2) ; get "'#{ensure_s(s)}' '#{ensure_s(s2)}'" end
94
+ def putin(s, s2) ; put "'#{ensure_s(s)}' '#{ensure_s(s2)}'" end
95
+
96
+ # with the bag:
97
+ def getbag(s); get "'#{ensure_s(s)}' '#{$bag}'" end
98
+ def putbag(s); put "'#{ensure_s(s)}' '#{$bag}'" end
99
+ def removebag(s); remove s ; putbag s end
100
+ def retire(s) ; removebag(s) end
101
+ def removebag_unkeep(s); remove s ; unkeep s ; putbag s end
102
+ def retire_unkeep(s) ; removebag_unkeep(s) end
103
+ def getwear(s) ; getbag s ; wear s end
104
+ def getwear_keep(s) ; getbag s ; keep s ; wear s end
105
+ def getdual(s) ; getbag s ; dual s end
106
+ def getdual_keep(s) ; getbag s ; keep s ; dual s end
107
+ def getquaff(s) ; getbag s ; quaff s end
108
+ def geteat(s) ; getbag s ; eat s end
109
+
110
+ # TODO: This has to be smarter..
111
+ # check for (item)
112
+ # Check for #, (item)
113
+ # check for #, (item name)
114
+ # def buyput(s, s2) ; write "buy #{s} '#{s2}' ; put all.#{s2} '#{$bag}'" end
115
+ # buy, then stuff things in the bag
116
+ # This can't deal with: pocket 10, "jade elixir"
117
+ conf.local_triggers[/^pocket (.*)$/] = Proc.new do |inwin, outwin, match| pocket match[1..-1].to_s.split("\"") ; false end
118
+ def pocket_alias(s)
119
+ # write s.inspect + "--"
120
+ # write s[0].inspect + "0"
121
+ # write s[1].inspect + "1"
122
+ write "buy #{s[0].strip} #{s[1]};put all.#{s[1]} '#{$bag}'"
123
+ end
124
+ def pocket(s, s2)
125
+ write "buy #{s} #{s2};put all.#{s2} '#{$bag}'"
126
+ end
127
+
128
+ # When writing out to the server, split separate commands with a semi-colon.
129
+ # This lets you do things like: write "run e;open d;d;cough"
130
+ # This has been fixed to work in all circumstances.. from methods, from methods called by triggers/aliases, from the commandline..
131
+ def write(string)
132
+ # Another method:
133
+ # def method_one
134
+ # string = ensure_s(string)
135
+ # # Split across any semi-colons.
136
+ # string = string.split(';')
137
+ # # Run through it, executing each command separately.
138
+ # string.each_index { |i|
139
+ # # Good for debugging. Then you don't have to connect to anything to try most stuff out.
140
+ # # Muby::InputWindow.get_instance.pprint string[i].lstrip + "\n"
141
+ # Muby::InputWindow.get_instance.send string[i].lstrip
142
+ # }
143
+ # false
144
+ # end
145
+ # Also, a generic search/replace of ; for \n would work.
146
+
147
+ # Leveraging muby's local substitution powers, I can do it this way:
148
+ string = ensure_s(string)
149
+ conf.local_substitutions[';'] = "\n"
150
+ Muby::InputWindow.get_instance.send string
151
+ conf.local_substitutions.delete ';'
152
+ end
153
+
154
+
155
+ #
156
+ # Internal helpers
157
+ #
158
+
159
+ # Ensure that a string is given.
160
+ def ensure_s(input)
161
+ # TODO: Technically, I should check the type of whatever I'm being sent, and ensure that it's possible to convert it to a string.
162
+ string = input.to_s.lstrip
163
+ # This does not detect or respect numbers. It returns them as type String.
164
+ return string
165
+ end
166
+
167
+ # Ensure that a word is given.
168
+ # These helpers can probably be done more intelligently.
169
+ def ensure_w(input)
170
+ string = ensure_s input
171
+ if string.split(' ')[1] != nil then
172
+ pprint "Warning: You should only send one word."
173
+ word = string.split(' ')[0]
174
+ else
175
+ word = string
176
+ end
177
+ return word
178
+ end
179
+
180
+ # Ensure that a character is given.
181
+ def ensure_c(input)
182
+ word = ensure_w(input)
183
+ if word.length > 1 then
184
+ pprint "Warning: You should only send one character."
185
+ end
186
+ character = word
187
+ # This does not detect or respect a number. It returns it as type String.
188
+ return character
189
+ end
190
+
191
+ # Ensure that a character is given.
192
+ def ensure_n(input)
193
+ if input.class != Fixnum then
194
+ pprint "Warning: You should only send a number."
195
+ # FIXME: This should rescue errors and react accordingly.
196
+ number = input.to_i
197
+ end
198
+ number = input
199
+ return number
200
+ end
201
+
202
+ # Ensure that a boolean value (true/false) is given.
203
+ def ensure_b(input)
204
+ if input.class != TrueClass && input.class != FalseClass then
205
+ pprint "Warning: You should only send a boolean value (true/false). Returning false."
206
+ return false
207
+ end
208
+ boolean = input
209
+ return boolean
210
+ end
211
+
212
+
213
+
214
+ # Generate a random number from a range of integers.
215
+ #example:
216
+ #random(6 .. 10)
217
+ #=> an integer from 6, 7, 8, 9, 10
218
+ def random(r)
219
+ # assume r is a range of integers first < last
220
+ r.first + rand(r.last - r.first + (r.exclude_end? ? 0 : 1))
221
+ end
222
+
223
+ # Random float:
224
+ # TODO one day I could detect additional decimal places and deal with them.. but not today.
225
+ # example:
226
+ # random_f(1.0..2.2)
227
+ # => a float: 1.0, 1.1, 1.2 .. 2.0, 2.1, 2.2
228
+ def random_f(r)
229
+ big = random((r.first * 10)..(r.last * 10))
230
+ big / 10.0
231
+ end
232
+
233
+ def prompt_routines
234
+ $afk = 0
235
+ ungag_blocks
236
+ ungag_prompts
237
+ undisable_gags_blocks
238
+ undisable_triggers_blocks
239
+ end
240
+
241
+ # This is wrapped into something reusable, so that I can easily re-enable it when I do trigger-disabling.
242
+ def prompt_triggers_setup
243
+ # Whenever you see the prompt, perform the appropriate routines:
244
+ conf.remote_triggers[$prompt1] = :prompt_routines
245
+ conf.remote_triggers[$bprompt1] = :prompt_routines
246
+ end
247
+ prompt_triggers_setup
248
+
249
+
250
+ # I like doing it this way.. it seems to work out fairly well.
251
+ def pprint(s)
252
+ Muby::InputWindow.get_instance.print(s.to_s + "\n").to_s
253
+ end
254
+
255
+ def _reload
256
+ afk 0
257
+ wake
258
+ # I don't do 'invis 0' because I sometimes hang out in an aggro room and do scripting-work.. and I don't want to become visible and get into fights.. especially when half the client might not work. =)
259
+ autotick 0
260
+ # Avoid disconnections:
261
+ Thread.new do
262
+ sleep 1
263
+ score
264
+ sleep 1
265
+ _affects_build_array("spellup")
266
+ end
267
+ end
268
+
269
+ def _startup
270
+ afk 0
271
+ wake
272
+ invis 0
273
+ autotick 0
274
+ p
275
+ end
276
+
277
+
278
+ def connection_triggers
279
+ conf.remote_triggers[/^Last on from|^Reconnecting\.\.\.\.$/] = Proc.new do
280
+ _startup
281
+ _reload
282
+ conf.remote_triggers.delete(/^Last on from|^Reconnecting\.\.\.\.$/)
283
+ end
284
+ end
285
+
286
+ def login_triggers(type)
287
+ @name = eval('$' + 'character_name' + type)
288
+ @pass = eval('$' + 'character_password' + type)
289
+ if @name == nil || @pass == nil then return nil end
290
+ conf.remote_triggers[/#############################################################################/] = Proc.new do
291
+ write "#{@name};#{@pass};y"
292
+ conf.remote_triggers.delete(/#############################################################################/)
293
+ end
294
+ end
295
+
296
+ def rc
297
+ # No compression:
298
+ # TODO: This will eventually change to aardwolf.com
299
+ # After playing for a bit, you should switch to port 4010
300
+ connection_triggers
301
+ login_triggers("")
302
+ connect "aardmud.org", 4000
303
+ end
304
+ def rcc
305
+ # Compression via MudProxy.
306
+ # connect "127.0.0.1", 9009
307
+ connection_triggers
308
+ login_triggers("")
309
+ connect "localhost", 9009
310
+ end
311
+ # Tester's Port.
312
+ # See also 'help tester', 'who tester'
313
+ def rct
314
+ connection_triggers
315
+ login_triggers("_tester")
316
+ connect "builder.aardmud.org", 6555
317
+ end
318
+ # Builder's Port
319
+ # See also 'help builder'
320
+ def rcb
321
+ connection_triggers
322
+ login_triggers("_builder")
323
+ connect "build.aardmud.org", 6000
324
+ end
325
+
326
+ # Repeat an action
327
+ # example:
328
+ # repeat("echo hi!", 3)
329
+ # TODO: Allow a method to be passed.
330
+ $repeat = {}
331
+ def repeat(action, times, echo)
332
+ @echo = ensure_b(echo)
333
+ # pprint action.inspect
334
+ @action = ensure_s(action)
335
+ # If sent nothing, do nothing.
336
+ if @action == "" then return nil end
337
+ times = ensure_n(times)
338
+ # Throw this data into a hash, so that multiple uses of repeat will accumulate.. and the counting will work well..
339
+ if $repeat[@action] == nil then
340
+ $repeat[@action] = times
341
+ else
342
+ $repeat[@action] = $repeat[@action] + times
343
+ end
344
+ # Wrap it in a thread so you can do other stuff.
345
+ Thread.new do
346
+ until $repeat[@action] < 1
347
+ # This is imperfect, since echo seems to bypass the action queue sometimes..
348
+ # Be more friendly, and don't spam and possibly get disconnected:
349
+ # TODO: Don't sleep on the very first action.. doesn't work.
350
+ # if $repeat[@action] != times then sleep random_f(0.6..1.8) end
351
+ sleep random_f(0.6..1.8)
352
+ if $repeat[action] > 0 then
353
+ if @echo == true then write "echo @w #{$repeat[@action]} - #{@action}" end
354
+ write @action
355
+ $repeat[@action] -= 1
356
+ end
357
+ end
358
+ end
359
+ end
360
+
361
+ def highlight(string)
362
+ string = ensure_s(string)
363
+ conf.gags << string
364
+ # This destroys the colour attributes of the entire line:
365
+ conf.remote_triggers[/(.*)#{string}(.*)/] = Proc.new do |inwin, outwin, match|
366
+ # Muby::Style.new(0, Ncurses.const_get("COLOR_RED"), :copy, true)
367
+ # /pprint RED, "red"
368
+ # /pprint Muby::Style.new(0, Ncurses.const_get("COLOR_RED"), :copy, true), "red"
369
+ # But I can't get it to work down here! :
370
+ # Ugly non-coloured "highlighting":
371
+ pprint " > #{match[1]} #{string} #{match[2]}"
372
+ end
373
+ end
374
+ def unhighlight(string)
375
+ string = ensure_s(string)
376
+ conf.gags.delete(string)
377
+ conf.remote_triggers.delete(/(.*)#{string}(.*)/)
378
+ end
379
+
380
+ # Use it like this:
381
+ # Singular:
382
+ # n = 1
383
+ # pprint "you have #{n} item#{plural(n)}."
384
+ # Plural:
385
+ # n = 0
386
+ # pprint "you have #{n} item#{plural(n)}."
387
+ # n = 10
388
+ # pprint "you have #{n} item#{plural(n)}."
389
+ # IDEA: This could be made much smarter, if I passed the appropriate word to use..
390
+ def plural(n)
391
+ if n > 1 || n == 0 then "s" end
392
+ end
393
+
394
+
395
+ # It just makes life easier..
396
+ def dagger ; "dagger" end
397
+ def axe ; "axe" end
398
+ def sword ; "sword" end
399
+ def whip ; "whip" end
400
+ def spear ; "spear" end
401
+ def halberd ; "halberd" end
402
+ def mace ; "mace" end
403
+
404
+ __END__
405
+
406
+ $cwd = ENV['HOME']
407
+ conf.local_triggers[/^!(.*)/] = Proc.new do |inwin, outwin, match| shell match[1..-1] ; false end
408
+
409
+ # TODO: Add globbing functionality.
410
+ def shell(*input)
411
+ input = input.to_s.split(' ')
412
+ input[0] = input[0]
413
+ # input[0] = input[0][1..-1]
414
+ command = input[0]
415
+ parameters = input[1..-1].join(' ')
416
+
417
+ case command
418
+ when "cd"
419
+ if input[1] != nil then
420
+ old_cwd = $cwd
421
+ $cwd = input[1]
422
+ begin
423
+ # Allows * in directory names.
424
+ Dir.chdir(Dir.glob($cwd).join(' '))
425
+ $cwd = Dir.getwd
426
+ # Example on my computer:
427
+ # Errno::ENOENT: No such file or directory - nothing
428
+ rescue SystemCallError
429
+ pprint "No such directory - " + input[1]
430
+ $cwd = old_cwd
431
+ Dir.chdir($cwd)
432
+ end
433
+ else
434
+ $cwd = ENV['HOME']
435
+ Dir.chdir($cwd)
436
+ $cwd = Dir.getwd
437
+ end
438
+ else
439
+ Dir.chdir($cwd)
440
+ # This looks good, but feels dangerous. =)
441
+ Thread.new do
442
+ pprint `#{input.join(' ')}`
443
+ end
444
+ end
445
+ pprint $cwd + " >"
446
+ end
447
+
448
+
449
+ # Why doesn't any of this work anymore?! =(
450
+ pprint (defined? $loaded).inspect
451
+ if (defined? $loaded) != "global-variable" then
452
+ # muby has just been started.
453
+ $loaded = true
454
+ pprint "Loaded user files."
455
+ $startup = true
456
+ _startup
457
+ _reload
458
+ else
459
+ # muby is already running, and this script is being summoned.
460
+ # Actions to perform after using /reload .. this ought to reset all self-awareness.
461
+ pprint "Re-loaded user files."
462
+ $startup = true
463
+ _startup
464
+ end