[Add] AI API functions, faction checks
This commit is contained in:
parent
fd3f3a78b6
commit
91f29c9511
@ -58,6 +58,16 @@ isstopped()
|
|||||||
-- Check if we are stopped.
|
-- Check if we are stopped.
|
||||||
-- return true if stopped, false otherwise.
|
-- return true if stopped, false otherwise.
|
||||||
|
|
||||||
|
isenemy(Pilot pilot)
|
||||||
|
-- Check if p is an enemy of current pilot.
|
||||||
|
-- p - Pilot to check if enemy.
|
||||||
|
-- return true if p is enemy.
|
||||||
|
|
||||||
|
isally(Pilot p)
|
||||||
|
-- Check if p is an ally of current pilot.
|
||||||
|
-- p - Pilot to check if ally.
|
||||||
|
-- return true if p is ally.
|
||||||
|
|
||||||
// ================
|
// ================
|
||||||
// MOVEMENT!
|
// MOVEMENT!
|
||||||
// ================
|
// ================
|
||||||
@ -78,6 +88,19 @@ face(number/Vec2 target, number invert)
|
|||||||
-- invert face away if 1
|
-- invert face away if 1
|
||||||
-- return number offset from target in grad
|
-- return number offset from target in grad
|
||||||
|
|
||||||
|
// ================
|
||||||
|
// COMBAT!
|
||||||
|
// ================
|
||||||
|
|
||||||
|
shoot([number weapon])
|
||||||
|
-- Make the pilot shoot weapons.
|
||||||
|
-- weapon to shoot, 1 if primary, 2 if secondary, 3 if both. Defaults to 1.
|
||||||
|
-- return nil.
|
||||||
|
|
||||||
|
getenemy()
|
||||||
|
-- return the id of the nearest enemy.
|
||||||
|
|
||||||
|
|
||||||
// ================
|
// ================
|
||||||
// MISC!
|
// MISC!
|
||||||
// ================
|
// ================
|
||||||
|
60
src/ai.c
60
src/ai.c
@ -73,6 +73,8 @@ static int ai_minbrakedist(lua_State* L); // Number minbrakedist()
|
|||||||
// Boolean expressions.
|
// Boolean expressions.
|
||||||
static int ai_ismaxvel(lua_State* L); // Boolean ismaxvel()
|
static int ai_ismaxvel(lua_State* L); // Boolean ismaxvel()
|
||||||
static int ai_isstopped(lua_State* L); // Boolean isstopped()
|
static int ai_isstopped(lua_State* L); // Boolean isstopped()
|
||||||
|
static int ai_isenemy(lua_State* L); // bool isenemy(pointer).
|
||||||
|
static int ai_isally(lua_State* L); // bool isally(pointer).
|
||||||
// Movement.
|
// Movement.
|
||||||
static int ai_accel(lua_State* L); // accel(number); nuimber <= 1.
|
static int ai_accel(lua_State* L); // accel(number); nuimber <= 1.
|
||||||
static int ai_turn(lua_State* L); // turn(number); abs(number) <= 1.
|
static int ai_turn(lua_State* L); // turn(number); abs(number) <= 1.
|
||||||
@ -80,6 +82,7 @@ static int ai_face(lua_State* L); // face(number/pointer)
|
|||||||
static int ai_brake(lua_State* L); // Brake()
|
static int ai_brake(lua_State* L); // Brake()
|
||||||
// Combat.
|
// Combat.
|
||||||
static int ai_shoot(lua_State* L); // shoot(number) number = 1,2,3.
|
static int ai_shoot(lua_State* L); // shoot(number) number = 1,2,3.
|
||||||
|
static int ai_getenemy(lua_State* L); // pointer getenemy().
|
||||||
// Misc.
|
// Misc.
|
||||||
static int ai_createvect(lua_State* L); // createvect(number, number)
|
static int ai_createvect(lua_State* L); // createvect(number, number)
|
||||||
static int ai_say(lua_State* L); // say(string)
|
static int ai_say(lua_State* L); // say(string)
|
||||||
@ -125,6 +128,8 @@ int ai_init(void) {
|
|||||||
// Boolean.
|
// Boolean.
|
||||||
lua_register(L, "ismaxvel", ai_ismaxvel);
|
lua_register(L, "ismaxvel", ai_ismaxvel);
|
||||||
lua_register(L, "isstopped", ai_isstopped);
|
lua_register(L, "isstopped", ai_isstopped);
|
||||||
|
lua_register(L, "isenemy", ai_isenemy);
|
||||||
|
lua_register(L, "isally", ai_isally);
|
||||||
// Movement.
|
// Movement.
|
||||||
lua_register(L, "accel", ai_accel);
|
lua_register(L, "accel", ai_accel);
|
||||||
lua_register(L, "turn", ai_turn);
|
lua_register(L, "turn", ai_turn);
|
||||||
@ -132,6 +137,7 @@ int ai_init(void) {
|
|||||||
lua_register(L, "brake", ai_brake);
|
lua_register(L, "brake", ai_brake);
|
||||||
// Combat.
|
// Combat.
|
||||||
lua_register(L, "shoot", ai_shoot);
|
lua_register(L, "shoot", ai_shoot);
|
||||||
|
lua_register(L, "getenemy", ai_getenemy);
|
||||||
// Misc.
|
// Misc.
|
||||||
lua_register(L, "createvect", ai_createvect);
|
lua_register(L, "createvect", ai_createvect);
|
||||||
lua_register(L, "say", ai_say);
|
lua_register(L, "say", ai_say);
|
||||||
@ -208,10 +214,15 @@ static int ai_pushtask(lua_State* L) {
|
|||||||
t->target = NULL;
|
t->target = NULL;
|
||||||
|
|
||||||
if(lua_gettop(L) > 2) {
|
if(lua_gettop(L) > 2) {
|
||||||
if(lua_isnumber(L, 3))
|
if(lua_isnumber(L, 3)) {
|
||||||
|
t->dtype = TYPE_INT;
|
||||||
t->ID = (unsigned int) lua_tonumber(L, 3);
|
t->ID = (unsigned int) lua_tonumber(L, 3);
|
||||||
else if(lua_islightuserdata(L, 3))
|
}
|
||||||
|
else if(lua_islightuserdata(L, 3)) {
|
||||||
|
t-dtype = TYPE_PTR;
|
||||||
t->target = (void*)lua_topointer(L, 3);
|
t->target = (void*)lua_topointer(L, 3);
|
||||||
|
} else
|
||||||
|
t->dtype = TYPE_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(cur_pilot->task == NULL) // No other tasks.
|
if(cur_pilot->task == NULL) // No other tasks.
|
||||||
@ -248,14 +259,20 @@ static int ai_taskname(lua_State* L) {
|
|||||||
|
|
||||||
// Grab the target pointer.
|
// Grab the target pointer.
|
||||||
static int ai_gettarget(lua_State* L) {
|
static int ai_gettarget(lua_State* L) {
|
||||||
|
if(cur_pilot->task->dtype == TYPE_PTR) {
|
||||||
lua_pushlightuserdata(L, cur_pilot->task->target);
|
lua_pushlightuserdata(L, cur_pilot->task->target);
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the ID.
|
// Get the ID.
|
||||||
static int ai_gettargetid(lua_State* L) {
|
static int ai_gettargetid(lua_State* L) {
|
||||||
|
if(cur_pilot->task->dtype == TYPE_INT) {
|
||||||
lua_pushnumber(L, cur_pilot->task->ID);
|
lua_pushnumber(L, cur_pilot->task->ID);
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the distance from the pointer.
|
// Get the distance from the pointer.
|
||||||
@ -310,6 +327,16 @@ static int ai_isstopped(lua_State* L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the pilot is an enemy.
|
||||||
|
static int ai_isenemy(lua_State* L) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the pilot is an ally.
|
||||||
|
static int ai_isally(lua_State* L) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Accelerate the pilot based on a param.
|
// Accelerate the pilot based on a param.
|
||||||
static int ai_accel(lua_State* L) {
|
static int ai_accel(lua_State* L) {
|
||||||
pilot_acc = (lua_gettop(L) > 1 && lua_isnumber(L, 1)) ? ABS((double)lua_tonumber(L, 1)) : 1.;
|
pilot_acc = (lua_gettop(L) > 1 && lua_isnumber(L, 1)) ? ABS((double)lua_tonumber(L, 1)) : 1.;
|
||||||
@ -356,6 +383,23 @@ static int ai_brake(lua_State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pew pew.. Says the pilot.
|
||||||
|
static int ai_shoot(lua_State* L) {
|
||||||
|
int n = 1;
|
||||||
|
if(lua_isnumber(L, 1)) n = (int)lua_tonumber(L,1);
|
||||||
|
|
||||||
|
if(n == 1) pilot_primary = 1;
|
||||||
|
//else if(n == 2) pilot_secondary = 1;
|
||||||
|
//else if(n == 3) pilot_primary = pilot_secondary = 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the nearest enemy.
|
||||||
|
static int ai_getenemy(lua_State* L) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Create a vector.
|
// Create a vector.
|
||||||
static int ai_createvect(lua_State* L) {
|
static int ai_createvect(lua_State* L) {
|
||||||
MIN_ARGS(2);
|
MIN_ARGS(2);
|
||||||
@ -392,15 +436,3 @@ static int ai_rng(lua_State* L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pew pew.. Says the pilot.
|
|
||||||
static int ai_shoot(lua_State* L) {
|
|
||||||
int n = 1;
|
|
||||||
if(lua_isnumber(L, 1)) n = (int)lua_tonumber(L,1);
|
|
||||||
|
|
||||||
if(n == 1) pilot_primary = 1;
|
|
||||||
//else if(n == 2) pilot_secondary = 1;
|
|
||||||
//else if(n == 3) pilot_primary = pilot_secondary = 1;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
4
src/ai.h
4
src/ai.h
@ -1,9 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
typedef enum { TYPE_NULL, TYPE_INT, TYPE_PTR } TaskData;
|
||||||
|
|
||||||
|
// Basic task.
|
||||||
typedef struct Task {
|
typedef struct Task {
|
||||||
struct Task* next;
|
struct Task* next;
|
||||||
char* name;
|
char* name;
|
||||||
|
|
||||||
|
TaskData dtype;
|
||||||
union {
|
union {
|
||||||
void* target; // Vec2 etc.
|
void* target; // Vec2 etc.
|
||||||
unsigned int ID; // Pilot ID etc.
|
unsigned int ID; // Pilot ID etc.
|
||||||
|
Loading…
Reference in New Issue
Block a user