2015-01-30 20:37:14 +01:00
|
|
|
/*
|
2010-08-08 10:06:34 +02:00
|
|
|
+-------------------+
|
|
|
|
| | Enno Rehling <enno@eressea.de>
|
|
|
|
| Eressea PBEM host | Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
| (c) 1998 - 2008 | Katja Zedel <katze@felidae.kn-bremen.de>
|
|
|
|
| | Henning Peters <faroul@beyond.kn-bremen.de>
|
|
|
|
+-------------------+
|
|
|
|
|
|
|
|
This program may not be used, modified or distributed
|
|
|
|
without prior permission by the authors of Eressea.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
#include "bind_ship.h"
|
|
|
|
#include "bind_unit.h"
|
2015-02-01 10:03:30 +01:00
|
|
|
#include "bind_dict.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-08-27 06:40:18 +02:00
|
|
|
#include "move.h"
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/unit.h>
|
|
|
|
#include <kernel/ship.h>
|
|
|
|
#include <kernel/build.h>
|
|
|
|
|
|
|
|
#include <util/language.h>
|
2014-06-13 17:04:06 +02:00
|
|
|
#include <util/log.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
#include <tolua.h>
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int tolua_shiplist_next(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship **ship_ptr = (ship **)lua_touserdata(L, lua_upvalueindex(1));
|
|
|
|
ship *u = *ship_ptr;
|
|
|
|
if (u != NULL) {
|
|
|
|
tolua_pushusertype(L, (void *)u, TOLUA_CAST "ship");
|
|
|
|
*ship_ptr = u->next;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0; /* no more values to return */
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_get_id(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, self->no);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_get_name(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
tolua_pushstring(L, ship_getname(self));
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-02 19:55:35 +01:00
|
|
|
static int tolua_ship_get_display(lua_State * L)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
tolua_pushstring(L, self->display);
|
|
|
|
return 1;
|
2014-03-02 19:55:35 +01:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_get_region(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
if (self) {
|
|
|
|
tolua_pushusertype(L, self->region, TOLUA_CAST "region");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_set_region(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
region *r = (region *)tolua_tousertype(L, 1, 0);
|
|
|
|
if (self) {
|
|
|
|
move_ship(self, self->region, r, NULL);
|
|
|
|
}
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_set_name(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
ship_setname(self, tolua_tostring(L, 2, 0));
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-02 19:55:35 +01:00
|
|
|
static int tolua_ship_set_display(lua_State * L)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
2014-03-02 19:55:35 +01:00
|
|
|
free(self->display);
|
|
|
|
self->display = _strdup(tolua_tostring(L, 2, 0));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_get_units(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
unit **unit_ptr = (unit **)lua_newuserdata(L, sizeof(unit *));
|
|
|
|
unit *u = self->region->units;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
while (u && u->ship != self)
|
|
|
|
u = u->next;
|
|
|
|
luaL_getmetatable(L, TOLUA_CAST "unit");
|
|
|
|
lua_setmetatable(L, -2);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
*unit_ptr = u;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_pushcclosure(L, tolua_unitlist_nexts, 1);
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_get_objects(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
2015-02-01 10:03:30 +01:00
|
|
|
tolua_pushusertype(L, (void *)&self->attribs, USERTYPE_DICT);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_create(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
region *r = (region *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char *sname = tolua_tostring(L, 2, 0);
|
|
|
|
if (sname) {
|
|
|
|
const ship_type *stype = st_find(sname);
|
|
|
|
if (stype) {
|
|
|
|
ship *sh = new_ship(stype, r, default_locale);
|
|
|
|
sh->size = stype->construction ? stype->construction->maxsize : 1;
|
|
|
|
tolua_pushusertype(L, (void *)sh, TOLUA_CAST "ship");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error("Unknown ship type '%s'\n", sname);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_ship_tostring(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
lua_pushstring(L, shipname(self));
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_get_flags(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, self->flags);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_set_flags(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
self->flags = (int)tolua_tonumber(L, 2, 0);
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_set_coast(lua_State * L)
|
2010-10-17 06:10:46 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
if (lua_isnil(L, 2)) {
|
|
|
|
self->coast = NODIRECTION;
|
|
|
|
}
|
|
|
|
else if (lua_isnumber(L, 2)) {
|
|
|
|
self->coast = (direction_t)tolua_tonumber(L, 2, 0);
|
|
|
|
}
|
|
|
|
return 0;
|
2010-10-17 06:10:46 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_ship_get_coast(lua_State * L)
|
2010-10-17 06:10:46 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
if (self->coast) {
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, self->coast);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2010-10-17 06:10:46 +02:00
|
|
|
}
|
|
|
|
|
2014-04-12 05:25:21 +02:00
|
|
|
static int tolua_ship_get_type(lua_State * L)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
2014-06-13 07:14:07 +02:00
|
|
|
tolua_pushstring(L, self->type->_name);
|
2014-04-12 05:25:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-11-15 02:20:55 +01:00
|
|
|
static int tolua_ship_get_damage(lua_State * L)
|
|
|
|
{
|
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
lua_pushinteger(L, self->damage);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tolua_ship_set_damage(lua_State * L)
|
|
|
|
{
|
|
|
|
ship *self = (ship *)tolua_tousertype(L, 1, 0);
|
|
|
|
self->damage = (int)tolua_tonumber(L, 2, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void tolua_ship_open(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
/* register user types */
|
|
|
|
tolua_usertype(L, TOLUA_CAST "ship");
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_module(L, NULL, 0);
|
|
|
|
tolua_beginmodule(L, NULL);
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_cclass(L, TOLUA_CAST "ship", TOLUA_CAST "ship", TOLUA_CAST "", NULL);
|
|
|
|
tolua_beginmodule(L, TOLUA_CAST "ship");
|
|
|
|
{
|
|
|
|
tolua_function(L, TOLUA_CAST "__tostring", tolua_ship_tostring);
|
|
|
|
tolua_variable(L, TOLUA_CAST "id", tolua_ship_get_id, NULL);
|
|
|
|
tolua_variable(L, TOLUA_CAST "name", tolua_ship_get_name,
|
|
|
|
tolua_ship_set_name);
|
|
|
|
tolua_variable(L, TOLUA_CAST "info", tolua_ship_get_display,
|
|
|
|
tolua_ship_set_display);
|
|
|
|
tolua_variable(L, TOLUA_CAST "units", tolua_ship_get_units, NULL);
|
|
|
|
tolua_variable(L, TOLUA_CAST "flags", &tolua_ship_get_flags,
|
|
|
|
tolua_ship_set_flags);
|
|
|
|
tolua_variable(L, TOLUA_CAST "region", tolua_ship_get_region,
|
|
|
|
tolua_ship_set_region);
|
|
|
|
tolua_variable(L, TOLUA_CAST "coast", tolua_ship_get_coast,
|
|
|
|
tolua_ship_set_coast);
|
|
|
|
tolua_variable(L, TOLUA_CAST "type", tolua_ship_get_type, 0);
|
2015-11-15 02:20:55 +01:00
|
|
|
tolua_variable(L, TOLUA_CAST "damage", tolua_ship_get_damage,
|
|
|
|
tolua_ship_set_damage);
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
#ifdef TODO
|
2015-01-30 20:37:14 +01:00
|
|
|
.property("weight", &ship_getweight)
|
|
|
|
.property("capacity", &ship_getcapacity)
|
|
|
|
.property("maxsize", &ship_maxsize)
|
|
|
|
.def_readwrite("damage", &ship::damage)
|
|
|
|
.def_readwrite("size", &ship::size)
|
2010-08-08 10:06:34 +02:00
|
|
|
#endif
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_variable(L, TOLUA_CAST "objects", tolua_ship_get_objects, 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_function(L, TOLUA_CAST "create", tolua_ship_create);
|
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
|
|
|
}
|