Ask HN: AI Wars – control your 2d space shooter with AI

5 points by thepromptgenius 3 hours ago

I've been trying to get around to building a game that allows players to control their characters using AI scripts. So essentially, you don't control your character, but instead the AI controls your character, you just design the AI.

I finally have a prototype up and running, its a 2d space shooter that allows you to program your ship using a simply ChatGPT-like AI prompt.

As a player, all you need to do is give a prompt just like you would with ChatGPT. So for example the prompt I used to program my ship was:

Play aggressively when score is low, become more defensive as score increases

The game then automatically converts this to code that is used to control your ship, in my case it was:

loadPlayerAI(` const score = ai.getScore(); const aggressiveThreshold = 1000; // Adjust as needed const nearestEnemy = ai.getNearestEnemy();

    if (score < aggressiveThreshold) {
        // Aggressive play
        if (nearestEnemy) {
            ai.moveTo(nearestEnemy.x);
            ai.fire();
        } else {
            const screenDims = ai.getScreenDimensions();
            ai.moveTo(screenDims.width / 2);
        }
    } else {
        // Defensive play
        const safeZone = ai.findSafeZone();
        ai.moveTo(safeZone);
        
        if (nearestEnemy && ai.isEnemyInFrontAndRange(nearestEnemy)) {
            if (ai.calculateDistance(ai.getPlayerPosition(), nearestEnemy) > 100) {
                ai.fire();
            }
        }
    }
`);

The ship then automatically moves around shooting down enemy ships, once a collision between you and the enemy ship happens its game over. Number of enemy ships keeps increasing as your score increases which makes it hard to last long without a good prompt. The game now accepts any kind of prompt that you provide and translates it to valid code which runs your ship in the game environment.

I was thinking to develop this further into a tournament/competition where players provide their scripts, and their ships will compete to see who scores the highest (max time limit is 1 minute of play time if your ship can last that long). Ships compete and the best prompt/program wins!

Would anyone be interested to try it out as a player? I just need a prompt and I will load it into your spaceship, I'll share a link where you can see it in action

Below is the "framework" that describes the capabilities available to the ship. You don't need to mention these explicitly in your script, the game engine translates your prompt to code based on these capabilities so for example if your prompt says when surrounded by 10 enemy ships find a safe spot it will probably use functions like `countNearbyEnemies()` and `findSafeZone()` in the code it outputs.

>>>AI Wars - Framework (couldn't add all of it due to character limit on HN)

>Sensing

getNearestEnemy(): Returns the closest enemy object getAllEnemies(): Returns an array of all enemies on screen getPlayerPosition(): Returns the current position of the player's ship getScreenDimensions(): Returns the dimensions of the game screen

>Movement

moveLeft(speed): Move the ship left at the specified speed moveRight(speed): Move the ship right at the specified speed moveTo(x): Move the ship to a specific x-coordinate

>Combat

fire(): Shoot a missile (subject to cooldown) getWeaponCooldown(): Returns the current weapon cooldown status

>Utility calculateDistance(obj1, obj2): Calculate the distance between two objects predictEnemyPosition(enemy, time): Predict where an enemy will be after a given time countNearbyEnemies(): Calculates the number of nearby enemies within 30% of the screen width

>Decision Making

shouldDodge(enemy): Determines if the ship should dodge a specific enemy isEnemyInFrontAndRange(enemy): Checks if an enemy is directly in front and within firing range

.. truncated

jhbadger 42 minutes ago

For the long history of such games (where people actually wrote the rather simple AI code for their avatar), take a look at RobotWar and its various later clones like C-Robots and Omega.

https://en.wikipedia.org/wiki/RobotWar

solardev an hour ago

FYI there is already a tower defense game series called AI War on Steam. It doesn't use code, but the name is similar enough to be confusing. https://store.steampowered.com/app/573410/AI_War_2/

  • thepromptgenius an hour ago

    thanks for the note! I didn't know about this game tbf, I don't have a name yet for the project. It just started out as a 2d space shooter, and evolved as an AI controlled game