#!/usr/bin/env ruby require 'optparse' require 'os' require_relative '../lib/gemwarrior/game' require_relative '../lib/gemwarrior/game_options' require_relative '../lib/gemwarrior/version' include Gemwarrior GAME_NAME = 'Gem Warrior' GW_HOME = "#{Dir.home}/.gemwarrior" GW_SAVE_FILE_YAML = "#{GW_HOME}/gw_sav.yaml" GW_SAVE_FILE_BIN = "#{GW_HOME}/gw_sav.dat" GW_OPTS_FILE = "#{GW_HOME}/gw_opts" GW_LOG_FILE = "#{GW_HOME}/gw_log" GW_DEFAULT_WORLD_YAML = File.expand_path('../../data/default_world.yaml', __FILE__) GW_DEFAULT_WORLD_BIN = File.expand_path('../../data/default_world.bin', __FILE__) GW_DEFAULT_WRAP_WIDTH = 80 def parse_options_cli # default options options = { beast_mode: false, debug_mode: false, god_mode: false, new_skip: false, resume_skip: false, sound_enabled: false, sound_system: 'bloops', sound_volume: 0.3, use_wordnik: false, fight_completion: false, extra_command: nil } # options file has next precedence unless (opts_file = read_options_file).nil? options[:sound_enabled] = (opts_file[:sound_enabled].eql?('false') ? false : true) if opts_file[:sound_enabled] options[:sound_system] = (opts_file[:sound_system].eql?('false') ? false : true) if opts_file[:sound_system] options[:sound_volume] = (opts_file[:sound_volume].to_f) if opts_file[:sound_volume] options[:use_wordnik] = (opts_file[:use_wordnik].eql?('false') ? false : true) if opts_file[:use_wordnik] options[:fight_completion] = (opts_file[:fight_completion].eql?('false') ? false : true) if opts_file[:fight_completion] end # command line has next precedence optparse = OptionParser.new do |opts| opts.on('-b', '--beast', 'Enable debug[beastmode]') do options[:beast_mode] = true end opts.on('-d', '--debug', 'Enable debug commands in-game') do options[:debug_mode] = true end opts.on('-g', '--god', 'Enable debug[godmode]') do options[:god_mode] = true end opts.on('-n', '--new', 'Immediately start a new game, skipping main menu') do options[:new_skip] = true end opts.on('-r', '--resume', 'Immediately resume the saved game, skipping main menu') do options[:resume_skip] = true end opts.on('-s', '--sound', 'Enable sound (experimental)') do options[:sound_enabled] = true end opts.on('-v', '--version', 'Display version number and exit') do puts "#{GAME_NAME} v#{Gemwarrior::VERSION}" exit end opts.on('-w', '--wordnik', 'Enable Wordnik to generate more diverse, dynamic descriptors of entities') do options[:use_wordnik] = true end opts.on('-f', '--fight-completion', 'Fighting without specifying an enemy will attack first one it finds') do options[:fight_completion] = false end opts.on('-x', '--extra COMMAND,PARAM1,PARAM2,PARAM3', String, 'Run a command, with optional params, immediately upon beginning the game') do |xc| options[:extra_command] = xc.gsub(',',' ') end end optparse.parse!() return options end def print_error(error) case error when OptionParser::InvalidOption puts "#{GAME_NAME}: illegal option #{error.args.join(' ')}" else puts "An unexpected error occurred while running #{GAME_NAME}:" puts " #{error}\n" end end def read_options_file if File.exist?(GameOptions.data['options_file_path']) options = {} File.open(GameOptions.data['options_file_path']).readlines.each do |line| kv = line.chomp.split(':') options[kv[0].to_sym] = kv[1] end # check for null return options[:sound_enabled] ? options : nil end nil end def init_config Dir.mkdir(GW_HOME) unless Dir.exist?(GW_HOME) sound_system_default = OS.windows? ? 'win32-sound' : 'bloops' GameOptions.add 'sound_system', sound_system_default GameOptions.add 'save_file_mode', 'Y' GameOptions.add 'default_world_path_yaml', GW_DEFAULT_WORLD_YAML GameOptions.add 'default_world_path_bin', GW_DEFAULT_WORLD_BIN GameOptions.add 'save_file_yaml_path', GW_SAVE_FILE_YAML GameOptions.add 'save_file_bin_path', GW_SAVE_FILE_BIN GameOptions.add 'log_file_path', GW_LOG_FILE GameOptions.add 'options_file_path', GW_OPTS_FILE GameOptions.add 'wrap_width', GW_DEFAULT_WRAP_WIDTH end begin init_config options = parse_options_cli Gemwarrior::Game.new(options) rescue => error print_error(error) exit(false) end