Skip to content

Instantly share code, notes, and snippets.

@iaman
Last active December 20, 2015 09:39
Show Gist options
  • Save iaman/6109255 to your computer and use it in GitHub Desktop.
Save iaman/6109255 to your computer and use it in GitHub Desktop.
Hacking out some basic POI-based AI pathfinding in GML (Game Maker Language). I've literally never studied or read up on AI before, this is just a stab in the dark that seems to work.
var i = 0;
var i2 = 0;
var thingInterest = 0;
var thingsInserted = 0;
var collision;
var thing;
var things = ds_priority_create();
var rayCount = 360;
var distance = 0;
self.frame++;
for (i2 = 4; i2 >= 0; i2--) {
thingsArray[i2] = 0;
}
if (self.focusedThing != noone) {
distance = distance_to_point(self.focusedThing.x, self.focusedThing.y);
var focusedThingInterest = self.focusedThing.novelty - self.focusedThing.focusedFor * self.focusedThing.rateOfDecay - distance;
if (self.focusedThing.investigated && self.focusedThing.furtherInvestigation == 0) {
focusedThingInterest = focusedThingInterest - 1000;
}
ds_priority_add(things, self.focusedThing, focusedThingInterest);
instance_deactivate_object(self.focusedThing);
}
for (i = 0; i < rayCount; i++) {
x2 = self.x + cos(i * 360 / rayCount) * self.viewDistance;
y2 = self.y + sin(i * 360 / rayCount) * self.viewDistance;
i2 = 0;
collision = collision_line(self.x, self.y, x2, y2, POIObj, false, false);
while (collision && i2 < 5) {
collision.inSight = true;
distance = distance_to_point(collision.x, collision.y);
thingInterest = collision.novelty - distance;
if (collision.investigated) { thingInterest = thingInterest - 1000; }
ds_priority_add(things, collision, thingInterest);
thingsArray[i2] = collision;
instance_deactivate_object(collision);
thingsInserted++;
if (thingsInserted >= self.maxThings) { break; }
else {
collision = collision_line(self.x, self.y, x2, y2, POIObj, false, false);
}
i2++;
}
for (i2 = 0; i2 < 5; i2++) {
if (thingsArray[i2] != 0) {
instance_activate_object(thingsArray[i2]);
thingsArray[i2] = 0;
}
}
if (thingsInserted >= self.maxThings) { break; }
}
instance_activate_object(self.focusedThing);
thing = ds_priority_find_max(things);
self.focusedThing = thing;
global.focusedThing = thing.id;
thing.inFocus = true;
if (thing == self.focusedThing) {
thing.focusedFor++;
}
else { thing.focusedFor = 1; }
if (distance_to_point(thing.x, thing.y) > 8) {
self.direction = point_direction(self.x, self.y, thing.x, thing.y);
self.speed = 8;
}
else {
self.speed = 0;
thing.investigated = true;
}
ds_priority_destroy(things);
if (self.inFocus) {
// OMG SENPAI'S LOOKING RIGHT AT ME
draw_sprite(focusedSprite, 0, round(self.x), round(self.y));
}
else if (self.investigated) {
// SENPAI COME BACK PLZ
draw_sprite(investigatedSprite, 0, round(self.x), round(self.y));
}
else if (self.inSight) {
// Senpai noticed me!!!!111!11!1!!!1!1 !
draw_sprite(seenSprite, 0, round(self.x), round(self.y));
}
else {
// I hope senpai will notice me
draw_sprite(unfocusedSprite, 0, round(self.x), round(self.y));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment