Class AIMain

    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • freeColServer

        private final FreeColServer freeColServer
        The server that this AI is operating within.
      • nextId

        private int nextId
        The next AI identifier index.
      • aiObjects

        private final java.util.Map<java.lang.String,​AIObject> aiObjects
        Contains mappings between FreeColGameObjects and AIObjects.
      • OLD_GOODS_WISH_TAG

        private static final java.lang.String OLD_GOODS_WISH_TAG
        See Also:
        Constant Field Values
      • OLD_TILE_IMPROVEMENT_PLAN_TAG

        private static final java.lang.String OLD_TILE_IMPROVEMENT_PLAN_TAG
        See Also:
        Constant Field Values
    • Constructor Detail

      • AIMain

        public AIMain​(FreeColServer freeColServer)
        Creates a new AIMain and searches the current game for FreeColGameObjects.
        Parameters:
        freeColServer - The main controller object for the server.
      • AIMain

        public AIMain​(FreeColServer freeColServer,
                      FreeColXMLReader xr)
               throws javax.xml.stream.XMLStreamException
        Creates a new AIMain from a reader.
        Parameters:
        freeColServer - The main controller object for the server.
        xr - The input stream containing the XML.
        Throws:
        javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.
    • Method Detail

      • getFreeColServer

        public FreeColServer getFreeColServer()
        Gets the main controller object for the server.
        Returns:
        The FreeColServer-object.
      • getGame

        public Game getGame()
        Convenience accessor for the Game.
        Overrides:
        getGame in class FreeColObject
        Returns:
        The Game this AI is operating in.
      • getNextId

        public java.lang.String getNextId()
        Gets a unique identifier for an AIObject.
        Returns:
        A unique identifier.
      • getRandomSeed

        public int getRandomSeed​(java.lang.String logMe)
        Gets a random value from the server to use for individual AI player PRNG seeds.
        Parameters:
        logMe - A logging string.
        Returns:
        A random seed.
      • shouldHaveAIObject

        private boolean shouldHaveAIObject​(FreeColGameObject fcgo)
        Should a FreeColGameObject have a corresponding AI object? Strictly true only for AI players and their units and colonies (not (yet) for native settlements). However object initialization is not necessarily complete when we arrive here, which means we can not yet use the Colony or Unit owner fields. So the actual test implemented here is somewhat sloppy.
        Parameters:
        fcgo - The FreeColGameObject to test.
        Returns:
        True if a corresponding AI object is needed.
      • findNewObjects

        public void findNewObjects​(boolean overwrite)
        Searches for new FreeColGameObjects. An AI-object is created for each new object.
        Parameters:
        overwrite - Determines wether any old AIObject should be overwritten or not.
      • getAIObject

        private AIObject getAIObject​(FreeColGameObject fcgo)
        Gets the AIObject for the given FreeColGameObject.
        Parameters:
        fcgo - The FreeColGameObject to find the AIObject for.
        Returns:
        The AIObject.
        See Also:
        getAIObject(String)
      • getAIObject

        public AIObject getAIObject​(java.lang.String id)
        Gets the AIObject for a given object identifier.
        Parameters:
        id - The object identifier.
        Returns:
        The AIObject.
        See Also:
        getAIObject(FreeColGameObject)
      • addAIObject

        public void addAIObject​(java.lang.String id,
                                AIObject aiObject)
        Adds a reference to the given AIObject. Note: this is called only for new AI objects, thus it is an error for the object to already be present in the aiObjects map.
        Parameters:
        id - The object identifier.
        aiObject - The AIObject to store a reference for.
      • removeAIObject

        public boolean removeAIObject​(java.lang.String id)
        Removes a reference to the given AIObject.
        Parameters:
        id - The object identifier.
        Returns:
        True if an object for the identifier is removed.
      • getAIObjects

        private java.util.List<AIObject> getAIObjects()
        Get a copy of the list of all AI objects.
        Returns:
        A list of AIObjects.
      • getAIObject

        public <T extends AIObject> T getAIObject​(java.lang.String id,
                                                  java.lang.Class<T> returnClass)
        Gets the AIObject with the specified object identifier and class.
        Type Parameters:
        T - The actual return type.
        Parameters:
        id - The object identifier.
        returnClass - The expected class of the object.
        Returns:
        The AIObject found, or null if not.
      • getAIColony

        public AIColony getAIColony​(Colony colony)
        Gets the AI colony corresponding to a given colony.
        Parameters:
        colony - The Colony to look up.
        Returns:
        The corresponding AI colony, or null if not found.
      • getAIPlayer

        public AIPlayer getAIPlayer​(Player player)
        Gets the AI player corresponding to a given player.
        Parameters:
        player - The Player to look up.
        Returns:
        The corresponding AI player, or null if not found.
      • getAIUnit

        public AIUnit getAIUnit​(Unit unit)
        Gets the AI unit corresponding to a given unit.
        Parameters:
        unit - The Unit to look up.
        Returns:
        The corresponding AI unit, or null if not found.
      • getAIStatistics

        public java.util.Map<java.lang.String,​java.lang.String> getAIStatistics()
        Computes how many objects of each class have been created, to track memory leaks over time
        Returns:
        A map of AI statistics.
      • checkSortConsistency

        public java.lang.String checkSortConsistency()
        Check sort consistency.
        Returns:
        A string describing sort consistency failures, or null on success.
      • checkIntegrity

        public Constants.IntegrityType checkIntegrity​(boolean fix,
                                                      LogBuilder lb)
        Check the AI integrity too.
        Parameters:
        fix - If true, fix problems if possible.
        lb - A LogBuilder to log to.
        Returns:
        -1 if there are problems remaining, zero if problems were fixed, +1 if no problems found at all.
      • removeFreeColGameObject

        public void removeFreeColGameObject​(java.lang.String id)
        Removes the AIObject for a given AI identifier. Needed for interface FreeColGameObjectListener.
        Specified by:
        removeFreeColGameObject in interface FreeColGameObjectListener
        Parameters:
        id - The object identifier.
      • ownerChanged

        public void ownerChanged​(FreeColGameObject source,
                                 Player oldOwner,
                                 Player newOwner)
        Replaces the AI object when ownership changes.
        Specified by:
        ownerChanged in interface FreeColGameObjectListener
        Parameters:
        source - The FreeColGameObject that has changed.
        oldOwner - The old owning Player.
        newOwner - The new owning Player.
      • copyIn

        public <T extends FreeColObject> boolean copyIn​(T other)
        Copy another FreeColObject into this one if it is compatible.
        Overrides:
        copyIn in class FreeColObject
        Type Parameters:
        T - The FreeColObject subclass of the object to copy in.
        Parameters:
        other - The other object.
        Returns:
        True if the copy in is succesful.
      • writeAttributes

        protected void writeAttributes​(FreeColXMLWriter xw)
                                throws javax.xml.stream.XMLStreamException
        Write the attributes of this object to a stream. To be overridden if required by any object that has attributes and uses the toXML(FreeColXMLWriter, String) call.
        Overrides:
        writeAttributes in class FreeColObject
        Parameters:
        xw - The FreeColXMLWriter to write to.
        Throws:
        javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.
      • writeChildren

        protected void writeChildren​(FreeColXMLWriter xw)
                              throws javax.xml.stream.XMLStreamException
        Write the children of this object to a stream. To be overridden if required by any object that has children and uses the toXML(FreeColXMLWriter, String) call.
        Overrides:
        writeChildren in class FreeColObject
        Parameters:
        xw - The FreeColXMLWriter to write to.
        Throws:
        javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.
      • readAttributes

        protected void readAttributes​(FreeColXMLReader xr)
                               throws javax.xml.stream.XMLStreamException
        Reads the attributes of this object from an XML stream.
        Overrides:
        readAttributes in class FreeColObject
        Parameters:
        xr - The FreeColXMLReader to read from.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.
      • readChildren

        protected void readChildren​(FreeColXMLReader xr)
                             throws javax.xml.stream.XMLStreamException
        Reads the children of this object from an XML stream.
        Overrides:
        readChildren in class FreeColObject
        Parameters:
        xr - The FreeColXMLReader to read from.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.
      • readChild

        protected void readChild​(FreeColXMLReader xr)
                          throws javax.xml.stream.XMLStreamException
        Reads a single child object. Subclasses must override to read their enclosed elements. This particular instance of the routine always throws XMLStreamException because we should never arrive here. However it is very useful to always call super.readChild() when an unexpected tag is encountered, as the exception thrown here provides some useful debugging context.
        Overrides:
        readChild in class FreeColObject
        Parameters:
        xr - The FreeColXMLReader to read from.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.
      • getXMLTagName

        public java.lang.String getXMLTagName()
        Get the serialization tag for this object.
        Specified by:
        getXMLTagName in class FreeColObject
        Returns:
        The tag.