From 91f29c9511638ba2517fe6587b71d3a2652431fe Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 8 Feb 2013 16:38:44 +0000 Subject: [PATCH] [Add] AI API functions, faction checks --- scripts/ai/API | 23 +++++++++++++++++ src/ai.c | 68 +++++++++++++++++++++++++++++++++++++------------- src/ai.h | 6 ++++- 3 files changed, 78 insertions(+), 19 deletions(-) diff --git a/scripts/ai/API b/scripts/ai/API index f802c5a..1f0f653 100644 --- a/scripts/ai/API +++ b/scripts/ai/API @@ -58,6 +58,16 @@ isstopped() -- Check if we are stopped. -- 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! // ================ @@ -78,6 +88,19 @@ face(number/Vec2 target, number invert) -- invert face away if 1 -- 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! // ================ diff --git a/src/ai.c b/src/ai.c index bfa0679..1472385 100644 --- a/src/ai.c +++ b/src/ai.c @@ -73,6 +73,8 @@ static int ai_minbrakedist(lua_State* L); // Number minbrakedist() // Boolean expressions. static int ai_ismaxvel(lua_State* L); // Boolean ismaxvel() 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. static int ai_accel(lua_State* L); // accel(number); nuimber <= 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() // Combat. static int ai_shoot(lua_State* L); // shoot(number) number = 1,2,3. +static int ai_getenemy(lua_State* L); // pointer getenemy(). // Misc. static int ai_createvect(lua_State* L); // createvect(number, number) static int ai_say(lua_State* L); // say(string) @@ -125,6 +128,8 @@ int ai_init(void) { // Boolean. lua_register(L, "ismaxvel", ai_ismaxvel); lua_register(L, "isstopped", ai_isstopped); + lua_register(L, "isenemy", ai_isenemy); + lua_register(L, "isally", ai_isally); // Movement. lua_register(L, "accel", ai_accel); lua_register(L, "turn", ai_turn); @@ -132,6 +137,7 @@ int ai_init(void) { lua_register(L, "brake", ai_brake); // Combat. lua_register(L, "shoot", ai_shoot); + lua_register(L, "getenemy", ai_getenemy); // Misc. lua_register(L, "createvect", ai_createvect); lua_register(L, "say", ai_say); @@ -208,10 +214,15 @@ static int ai_pushtask(lua_State* L) { t->target = NULL; 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); - else if(lua_islightuserdata(L, 3)) + } + else if(lua_islightuserdata(L, 3)) { + t-dtype = TYPE_PTR; t->target = (void*)lua_topointer(L, 3); + } else + t->dtype = TYPE_NULL; } if(cur_pilot->task == NULL) // No other tasks. @@ -248,14 +259,20 @@ static int ai_taskname(lua_State* L) { // Grab the target pointer. static int ai_gettarget(lua_State* L) { - lua_pushlightuserdata(L, cur_pilot->task->target); - return 1; + if(cur_pilot->task->dtype == TYPE_PTR) { + lua_pushlightuserdata(L, cur_pilot->task->target); + return 1; + } + return 0; } // Get the ID. static int ai_gettargetid(lua_State* L) { - lua_pushnumber(L, cur_pilot->task->ID); - return 1; + if(cur_pilot->task->dtype == TYPE_INT) { + lua_pushnumber(L, cur_pilot->task->ID); + return 1; + } + return 0; } // Get the distance from the pointer. @@ -310,6 +327,16 @@ static int ai_isstopped(lua_State* L) { 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. static int ai_accel(lua_State* L) { 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; } +// 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. static int ai_createvect(lua_State* L) { MIN_ARGS(2); @@ -392,15 +436,3 @@ static int ai_rng(lua_State* L) { 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; -} - diff --git a/src/ai.h b/src/ai.h index ccffc6b..c350946 100644 --- a/src/ai.h +++ b/src/ai.h @@ -1,9 +1,13 @@ #pragma once +typedef enum { TYPE_NULL, TYPE_INT, TYPE_PTR } TaskData; + +// Basic task. typedef struct Task { struct Task* next; char* name; - + + TaskData dtype; union { void* target; // Vec2 etc. unsigned int ID; // Pilot ID etc.