Add the ability to get node information. This is used by ladder but something is still wrong.

master
Dennis Payne 2016-10-29 19:38:03 -04:00
parent f02e217b93
commit ab03c42fa6
1 changed files with 117 additions and 101 deletions

View File

@ -6,39 +6,45 @@ lua_State *Lg = NULL;
const char *name = NULL;
const char *air = "air";
const char *black_wool = "wool:black";
const char *blue_wool = "wool:blue";
const char *brown_wool = "wool:brown";
const char *cobblestone = "default:cobble";
const char *cyan_wool = "wool:cyan";
const char *diamond_block = "default:diamondblock";
const char *dirt = "default:dirt";
const char *glass_pane = "xpanes:pane";
const char *gold_block = "default:goldblock";
const char *gold_ore = "default:stone_with_gold";
const char *grass = "default:dirt_with_grass";
const char *gravel = "default:gravel";
const char *green_wool = "wool:dark_green";
const char *iron_ore = "default:stone_with_iron";
const char *lime_wool = "wool:green";
const char *magenta_wool = "wool:magenta";
const char *oakwood_plank = "default:wood";
const char *oak_wood = "default:tree";
const char *oak_wood_slab = "stairs:slab_wood";
const char *orange_wool = "wool:orange";
const char *pink_wool = "wool:pink";
const char *purple_wool = "wool:violet";
const char *red_wool = "wool:red";
const char *sand = "default:sand";
const char *spruce_fence = "default:fence_pine_wood";
const char *sprucewood_plank = "default:pine_wood";
const char *spruce_wood = "default:pine_tree";
const char *spruce_wood_stairs = "stairs:stair_pine_wood";
const char *stone = "default:stone";
const char *stone_brick = "default:stonebrick";
const char *stone_brick_stairs = "stairs:stair_stonebrick";
const char *white_wool = "wool:white";
const char *yellow_wool = "wool:yellow";
const char *blockIDToNodeName[][2] =
{
{"1", "default:stone"},
{"2", "default:dirt_with_grass"},
{"3", "default:dirt"},
{"4", "default:cobble"},
{"5", "default:wood"},
{"5:1", "default:pine_wood"},
{"12", "default:sand"},
{"13", "default:gravel"},
{"14", "default:stone_with_gold"},
{"15", "default:stone_with_iron"},
{"17", "default:tree"},
{"17:1", "default:pine_tree"},
{"35", "wool:white"},
{"35:1", "wool:orange"},
{"35:2", "wool:magenta"},
{"35:4", "wool:yellow"},
{"35:5", "wool:green"},
{"35:6", "wool:pink"},
{"35:9", "wool:cyan"},
{"35:10", "wool:violet"},
{"35:11", "wool:blue"},
{"35:12", "wool:brown"},
{"35:13", "wool:dark_green"},
{"35:14", "wool:red"},
{"35:15", "wool:black"},
{"41", "default:goldblock"},
{"57", "default:diamondblock"},
{"64", "doors:door_wood"},
{"65", "default:ladder_wood"},
{"98", "default:stonebrick"},
{"102", "xpanes:pane"},
{"109", "stairs:stair_stonebrick"},
{"126", "stairs:slab_wood"},
{"134", "stairs:stair_pine_wood"},
{"188", "default:fence_pine_wood"}
};
const char *blockID_to_node_name(const char *blockIDOrig)
{
@ -49,74 +55,24 @@ const char *blockID_to_node_name(const char *blockIDOrig)
strcpy(blockID, blockIDOrig);
if ((len > 2) && (strcmp(blockIDOrig + len - 2, ":0") == 0))
blockID[len - 2] = 0;
if (strcmp(blockID, "1") == 0)
return stone;
else if (strcmp(blockID, "2") == 0)
return grass;
else if (strcmp(blockID, "3") == 0)
return dirt;
else if (strcmp(blockID, "4") == 0)
return cobblestone;
else if (strcmp(blockID, "5") == 0)
return oakwood_plank;
else if (strcmp(blockID, "5:1") == 0)
return sprucewood_plank;
else if (strcmp(blockID, "12") == 0)
return sand;
else if (strcmp(blockID, "13") == 0)
return gravel;
else if (strcmp(blockID, "14") == 0)
return gold_ore;
else if (strcmp(blockID, "15") == 0)
return iron_ore;
else if (strcmp(blockID, "17") == 0)
return oak_wood;
else if (strcmp(blockID, "17:1") == 0)
return spruce_wood;
else if (strcmp(blockID, "35") == 0)
return white_wool;
else if (strcmp(blockID, "35:1") == 0)
return orange_wool;
else if (strcmp(blockID, "35:2") == 0)
return magenta_wool;
else if (strcmp(blockID, "35:4") == 0)
return yellow_wool;
else if (strcmp(blockID, "35:5") == 0)
return lime_wool;
else if (strcmp(blockID, "35:6") == 0)
return pink_wool;
else if (strcmp(blockID, "35:9") == 0)
return cyan_wool;
else if (strcmp(blockID, "35:10") == 0)
return purple_wool;
else if (strcmp(blockID, "35:11") == 0)
return blue_wool;
else if (strcmp(blockID, "35:12") == 0)
return brown_wool;
else if (strcmp(blockID, "35:13") == 0)
return green_wool;
else if (strcmp(blockID, "35:14") == 0)
return red_wool;
else if (strcmp(blockID, "35:15") == 0)
return black_wool;
else if (strcmp(blockID, "41") == 0)
return gold_block;
else if (strcmp(blockID, "57") == 0)
return gold_block;
else if (strcmp(blockID, "98") == 0)
return stone_brick;
else if (strcmp(blockID, "102") == 0)
return glass_pane;
else if (strcmp(blockID, "109") == 0)
return stone_brick_stairs;
else if (strcmp(blockID, "126") == 0)
return oak_wood_slab;
else if (strcmp(blockID, "134") == 0)
return spruce_wood_stairs;
else if (strcmp(blockID, "188") == 0)
return spruce_fence;
else
return air;
int maxBlockID = sizeof(blockIDToNodeName) / 2 / (sizeof(const char *));
for (int i = 0; i < maxBlockID; ++i)
{
if (strcmp(blockID, blockIDToNodeName[i][0]) == 0)
return blockIDToNodeName[i][1];
}
return air;
}
const char *node_name_to_blockID(const char *node_name)
{
int maxBlockID = sizeof(blockIDToNodeName) / 2 / (sizeof(const char *));
for (int i = 0; i < maxBlockID; ++i)
{
if (strcmp(node_name, blockIDToNodeName[i][1]) == 0)
return blockIDToNodeName[i][0];
}
return "0";
}
duk_ret_t echo(duk_context *ctx)
@ -160,6 +116,62 @@ duk_ret_t readFile(duk_context *ctx)
return 1;
}
duk_ret_t get_node(duk_context *ctx)
{
printf("get_node\n");
int z = duk_to_int(ctx, -3);
int y = duk_to_int(ctx, -2);
int x = duk_to_int(ctx, -1);
lua_getfield(Lg, LUA_GLOBALSINDEX, "minetest"); // [minetest]
lua_getfield(Lg, -1, "get_node"); // [minetest get_node]
lua_remove(Lg, -2); // [get_node]
lua_newtable(Lg);
lua_pushnumber(Lg, x);
lua_setfield(Lg, -2, "x");
lua_pushnumber(Lg, y);
lua_setfield(Lg, -2, "y");
lua_pushnumber(Lg, z);
lua_setfield(Lg, -2, "z");
lua_call(Lg, 1, 0);
lua_getfield(Lg, -1, "name");
const char *node_name = lua_tostring(Lg, -1);
printf("get_node %s\n", node_name);
const char *blockID = node_name_to_blockID(node_name);
printf("get_node %s\n", blockID);
duk_idx_t ary_indx = duk_push_array(ctx);
duk_push_string(ctx, blockID);
duk_put_prop_string(ctx, ary_indx, "typeId");
duk_push_string(ctx, node_name);
duk_put_prop_string(ctx, ary_indx, "nodeName");
lua_remove(Lg, -1);
lua_remove(Lg, -1);
return 1;
}
duk_ret_t place_node(duk_context *ctx)
{
int z = duk_to_int(ctx, -4);
int y = duk_to_int(ctx, -3);
int x = duk_to_int(ctx, -2);
const char *blockID = duk_to_string(ctx, -1);
const char *nodeName = blockID_to_node_name(blockID);
lua_getfield(Lg, LUA_GLOBALSINDEX, "minetest"); // [minetest]
lua_getfield(Lg, -1, "place_node"); // [minetest new_node]
lua_remove(Lg, -2); // [set_node]
lua_newtable(Lg);
lua_pushnumber(Lg, x);
lua_setfield(Lg, -2, "x");
lua_pushnumber(Lg, y);
lua_setfield(Lg, -2, "y");
lua_pushnumber(Lg, z);
lua_setfield(Lg, -2, "z");
lua_newtable(Lg);
lua_pushstring(Lg, nodeName);
lua_setfield(Lg, -2, "name");
lua_call(Lg, 2, 0);
return 1;
}
duk_ret_t set_node(duk_context *ctx)
{
int z = duk_to_int(ctx, -4);
@ -337,6 +349,10 @@ int luaopen_scriptmine(lua_State *L)
duk_put_prop_string(ctx, -2, "echo");
duk_push_c_function(ctx, readFile, 1);
duk_put_prop_string(ctx, -2, "readFile");
duk_push_c_function(ctx, get_node, 3);
duk_put_prop_string(ctx, -2, "get_node");
duk_push_c_function(ctx, place_node, 3);
duk_put_prop_string(ctx, -2, "place_node");
duk_push_c_function(ctx, set_node, 4);
duk_put_prop_string(ctx, -2, "set_node");
duk_push_c_function(ctx, get_player_location, 0);