Example #1
0
def initFreeOrionAI():  # pylint: disable=invalid-name
    """Called by client when Python AI starts, before any game new game starts or saved game is resumed."""
    ai_config = fo.getAIConfigStr()
    print "Initialized FreeOrion Python AI with ai_config string '%s'" % ai_config
    user_dir = fo.getUserDir()
    print "Path to folder for user specific data: %s" % user_dir
    print(sys.path)
Example #2
0
def _get_option_file_name():
    # hack to allow lunch this code separately to dump default config
    try:
        return fo.getAIConfigStr()
    except AttributeError as e:
        print "Cant get options file", e
        return None
Example #3
0
def _get_option_file_name():
    # hack to allow lunch this code separately to dump default config
    try:
        return fo.getAIConfigStr()
    except AttributeError as e:
        print "Cant get options file", e
        return None
Example #4
0
def _parse_options():
    # get defaults; check if don't already exist and can write
    default_file = _get_option_file()
    if os.path.exists(default_file):
        config = SafeConfigParser()
        config.read([default_file])
    else:
        try:
            config = _create_default_config_file(default_file)
        except IOError:
            sys.stderr.write("AI Config: default file is not present and not writable at location %s\n" % default_file)
            config = _create_default_config_file(os.path.join(fo.getUserConfigDir(), CONFIG_DEFAULT_FILE))

    option_string = fo.getAIConfigStr()

    if option_string:
        config_files = [option_string]
        configs_read = config.read(config_files)
        print "AI Config read config file(s): %s" % configs_read
        if len(configs_read) != len(config_files):
            sys.stderr.write("AI Config Error; could NOT read config file(s): %s\n"
                             % list(set(config_files).difference(configs_read)))
    for section in config.sections():
        sectioned_options.setdefault(section, odict())
        for k, v in config.items(section):
            flat_options[k] = v
            sectioned_options[section][k] = v
Example #5
0
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
Example #6
0
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
Example #7
0
def initFreeOrionAI():  # pylint: disable=invalid-name
    """Called by client when Python AI starts, before any game new game starts or saved game is resumed."""
    ai_config = fo.getAIConfigStr()
    print "Initialized FreeOrion Python AI with ai_config string '%s'" % ai_config
    print(sys.path)
Example #8
0
"""The FreeOrionAI module contains the methods which can be made by the C AIInterface;
these methods in turn activate other portions of the python AI code."""
import pickle  # Python object serialization library
import sys
import random

# IMPORTANT! this import also execute python code to update freeOrionAIInterface interface,
# removing this import will brake AI in unexpected way.

import freeOrionAIInterface as fo  # interface used to interact with FreeOrion AI client  # pylint: disable=import-error

from common.option_tools import parse_config
parse_config(fo.getAIConfigStr(), fo.getUserConfigDir())

from freeorion_tools import patch_interface
patch_interface()

import AIstate
import ColonisationAI
import ExplorationAI
import DiplomaticCorp
import FleetUtilsAI
import InvasionAI
import MilitaryAI
import PlanetUtilsAI
import PriorityAI
import ProductionAI
import ResearchAI
import ResourcesAI
from freeorion_tools import UserStringList, chat_on_error, print_error, UserString, handle_debug_chat, Timer, init_handlers
from common.listeners import listener
Example #9
0
def initFreeOrionAI():  # pylint: disable=invalid-name
    """Called by client when Python AI starts, before any game new game starts or saved game is resumed."""
    ai_config = fo.getAIConfigStr()
    print "Initialized FreeOrion Python AI with ai_config string '%s'" % ai_config
    print(sys.path)
Example #10
0
from freeorion_tools import UserStringList, chat_on_error, print_error, UserString
from freeorion_debug import Timer

main_timer = Timer('timer', write_log=True)
turn_timer = Timer('bucket', write_log=True)

using_statprof = False
try:
    import statprof
    # statprof.start()
    # using_statprof = True
except ImportError:
    pass


ai_config = fo.getAIConfigStr()
print "Initialized FreeOrion Python AI with ai_config string '%s'" % ai_config
user_dir = fo.getUserDataDir()
print "Path to folder for user specific data: %s" % user_dir
print 'Python paths', sys.path

_aggression_names = {fo.aggression.beginner: "GSETUP_BEGINNER",
                     fo.aggression.turtle: "GSETUP_TURTLE",
                     fo.aggression.cautious: "GSETUP_CAUTIOUS",
                     fo.aggression.typical: "GSETUP_TYPICAL",
                     fo.aggression.aggressive: "GSETUP_AGGRESSIVE",
                     fo.aggression.maniacal: "GSETUP_MANIACAL"}

_capitals = {fo.aggression.beginner: UserStringList("AI_CAPITOL_NAMES_BEGINNER"),
             fo.aggression.turtle: UserStringList("AI_CAPITOL_NAMES_TURTLE"),
             fo.aggression.cautious: UserStringList("AI_CAPITOL_NAMES_CAUTIOUS"),
def initFreeOrionAI():  # pylint: disable=invalid-name
    """called by client to initialize AI """
    ai_config = fo.getAIConfigStr()
    print "Initialized FreeOrion Python AI with ai_config string '%s'" % ai_config
    print(sys.path)
Example #12
0
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)
Example #13
0
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)
Example #14
0
"""The FreeOrionAI module contains the methods which can be made by the C AIInterface;
these methods in turn activate other portions of the python AI code."""
import pickle  # Python object serialization library
import sys
import random

from common import configure_logging

import freeOrionAIInterface as fo  # interface used to interact with FreeOrion AI client  # pylint: disable=import-error

from common.option_tools import parse_config
parse_config(fo.getAIConfigStr(), fo.getUserConfigDir())

from freeorion_tools import patch_interface
patch_interface()

import AIstate
import ColonisationAI
import ExplorationAI
import DiplomaticCorp
import FleetUtilsAI
import InvasionAI
import MilitaryAI
import PlanetUtilsAI
import PriorityAI
import ProductionAI
import ResearchAI
import ResourcesAI
import TechsListsAI
from freeorion_tools import UserStringList, chat_on_error, print_error, UserString, handle_debug_chat, Timer, init_handlers
from common.listeners import listener