From 6dad718ef2ea01a85532637bb151b78f0edd1950 Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 8 Feb 2013 14:34:09 +0000 Subject: [PATCH] [Add] Random number generator to lua/ai. --- scripts/ai/API | 11 +++++++++++ src/ai.c | 21 +++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/scripts/ai/API b/scripts/ai/API index b4db7a1..f802c5a 100644 --- a/scripts/ai/API +++ b/scripts/ai/API @@ -88,3 +88,14 @@ createvect(number x, number y) -- y coord of the vector. -- return pointer to the Vec2 +say(string message) + -- Make the pilot say something to the player. + -- message - string to output. + -- return nil + +rng(number low, number high) + -- Return a random number between low and high. + -- low - minimum to return. + -- high - maximum to return + -- return random number between low and high. + diff --git a/src/ai.c b/src/ai.c index 60bce9c..bfa0679 100644 --- a/src/ai.c +++ b/src/ai.c @@ -11,6 +11,7 @@ #include "player.h" #include "physics.h" #include "pack.h" +#include "rng.h" #include "ai.h" // == AI ====================================================== @@ -77,11 +78,12 @@ 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_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. // Misc. static int ai_createvect(lua_State* L); // createvect(number, number) static int ai_say(lua_State* L); // say(string) -// Combat. -static int ai_shoot(lua_State* L); // shoot(number) number = 1,2,3. +static int ai_rng(lua_State* L); // rng(number, number) // Global Lua interpreter. static lua_State* L = NULL; @@ -133,6 +135,7 @@ int ai_init(void) { // Misc. lua_register(L, "createvect", ai_createvect); lua_register(L, "say", ai_say); + lua_register(L, "rng", ai_rng); char* buf = pack_readfile(DATA, "../scripts/ai/test.lua", NULL); @@ -366,6 +369,7 @@ static int ai_createvect(lua_State* L) { return 1; } +// Have the pilot say something to player. static int ai_say(lua_State* L) { MIN_ARGS(1); @@ -375,6 +379,19 @@ static int ai_say(lua_State* L) { return 0; } +// Return a number between low and high. +static int ai_rng(lua_State* L) { + MIN_ARGS(2); + + int l, h; + + if(lua_isnumber(L,1)) l = (int)lua_tonumber(L, 1); + if(lua_isnumber(L,1)) h = (int)lua_tonumber(L, 2); + + lua_pushnumber(L, RNG(l,h)); + return 1; +} + // Pew pew.. Says the pilot. static int ai_shoot(lua_State* L) { int n = 1;