예제 #1
0
    public FieldStateData stateToStateData(FieldState state)
    {
        FieldStateData stateData = new FieldStateData();

        stateData.explorationRate      = state.explorationRate;
        stateData.damageReceived       = state.damageReceived;
        stateData.damageDealt          = state.damageDealt;
        stateData.killed               = state.killed;
        stateData.lowDurability        = state.lowDurability;
        stateData.touchingWall         = state.touchingWall;
        stateData.enemiesKilled        = state.enemiesKilled;
        stateData.enemiesLowDurability = state.enemiesLowDurability;
        stateData.canSeeEnemies        = state.canSeeEnemies;
        stateData.touchingObstacles    = state.touchingObstacles;
        stateData.enemiesCanHit        = state.enemiesCanHit;
        stateData.attachmentsCharging  = state.attachmentsCharging;
        stateData.attachmentsCooling   = state.attachmentsCooling;
        stateData.numberOfEnemies      = state.numberOfEnemies;
        stateData.timeSinceLastAction  = state.timeSinceLastAction;
        List <PlayerPartData> attachmentsData = new List <PlayerPartData>();

        foreach (Attachment attachment in state.attachments)
        {
            PlayerPartData partData = new PlayerPartData();
            partData.id = attachment.getID();
            partData.remainingDurability = attachment.getRemainingDurability();
            attachmentsData.Add(partData);
        }
        stateData.attachments              = attachmentsData.ToArray();
        stateData.enemiesPosition          = state.enemiesPosition;
        stateData.enemiesTouchingObstacles = state.enemiesTouchingObstacles;
        stateData.actions = actionsToActionsData(state.actions);
        return(stateData);
    }
예제 #2
0
    public PlayerData dataToPlayerData(long experience, long credits, double previousRoundDamageDifference, double previousRoundMaxDamageDifference, double previousRoundTimeElapsed, List <Part> humanRobotParts, List <Robot> myRobots, List <ObstacleData> obstaclesData)
    {
        PlayerData            playerData      = new PlayerData();
        List <PlayerPartData> playerPartsData = new List <PlayerPartData>();

        foreach (Part part in humanRobotParts)
        {
            PlayerPartData playerPartData = new PlayerPartData();
            playerPartData.id = part.getID();
            playerPartData.remainingDurability = part.getRemainingDurability();
            playerPartsData.Add(playerPartData);
        }
        List <RobotData> robotsData = new List <RobotData>();

        foreach (Robot robot in myRobots)
        {
            RobotData robotData = new RobotData();
            robotData.name  = robot.getName();
            robotData.human = robot.isHuman();
            List <int> partIndices = new List <int>();
            Part[]     parts       = robot.getParts();
            for (int partIndex = 0; partIndex < parts.Length; ++partIndex)
            {
                partIndices.Add(playerPartsData.FindIndex(p => (p.id == parts[partIndex].getID())));
            }
            robotData.partIndices = partIndices.ToArray();
            robotsData.Add(robotData);
        }
        playerData.experience = experience;
        playerData.credits    = credits;
        playerData.previousRoundDamageDifference    = previousRoundDamageDifference;
        playerData.previousRoundMaxDamageDifference = previousRoundMaxDamageDifference;
        playerData.previousRoundTimeElapsed         = previousRoundTimeElapsed;
        playerData.humanRobotParts = playerPartsData.ToArray();
        playerData.myRobots        = robotsData.ToArray();
        playerData.obstacles       = obstaclesData.ToArray();
        return(playerData);
    }
