#!/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__) def parse_options_cli options = { beast_mode: false, debug_mode: false, god_mode: false, new_skip: false, resume_skip: false, sound_enabled: false, sound_system: nil, sound_volume: 0.3, use_wordnik: false, extra_command: nil } options_file = read_options_file unless options_file.nil? sound_enabled_option = options_file[0][1].eql?('false') ? false : true sound_system_selected = options_file[1][1] sound_volume_option = options_file[2][1].to_f use_wordnik_option = options_file[3][1].eql?('false') ? false : true options[:sound_enabled] = sound_enabled_option options[:sound_system] = sound_system_selected options[:sound_volume] = sound_volume_option options[:use_wordnik] = use_wordnik_option end 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('-x', '--extra COMMAND', 'Run a command immediately upon beginning the game') do |xc| options[:extra_command] = xc.to_s 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| options << line.chomp.split(':') end return options end nil end def init_config Dir.mkdir(GW_HOME) unless Dir.exist?(GW_HOME) sound_system_default = OS.windows? ? 'win32-sound' : 'feep' 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 end begin init_config options = parse_options_cli Gemwarrior::Game.new(options) rescue => error print_error(error) exit(false) end