[Add] Added math lib to basic llua.

This commit is contained in:
Allanis 2014-01-25 19:29:42 +00:00
parent 63e2bb55b0
commit 37d61803d7
4 changed files with 85 additions and 3 deletions

View File

@ -8,6 +8,7 @@ armour_return = 100
function create()
ai.setcredits(ai.shipprice()/1000, ai.shipprice()/100)
mem.bribe = math.sqrt(ai.shipmass()) * (300. * rnd.int() + 850.)
attack_choose()
end

View File

@ -129,7 +129,8 @@ static int ai_getdistance(lua_State* L); /* Number getdist(Vec2) */
static int ai_getpos(lua_State* L); /* getpos(number) */
static int ai_minbrakedist(lua_State* L); /* Number minbrakedist() */
static int ai_cargofree(lua_State* L); /* Number cargofree(). */
static int ai_shipclass(lua_State* L); /* string shipclass(). */
static int ai_shipclass(lua_State* L); /* string shipclass(number). */
static int ai_shipmass(lua_State* L); /* string shipmass(number) */
static int ai_isbribed(lua_State* L); /* bool isbribed(number). */
/* Boolean expressions. */
static int ai_exists(lua_State* L); /* boolean exists */
@ -204,6 +205,7 @@ static const luaL_Reg ai_methods[] = {
{ "minbrakedist", ai_minbrakedist },
{ "cargofree", ai_cargofree },
{ "shipclass", ai_shipclass },
{ "shipmass", ai_shipmass },
{ "isbribed", ai_isbribed },
/* Movement. */
{ "nearestplanet", ai_getnearestplanet },
@ -866,7 +868,41 @@ static int ai_cargofree(lua_State* L) {
/* Get the pilots ship class. */
static int ai_shipclass(lua_State* L) {
lua_pushstring(L, ship_class(cur_pilot->ship));
Pilot* p;
if(lua_gettop(L) > 0) {
if(lua_isnumber(L, 1))
p = pilot_get((unsigned int)lua_tonumber(L, 1));
else LLUA_INVALID_PARAMETER();
} else
p = cur_pilot;
if(p == NULL) {
LLUA_DEBUG("Trying to get class of unexpectant ship!");
return 0;
}
lua_pushstring(L, ship_class(p->ship));
return 1;
}
/* Get the ships mass. */
static int ai_shipmass(lua_State* L) {
Pilot* p;
if(lua_gettop(L) > 0) {
if(lua_isnumber(L, 1))
p = pilot_get((unsigned int)lua_tonumber(L,1));
else LLUA_INVALID_PARAMETER();
} else
p = cur_pilot;
if(p == NULL) {
LLUA_DEBUG("Trying to get class of unexistant ship!");
return 0;
}
lua_pushnumber(L, p->solid->mass);
return 1;
}

View File

@ -10,6 +10,7 @@
#include "dialogue.h"
#include "pilot.h"
#include "rng.h"
#include "llua.h"
#include "comm.h"
#define BUTTON_WIDTH 80 /**< Button width. */
@ -17,7 +18,11 @@
static Pilot* comm_pilot = NULL; /**< Pilot currently talking to. */
/* Static. */
static void comm_bribe(unsigned int wid, char* str);
static unsigned int comm_getBribeAmount(void);
/* Extern. */
void ai_setPilot(Pilot* p);
/**
* @brief Open the communication dialogue with a pilot.
@ -101,7 +106,13 @@ static void comm_bribe(unsigned int wid, char* str) {
int answer;
int price;
price = (int) sqrt(comm_pilot->solid->mass) * (300.*RNGF() + 850.);
price = comm_getBribeAmount();
/* Unbribeable. */
if(price == 0) {
dialogue_msg("Bribe Pilot", "\"Money won't save your hide now!\"");
return;
}
answer = dialogue_YesNo("Bribe Pilot", "\"I'm gonna need at least %d credits \
to not leave you as a hunk of floating debris.\"\n\npay %d Credits?", price, price);
@ -118,3 +129,35 @@ static void comm_bribe(unsigned int wid, char* str) {
}
}
/**
* @brief Get the amount the communicating pilot wants as a bribe.
*
* Note: It's a hack around the AI stuff, probably not too good of an idea.
* @return Amount pilot wants.
*/
static unsigned int comm_getBribeAmount(void) {
lua_State* L;
double bribe;
/* Set up the state. */
ai_setPilot(comm_pilot);
L = comm_pilot->ai->L;
/* Get the bribe amount. */
lua_getglobal(L, "mem");
lua_getfield(L, -1, "bribe");
/* If not number consider unbribeable. */
if(!lua_isnumber(L, -1)) {
lua_pop(L, 2);
return 0;
}
/* Get amount. */
bribe = (unsigned int) lua_tonumber(L, -1);
/* Clean up. */
lua_pop(L, 2);
return bribe;
}

View File

@ -120,6 +120,8 @@ int llua_loadBasic(lua_State* L) {
lua_setglobal(L, override[i]);
}
llua_load(L, luaopen_math); /* Open math. */
/* Add our own. */
lua_register(L, "include", llua_packfileLoader);