1

Since i want my game to be singleplayer (Player vs COM) i need a way to tell the Enemy (COM) what to do. For now i implemented a method so that he turns towards the player and shoots when the turret i aligned. But that seems unrealistic and is kind of boring to be honest.

So i came to an idea: Why not use AI for this? I want to use some kind of library since i dont think im able to make one myself considering my skills. I want to train the tanks by maybe fighting COM1 vs COM2 with fitness points for surviving time / killing (i dont know what to choose???). Although Neural Networks are really mathematic i understand how they work. I want my Enemy to be able to turn his turret left or right and or shoot (3 outputs). This should be done by the input of the map with the player tank (and obstacles on it). This sounds like a not too hard beginning for playing with AI. I tried following an AI tutorial but i couldnt figure out how to implement that for my game. Also, how would this minimap like overview of the Enemy Tank look as a input? And how many neurons should i have for the hidden layer?

In conclusion: I need a library / easy adjustable code for beginners (Maybe even a few or just one single java class with no external ressources) two steer the enemy turret either left or right or even not turning it and shooting/not shooting (doenst have to be the fastest learning one). How would i choose my input type? The most simple AIs worked flawlessly in IntelliJ but they were just calculating with numbers and where still very complicated/not implementable for any purpose. How many neurons in the hidden layer are recommended? How many hidden layers are recommended? How do i let tanks train and how can i save some of the best as a cast for new tanks created in each game? Is saving some AITank of Generation x and mutating him for each game even possible?

Thanks for any answer given!

Code in Greenfoot for the enemy tank as for now:

import greenfoot.*;
import java.util.List;  
public class GoldTank extends EnemyTank
{
    private int shootCoolDown = 200;
    public GoldTank() {
        this.tankImage = new GreenfootImage("Tank_Gold_Base_Idle.png");
        setImage(tankImage);
        getImage().scale(35,20);
        this.speed = 0;
        this.coolDownTime = 100;
    }
    public void act() {
        if(shootCoolDown > 0) {
            shootCoolDown--;
        }
        int rotOffset = (int)calculateTurretRotation() - turret.getRotation(); 
        if(Math.abs(rotOffset) <= 90 || Math.abs(rotOffset) >= 270) {
            if(rotOffset == 0) {
                if(shootCoolDown == 0) {
                    shoot();
                    shootCoolDown = 200;
                }
            } else if(rotOffset < 180 && rotOffset > 0 || rotOffset < -180) {
                getTurret().turn(50);
            } else if(rotOffset > 180 || rotOffset > -180 && rotOffset < 0) {
                getTurret().turn(-50);
            }
        } 
        else {
            /** 
             * Too smart.. turns in right direction for scanning: JUST WAIT OR SO?
             */            
            if(rotOffset < 180 && rotOffset > 0 || rotOffset < -180) {
                getTurret().turn(15);
            } else if(rotOffset > 180 || rotOffset > -180 && rotOffset < 0) {
                getTurret().turn(-15);
            }
        }
    } 
    public double calculateTurretRotation() {
        List<PlayerTank> playerTanks = getWorld().getObjects(PlayerTank.class);
        PlayerTank playerTank = playerTanks.get(0);
        int px = playerTank.getX();
        int py = playerTank.getY();
        int tx = getX();
        int ty = getY();
        double xres = px-tx;
        double yres = py-ty;
        double m = (yres/xres);
        double result = (Math.atan(m)*360)/(2*Math.PI);
        double magicNumber;
        double xresabs = Math.abs(xres);
        double yresabs = Math.abs(yres);
        if(yres > 0) {
            if(xres < 0){
                magicNumber = 180;
                result += magicNumber;
            }
        } else if(yres < 0){
            if(xres < 0){
                magicNumber = 180;
                result += magicNumber;
            } else {
                magicNumber = 360;
                result += magicNumber; 
            }
        }
        return result;
    }
    public void directionMove(int steps, int rotation) {
        int oldRot = getRotation();
        setRotation(rotation);
        move(steps);
        setRotation(oldRot);
    }
}




0 Answers0