Skip to content

Commit

Permalink
Move implementations of some LuaVoxelManip functions to l_mapgen
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Aug 14, 2023
1 parent c644484 commit 507815b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class ServerMap : public Map
void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) override;

private:
friend class LuaVoxelManip;
friend class ModApiMapgen; // for m_transforming_liquid

// Emerge manager
EmergeManager *m_emerge;
Expand Down
45 changes: 45 additions & 0 deletions src/script/lua_api/l_mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,51 @@ int ModApiMapgen::l_read_schematic(lua_State *L)
return 1;
}

int ModApiMapgen::update_liquids(lua_State *L, MMVManip *vm)
{
GET_ENV_PTR;

ServerMap *map = &(env->getServerMap());
const NodeDefManager *ndef = getServer(L)->getNodeDefManager();

Mapgen mg;
mg.vm = vm;
mg.ndef = ndef;

mg.updateLiquid(&map->m_transforming_liquid,
vm->m_area.MinEdge, vm->m_area.MaxEdge);
return 0;
}

int ModApiMapgen::calc_lighting(lua_State *L, MMVManip *vm,
v3s16 pmin, v3s16 pmax, bool propagate_shadow)
{
const NodeDefManager *ndef = getGameDef(L)->ndef();
EmergeManager *emerge = getServer(L)->getEmergeManager();

assert(vm->m_area.contains(VoxelArea(pmin, pmax)));

Mapgen mg;
mg.vm = vm;
mg.ndef = ndef;
mg.water_level = emerge->mgparams->water_level;

mg.calcLighting(pmin, pmax, vm->m_area.MinEdge, vm->m_area.MaxEdge,
propagate_shadow);
return 0;
}

int ModApiMapgen::set_lighting(lua_State *L, MMVManip *vm,
v3s16 pmin, v3s16 pmax, u8 light)
{
assert(vm->m_area.contains(VoxelArea(pmin, pmax)));

Mapgen mg;
mg.vm = vm;

mg.setLighting(light, pmin, pmax);
return 0;
}

void ModApiMapgen::Initialize(lua_State *L, int top)
{
Expand Down
19 changes: 19 additions & 0 deletions src/script/lua_api/l_mapgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once

#include "lua_api/l_base.h"
#include "irr_v3d.h"

typedef u16 biome_t; // copy from mg_biome.h to avoid an unnecessary include

class MMVManip;

class ModApiMapgen : public ModApiBase
{
friend class LuaVoxelManip;
private:
// get_biome_id(biomename)
// returns the biome id as used in biomemap and returned by 'get_biome_data()'
Expand Down Expand Up @@ -139,6 +143,21 @@ class ModApiMapgen : public ModApiBase
// read_schematic(schematic, options={...})
static int l_read_schematic(lua_State *L);

// Foreign implementations
/*
* In this case the API functions belong to LuaVoxelManip (so l_vmanip.cpp),
* but the implementations are so deeply connected to mapgen-related code
* that they are better off being here.
*/

static int update_liquids(lua_State *L, MMVManip *vm);

static int calc_lighting(lua_State *L, MMVManip *vm,
v3s16 pmin, v3s16 pmax, bool propagate_shadow);

static int set_lighting(lua_State *L, MMVManip *vm,
v3s16 pmin, v3s16 pmax, u8 light);

public:
static void Initialize(lua_State *L, int top);

Expand Down
53 changes: 9 additions & 44 deletions src/script/lua_api/l_vmanip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include <map>
#include "lua_api/l_vmanip.h"
#include "lua_api/l_mapgen.h"
#include "lua_api/l_internal.h"
#include "common/c_content.h"
#include "common/c_converter.h"
#include "common/c_packer.h"
#include "emerge.h"
#include "environment.h"
#include "map.h"
#include "mapblock.h"
#include "server.h"
#include "mapgen/mapgen.h"
#include "voxelalgorithms.h"

// garbage collector
Expand Down Expand Up @@ -162,73 +161,44 @@ int LuaVoxelManip::l_set_node_at(lua_State *L)

int LuaVoxelManip::l_update_liquids(lua_State *L)
{
GET_ENV_PTR;

LuaVoxelManip *o = checkObject<LuaVoxelManip>(L, 1);

ServerMap *map = &(env->getServerMap());
const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
MMVManip *vm = o->vm;

Mapgen mg;
mg.vm = vm;
mg.ndef = ndef;

mg.updateLiquid(&map->m_transforming_liquid,
vm->m_area.MinEdge, vm->m_area.MaxEdge);

return 0;
return ModApiMapgen::update_liquids(L, o->vm);
}

int LuaVoxelManip::l_calc_lighting(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;

LuaVoxelManip *o = checkObject<LuaVoxelManip>(L, 1);
if (!o->is_mapgen_vm) {
warningstream << "VoxelManip:calc_lighting called for a non-mapgen "
"VoxelManip object" << std::endl;
log_deprecated(L, "calc_lighting called for a non-mapgen "
"VoxelManip object");
return 0;
}

const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
EmergeManager *emerge = getServer(L)->getEmergeManager();
MMVManip *vm = o->vm;

v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
v3s16 fpmin = vm->m_area.MinEdge;
v3s16 fpmax = vm->m_area.MaxEdge;
v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) : fpmin + yblock;
v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) : fpmax - yblock;
v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) : vm->m_area.MinEdge + yblock;
v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) : vm->m_area.MaxEdge - yblock;
bool propagate_shadow = !lua_isboolean(L, 4) || readParam<bool>(L, 4);

sortBoxVerticies(pmin, pmax);
if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
throw LuaError("Specified voxel area out of VoxelManipulator bounds");

Mapgen mg;
mg.vm = vm;
mg.ndef = ndef;
mg.water_level = emerge->mgparams->water_level;

mg.calcLighting(pmin, pmax, fpmin, fpmax, propagate_shadow);

return 0;
return ModApiMapgen::calc_lighting(L, vm, pmin, pmax, propagate_shadow);
}

int LuaVoxelManip::l_set_lighting(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;

LuaVoxelManip *o = checkObject<LuaVoxelManip>(L, 1);
if (!o->is_mapgen_vm) {
warningstream << "VoxelManip:set_lighting called for a non-mapgen "
"VoxelManip object" << std::endl;
return 0;
}

if (!lua_istable(L, 2))
throw LuaError("VoxelManip:set_lighting called with missing parameter");
luaL_checktype(L, 2, LUA_TTABLE);

u8 light;
light = (getintfield_default(L, 2, "day", 0) & 0x0F);
Expand All @@ -244,12 +214,7 @@ int LuaVoxelManip::l_set_lighting(lua_State *L)
if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
throw LuaError("Specified voxel area out of VoxelManipulator bounds");

Mapgen mg;
mg.vm = vm;

mg.setLighting(light, pmin, pmax);

return 0;
return ModApiMapgen::set_lighting(L, vm, pmin, pmax, light);
}

int LuaVoxelManip::l_get_light_data(lua_State *L)
Expand Down

0 comments on commit 507815b

Please sign in to comment.