2003-11-10 00:36:11 +01:00
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <eressea.h>
|
2003-12-14 11:02:29 +01:00
|
|
|
|
#include "list.h"
|
2003-11-10 00:36:11 +01:00
|
|
|
|
|
|
|
|
|
// kernel includes
|
2005-01-01 12:48:36 +01:00
|
|
|
|
#include <kernel/building.h>
|
2005-01-13 13:42:36 +01:00
|
|
|
|
#include <kernel/item.h>
|
2004-04-11 01:21:58 +02:00
|
|
|
|
#include <kernel/plane.h>
|
|
|
|
|
#include <kernel/region.h>
|
|
|
|
|
#include <kernel/ship.h>
|
2005-01-01 12:48:36 +01:00
|
|
|
|
#include <kernel/unit.h>
|
2003-11-10 00:36:11 +01:00
|
|
|
|
|
|
|
|
|
// lua includes
|
|
|
|
|
#include <lua.hpp>
|
|
|
|
|
#include <luabind/luabind.hpp>
|
|
|
|
|
#include <luabind/iterator_policy.hpp>
|
2004-09-11 19:29:13 +02:00
|
|
|
|
#ifdef HAVE_LUABIND_B7
|
|
|
|
|
# include <luabind/operator.hpp>
|
|
|
|
|
#endif
|
2003-11-10 00:36:11 +01:00
|
|
|
|
|
2004-04-11 18:16:26 +02:00
|
|
|
|
#include <ostream>
|
2003-11-10 00:36:11 +01:00
|
|
|
|
using namespace luabind;
|
|
|
|
|
|
2004-06-26 22:51:19 +02:00
|
|
|
|
static eressea::list<region *>
|
2003-11-10 00:36:11 +01:00
|
|
|
|
get_regions(void) {
|
2004-06-26 22:51:19 +02:00
|
|
|
|
return eressea::list<region *>(regions);
|
2003-11-10 00:36:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-26 22:51:19 +02:00
|
|
|
|
static eressea::list<unit *>
|
2003-11-10 00:36:11 +01:00
|
|
|
|
region_units(const region& r) {
|
2004-06-26 22:51:19 +02:00
|
|
|
|
return eressea::list<unit *>(r.units);
|
2003-11-10 00:36:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-26 22:51:19 +02:00
|
|
|
|
static eressea::list<building *>
|
2003-11-10 00:36:11 +01:00
|
|
|
|
region_buildings(const region& r) {
|
2004-06-26 22:51:19 +02:00
|
|
|
|
return eressea::list<building *>(r.buildings);
|
2003-11-10 00:36:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-26 22:51:19 +02:00
|
|
|
|
static eressea::list<ship *>
|
2003-11-10 00:36:11 +01:00
|
|
|
|
region_ships(const region& r) {
|
2004-06-26 22:51:19 +02:00
|
|
|
|
return eressea::list<ship *>(r.ships);
|
2003-11-10 00:36:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
region_setname(region& r, const char * name) {
|
|
|
|
|
if (r.land) rsetname((&r), name);
|
|
|
|
|
}
|
|
|
|
|
|
2003-12-14 20:17:59 +01:00
|
|
|
|
static const char *
|
|
|
|
|
region_getterrain(const region& r) {
|
|
|
|
|
return terrain[r.terrain].name;
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-10 00:36:11 +01:00
|
|
|
|
static const char *
|
|
|
|
|
region_getname(const region& r) {
|
|
|
|
|
if (r.land) return r.land->name;
|
2003-12-14 11:02:29 +01:00
|
|
|
|
return terrain[r.terrain].name;
|
2003-11-10 00:36:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
region_setinfo(region& r, const char * info) {
|
2003-12-14 11:02:29 +01:00
|
|
|
|
set_string(&r.display, info);
|
2003-11-10 00:36:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
|
region_getinfo(const region& r) {
|
2003-12-14 11:02:29 +01:00
|
|
|
|
return r.display;
|
2003-11-10 00:36:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2004-04-11 01:21:58 +02:00
|
|
|
|
static int
|
|
|
|
|
region_plane(const region& r)
|
|
|
|
|
{
|
|
|
|
|
if (r.planep==NULL) return 0;
|
|
|
|
|
return r.planep->id;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-11 02:45:12 +02:00
|
|
|
|
static void
|
|
|
|
|
region_addnotice(region& r, const char * str)
|
|
|
|
|
{
|
|
|
|
|
addmessage(&r, NULL, str, MSG_MESSAGE, ML_IMPORTANT);
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-11 18:16:26 +02:00
|
|
|
|
static std::ostream&
|
2004-09-26 20:00:49 +02:00
|
|
|
|
operator<<(std::ostream& stream, const region& r)
|
2004-04-11 18:16:26 +02:00
|
|
|
|
{
|
|
|
|
|
stream << regionname(&r, NULL) << ", " << region_getterrain(r);
|
|
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
operator==(const region& a, const region&b)
|
|
|
|
|
{
|
|
|
|
|
return a.x==b.x && a.y==b.y;
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-30 14:09:45 +02:00
|
|
|
|
static bool
|
|
|
|
|
region_getflag(const region& r, int bit)
|
|
|
|
|
{
|
|
|
|
|
if (r.flags & (1<<bit)) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
region_setflag(region& r, int bit, bool set)
|
|
|
|
|
{
|
|
|
|
|
if (set) r.flags |= (1<<bit);
|
|
|
|
|
else r.flags &= ~(1<<bit);
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-13 13:42:36 +01:00
|
|
|
|
static int
|
|
|
|
|
region_getresource(const region& r, const char * type)
|
|
|
|
|
{
|
|
|
|
|
const resource_type * rtype = rt_find(type);
|
|
|
|
|
if (rtype==rt_find("money")) return rmoney(&r);
|
|
|
|
|
if (rtype==rt_find("peasant")) return rpeasants(&r);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2005-10-09 23:41:04 +02:00
|
|
|
|
static void
|
2005-10-09 23:48:56 +02:00
|
|
|
|
region_setresource(region& r, const char * type, int value)
|
2005-10-09 23:41:04 +02:00
|
|
|
|
{
|
|
|
|
|
const resource_type * rtype = rt_find(type);
|
|
|
|
|
if (rtype==rt_find("money")) rsetmoney(&r, value);
|
|
|
|
|
if (rtype==rt_find("peasant")) return rsetpeasants(&r, value);
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-19 17:39:51 +01:00
|
|
|
|
static void
|
|
|
|
|
region_setroad(region& r, int dir, lua_Number size)
|
|
|
|
|
{
|
2005-06-12 11:48:19 +02:00
|
|
|
|
rsetroad(&r, (direction_t)dir, (short)(terrain[rterrain(&r)].roadreq * size));
|
2004-12-19 17:39:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static lua_Number
|
|
|
|
|
region_getroad(region& r, int dir)
|
|
|
|
|
{
|
|
|
|
|
lua_Number result = rroad(&r, (direction_t)dir);
|
|
|
|
|
return terrain[rterrain(&r)].roadreq / result;
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-31 12:27:33 +02:00
|
|
|
|
static region *
|
2005-06-10 00:10:35 +02:00
|
|
|
|
terraform_region(short x, short y, const char * tname)
|
2004-05-31 12:27:33 +02:00
|
|
|
|
{
|
|
|
|
|
terrain_t t;
|
|
|
|
|
|
|
|
|
|
if (tname==NULL) {
|
|
|
|
|
t = NOTERRAIN;
|
|
|
|
|
} else {
|
|
|
|
|
for (t=0;t!=MAXTERRAINS;++t) {
|
|
|
|
|
if (strcmp(terrain[t].name, tname)==0) break;
|
|
|
|
|
}
|
|
|
|
|
if (t==MAXTERRAINS) return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
region * r = findregion(x, y);
|
|
|
|
|
if (t==NOTERRAIN) {
|
|
|
|
|
if (r!=NULL) {
|
|
|
|
|
if (r->units!=NULL) {
|
|
|
|
|
// TODO: error message
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
terraform(r, T_FIREWALL);
|
|
|
|
|
// TODO: durch einen NULL-<2D>quivalenten terraintyp ersetzen
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (r==NULL) r = new_region(x, y);
|
|
|
|
|
terraform(r, t);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-16 01:29:27 +02:00
|
|
|
|
static region *
|
|
|
|
|
region_next(const region& r, int dir)
|
|
|
|
|
{
|
|
|
|
|
if (dir<0 || dir >=MAXDIRECTIONS) return NULL;
|
|
|
|
|
return r_connect(&r, (direction_t)dir);
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-01 12:48:36 +01:00
|
|
|
|
static void
|
|
|
|
|
region_adddirection(region& r, region &rt, const char * name, const char * info)
|
|
|
|
|
{
|
|
|
|
|
create_special_direction(&r, &rt, -1, info, name);
|
|
|
|
|
spec_direction * sd = special_direction(&r, &rt);
|
|
|
|
|
sd->active = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-01 16:16:41 +01:00
|
|
|
|
static void
|
|
|
|
|
region_remove(region& r)
|
|
|
|
|
{
|
|
|
|
|
region ** rp = ®ions;
|
|
|
|
|
while (*rp) {
|
|
|
|
|
if (*rp==&r) {
|
2005-01-01 18:14:51 +01:00
|
|
|
|
while (r.units) {
|
|
|
|
|
destroy_unit(r.units);
|
2005-01-01 16:16:41 +01:00
|
|
|
|
}
|
|
|
|
|
*rp = r.next;
|
|
|
|
|
#ifdef FAST_CONNECT
|
|
|
|
|
direction_t dir;
|
|
|
|
|
for (dir=0;dir!=MAXDIRECTIONS;++dir) {
|
|
|
|
|
region * rn = r.connect[dir];
|
|
|
|
|
if (rn) {
|
|
|
|
|
direction_t reldir = reldirection(rn, &r);
|
|
|
|
|
r.connect[dir] = NULL;
|
|
|
|
|
rn->connect[reldir] = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2005-01-03 23:09:33 +01:00
|
|
|
|
runhash(&r);
|
2005-01-01 16:16:41 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
rp = &(*rp)->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-03 23:09:33 +01:00
|
|
|
|
void
|
2005-06-10 00:10:35 +02:00
|
|
|
|
region_move(region& r, short x, short y)
|
2005-01-03 23:09:33 +01:00
|
|
|
|
{
|
2005-01-04 01:12:23 +01:00
|
|
|
|
if (findregion(x,y)) {
|
|
|
|
|
log_error(("Bei %d, %d gibt es schon eine Region.\n", x, y));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-01-03 23:09:33 +01:00
|
|
|
|
#ifdef FAST_CONNECT
|
|
|
|
|
direction_t dir;
|
|
|
|
|
for (dir=0;dir!=MAXDIRECTIONS;++dir) {
|
|
|
|
|
region * rn = r.connect[dir];
|
|
|
|
|
if (rn!=NULL) {
|
|
|
|
|
direction_t reldir = reldirection(rn, &r);
|
|
|
|
|
rn->connect[reldir] = NULL;
|
|
|
|
|
}
|
|
|
|
|
rn = findregion(x+delta_x[dir], y+delta_y[dir]);
|
|
|
|
|
if (rn!=NULL) {
|
2005-01-14 18:47:51 +01:00
|
|
|
|
direction_t reldir = (direction_t)((dir + 3) % MAXDIRECTIONS);
|
2005-01-03 23:09:33 +01:00
|
|
|
|
rn->connect[reldir] = &r;
|
|
|
|
|
}
|
|
|
|
|
r.connect[dir] = rn;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
runhash(&r);
|
|
|
|
|
r.x = x;
|
|
|
|
|
r.y = y;
|
|
|
|
|
rhash(&r);
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-10 00:36:11 +01:00
|
|
|
|
void
|
|
|
|
|
bind_region(lua_State * L)
|
|
|
|
|
{
|
|
|
|
|
module(L)[
|
2003-12-14 11:02:29 +01:00
|
|
|
|
def("regions", &get_regions, return_stl_iterator),
|
2003-11-10 00:36:11 +01:00
|
|
|
|
def("get_region", &findregion),
|
2004-05-31 12:27:33 +02:00
|
|
|
|
def("terraform", &terraform_region),
|
2003-11-10 00:36:11 +01:00
|
|
|
|
|
|
|
|
|
class_<struct region>("region")
|
2004-04-11 18:16:26 +02:00
|
|
|
|
.def(tostring(self))
|
|
|
|
|
.def(self == region())
|
2003-11-10 00:36:11 +01:00
|
|
|
|
.property("name", ®ion_getname, ®ion_setname)
|
|
|
|
|
.property("info", ®ion_getinfo, ®ion_setinfo)
|
2003-12-14 20:17:59 +01:00
|
|
|
|
.property("terrain", ®ion_getterrain)
|
2004-04-11 02:45:12 +02:00
|
|
|
|
.def("add_notice", ®ion_addnotice)
|
2005-01-01 12:48:36 +01:00
|
|
|
|
.def("add_direction", ®ion_adddirection)
|
2004-12-19 17:39:51 +01:00
|
|
|
|
|
2004-05-30 14:14:46 +02:00
|
|
|
|
.def("get_flag", ®ion_getflag)
|
|
|
|
|
.def("set_flag", ®ion_setflag)
|
2004-12-19 17:39:51 +01:00
|
|
|
|
|
2005-01-01 16:16:41 +01:00
|
|
|
|
.def("remove", ®ion_remove)
|
2005-01-03 23:09:33 +01:00
|
|
|
|
.def("move", ®ion_move)
|
2005-01-01 16:16:41 +01:00
|
|
|
|
|
2004-12-19 17:39:51 +01:00
|
|
|
|
.def("get_road", ®ion_getroad)
|
|
|
|
|
.def("set_road", ®ion_setroad)
|
|
|
|
|
|
2004-06-16 01:29:27 +02:00
|
|
|
|
.def("next", ®ion_next)
|
2005-01-13 13:42:36 +01:00
|
|
|
|
.def("get_resource", ®ion_getresource)
|
2005-10-09 23:41:04 +02:00
|
|
|
|
.def("set_resource", ®ion_setresource)
|
2003-11-10 00:36:11 +01:00
|
|
|
|
.def_readonly("x", ®ion::x)
|
|
|
|
|
.def_readonly("y", ®ion::y)
|
|
|
|
|
.def_readwrite("age", ®ion::age)
|
2004-04-11 01:21:58 +02:00
|
|
|
|
.property("plane_id", ®ion_plane)
|
2003-11-10 00:36:11 +01:00
|
|
|
|
.property("units", ®ion_units, return_stl_iterator)
|
|
|
|
|
.property("buildings", ®ion_buildings, return_stl_iterator)
|
|
|
|
|
.property("ships", ®ion_ships, return_stl_iterator)
|
|
|
|
|
];
|
|
|
|
|
}
|