server/src/eressea/lua/eressea.cpp
2007-01-13 21:34:32 +00:00

275 lines
5.7 KiB
C++

#include <config.h>
#include <eressea.h>
#include "bindings.h"
#include <attributes/key.h>
#include <modules/autoseed.h>
#include <modules/score.h>
// kernel includes
#include <kernel/alliance.h>
#include <kernel/calendar.h>
#include <kernel/equipment.h>
#include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/plane.h>
#include <kernel/race.h>
#include <kernel/reports.h>
#include <kernel/save.h>
#include <kernel/skill.h>
#include <kernel/teleport.h>
#include <kernel/unit.h>
// lua includes
#include <lua.hpp>
#include <luabind/luabind.hpp>
#include <luabind/iterator_policy.hpp>
// util includes
#include <util/language.h>
#include <util/base36.h>
#include <util/rand.h>
#include <util/rng.h>
#include <cstring>
#include <ctime>
using namespace luabind;
static int
lua_addequipment(const char * eqname, const char * iname, const char * value)
{
if (iname==NULL) return -1;
const struct item_type * itype = it_find(iname);
if (itype==NULL) return -1;
equipment_setitem(create_equipment(eqname), itype, value);
return 0;
}
static int
get_turn(void)
{
return turn;
}
static int
get_nmrs(int n)
{
if (n<=NMRTimeout()) {
if (nmrs==NULL) update_nmrs();
return nmrs[n];
}
return 0;
}
static int
find_plane_id(const char * name)
{
plane * pl = getplanebyname(name);
return pl?pl->id:0;
}
static bool
get_key(const char * name)
{
int flag = atoi36(name);
attrib * a = find_key(global.attribs, flag);
return (a!=NULL);
}
static void
set_key(const char * name, bool value)
{
int flag = atoi36(name);
attrib * a = find_key(global.attribs, flag);
if (a==NULL && value) {
add_key(&global.attribs, flag);
} else if (a!=NULL && !value) {
a_remove(&global.attribs, a);
}
}
static const char *
get_gamename(void)
{
return global.gamename;
}
static void
lua_setstring(const char * lname, const char * key, const char * str)
{
struct locale * lang = find_locale(lname);
locale_setstring(lang, key, str);
}
static const char *
lua_getstring(const char * lname, const char * key)
{
struct locale * lang = find_locale(lname);
return locale_getstring(lang, key);
}
#define ISLANDSIZE 20
#define TURNS_PER_ISLAND 4
static void
lua_autoseed(const char * filename, bool new_island)
{
newfaction * players = read_newfactions(filename);
if (players!=NULL) {
rng_init(players->subscription);
while (players) {
int n = listlen(players);
int k = (n+ISLANDSIZE-1)/ISLANDSIZE;
k = n / k;
n = autoseed(&players, k, new_island || (turn % TURNS_PER_ISLAND)==0);
if (n==0) {
break;
}
}
}
}
#ifdef LUABIND_NO_EXCEPTIONS
static void
error_callback(lua_State * L)
{
}
#endif
static int
get_direction(const char * name)
{
for (int i=0;i!=MAXDIRECTIONS;++i) {
if (strcasecmp(directions[i], name)==0) return i;
}
return NODIRECTION;
}
static void
lua_equipunit(unit& u, const char * eqname)
{
equip_unit(&u, get_equipment(eqname));
}
static void
update_subscriptions(void)
{
FILE * F;
char zText[MAX_PATH];
faction * f;
strcat(strcpy(zText, basepath()), "/subscriptions");
F = fopen(zText, "r");
if (F==NULL) {
log_info((0, "could not open %s.\n", zText));
return;
}
for (;;) {
char zFaction[5];
int subscription, fno;
if (fscanf(F, "%d %s", &subscription, zFaction)<=0) break;
fno = atoi36(zFaction);
f = findfaction(fno);
if (f!=NULL) {
f->subscription=subscription;
}
}
fclose(F);
sprintf(zText, "subscriptions.%u", turn);
F = fopen(zText, "w");
for (f=factions;f!=NULL;f=f->next) {
fprintf(F, "%s:%u:%s:%s:%s:%u:\n",
itoa36(f->no), f->subscription, f->email, f->override,
dbrace(f->race), f->lastorders);
}
fclose(F);
}
static void
lua_learnskill(unit& u, const char * skname, float chances)
{
skill_t sk = sk_find(skname);
if (sk!=NOSKILL) {
learn_skill(&u, sk, chances);
}
}
bool
is_function(struct lua_State * luaState, const char * fname)
{
#if LUABIND_BETA>7 || (LUABIND_BETA==7 && LUABIND_DEVEL>=2)
object g = globals(luaState);
object fun = g[fname];
if (fun.is_valid()) {
if (type(fun)==LUA_TFUNCTION) {
return true;
}
log_warning(("Lua global object %s is not a function, type is %u\n", fname, type(fun)));
if (type(fun)!=LUA_TNIL) {
log_warning(("Lua global object %s is not a function, type is %u\n", fname, type(fun)));
}
}
#else
object g = get_globals(luaState);
object fun = g[fname];
if (fun.is_valid()) {
if (fun.type()==LUA_TFUNCTION) {
return true;
}
if (fun.type()!=LUA_TNIL) {
log_warning(("Lua global object %s is not a function, type is %u\n", fname, fun.type()));
}
}
#endif
return false;
}
static const char *
get_season(void)
{
gamedate gd;
get_gamedate(turn, &gd);
return seasonnames[gd.season];
}
void
bind_eressea(lua_State * L)
{
module(L)[
def("atoi36", &atoi36),
def("itoa36", &itoa36),
def("dice_roll", &dice_rand),
def("equipment_setitem", &lua_addequipment),
def("get_turn", &get_turn),
def("get_season", &get_season),
def("get_nmrs", &get_nmrs),
def("remove_empty_units", &remove_empty_units),
def("update_subscriptions", &update_subscriptions),
def("update_scores", &score),
def("equip_unit", &lua_equipunit),
def("learn_skill", &lua_learnskill),
/* map making */
def("autoseed", lua_autoseed),
/* string to enum */
def("direction", &get_direction),
/* localization: */
def("set_string", &lua_setstring),
def("get_string", &lua_getstring),
def("set_key", &set_key),
def("get_key", &get_key),
def("get_gamename", &get_gamename),
/* planes not really implemented */
def("get_plane_id", &find_plane_id)
];
#ifdef LUABIND_NO_EXCEPTIONS
luabind::set_error_callback(error_callback);
#endif
}