[Add] Random number generator to lua/ai.

This commit is contained in:
Allanis 2013-02-08 14:34:09 +00:00
parent 943cbecf70
commit 6dad718ef2
2 changed files with 30 additions and 2 deletions

View File

@ -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.

View File

@ -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;