Beispiel #1
0
    // get exact clone of creature (maintains state)
    public static Creature getCreatureCopy(Creature c)
    {
        Creature creatureCopy = c.getShallowCopy();
        int      actualSeed;
        int      timeInMillis = System.DateTime.Now.Millisecond;

        lock (seedGenLock)
        {
            // reset seed if too large
            if (seed == Int32.MaxValue - 1000)
            {
                seed = 0;
            }
            // increment seed
            seed++;
            // generate random seed
            actualSeed = seedGen.Next(seed);
        }
        // Debug.Log("new rand gen seed: " + actualSeed);
        // use combination of random seed and time in milliseconds to create random number generator for new creature
        creatureCopy.rand  = new System.Random(actualSeed + timeInMillis);
        creatureCopy.rand2 = new System.Random(actualSeed + timeInMillis + 999);

        // copy dummy land
        creatureCopy.dummyLand         = new Land();
        creatureCopy.dummyLand.isDummy = true;

        // copy creature networks
        List <Dictionary <string, Network> > networks = new List <Dictionary <string, Network> >();

        creatureCopy.networks = networks;
        List <Dictionary <string, Network> > origNetworks = c.networks;

        copyCreatureNetworks(origNetworks, networks, creatureCopy);

        // copy phenotype network template
        //Debug.Log("before copy: " + c.phenotypeNetTemplate.net[0].Count);
        creatureCopy.phenotypeNetTemplate = (PhenotypeNetwork)copyNetwork(c.phenotypeNetTemplate, creatureCopy);
        creatureCopy.phenotypeNetTemplate.parentCreature = creatureCopy;
        //Debug.Log("after copy: " + creatureCopy.phenotypeNetTemplate.net[0].Count);

        // copy position (overridden when populating)
        creatureCopy.position    = new int[2];
        creatureCopy.position[0] = c.position[0];
        creatureCopy.position[1] = c.position[1];

        creatureCopy.neighborLands = new Land[5]; // assigne when creature is placed on the map

        // copy actions
        // TODO: test to make sure that order is maintained
        creatureCopy.actionQueue = new SimplePriorityQueue <Action>();
        while (c.actionQueue.Count > 0)
        {
            Action a = c.actionQueue.Dequeue();
            creatureCopy.actionQueue.Enqueue(getNewAction(a), a.priority);
        }

        // copy output comm signals
        creatureCopy.outputCommSignals = new List <CommSignal>();
        copyCommList(c.outputCommSignals, creatureCopy.outputCommSignals);

        // copy prevNetStates
        creatureCopy.prevNetStates = new List <List <Dictionary <string, Network> > >();
        foreach (List <Dictionary <string, Network> > origState in c.prevNetStates)
        {
            List <Dictionary <string, Network> > stateCopy = new List <Dictionary <string, Network> >();
            copyCreatureNetworks(origState, stateCopy, creatureCopy);
            creatureCopy.prevNetStates.Add(stateCopy);
        }

        // copy abilities
        creatureCopy.abilities = new Dictionary <string, Ability>();
        foreach (string ability in c.abilities.Keys)
        {
            Ability oldAbility = c.abilities[ability];
            Ability newAbility = getNewAbility(oldAbility);
            creatureCopy.abilities[ability] = newAbility;
        }

        // copy stored resources
        creatureCopy.storedResources = new Dictionary <string, CreatureResource>();
        foreach (string resKey in c.storedResources.Keys)
        {
            creatureCopy.storedResources[resKey] = c.storedResources[resKey].getShallowCopy();
        }

        // copy phenotype
        creatureCopy.phenotype = new bool[c.phenotype.Length];
        Array.Copy(c.phenotype, creatureCopy.phenotype, c.phenotype.Length);

        // copy inputCommList
        creatureCopy.inputCommList = new List <CommSignal>();
        copyCommList(c.inputCommList, creatureCopy.inputCommList);

        // copy commInNetTemplate
        creatureCopy.commInNetTemplate     = c.commInNetTemplate.getShallowCopy();
        creatureCopy.commInNetTemplate.net = new List <List <Node> >();
        List <List <Node> > newCommNet  = creatureCopy.commInNetTemplate.net;
        List <List <Node> > origCommNet = c.commInNetTemplate.net;

        for (int j = 0; j < origCommNet.Count; j++)
        {
            newCommNet.Add(new List <Node>());
            for (int k = 0; k < origCommNet[j].Count; k++)
            {
                newCommNet[j].Add(getNewNode(origCommNet[j][k], creatureCopy, creatureCopy.commInNetTemplate));
            }
        }
        // copy commOutNetTemplate
        creatureCopy.commOutNetTemplate     = c.commOutNetTemplate.getShallowCopy();
        creatureCopy.commOutNetTemplate.net = new List <List <Node> >();
        List <List <Node> > newCommOutNet  = creatureCopy.commOutNetTemplate.net;
        List <List <Node> > origCommOutNet = c.commOutNetTemplate.net;

        for (int j = 0; j < origCommOutNet.Count; j++)
        {
            newCommOutNet.Add(new List <Node>());
            for (int k = 0; k < origCommOutNet[j].Count; k++)
            {
                newCommOutNet[j].Add(getNewNode(origCommOutNet[j][k], creatureCopy, creatureCopy.commOutNetTemplate));
            }
        }

        // copy reproductionRequests
        creatureCopy.reproductionRequests = new List <ReproAction>();
        foreach (ReproAction reproAction in c.reproductionRequests)
        {
            ReproAction reproActionCopy = reproAction.shallowCopy();
            creatureCopy.reproductionRequests.Add(reproActionCopy);
        }

        // TODO: copy reproduction decider network


        // copy action pool
        creatureCopy.actionPool = new Dictionary <string, Action>();
        foreach (string key in c.actionPool.Keys)
        {
            creatureCopy.actionPool[key] = getNewAction(c.actionPool[key]);
        }

        return(creatureCopy);
    }
 public ReproActionEditor(ReproAction reproAction)
 {
     action = reproAction;
 }