1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
--atc.lua
--registers and controls the ATC system
local atc={}
local eval_conditional
-- ATC persistence table. advtrains.atc is created by init.lua when it loads the save file.
atc.controllers = {}
function atc.load_data(data)
local temp = data and data.controllers or {}
--transcode atc controller data to node hashes: table access times for numbers are far less than for strings
for pts, data in pairs(temp) do
if type(pts)=="number" then
pts=minetest.pos_to_string(minetest.get_position_from_hash(pts))
end
atc.controllers[pts] = data
end
end
function atc.save_data()
return {controllers = atc.controllers}
end
--contents: {command="...", arrowconn=0-15 where arrow points}
--general
function atc.train_set_command(train, command, arrow)
atc.train_reset_command(train, true)
train.atc_delay = 0
train.atc_arrow = arrow
train.atc_command = command
end
function atc.send_command(pos, par_tid)
local pts=minetest.pos_to_string(pos)
if atc.controllers[pts] then
--atprint("Called send_command at "..pts)
local train_id = par_tid or advtrains.get_train_at_pos(pos)
if train_id then
if advtrains.trains[train_id] then
--atprint("send_command inside if: "..sid(train_id))
if atc.controllers[pts].arrowconn then
atlog("ATC controller at",pts,": This controller had an arrowconn of", atc.controllers[pts].arrowconn, "set. Since this field is now deprecated, it was removed.")
atc.controllers[pts].arrowconn = nil
end
local train = advtrains.trains[train_id]
local index = advtrains.path_lookup(train, pos)
local iconnid = 1
if index then
iconnid = train.path_cn[index]
else
atwarn("ATC rail at", pos, ": Rail not on train's path! Can't determine arrow direction. Assuming +!")
end
local command = atc.controllers[pts].command
command = eval_conditional(command, iconnid==1, train.velocity)
if not command then command="" end
command=string.match(command, "^%s*(.*)$")
if command == "" then
atprint("Sending ATC Command to", train_id, ": Not modifying, conditional evaluated empty.")
return true
end
atc.train_set_command(train, command, iconnid==1)
atprint("Sending ATC Command to", train_id, ":", command, "iconnid=",iconnid)
return true
else
atwarn("ATC rail at", pos, ": Sending command failed: The train",train_id,"does not exist. This seems to be a bug.")
end
else
atwarn("ATC rail at", pos, ": Sending command failed: There's no train at this position. This seems to be a bug.")
end
else
atwarn("ATC rail at", pos, ": Sending command failed: Entry for controller not found.")
atwarn("ATC rail at", pos, ": Please visit controller and click 'Save'")
end
return false
end
-- Resets any ATC commands the train is currently executing, including the target speed (tarvelocity) it is instructed to hold
-- if keep_tarvel is set, does not clear the tarvelocity
function atc.train_reset_command(train, keep_tarvel)
train.atc_command=nil
train.atc_delay=nil
train.atc_brake_target=nil
train.atc_wait_finish=nil
train.atc_arrow=nil
if not keep_tarvel then
train.tarvelocity=nil
end
end
--nodes
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "lua_api/l_noise.h"
#include "lua_api/l_internal.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include "log.h"
#include "porting.h"
#include "util/numeric.h"
///////////////////////////////////////
/*
LuaPerlinNoise
*/
LuaPerlinNoise::LuaPerlinNoise(NoiseParams *params) :
np(*params)
{
}
int LuaPerlinNoise::l_get_2d(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoise *o = checkobject(L, 1);
v2f p = readParam<v2f>(L, 2);
lua_Number val = NoisePerlin2D(&o->np, p.X, p.Y, 0);
lua_pushnumber(L, val);
return 1;
}
int LuaPerlinNoise::l_get_3d(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoise *o = checkobject(L, 1);
v3f p = check_v3f(L, 2);
lua_Number val = NoisePerlin3D(&o->np, p.X, p.Y, p.Z, 0);
lua_pushnumber(L, val);
return 1;
}
int LuaPerlinNoise::create_object(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
NoiseParams params;
if (lua_istable(L, 1)) {
read_noiseparams(L, 1, ¶ms);
} else {
params.seed = luaL_checkint(L, 1);
params.octaves = luaL_checkint(L, 2);
params.persist = readParam<float>(L, 3);
params.spread = v3f(1,
end
return
end
meta:set_string("mode", idxtrans[fields.mode])
meta:set_string("command", fields.command)
meta:set_string("command_on", fields.command_on)
meta:set_string("channel", fields.channel)
if fields.mode=="digiline" then
meta:set_string("infotext", attrans("ATC controller, mode @1\nChannel: @2", (fields.mode or "?"), meta:get_string("command")) )
else
meta:set_string("infotext", attrans("ATC controller, mode @1\nCommand: @2", (fields.mode or "?"), meta:get_string("command")) )
end
meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta))
local pts=minetest.pos_to_string(pos)
local _, conns=advtrains.get_rail_info_at(pos, advtrains.all_tracktypes)
atc.controllers[pts]={command=fields.command}
if #advtrains.occ.get_trains_at(pos) > 0 then
atc.send_command(pos)
end
end
end)
end,
advtrains = {
on_train_enter = function(pos, train_id)
atc.send_command(pos)
end,
},
}
end
function atc.get_atc_controller_formspec(pos, meta)
local mode=tonumber(meta:get_string("mode")) or 1
local command=meta:get_string("command")
local command_on=meta:get_string("command_on")
local channel=meta:get_string("channel")
local formspec="size[8,6]"
-- "dropdown[0,0;3;mode;static,mesecon,digiline;"..mode.."]"
if mode<3 then
formspec=formspec.."field[0.5,1.5;7,1;command;"..attrans("Command")..";"..minetest.formspec_escape(command).."]"
if tonumber(mode)==2 then
formspec=formspec.."field[0.5,3;7,1;command_on;"..attrans("Command (on)")..";"..minetest.formspec_escape(command_on).."]
noise = new Noise(&np, seed, size.X, size.Y, size.Z);
} catch (InvalidNoiseParamsException &e) {
throw LuaError(e.what());
}
}
LuaPerlinNoiseMap::~LuaPerlinNoiseMap()
{
delete noise;
}
int LuaPerlinNoiseMap::l_get_2d_map(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
size_t i = 0;
LuaPerlinNoiseMap *o = checkobject(L, 1);
v2f p = readParam<v2f>(L, 2);
Noise *n = o->noise;
n->perlinMap2D(p.X, p.Y);
lua_createtable(L, n->sy, 0);
for (u32 y = 0; y != n->sy; y++) {
lua_createtable(L, n->sx, 0);
for (u32 x = 0; x != n->sx; x++) {
lua_pushnumber(L, n->result[i++]);
lua_rawseti(L, -2, x + 1);
}
lua_rawseti(L, -2, y + 1);
}
return 1;
}
int LuaPerlinNoiseMap::l_get_2d_map_flat(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoiseMap *o = checkobject(L, 1);
v2f p = readParam<v2f>(L, 2);
bool use_buffer = lua_istable(L, 3);
Noise *n = o->noise;
n->perlinMap2D(p.X, p.Y);
size_t maplen = n->sx * n->sy;
if (use_buffer)
lua_pushvalue(L, 3);
else
lua_createtable(L, maplen, 0);
for (size_t i = 0; i != maplen; i++) {
lua_pushnumber(L, n->result[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
int LuaPerlinNoiseMap::l_get_3d_map(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
size_t i = 0;
LuaPerlinNoiseMap *o = checkobject(L, 1);
v3f p = check_v3f(L, 2);
if (!o->m_is3d)
return 0;
Noise *n = o->noise;
n->perlinMap3D(p.X, p.Y, p.Z);
lua_createtable(L, n->sz, 0);
for (u32 z = 0; z != n->sz; z++) {
lua_createtable(L, n->sy, 0);
for (u32 y = 0; y != n->sy; y++) {
lua_createtable(L, n->sx, 0);
for (u32 x = 0; x != n->sx; x++) {
lua_pushnumber(L, n->result[i++]);
lua_rawseti(L, -2, x + 1);
}
lua_rawseti(L, -2, y + 1);
}
lua_rawseti(L, -2, z + 1);
}
return 1;
}
int LuaPerlinNoiseMap::l_get_3d_map_flat(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoiseMap *o = checkobject(L, 1);
v3f p = check_v3f(L, 2);
bool use_buffer = lua_istable(L, 3);
if (!o->m_is3d)
return 0;
Noise *n = o->noise;
n->perlinMap3D(p.X, p.Y, p.Z);
size_t maplen = n->sx * n->sy * n->sz;
if (use_buffer)
lua_pushvalue(L, 3);
else
lua_createtable(L, maplen, 0);
for (size_t i = 0; i != maplen; i++) {
lua_pushnumber(L, n->result[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
int LuaPerlinNoiseMap::l_calc_2d_map(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoiseMap *o = checkobject(L, 1);
v2f p = readParam<v2f>(L, 2);
Noise *n = o->noise;
n->perlinMap2D(p.X, p.Y);
return 0;
}
int LuaPerlinNoiseMap::l_calc_3d_map(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoiseMap *o = checkobject(L, 1);
v3f p = check_v3f(L, 2);
if (!o->m_is3d)
return 0;
Noise *n = o->noise;
n->perlinMap3D(p.X, p.Y, p.Z);
return 0;
}
int LuaPerlinNoiseMap::l_get_map_slice(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoiseMap *o = checkobject(L, 1);
v3s16 slice_offset = read_v3s16(L, 2);
v3s16 slice_size = read_v3s16(L, 3);
bool use_buffer = lua_istable(L, 4);
Noise *n = o->noise;
if (use_buffer)
lua_pushvalue(L, 4);
else
lua_newtable(L);
write_array_slice_float(L, lua_gettop(L), n->result,
v3u16(n->sx, n->sy, n->sz),
v3u16(slice_offset.X, slice_offset.Y, slice_offset.Z),
v3u16(slice_size.X, slice_size.Y, slice_size.Z));
return 1;
}
int LuaPerlinNoiseMap::create_object(lua_State *Lend
function atc.execute_atc_command(id, train)
--strip whitespaces
local command=string.match(train.atc_command, "^%s*(.*)$")
if string.match(command, "^%s*$") then
train.atc_command=nil
return
end
train.atc_command = eval_conditional(command, train.atc_arrow, train.velocity)
if not train.atc_command then return end
command=string.match(train.atc_command, "^%s*(.*)$")
if string.match(command, "^%s*$") then
train.atc_command=nil
return
end
for pattern, func in pairs(matchptn) do
local match=string.match(command, "^"..pattern)
if match then
local patlen=func(id, train, match)
atprint("Executing: "..string.sub(command, 1, patlen))
train.atc_command=string.sub(command, patlen+1)
if train.atc_delay<=0 and not train.atc_wait_finish then
--continue (recursive, cmds shouldn
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1);
markAliasDeprecated(methods);
luaL_openlib(L, 0, methods, 0);
lua_pop(L, 1);
lua_register(L, className, create_object);
}
const char LuaPerlinNoiseMap::className[] = "PerlinNoiseMap";
luaL_Reg LuaPerlinNoiseMap::methods[] = {
luamethod_aliased(LuaPerlinNoiseMap, get_2d_map, get2dMap),
luamethod_aliased(LuaPerlinNoiseMap, get_2d_map_flat, get2dMap_flat),
luamethod_aliased(LuaPerlinNoiseMap, calc_2d_map, calc2dMap),
luamethod_aliased(LuaPerlinNoiseMap, get_3d_map, get3dMap),
luamethod_aliased(LuaPerlinNoiseMap, get_3d_map_flat, get3dMap_flat),
luamethod_aliased(LuaPerlinNoiseMap, calc_3d_map, calc3dMap),
luamethod_aliased(LuaPerlinNoiseMap, get_map_slice, getMapSlice),
{0,0}
};
///////////////////////////////////////
/*
LuaPseudoRandom
*/
int LuaPseudoRandom::l_next(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPseudoRandom *o = checkobject(L, 1);
int min = 0;
int max = 32767;
lua_settop(L, 3);
if (lua_isnumber(L, 2))
min = luaL_checkinteger(L, 2);
if (lua_isnumber(L, 3))
max = luaL_checkinteger(L, 3);
if (max < min) {
errorstream<<"PseudoRandom.next(): max="<<max<<" min="<<min<<std::endl;
throw LuaError("PseudoRandom.next(): max < min");
}
if(max - min != 32767 && max - min > 32767/5)
throw LuaError("PseudoRandom.next() max-min is not 32767"
" and is > 32768/5. This is disallowed due to"
" the bad random distribution the"
" implementation would otherwise make.");
PseudoRandom &pseudo = o->m_pseudo;
int val = pseudo.next();
val = (val % (max-min+1)) + min;
lua_pushinteger(L, val);
return 1;
}
int LuaPseudoRandom::create_object(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
u64 seed = luaL_checknumber(L, 1);
LuaPseudoRandom *o = new LuaPseudoRandom(seed);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
return 1;
}
int LuaPseudoRandom::gc_object(lua_State *L)
{
LuaPseudoRandom *o = *(LuaPseudoRandom **)(lua_touserdata(L, 1));
delete o;
return 0;
}
LuaPseudoRandom *LuaPseudoRandom::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);
return *(LuaPseudoRandom **)ud;
}
void LuaPseudoRandom::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1);
luaL_openlib(L, 0, methods, 0);
lua_pop(L, 1);
lua_register(L, className, create_object);
}
const char LuaPseudoRandom::className[] = "PseudoRandom";
const luaL_Reg LuaPseudoRandom::methods[] = {
luamethod(LuaPseudoRandom, next),
{0,0}
};
///////////////////////////////////////
/*
LuaPcgRandom
*/
int LuaPcgRandom::l_next(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPcgRandom *o = checkobject(L, 1);
u32 min = lua_isnumber(L, 2) ? lua_tointeger(L, 2) : o->m_rnd.RANDOM_MIN;
u32 max = lua_isnumber(L, 3) ? lua_tointeger(L, 3) : o->m_rnd.RANDOM_MAX;
lua_pushinteger(L, o->m_rnd.range(min, max));
return 1;
}
int LuaPcgRandom::l_rand_normal_dist(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPcgRandom *o = checkobject(L, 1);
u32 min = lua_isnumber(L, 2) ? lua_tointeger(L, 2) : o->m_rnd.RANDOM_MIN;
u32 max = lua_isnumber(L, 3) ? lua_tointeger(L, 3) : o->m_rnd.RANDOM_MAX;
int num_trials = lua_isnumber(L, 4) ? lua_tointeger(L, 4) : 6;
lua_pushinteger(L, o->m_rnd.randNormalDist(min, max, num_trials));
return 1;
}
int LuaPcgRandom::create_object(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
u64 seed = luaL_checknumber(L, 1);
LuaPcgRandom *o = lua_isnumber(L, 2) ?
new LuaPcgRandom(seed, lua_tointeger(L, 2)) :
new LuaPcgRandom(seed);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
return 1;
}
int LuaPcgRandom::gc_object(lua_State *L)
{
LuaPcgRandom *o = *(LuaPcgRandom **)(lua_touserdata(L, 1));
delete o;
return 0;
}
LuaPcgRandom *LuaPcgRandom::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);
return *(LuaPcgRandom **)ud;
}
void LuaPcgRandom::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1);
luaL_openlib(L, 0, methods, 0);
lua_pop(L, 1);
lua_register(L, className, create_object);
}
const char LuaPcgRandom::className[] = "PcgRandom";
const luaL_Reg LuaPcgRandom::methods[] = {
luamethod(LuaPcgRandom, next),
luamethod(LuaPcgRandom, rand_normal_dist),
{0,0}
};
///////////////////////////////////////
/*
LuaSecureRandom
*/
bool LuaSecureRandom::fillRandBuf()
{
return porting::secure_rand_fill_buf(m_rand_buf, RAND_BUF_SIZE);
}
int LuaSecureRandom::l_next_bytes(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaSecureRandom *o = checkobject(L, 1);
u32 count = lua_isnumber(L, 2) ? lua_tointeger(L, 2) : 1;
// Limit count
count = MYMIN(RAND_BUF_SIZE, count);
// Find out whether we can pass directly from our array, or have to do some gluing
size_t count_remaining = RAND_BUF_SIZE - o->m_rand_idx;
if (count_remaining >= count) {
lua_pushlstring(L, o->m_rand_buf + o->m_rand_idx, count);
o->m_rand_idx += count;
} else {
char output_buf[RAND_BUF_SIZE];
// Copy over with what we have left from our current buffer
memcpy(output_buf, o->m_rand_buf + o->m_rand_idx, count_remaining);
// Refill buffer and copy over the remainder of what was requested
o->fillRandBuf();
memcpy(output_buf + count_remaining, o->m_rand_buf, count - count_remaining);
// Update index
o->m_rand_idx = count - count_remaining;
lua_pushlstring(L, output_buf, count);
}
return 1;
}
int LuaSecureRandom::create_object(lua_State *L)
{
LuaSecureRandom *o = new LuaSecureRandom();
// Fail and return nil if we can't securely fill the buffer
if (!o->fillRandBuf()) {
delete o;
return 0;
}
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
return 1;
}
int LuaSecureRandom::gc_object(lua_State *L)
{
LuaSecureRandom *o = *(LuaSecureRandom **)(lua_touserdata(L, 1));
delete o;
return 0;
}
LuaSecureRandom *LuaSecureRandom::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);
return *(LuaSecureRandom **)ud;
}
void LuaSecureRandom::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1);
luaL_openlib(L, 0, methods, 0);
lua_pop(L, 1);
lua_register(L, className, create_object);
}
const char LuaSecureRandom::className[] = "SecureRandom";
const luaL_Reg LuaSecureRandom::methods[] = {
luamethod(LuaSecureRandom, next_bytes),
{0,0}
};
|