예제 #3
0
    private async void initializeScene(string sceneName) //test stuff inside! will need to be rewritten for actual game
    {
        running        = true;
        saveDataThread = new Thread(saveData);
        saveDataThread.Start();
        if (currentSettings == "")
        {
            currentSettings = SettingsManager.DEFAULT_SETTINGS;
        }
        SettingPairs settingPairs = settingsManager.getSettingPairs(currentSettings);

        settingList = settingsManager.getSettingList(currentSettings);
        fieldSize   = Field.getFieldSize(settingPairs.field_size);
        foreach (PartData partData in partsData)
        {
            Part part = partManager.partDataToPart(partData);
            if (part.GAME_OBJECT != null)
            {
                part.toggleGameObject(false);
            }
            parts.Add(part);
        }
        if (currentTest == TESTS.BUILD_HUB)
        {
            IconGenerator iconGenerator = new IconGenerator();
            Camera        camera        = null;
            foreach (Camera currCam in Camera.allCameras)
            {
                if (currCam.name == "IconCamera")
                {
                    camera = currCam;
                    camera.forceIntoRenderTexture = true;
                    break;
                }
            }
            iconGenerator.camera      = camera;
            Camera.main.targetDisplay = 1;
            foreach (Part part in parts)
            {
                if (part.GAME_OBJECT == null)
                {
                    continue;
                }
                part.toggleGameObject(true);
                part.GAME_OBJECT.transform.Rotate(new Vector3(9, 120, -15));
                iconGenerator.gameObjectOfIcon = part.GAME_OBJECT;
                while (part.getIcon() == null)
                {
                    if (part.GAME_OBJECT.GetComponent <Renderer>().isVisible)
                    {
                        iconGenerator.initialize();
                        if (iconGenerator.getIcon() != null)
                        {
                            part.setIcon(iconGenerator.getIcon());
                        }
                    }
                    if (part.getIcon() == null)
                    {
                        await Task.Delay(25);
                    }
                }
                part.toggleGameObject(false);
                part.destroyGameObject();
            }
            Camera.main.targetDisplay = 0;
        }
        else
        {
            foreach (Part part in parts)
            {
                if (part.GAME_OBJECT != null)
                {
                    part.destroyGameObject();
                }
            }
        }
        experience = playerData.experience;
        credits    = playerData.credits;
        previousRoundDamageDifference    = playerData.previousRoundDamageDifference;
        previousRoundMaxDamageDifference = playerData.previousRoundMaxDamageDifference;
        previousRoundTimeElapsed         = playerData.previousRoundTimeElapsed;
        ObstacleGenerator obstacleGenerator = new ObstacleGenerator(experience, settingPairs.max_obstacles, fieldSize);

        humanRobotParts = new List <Part>();
        obstaclesData   = new List <ObstacleData>();
        List <int> badIndices = new List <int>();
        PerformanceMetricCalculator performanceMetricCalculator = new PerformanceMetricCalculator();
        Head     cheapestHead     = null;
        Body     cheapestBody     = null;
        Mobility cheapestMobility = null;
        Blaster  cheapestBlaster  = null;

        foreach (Part part in parts)
        {
            if (part is Head && (cheapestHead == null || performanceMetricCalculator.calculateCost(part) < performanceMetricCalculator.calculateCost(cheapestHead)))
            {
                cheapestHead = (Head)part;
            }
            else if (part is Body && (cheapestBody == null || performanceMetricCalculator.calculateCost(part) < performanceMetricCalculator.calculateCost(cheapestBody)))
            {
                cheapestBody = (Body)part;
            }
            else if (part is Mobility && (cheapestMobility == null || performanceMetricCalculator.calculateCost(part) < performanceMetricCalculator.calculateCost(cheapestMobility)))
            {
                cheapestMobility = (Mobility)part;
            }
            else if (part is Blaster && (cheapestBlaster == null || performanceMetricCalculator.calculateCost(part) < performanceMetricCalculator.calculateCost(cheapestBlaster)))
            {
                cheapestBlaster = (Blaster)part;
            }
        }
        if (playerData.humanRobotParts != default)
        {
            for (int partIndex = 0; partIndex < playerData.humanRobotParts.Length; ++partIndex)
            {
                PlayerPartData playerPartData = playerData.humanRobotParts[partIndex];
                if (parts.Exists(p => p.getID() == playerPartData.id))
                {
                    Part part = parts.Find(p => p.getID() == playerPartData.id);
                    part.damage(part.getDurability() - playerPartData.remainingDurability);
                    if (part is Head)
                    {
                        humanRobotParts.Add((Head)part.clone(true));
                    }
                    else if (part is Body)
                    {
                        humanRobotParts.Add((Body)part.clone(true));
                    }
                    else if (part is Mobility)
                    {
                        humanRobotParts.Add((Mobility)part.clone(true));
                    }
                    else if (part is Attachment)
                    {
                        humanRobotParts.Add((Attachment)part.clone(true));
                    }
                }
                else
                {
                    humanRobotParts.Add(null);
                    badIndices.Add(partIndex);
                }
            }
        }
        else
        {
            humanRobotParts.AddRange(new Part[] { cheapestHead, cheapestBody, cheapestMobility, cheapestBlaster });
        }
        myRobots = new List <Robot>();
        if (playerData.myRobots != default)
        {
            foreach (RobotData robotData in playerData.myRobots)
            {
                List <Part> robotPartList = new List <Part>();
                foreach (int partIndex in robotData.partIndices)
                {
                    if (!badIndices.Contains(partIndex))
                    {
                        robotPartList.Add(humanRobotParts[partIndex]);
                    }
                }
                try
                {
                    Robot robot = new Robot(robotData.name, true, robotData.human, robotPartList.ToArray());
                    myRobots.Add(robot);
                }
                catch {}
            }
        }
        foreach (int index in badIndices)
        {
            humanRobotParts.RemoveAt(index);
        }
        if (!humanRobotParts.Exists(part => part is Head))
        {
            humanRobotParts.Add(cheapestHead);
        }
        if (!humanRobotParts.Exists(part => part is Body))
        {
            humanRobotParts.Add(cheapestBody);
        }
        if (!humanRobotParts.Exists(part => part is Mobility))
        {
            humanRobotParts.Add(cheapestMobility);
        }
        if (!humanRobotParts.Exists(part => part is Attachment && ((Attachment)part).isWeapon()))
        {
            humanRobotParts.Add(cheapestBlaster);
        }
        if (humanRobotParts.Contains(null))
        {
            throw new Exception("There are missing part files. There neeeds to be at least one part file each of types Head, Body, Mobility, and a weapon Attachment.");
        }
        if (playerData.obstacles != default)
        {
            obstaclesData.AddRange(playerData.obstacles);
        }
        else
        {
            obstaclesData.AddRange(obstacleGenerator.getObstaclesData());
        }
        if (currentSettingValueList.Count == 0)
        {
            foreach (Setting setting in settingList)
            {
                currentSettingValueList.Add(setting.currentValue);
            }
        }
        Head       head1       = null;
        Head       head2       = null;
        Body       body1       = null;
        Body       body2       = null;
        Mobility   mobility    = null;
        Attachment attachment1 = null;
        Attachment attachment2 = null;
        Attachment attachment3 = null;

        foreach (Part part in parts)
        {
            if (part is Head)
            {
                if (head1 == null)
                {
                    head1 = (Head)part.clone(true);
                    head1.damage(2);
                }
                else if (head2 == null)
                {
                    head2 = (Head)part.clone(true);
                    head2.damage(3);
                }
            }
            else if (part is Body)
            {
                if (body1 == null)
                {
                    body1 = (Body)part.clone(true);
                    body1.damage(3);
                }
                else if (body2 == null)
                {
                    body2 = (Body)part.clone(true);
                    body2.damage(4);
                }
            }
            else if (part is Mobility)
            {
                if (mobility == null)
                {
                    mobility = (Mobility)part.clone(true);
                    mobility.damage(1);
                }
            }
            else if (part is Attachment)
            {
                if (attachment1 == null)
                {
                    attachment1 = (Attachment)part.clone(true);
                    attachment1.damage(1);
                }
                else if (attachment2 == null)
                {
                    attachment2 = (Attachment)part.clone(true);
                    attachment2.damage(1);
                }
                else if (attachment3 == null)
                {
                    attachment3 = (Attachment)part.clone(true);
                    attachment3.damage(3);
                }
            }
        }
        List <Robot> robots = new List <Robot>();
        int          numberOfNonHumanRobots = 0;
        int          numberOfHumanRobots    = 0;

        Obstacle[]           obstacles      = null;
        List <BuildHubState> buildHubStates = default;

        if (buildHubStatesData != default && buildHubStatesData.Count > 0)
        {
            buildHubStates = new List <BuildHubState>();
            foreach (BuildHubStateData buildHubStateData in buildHubStatesData)
            {
                buildHubStates.Add(buildHubStateDataManager.stateDataToState(buildHubStateData));
            }
        }
        List <FieldState> fieldStates = default;

        if (fieldStatesData != default && fieldStatesData.Count > 0)
        {
            fieldStates = new List <FieldState>();
            foreach (FieldStateData fieldStateData in fieldStatesData)
            {
                fieldStates.Add(fieldStateDataManager.stateDataToState(fieldStateData, parts));
            }
        }
        BuildHub.MODES mode;
        switch (currentTest)
        {
        case TESTS.FIELD:
        case TESTS.HUMAN_V_VOID_ROBOT:
            List <Part> humanParts = new List <Part>();
            foreach (Part part in humanRobot.getParts())
            {
                Part clonePart = part.clone(false);
                if (clonePart is Head)
                {
                    clonePart = (Head)clonePart;
                }
                else if (clonePart is Body)
                {
                    clonePart = (Body)clonePart;
                }
                else if (clonePart is Mobility)
                {
                    clonePart = (Mobility)clonePart;
                }
                else if (clonePart is Attachment)
                {
                    clonePart = (Attachment)clonePart;
                }
                humanParts.Add(clonePart);
            }
            humanRobot = new Robot(humanRobot.getName(), humanRobot.isHuman(), humanParts.ToArray());
            currentHumanRobotHealth = humanRobot.getRemainingDurability();
            Attachment[] nonHumanAttachments = { (Attachment)attachment1.clone(true) };
            numberOfNonHumanRobots = RANDOM.Next(1, settingPairs.max_ai_robots + 1);
            numberOfHumanRobots    = 1;
            obstacles = obstacleGenerator.obstaclesDataToObstacles(obstaclesData.ToArray());
            aiManager = new AIManager(numberOfNonHumanRobots, numberOfNonHumanRobots - 1 + numberOfHumanRobots, new Robot[] { humanRobot }, parts.ToArray(), new Part[] { cheapestHead, cheapestBody, cheapestMobility, cheapestBlaster }, obstacles, fieldSize, buildHubStates, fieldStates, experience);
            robots.Add(humanRobot);
            robots.AddRange(aiManager.getAgentRobots());
            setScreen(new Field(settingList, robots.ToArray(), obstacles, previousRoundDamageDifference, previousRoundMaxDamageDifference, previousRoundTimeElapsed));
            break;

        case TESTS.AI_AGENT_TRAINING:
            numberOfNonHumanRobots = NUMBER_OF_AGENTS;
            numberOfHumanRobots    = 0;
            obstacles = obstacleGenerator.obstaclesDataToObstacles(obstaclesData.ToArray());
            aiManager = new AIManager(numberOfNonHumanRobots, numberOfNonHumanRobots - 1 + numberOfHumanRobots, null, parts.ToArray(), new Part[] { cheapestHead, cheapestBody, cheapestMobility, cheapestBlaster }, obstacles, fieldSize, buildHubStates, fieldStates, experience);
            Robot[] agentRobots = aiManager.getAgentRobots();
            for (int agentIndex = 0; agentIndex < NUMBER_OF_AGENTS; ++agentIndex)
            {
                FIELD_AGENT_ROBOTS[agentIndex] = agentRobots[agentIndex];
            }
            robots.AddRange(FIELD_AGENT_ROBOTS);
            setScreen(new Field(settingList, robots.ToArray(), obstacles, previousRoundDamageDifference, previousRoundMaxDamageDifference, previousRoundTimeElapsed));
            break;

        case TESTS.BUILD_HUB:
            mode = BuildHub.MODES.MY_ROBOTS;
            setScreen(new BuildHub(settingList, obstaclesData, myRobots, humanRobotParts, parts.ToArray(), credits, mode));
            break;
        }
    }