Esempio n. 1
0
    public void Generate(Genotype genotype, Creature creature)
    {
        timeOffset = Random.Range(0f, 7f);

        Clear();

        List <Cell> spawningFromCells = new List <Cell>();

        spawningFromCells.Add(SpawnCell(genotype.GetGeneAt(0), new Vector2i(), 0, CardinalDirectionHelper.ToIndex(CardinalDirection.north), creature)); //root

        List <Cell> nextSpawningFromCells = new List <Cell>();

        for (int buildOrderIndex = 1; spawningFromCells.Count != 0 && buildOrderIndex < 4; buildOrderIndex++)
        {
            foreach (Cell cell in spawningFromCells)
            {
                for (int referenceDirection = 0; referenceDirection < 6; referenceDirection++)
                {
                    if (cell.gene.getReference(referenceDirection) != null)
                    {
                        int      referenceHeading         = (cell.heading + referenceDirection + 5) % 6; //!!
                        Gene     referenceGene            = genotype.GetGeneAt((int)cell.gene.getReference(referenceDirection));
                        Vector2i referenceCellMapPosition = cellMap.GetGridNeighbourGridPosition(cell.mapPosition, referenceHeading);

                        if (cellMap.IsLegalPosition(referenceCellMapPosition))
                        {
                            Cell residentCell = cellMap.GetCell(referenceCellMapPosition);
                            if (residentCell == null)
                            {
                                //only time we spawn a cell if there is a vacant spot
                                Cell newCell = SpawnCell(referenceGene, referenceCellMapPosition, buildOrderIndex, referenceHeading, creature);
                                nextSpawningFromCells.Add(newCell);
                                cellList.Add(cell);
                            }
                            else
                            {
                                if (residentCell.buildOrderIndex > buildOrderIndex)
                                {
                                    throw new System.Exception("Trying to spawn a cell at a location where a cell of higher build order are allready present.");
                                }
                                else if (residentCell.buildOrderIndex == buildOrderIndex)
                                {
                                    //trying to spawn a cell where ther is one allready with the same buildOrderIndex, in fight over this place bothe cwlls will loose, so the resident will be removed
                                    GameObject.Destroy(residentCell.gameObject);
                                    cellList.Remove(residentCell);
                                    cellMap.removeCellAtGridPosition(residentCell.mapPosition);
                                    nextSpawningFromCells.Remove(residentCell);
                                    cellMap.MarkAsIllegal(residentCell.mapPosition);
                                }
                                else
                                {
                                    // trying to spawn a cell where there is one with lowerBuildOrder index, no action needed
                                }
                            }
                        }
                    }
                }
            }
            spawningFromCells.Clear();
            spawningFromCells.AddRange(nextSpawningFromCells);
            nextSpawningFromCells.Clear();
            if (buildOrderIndex == 99)
            {
                throw new System.Exception("Creature generation going on for too long");
            }
        }

        ConnectCells();
        edges.GenerateWings(cellList);
        UpdateSpringsFrequenze();
    }