Changes related to ray-collision, and luprex global variable stuff

This commit is contained in:
2025-01-14 18:37:31 -05:00
parent cd3e78a206
commit a01f6f4e7b
24 changed files with 180 additions and 317 deletions

View File

@@ -1,6 +1,7 @@
#include "world.hpp"
#include "pprint.hpp"
#include "serializelua.hpp"
#include <cmath>
static void tangible_getall(LuaCoreStack &LS0, LuaSlot list, const util::IdVector &idv) {
@@ -949,14 +950,7 @@ LuaDefine(global_set, "varname, value",
"|lua identifier."
"|"
"|You can store global data using global.set, then you can"
"|retrieve it using global.get. You can also retrieve data using"
"|gv.varname, which is just shorthand for global.get. You may not"
"|store data using gv.varname=value, this yields the error 'Use "
"|global.set to store data in the global data table.'"
"|"
"|Values stored using global.set are transmitted to all"
"|connected clients immediately. When a new client connects,"
"|he will receive all the global data."
"|retrieve it using global.get."
"|"
"|The global data table is not the same thing as the lua "
"|environment table. Trying to store data in the lua environment"
@@ -966,18 +960,16 @@ LuaDefine(global_set, "varname, value",
"|global data table (using global.set) instead of in the lua"
"|environment table."
"|"
"|There are certain restrictions on the values that you store."
"|Only data that can be serialized according to doc(table.serialize)"
"|can be stored."
"|Data is stored in a serialized form. The data is serialized"
"|at the moment you call global.set. It is deserialized when you"
"|call global.get. Therefore, each time you call global.get, you"
"|construct another copy of the value."
"|"
"|When you store the value, it is immediately serialized and then"
"|deserialized again, and the deserialized copy is stored in the"
"|variable."
"|"
// "|When you call global.get, you obtain the copy. Any attempt to"
// "|mutate the copy will fail with this lua error message: 'Tables"
// "|returned by global.get are immutable.' This rule prevents'"
// "|aliasing between global data and other data structures."
"|The serialization routine can only serialize certain kinds of"
"|values. For example, closures cannot be serialized at all."
"|Serializing a tangible serializes the tangible's ID, but"
"|none of the contents of the tangible. See doc(table.serialize)"
"|for more information about what can and can't be serialized."
"|") {
LuaArg varname;
LuaArg value;
@@ -990,8 +982,16 @@ LuaDefine(global_set, "varname, value",
return LS.result();
}
// Serialize the data.
StreamBuffer sb;
eng::string error = serialize_lua(LS, value, &sb);
if (!error.empty()) {
luaL_error(L, "%s", error.c_str());
return LS.result();
}
World *w = World::fetch_global_pointer(L);
w->set_global(LS, gvar, value);
w->set_global(gvar, sb.view());
return LS.result();
}
@@ -1000,16 +1000,33 @@ LuaDefine(global_get, "varname",
"|"
"|See doc(global.set) for information on how to store global data."
"|"
"|Do not mutate data returned by global.get: doing so will produce"
"|unpredictable results. Instead, using global.set to mutate global"
"|variables."
"|Performance note: each time you call global.get, you deserialize the"
"|stored serialized version, constructing another copy of the data."
"|") {
LuaArg varname;
LuaRet value;
LuaVar globaldb;
LuaDefStack LS(L, varname, value, globaldb);
LS.rawget(globaldb, LuaRegistry, "globaldb");
LS.rawget(value, globaldb, varname);
LuaDefStack LS(L, varname, value);
LS.set(value, LuaNil);
// Check the varname argument.
eng::string gvar = LS.ckstring(varname);
if (!sv::is_lua_id(gvar)) {
luaL_error(L, "variable name must be a valid lua identifier: %s", gvar.c_str());
return LS.result();
}
// Fetch the serialized blob.
World *w = World::fetch_global_pointer(L);
std::string_view data = w->get_global(gvar);
if (data.empty()) return LS.result();
// Deserialize the blob.
StreamBuffer sb(data);
eng::string error = deserialize_lua(LS, value, &sb);
if (!error.empty()) {
luaL_error(L, "%s", error.c_str());
return LS.result();
}
return LS.result();
}
@@ -1022,8 +1039,8 @@ LuaDefine(global_once, "varname",
"|") {
LuaArg varname;
LuaRet result;
LuaVar globaldb, flag;
LuaDefStack LS(L, varname, flag, result, globaldb);
LuaDefStack LS(L, varname, result);
LS.set(result, false);
// Check the varname argument.
eng::string gvar = LS.ckstring(varname);
@@ -1033,17 +1050,12 @@ LuaDefine(global_once, "varname",
}
gvar += ":once";
LS.rawget(globaldb, LuaRegistry, "globaldb");
LS.rawget(flag, globaldb, gvar);
if (!LS.isnil(flag)) {
LS.set(result, false);
return LS.result();
}
LS.set(result, true);
World *w = World::fetch_global_pointer(L);
w->set_global(LS, gvar, result);
LS.set(result, false);
if (w->get_global(gvar).empty()) {
LS.set(result, true);
w->set_global(gvar, "x");
}
return LS.result();
}
@@ -1055,8 +1067,7 @@ LuaDefine(global_clearonce, "varname",
"|uses global.set under the covers."
"|") {
LuaArg varname;
LuaVar null;
LuaDefStack LS(L, varname, null);
LuaDefStack LS(L, varname);
// Check the varname argument.
eng::string gvar = LS.ckstring(varname);
@@ -1066,8 +1077,7 @@ LuaDefine(global_clearonce, "varname",
}
gvar += ":once";
LS.set(null, LuaNil);
World *w = World::fetch_global_pointer(L);
w->set_global(LS, gvar, null);
w->set_global(gvar, "");
return LS.result();
}