Skip to content

Instantly share code, notes, and snippets.

@dgant
Created December 5, 2022 19:47
Show Gist options
  • Select an option

  • Save dgant/fe81ddedea0cfb3ebbf6c74caa18e3dc to your computer and use it in GitHub Desktop.

Select an option

Save dgant/fe81ddedea0cfb3ebbf6c74caa18e3dc to your computer and use it in GitHub Desktop.
Me:
Write a StarCraft bot in C++, which replaces ExampleAIModule.dll. The bot should rush the opponent with workers, and use mineral walking to stack workers as they attack the enemy.
ChatGPT:
Here is an example of a simple StarCraft bot written in C++ that rushes the opponent with workers and uses mineral walking to stack workers as they attack the enemy:
#include "ExampleAIModule.h"
using namespace BWAPI;
void ExampleAIModule::onStart()
{
// Set the game speed to the fastest possible setting
Broodwar->setLocalSpeed(0);
// Use setCommandOptimizationLevel to change the level of optimization
// for commands sent to units.
//
// This level determines how many commands are packed into a single
// command packet, which can improve network performance. However,
// setting this to a higher level can cause units to behave unpredictably
// because it can cause them to execute multiple commands in a single
// frame, which can lead to unintended side effects.
//
// In this example, we will set the optimization level to 3, which
// is the highest level of optimization.
Broodwar->setCommandOptimizationLevel(3);
}
void ExampleAIModule::onEnd(bool isWinner)
{
// Called when the game ends
if (isWinner)
{
// Log victory message
Broodwar << "I won the game!" << std::endl;
}
}
void ExampleAIModule::onFrame()
{
// Called once every game frame
// Use Broodwar->self() and Broodwar->enemy() to get information about the
// current players.
// Use Broodwar->getUnits() to get a set of all the units currently in the game.
for (auto& unit : Broodwar->getUnits())
{
// Ignore the unit if it no longer exists
// Make sure to include this block when handling any Unit pointer!
if (!unit)
continue;
// Ignore the unit if it belongs to the other player
if (unit->getPlayer() != Broodwar->self())
continue;
// Ignore the unit if it is a worker
if (unit->getType().isWorker())
continue;
// If the unit is a worker, make it attack the enemy base
if (unit->attack(Broodwar->enemy()->getStartLocation()))
{
// If the unit successfully attacked, do nothing
}
else
{
// If the unit was unable to attack, try to move it to a position
// that is closer to the enemy base
unit->move(Position(Broodwar->enemy()->getStartLocation()));
}
}
// Use Broodwar->getMinerals() and Broodwar->getGeysers() to get
// a set of all the mineral and gas patches currently in the game.
for (auto& unit : Broodwar->getMinerals())
{
// Ignore the unit if it no longer exists
// Make sure to include this block when handling any Unit pointer!
if (!unit)
continue;
// Use unit->isBeingGathered() to determine if the unit is currently being
// gathered by a worker. If it is, do nothing.
if (unit->isBeingGathered())
continue;
// Use unit->getClosestUnit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment