[Change] Cleaned up the ai lua wrappers a bit, needs plenty more work.

This commit is contained in:
Allanis 2013-06-10 00:42:08 +01:00
parent 32d57143d4
commit 3c4383b325
6 changed files with 35 additions and 66 deletions

View File

@ -34,16 +34,16 @@ function attacked(attacker)
end
function create()
if ai.rnd(0,2)==0 then -- More money, but less often.
ai.setcredits(ai.rnd(1000, ai.shipprice()/70))
if rnd.int(0,2)==0 then -- More money, but less often.
ai.setcredits(rnd.int(1000, ai.shipprice()/70))
end
if ai.rnd(0,2)==0 then
if rnd.int(0,2)==0 then
ai.broadcast("The Empire is watching")
end
end
function taunt(target)
num = ai.rnd(0,4)
num = rnd.int(0,4)
if num == 0 then msg = "How dare you attack me!?"
elseif num == 1 then msg = "You can not defeat the Empire!"
elseif num == 2 then msg = "You will hang for this!"
@ -94,7 +94,7 @@ function stop()
if ai.isstopped() then
ai.stop()
ai.poptask()
ai.settimer(0, ai.rnd(8000, 15000))
ai.settimer(0, rnd.int(8000, 15000))
ai.pushtask(0, "land")
else
ai.brake()

View File

@ -25,7 +25,7 @@ end
function attacked(attacker)
if ai.taskname() ~= "runaway" then
-- Let's have some messages.
num = ai.rnd(0,3)
num = rnd.int(0,3)
if num == 0 then msg = "Mayday! We are under attack!"
elseif num == 1 then msg = "Requesting assistance! Some scoundral is attacking us!"
elseif num == 2 then msg = "Merchant vessle under attack here! HALP!"
@ -41,9 +41,9 @@ function attacked(attacker)
end
function create()
ai.setcredits(ai.rnd(200, ai.shipprice()/100))
ai.setcredits(rnd.int(200, ai.shipprice()/100))
num = ai.rnd(12)
num = rnd.int(12)
if num < 5 then
cargo = "Food"
elseif num < 8 then
@ -55,7 +55,7 @@ function create()
else
cargo = "Medicine"
end
ai.setcargo(cargo, ai.rnd(0, ai.cargofree()))
ai.setcargo(cargo, rnd.int(0, ai.cargofree()))
end
-- Runs away.
@ -99,7 +99,7 @@ function stop()
if ai.isstopped() then
ai.stop()
ai.poptask()
ai.settimer(0, ai.rnd(8000, 15000))
ai.settimer(0, rnd.int(8000, 15000))
ai.pushtask(0, "land")
else
ai.brake()

View File

@ -32,8 +32,8 @@ function attacked(attacker)
end
function create()
ai.setcredits(ai.rnd(1000, ai.shipprice()/200))
if ai.rnd(0, 2)==0 then
ai.setcredits(rnd.int(1000, ai.shipprice()/200))
if rnd.int(0, 2)==0 then
ai.broadcast("This area is under militia survellance.")
end
end

View File

@ -20,7 +20,7 @@ function control()
enemy = ai.getenemy()
if ai.parmour() == 100 and enemy ~= 0 then
-- Taunts.
num = ai.rnd(0,4)
num = rnd.int(0,4)
if num == 0 then msg = "Prepare to be boarded!"
elseif num == 1 then msg = "Whoa! Lookie what we found here!"
elseif num == 2 then msg = "What's a ship like you doing in a place like this?"
@ -57,13 +57,13 @@ function attacked(attacker)
end
function create()
if ai.rnd(0,5) ~= 0 then
if rnd.int(0,5) ~= 0 then
ai.setcredits(0, ai.shipprice()/300)
end
end
function taunt(target)
num = ai.rnd(0,4)
num = rnd.int(0,4)
if num == 0 then msg = "How dare you attack me?!"
elseif num == 1 then msg = "Aha! You think you can best ME?!"
elseif num == 2 then msg = "JUST! DIE!"

View File

@ -16,6 +16,8 @@
#include "rng.h"
#include "space.h"
#include "faction.h"
#include "llua.h"
#include "lluadef.h"
#include "ai.h"
// == AI ======================================================
@ -49,20 +51,8 @@
// (task).
// ============================================================
// FUCK LUA!!!
//#define luaL_register(L,n,l) (luaL_openlib(L, (n),(l), 0))
// Creates a new lua table.
#define newtable(L) (lua_newtable(L), lua_gettop(L))
// Register a number constant n to name s (syntax is just like lua_regfunc).
#define lua_regnumber(l,s,n) (lua_pushnumber(l,n), lua_setglobal(l,s))
// Registers a C function.
#define lua_regfunc(l,s,f) (lua_pushcfunction(l,f), lua_setglobal(L,s))
// L state, void* buf, int n size, char* s identifier.
#define luaL_dobuffer(L,b,n,s) \
(luaL_loadbuffer(L,b,n,s) || lua_pcall(L, 0, LUA_MULTRET, 0))
// Don't run the function if (n) params aren't passed.
#define MIN_ARGS(n) if(lua_gettop(L) < n) return 0
// Ai flags.
#define ai_setFlag(f) (pilot_flags |= f)
@ -147,8 +137,6 @@ static int ai_broadcast(lua_State* L); // broadcast(string)
static int ai_credits(lua_State* L); // credits(number).
static int ai_cargo(lua_State* L); // cargo(name, quantity).
static int ai_shipprice(lua_State* L); // shipprice().
// Random.
static int ai_rng(lua_State* L); // rng(number, number)
static const luaL_Reg ai_methods[] = {
// Tasks.
@ -201,8 +189,6 @@ static const luaL_Reg ai_methods[] = {
{ "setcredits", ai_credits },
{ "setcargo", ai_cargo },
{ "shipprice", ai_shipprice },
// Random.
{ "rnd", ai_rng },
{ 0, 0 } // End.
};
@ -291,6 +277,7 @@ static int ai_loadProfile(char* filename) {
// Register C funstions in Lua.
luaL_register(L, "ai", ai_methods);
lua_loadRnd(L);
// Now load the file, since all the functions have been previously loaded.
buf = pack_readfile(DATA, filename, &bufsize);
@ -542,7 +529,7 @@ static int ai_pshield(lua_State* L) {
static int ai_getdistance(lua_State* L) {
Vec2* vect;
MIN_ARGS(1);
LLUA_MIN_ARGS(1);
vect = (lua_islightuserdata(L,1)) ?
(Vec2*)lua_topointer(L,1) : NULL;
lua_pushnumber(L, vect_dist(vect, &cur_pilot->solid->pos));
@ -592,7 +579,7 @@ static int ai_cargofree(lua_State* L) {
}
static int ai_exists(lua_State* L) {
MIN_ARGS(1);
LLUA_MIN_ARGS(1);
if(lua_isnumber(L,1)) {
lua_pushboolean(L, (pilot_get((unsigned int)lua_tonumber(L,1)) != NULL)?1:0);
@ -659,14 +646,14 @@ static int ai_accel(lua_State* L) {
// Turn the pilot based on a param.
static int ai_turn(lua_State* L) {
MIN_ARGS(1);
LLUA_MIN_ARGS(1);
pilot_turn = (lua_isnumber(L, 1)) ? (double)lua_tonumber(L, 1) : 0.;
return 0;
}
// Face the target.
static int ai_face(lua_State* L) {
MIN_ARGS(1);
LLUA_MIN_ARGS(1);
Vec2* v, sv, tv; // Grab the position to face.
Pilot* p;
double mod, diff;
@ -816,7 +803,7 @@ static int ai_combat(lua_State* L) {
// Set the pilots target.
static int ai_settarget(lua_State* L) {
MIN_ARGS(1);
LLUA_MIN_ARGS(1);
if(lua_isnumber(L,1)) pilot_target = (int)lua_tonumber(L,1);
return 0;
@ -874,7 +861,7 @@ static int ai_getenemy(lua_State* L) {
// Set the enemy hostile. (Simply notifies of an impending attack).
static int ai_hostile(lua_State* L) {
MIN_ARGS(1);
LLUA_MIN_ARGS(1);
if(lua_isnumber(L,1) && ((unsigned int)lua_tonumber(L,1) == PLAYER_ID))
pilot_setFlag(cur_pilot, PILOT_HOSTILE);
@ -884,7 +871,7 @@ static int ai_hostile(lua_State* L) {
// Set the timer.
static int ai_settimer(lua_State* L) {
MIN_ARGS(2);
LLUA_MIN_ARGS(2);
int n; // Get the timer.
if(lua_isnumber(L, 1)) n = lua_tonumber(L,1);
@ -896,7 +883,7 @@ static int ai_settimer(lua_State* L) {
// Check the timer.
static int ai_timeup(lua_State* L) {
MIN_ARGS(1);
LLUA_MIN_ARGS(1);
int n; // Get the timer.
if(lua_isnumber(L,1)) n = lua_tonumber(L,1);
@ -907,7 +894,7 @@ static int ai_timeup(lua_State* L) {
// Have the pilot say something to player.
static int ai_comm(lua_State* L) {
MIN_ARGS(2);
LLUA_MIN_ARGS(2);
if(lua_isnumber(L,1) && (lua_tonumber(L,1)==PLAYER_ID) && lua_isstring(L,2))
player_message("Comm: %s> \"%s\"", cur_pilot->name, lua_tostring(L,2));
@ -917,7 +904,7 @@ static int ai_comm(lua_State* L) {
// Broadcasts to the entire area.
static int ai_broadcast(lua_State* L) {
MIN_ARGS(1);
LLUA_MIN_ARGS(1);
if(lua_isstring(L, 1))
player_message("Broadcast: %s> \"%s\"", cur_pilot->name, lua_tostring(L,1));
@ -927,7 +914,7 @@ static int ai_broadcast(lua_State* L) {
// Set the pilots credits.
static int ai_credits(lua_State* L) {
MIN_ARGS(1);
LLUA_MIN_ARGS(1);
if(ai_status != AI_STATUS_CREATE) return 0;
if(lua_isnumber(L,1))
@ -938,7 +925,7 @@ static int ai_credits(lua_State* L) {
// Set the pilots cargo.
static int ai_cargo(lua_State* L) {
MIN_ARGS(2);
LLUA_MIN_ARGS(2);
if(ai_status != AI_STATUS_CREATE) return 0;
if(lua_isstring(L,1) && lua_isnumber(L,2))
@ -954,26 +941,3 @@ static int ai_shipprice(lua_State* L) {
return 1;
}
// Return a number between low and high.
static int ai_rng(lua_State* L) {
int o;
o = lua_gettop(L);
if(o == 0) lua_pushnumber(L, RNGF()); // Random double 0 <= x <= 1.
else if(o == 1) { // Random int o <= x <= param.
if(lua_isnumber(L, -1))
lua_pushnumber(L, RNG(0, (int)lua_tonumber(L, -1)));
else return 0;
}
else if(o >= 2) { // Random int param 1 <= x <= param 2.
if(lua_isnumber(L, -1) && lua_isnumber(L, -2))
lua_pushnumber(L,
RNG((int)lua_tonumber(L, -2), (int)lua_tonumber(L, -1)));
else return 0;
}
else return 0;
return 1; // Unless it's returned 0 already it'll always return a param.
}

View File

@ -1,5 +1,6 @@
#pragma once
// Debug stuff.
#define LLUA_DEBUG(str, args...) \
(fprintf(stdout, "Lua: "str"\n", ## args))
@ -14,3 +15,7 @@
return 0; \
}
// Comfortability macros.
#define luaL_dobuffer(L, b, n, s) \
(luaL_loadbuffer(L, b, n, s) || lua_pcall(L, 0, LUA_MULTRET, 0))