Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Last active February 21, 2017 20:04
Show Gist options
  • Select an option

  • Save thomasjslone/f58beeea5f601d2561a41a749e1ce695 to your computer and use it in GitHub Desktop.

Select an option

Save thomasjslone/f58beeea5f601d2561a41a749e1ce695 to your computer and use it in GitHub Desktop.
Venture Engine, choose your own adventure, novel based game engine for ruboto on android
$rom_header = ["Game Title",
"0.0.0",
"Author : Full Name",
"Rpg/Adventure/TextBased",
"Janurary 2017",
"XXCID-001CY-X86-USA",
]
@var="helloworld"
def save_file ; ; end
def load_file ; ; end
##;##;##;##;##;##
[
["scene1","TITLE SCREEN","
button :text => 'next', :on_click_listener => ( proc{ goto_scene 'scene2' } )
"],
["scene2","scene two title","text_view :text => \"this is scene two.\""],
["scene3","title",""],
["scene4","title",""],
["scene5","title",""]
]
# Venture Engine, by Thomas J. Slone, March 2017, Version 0.0.1
#
# This engine activity attaches to a "rom" game file in the local directory.
# Virtually all your game programming will take place in the rom file.
#
def splice(str,b,e) ; res = [] ; loop do ; p = str[/#{b}(.*?)#{e}/m, 1].to_s ; if p.to_s.length == 0 ; break ; else ; res << p ; str = str.split(b + p + e)[-1].to_s ; end ; end ; return res ; end
def starts_with(string,tag) ; chk = true ; i = 0 ; string.to_s.split("").each { |ch| if ch.to_s != tag.to_s[i].to_s ; chk = false ; end ; if i + 1 == tag.length.to_i ; break ; else ; i += 1 ; end } ; chk ; end
def ends_with(string,tag) ; string = string.reverse ; tag = tag.reverse ; chk = true ; i = 0 ; string.to_s.reverse.split("").each { |ch| if ch.to_s != tag.to_s[i].to_s ; chk = false ; end ; if i + 1 == tag.length.to_i ; break ; else ; i += 1 ; end } ; chk ; end
def list_spacer(spaces,left,right) ; width = spaces .to_i+ 5 ; str = '' ; left.each do |l| ; spacer = "" ; t = width - l.length.to_i ; t.to_i.times { spacer << " " } ; str << l.to_s + spacer.to_s + right[left.index(l)].to_s + "\n" ; end ; return str.to_s ; end
def fnum(n) ; str = "" ; s = n.to_s.split("").reverse ; i=0 ; s.each do |nc| ; if i == 2 ; i=0 ; str << nc.to_s + "," ; else ; str << nc.to_s ; i+=1 ; end ; end ; if str.to_s[-1].to_s == "," ; str = str.reverse.to_s.split("")[1..-1].join("").to_s ; else ; str = str.reverse.to_s ; end ; return str ; end
def rands(l) ; key = "" ; l.times { key << rand(10**8..10**9).to_s(36).to_s[rand(6).to_i].to_s } ; key.to_s ; end
def numerize(str) ; if str == "" ; return 0 ; else ; chbytes = ["97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "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", "90", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "126", "96", "33", "64", "35", "36", "37", "94", "38", "42", "40", "41", "45", "95", "61", "43", "91", "93", "123", "125", "59", "58", "34", "39", "92", "47", "124", "63", "60", "62", "44", "46", "32", "9", "10"] ; ch_inds = [] ; str.to_s.split("").each { |ch| ch_inds << chbytes.index(ch.to_s.ord.to_s) } ; enc = [] ; ch_inds.each do |ch| ; code = ch.to_i + 1 ; if code.to_s.length == 1 ; code = "0" + code.to_s ; else ; code = code.to_s ; end ; enc << code.to_s ; end ; enc = enc.join("").to_s ; if enc.to_s[0].to_s == "0" ; enc = enc[1..-1].to_s ; end ; return enc.to_s ; end ; end
def denumerize(str) ; kbchars = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9","~","`","!","@","#","$","%","^","&","*","(",")","-","_","=","+","[","]","{","}",";",":","\"","'","\\","/","|","?","<",">",",","."," ","\t","\n"] ; if str.to_s.length.odd? ; str = "0" + str.to_s ; end ; str_codes = [] ; i = 0 ; hold = "" ; str.to_s.split("").each do |num| ; if i.to_i == 0 ; hold << num.to_s ; i += 1 ; elsif i.to_i == 1 ; hold << num.to_s ; i = 0 ; str_codes << hold.to_s ; hold = "" ; end ; end ; str = str_codes ; str_codes = [] ; str.each do |c| ; str_codes << c.to_i - 1 ; end ; dec_str = "" ; str_codes.each { |c| dec_str << kbchars[c.to_i].to_s } ; dec_str.to_s ; end
def map_folder(dir) ; if File.directory?(dir) ; remaining = [dir] ; found_files = [] ; file_size = [] ; found_folders = [] ; until remaining.length == 0 ; cur_dir = remaining[0].to_s ; remaining.delete_at(0) ; begin ; cont = Dir.entries(cur_dir) ; cont.delete(".") ; cont.delete("..") ; rescue ; cont = [] ; end ; if cont.length > 0 ; cont.each do |obj| ; if File.file?(cur_dir.to_s + "/" + obj.to_s) ; found_files << cur_dir.to_s + "/" + obj.to_s ; file_size << File.size?(found_files[-1]) ; elsif File.directory?(cur_dir.to_s + "/" + obj.to_s) ; found_folders << cur_dir.to_s + "/" + obj.to_s ; remaining << cur_dir.to_s + "/" + obj.to_s ; end ; end ; else ; end ; end ; return [file_size,found_files,found_folders] ; else "DIRECTORY NON-EXISTENT: " + dir.to_s ; end ; end
##;##;##
$top_dir = Dir.getwd.to_s
$sys_dir = $top_dir.to_s + "/venture_engine"
if File.directory?($sys_dir) == false ; Dir.mkdir($sys_dir) ; end
if File.directory?($sys_dir + "/roms") == false ; Dir.mkdir($sys_dir + "/roms") ; end
if File.directory?($sys_dir + "/rom_data") == false ; Dir.mkdir($sys_dir + "/rom_data") ; end
$rom_path = $sys_dir + "/roms/rom_game.rb"
$debug = true
##;##;#
require 'ruboto/activity' ; require 'ruboto/widget' ; require 'ruboto/util/toast' ; ruboto_import_widgets :TextView, :LinearLayout, :Button, :ListView, :EditText ## load all widgets and libraries that can be called by a rom here
class Venture_Engine_Activity
def on_create(b)
super
@scene_data, @scene_names = [], [] ; @scene, @previous_scene = nil, nil ; @scene_history = []
if File.exist?($rom_path)
begin
fi = File.open($rom_path,"r") ; rom = fi.read.to_s ; fi.close
@scene_data = eval(rom.to_s)
@scene_names = [] ; @scene_data.each { |s| @scene_names << s[0].to_s }
@scene = @scene_data[0]
if $debug ; build_ui("SHELL") ; else ; build_ui("SCENE") ; end
true
rescue => e
finish ; raise "ROM execution failed: " + $rom.to_s + ", Backtrace:\n" + e.backtrace.join("\n").to_s
end
else
finish ; raise "ROM file does not exist: " + $rom.to_s
end
end
def goto_scene(scene) ; if @scene_names.include?(scene) ; @previous_scene = @scene ; @scene = @scene_data[@scene_names.index(scene)] ; @scene_history << @scene ; build_ui("SCENE") ; true ; else ; finish ; raise "goto_scene(scene) cannot process scene: " + scene.to_s ; end ; end
def build_ui(ui) # update screen
if ui.to_s == "SHELL" # for debugging/hacking of your roms
setTitle "Venture Engine Shell"
set_content_view(linear_layout(:orientation => :vertical) do
@fid = 0 ; scl = 10 ; $scl = scl - scl - scl ; @sc1 = $scl ; @sc2 = -1 ; $display_lines = [] ; scl.times { $display_lines << "" }
@display = text_view :text => $display_lines.join("\n") ; @wrap_length = 95
set_content_view(linear_layout(:orientation => :horizontal) do
@fid_label = text_view :text => "<" + @fid.to_s + ": "
@input_bar = edit_text
button :text => "E", :on_click_listener => (proc{forward_input}) , :height => 60 , :width => 40
end)
end)
elsif ui.to_s == "SCENE"
unless @scene[1].to_s == ""
setTitle @scene[1].to_s
end
begin
set_content_view( linear_layout { eval(@scene[2].to_s) } )
rescue => e
finish ; raise "Scene Evaluation Failed: " + @scene[0].to_s + ", Backtrace:\n" + e.backtrace.join("\n").to_s
end
end
end
def append_consol(inp) ; scroll_bottom ; ll = [] ; inp.to_s.split("\n").each { |l| ll << l.to_s } ; nl = [] ; ll.each do |l| ; if l.to_s.length > @wrap_length.to_i ; c = 0 ; line = "" ; l.split('').each { |ch| if c < @wrap_length.to_i - 1 ; line << ch.to_s ; c += 1 ; else ; nl << line.to_s + ch.to_s ; line = "" ; c = 0 ; end } ; else ; nl << l.to_s ; end ; end ; nl.each { |l| $display_lines << l.to_s } ; update_display ; end
def update_display ; @display.set_text($display_lines[@sc1..@sc2].join("\n").to_s) ; end
def set_display_lines(lines) ; $scl = lines.to_i - lines.to_i - lines.to_i ; @sc1 = $scl ; update_display ; end
def set_display_width(length) ; @wrap_length = length.to_i ; end
def scroll_top ; l = $display_lines.length.to_i ; l = l - l - l ; @sc1 = l.to_i ; @sc2 = @sc1 - $scl - 2 ; update_display ; end
def scroll_up ; cpv = $display_lines.length - $display_lines.length - $display_lines.length ; if @sc1.to_i > cpv.to_i ; @sc1 -= 1 ; @sc2 -= 1 ; update_display ; else ; ; end ; end
def scroll_bottom ; @sc1 = $scl ; @sc2 = -1 ; update_display ; end
def scroll_down ; if @sc2.to_i < -1 ; @sc1 += 1 ; @sc2 += 1 ; update_display ; else ; ; end ; end
def forward_input ; cmd = @input_bar.get_text.to_s ; @input_bar.set_text("") ; res = "" ; if cmd[0].to_s == "*" ; cmd = cmd[1..-1].to_s ; @input_bar.set_text("*" + cmd.to_s) ; end ; if cmd.to_s == "[u" ; res = nil ; scroll_up ; @fid -= 1 ; elsif cmd.to_s == "[d" ; res = nil ; scroll_down ; @fid -= 1 ; elsif cmd.to_s == "[h" ; res = nil ; scroll_top ; @fid -= 1 ; elsif cmd.to_s == "[e" ; res = nil ; scroll_bottom ; @fid -= 1 ; elsif cmd[0..2].to_s == "[h:" ; l = cmd.to_s[3..-1].to_i ; if l >= 5 ; set_display_lines(l) ; else ; res = "[h: must be >= 5" ; end ; @fid -= 1 ; elsif cmd[0..2].to_s == "[w:" ; w = cmd[3..-1].to_i ; if w >= 50 ; set_display_width(w) ; else ; res = "[w: must be >= 50" ; end ; @fid -= 1 ; else ; append_consol("<" + @fid.to_s + ": " + cmd.to_s) ; begin ; res = eval(cmd.to_s) ; rescue => e ; res = e.backtrace.join("\n").to_s ; end ; end ; if res == nil ; append_consol(":" + @fid.to_s + "> Nilclass") ; elsif res == "" ; append_consol(":" + @fid.to_s + "> NilSTRING") ; else ; append_consol(":" + @fid.to_s + ">" + res.to_s) ; end ; @fid += 1 ; @fid_label.set_text(@fid.to_s) ; end
end
class Open_Rom_Dialog_Activity
def on_create(b)
super
$roms = []
m = map_folder($sys_dir)[1]
m.each { |r| if r.to_s.split(".")[-1].to_s == "rom" ; $roms << r.to_s ; end }
$names = [] ; $roms.each { |r| $names << r.to_s.split("/")[-1].to_s.split(".")[0].to_s }
setTitle "Select Rom, " + $roms.length.to_s + " roms found."
set_content_view(linear_layout(:orientation => :vertical) do
$names.each do |r|
button :text => r.to_s, :on_click_listener => (proc{rom_selected($roms[$names.index(r)])}), :height => 47
end
button :text => "Exit", :on_click_listener => (proc{ $dialog_canceled = true ; finish }), :height => 47
end)
end
def rom_selected(r)
$rom_path = r.to_s ; finish
end
end
$irb.start_ruboto_activity :class_name => "Venture_Engine_Activity"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment