def _get_AI_folder_path(): # hack to allow lunch this code separately to dump default config try: return fo.getAIDir() except AttributeError as e: print "Cant get options file", e return None
def _get_option_file_path(): # hack to allow lunch this code separately to dump default config ai_path = fo.getAIDir() option_file = fo.getAIConfigStr() if ai_path and option_file: return os.path.join(ai_path, option_file) else: return None
try: main_timer.start(action.__name__) action() main_timer.stop() except Exception as e: error("Exception %s while trying to %s" % (e, action.__name__), exc_info=True) main_timer.stop_print_and_clear() turn_timer.stop_print_and_clear() debug('Size of issued orders: ' + str(fo.getOrders().size)) turn_timer.start("Server_Processing") try: fo.doneTurn() except Exception as e: error("Exception %s while trying doneTurn()" % e, exc_info=True) # TODO move it to cycle above finally: aistate.last_turn_played = fo.currentTurn() if using_statprof: try: statprof.stop() statprof.display() statprof.start() except: pass init_handlers(fo.getOptionsDBOptionStr("ai-config"), fo.getAIDir())
print_error(e, location=action.__name__) main_timer.end() turn_timer.end() turn_timer.start("Server_Processing") try: fo.doneTurn() except Exception as e: print_error(e) # TODO move it to cycle above if using_statprof: try: statprof.stop() statprof.display() statprof.start() except: pass # The following methods should probably be moved to the AIstate module, to keep this module more focused on implementing required interface def declare_war_on_all(): # pylint: disable=invalid-name """Used to declare war on all other empires (at start of game)""" my_emp_id = fo.empireID() for emp_id in fo.allEmpireIDs(): if emp_id != my_emp_id: msg = fo.diplomaticMessage(my_emp_id, emp_id, fo.diplomaticMessageType.warDeclaration) fo.sendDiplomaticMessage(msg) init_handlers(fo.getAIConfigStr(), fo.getAIDir())
def _get_default_file_path(): # TODO: determine more robust treatment in case ResourceDir is not writable by user return os.path.join(fo.getAIDir() or ".", DEFAULT_CONFIG_FILE)
import sys import charts_handler from traceback import print_exc from shlex import split import os import freeOrionAIInterface as fo from freeorion_debug.option_tools import get_option_dict, HANDLERS handlers = split(get_option_dict()[HANDLERS]) for handler in handlers: module = os.path.basename(handler)[:-3] if os.path.exists(handler): module_path = os.path.dirname(handler) else: module_path = os.path.join(fo.getAIDir(), os.path.dirname(handler)) sys.path.insert(0, module_path) try: __import__(module) except Exception as e: for p in sys.path: print p print >> sys.stderr, "Fail to import handler %s with error %s" % ( handler, e) print_exc() exit(1)
from shlex import split import os import freeOrionAIInterface as fo from freeorion_debug.option_tools import get_option_dict, HANDLERS handlers = split(get_option_dict()[HANDLERS]) for handler in handlers: module = os.path.basename(handler)[:-3] # There is 3 way to specify path: # - absolute path to module # - single name, then module should be in same directory as config # - relative path, then use AI folder as base path. if os.path.exists(handler): module_path = os.path.dirname(handler) elif not os.path.dirname(handler): module_path = os.path.dirname(fo.getAIConfigStr()) else: module_path = os.path.join(fo.getAIDir(), os.path.dirname(handler)) sys.path.insert(0, module_path) try: __import__(module) except Exception as e: for p in sys.path: print p print >> sys.stderr, "Fail to import handler %s with error %s" % (handler, e) print_exc() exit(1)