Permalink
Browse files

Add more block IDs. Implement box0.

  • Loading branch information...
dulsi committed Oct 28, 2016
1 parent 012dac2 commit 38ff3a6ae744893cfcf2e3a3667f94b740846e22
Showing with 146 additions and 8 deletions.
  1. +80 −1 drone.c
  2. +27 −0 drone.js
  3. +2 −0 init.lua
  4. +36 −6 scriptmine.c
  5. +1 −1 scriptmine.h
View
81 drone.c
@@ -15,7 +15,7 @@ duk_ret_t drone_box(duk_context *ctx)
duk_get_prop_string(ctx, -1, "facing");
int facing = duk_to_int(ctx, -1);
duk_remove(ctx, -1);
int blockID = duk_to_int(ctx, -5);
const char *blockID = duk_to_string(ctx, -5);
int w = duk_to_int(ctx, -4);
int h = duk_to_int(ctx, -3);
int d = duk_to_int(ctx, -2);
@@ -75,6 +75,83 @@ duk_ret_t drone_box(duk_context *ctx)
return 0;
}
duk_ret_t drone_box0(duk_context *ctx)
{
duk_push_this(ctx); // [b w h d this]
duk_get_prop_string(ctx, -1, "x");
int x = duk_to_int(ctx, -1);
duk_remove(ctx, -1);
duk_get_prop_string(ctx, -1, "y");
int y = duk_to_int(ctx, -1);
duk_remove(ctx, -1);
duk_get_prop_string(ctx, -1, "z");
int z = duk_to_int(ctx, -1);
duk_remove(ctx, -1);
duk_get_prop_string(ctx, -1, "facing");
int facing = duk_to_int(ctx, -1);
duk_remove(ctx, -1);
const char *blockID = duk_to_string(ctx, -5);
int w = duk_to_int(ctx, -4);
int h = duk_to_int(ctx, -3);
int d = duk_to_int(ctx, -2);
if (w < 1)
w = 1;
if (h < 1)
h = 1;
if (d < 1)
d = 1;
const char *nodeName = blockID_to_node_name(blockID);
lua_getfield(Lg, LUA_GLOBALSINDEX, "minetest"); // [minetest]
lua_getfield(Lg, -1, "set_node"); // [minetest new_node]
lua_remove(Lg, -2); // [set_node]
for (int changeW = 0; changeW < w; changeW++)
{
for (int changeH = 0; changeH < h; changeH++)
{
for (int changeD = 0; changeD < d; changeD++)
{
if ((changeW != 0) && (changeW != w - 1) && (changeD != 0) && (changeD != d - 1))
continue;
lua_pushvalue(Lg, -1); // [set_node set_node]
lua_newtable(Lg);
int fx = x;
int fz = z;
switch (facing)
{
case 0:
fx -= changeD;
fz += changeW;
break;
case 1:
fz += changeD;
fx += changeW;
break;
case 2:
fx += changeD;
fz -= changeW;
break;
case 3:
fz -= changeD;
fx -= changeW;
break;
};
lua_pushnumber(Lg, fx);
lua_setfield(Lg, -2, "x");
lua_pushnumber(Lg, y + changeH);
lua_setfield(Lg, -2, "y");
lua_pushnumber(Lg, fz);
lua_setfield(Lg, -2, "z");
lua_newtable(Lg);
lua_pushstring(Lg, nodeName);
lua_setfield(Lg, -2, "name");
lua_call(Lg, 2, 0);
}
}
}
lua_remove(Lg, -1);
return 0;
}
duk_ret_t drone_constructor(duk_context *ctx)
{
if (!duk_is_constructor_call(ctx))
@@ -162,6 +239,8 @@ void drone_init(duk_context *ctx)
duk_push_object(ctx);
duk_push_c_function(ctx, drone_box, 4);
duk_put_prop_string(ctx, -2, "box");
duk_push_c_function(ctx, drone_box0, 4);
duk_put_prop_string(ctx, -2, "box0");
duk_put_prop_string(ctx, -2, "prototype");
duk_put_global_string(ctx, "Drone");
}
View
@@ -0,0 +1,27 @@
function box()
{
var d = new Drone();
if (arguments.length >= 4)
d.box(arguments[0], arguments[1], arguments[2], arguments[3]);
else if (arguments.length == 3)
d.box(arguments[0], arguments[1], arguments[2]);
else if (arguments.length == 2)
d.box(arguments[0], arguments[1]);
else if (arguments.length == 1)
d.box(arguments[0]);
return d;
}
function box0()
{
var d = new Drone();
if (arguments.length >= 4)
d.box0(arguments[0], arguments[1], arguments[2], arguments[3]);
else if (arguments.length == 3)
d.box0(arguments[0], arguments[1], arguments[2]);
else if (arguments.length == 2)
d.box0(arguments[0], arguments[1]);
else if (arguments.length == 1)
d.box0(arguments[0]);
return d;
}
View
@@ -1,5 +1,7 @@
require("scriptmine")
loadscript(minetest.get_modpath(minetest.get_current_modname()))
minetest.register_chatcommand("js", {
params = "<command>",
description = "Run javascript command",
View
@@ -8,21 +8,36 @@ const char *air = "air";
const char *cobblestone = "default:cobble";
const char *dirt = "default:dirt";
const char *grass = "default:dirt_with_grass";
const char *gravel = "default:gravel";
const char *oakwood_plank = "default:wood";
const char *oak_wood = "default:tree";
const char *sand = "default:sand";
const char *sprucewood_plank = "default:pine_wood";
const char *spruce_wood = "default:pine_tree";
const char *stone = "default:stone";
const char *blockID_to_node_name(int blockID)
const char *blockID_to_node_name(const char *blockID)
{
if (blockID == 1)
if (strcmp(blockID, "1") == 0)
return stone;
else if (blockID == 2)
else if (strcmp(blockID, "2") == 0)
return grass;
else if (blockID == 3)
else if (strcmp(blockID, "3") == 0)
return dirt;
else if (blockID == 4)
else if (strcmp(blockID, "4") == 0)
return cobblestone;
else if (blockID == 5)
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, "17") == 0)
return oak_wood;
else if (strcmp(blockID, "17:1") == 0)
return spruce_wood;
else
return air;
}
@@ -49,6 +64,20 @@ static int javascript(lua_State *L)
return 1;
}
static int loadscript(lua_State *L)
{
const char *modpath = lua_tostring(L, -1);
char *fullpath = malloc(strlen(modpath) + 10);
strcpy(fullpath, modpath);
strcat(fullpath, "/drone.js");
if (duk_peval_file_noresult(ctx, fullpath) != 0) {
printf("Error: %s\n", duk_safe_to_string(ctx, -1));
exit(1);
}
free(fullpath);
return 1;
}
int luaopen_scriptmine(lua_State *L)
{
ctx = duk_create_heap_default();
@@ -62,5 +91,6 @@ int luaopen_scriptmine(lua_State *L)
exit(1);
}
lua_register(L, "javascript", javascript);
lua_register(L, "loadscript", loadscript);
return 0;
}
View
@@ -6,5 +6,5 @@
extern lua_State *Lg;
extern const char *name;
extern const char *blockID_to_node_name(int blockID);
extern const char *blockID_to_node_name(const char *blockID);
extern void drone_init(duk_context *ctx);

0 comments on commit 38ff3a6

Please sign in to comment.