aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api/l_nodemeta.cpp
blob: 4f20e56f90bd37dc133a0c71bc4bc4c0cf5009b6 (plain)
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
/*
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_nodemeta.h"
#include "lua_api/l_internal.h"
#include "lua_api/l_inventory.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include "environment.h"
#include "map.h"
#include "nodemetadata.h"



/*
	NodeMetaRef
*/
NodeMetaRef* NodeMetaRef::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 *(NodeMetaRef**)ud;  // unbox pointer
}

NodeMetadata* NodeMetaRef::getmeta(NodeMetaRef *ref, bool auto_create)
{
	NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p);
	if(meta == NULL && auto_create)	{
		meta = new NodeMetadata(ref->m_env->getGameDef());
		if(!ref->m_env->getMap().setNodeMetadata(ref->m_p, meta)) {
			delete meta;
			return NULL;
		}
	}
	return meta;
}

void NodeMetaRef::reportMetadataChange(NodeMetaRef *ref)
{
	// NOTE: This same code is in rollback_interface.cpp
	// Inform other things that the metadata has changed
	v3s16 blockpos = getNodeBlockPos(ref->m_p);
	MapEditEvent event;
	event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
	event.p = blockpos;
	ref->m_env->getMap().dispatchEvent(&event);
	// Set the block to be saved
	MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
	if(block)
		block->raiseModified(MOD_STATE_WRITE_NEEDED,
				"NodeMetaRef::reportMetadataChange");
}

// Exported functions

// garbage collector
int NodeMetaRef::gc_object(lua_State *L) {
	NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
	delete o;
	return 0;
}

// get_string(self, name)
int NodeMetaRef::l_get_string(lua_State *L)
{
	NodeMetaRef *ref = checkobject(L, 1);
	std::string name = luaL_checkstring(L, 2);

	NodeMetadata *meta = getmeta(ref, false);
	if(meta == NULL){
		lua_pushlstring(L, "", 0);
		return 1;
	}
	std::string str = meta->getString(name);
	lua_pushlstring(L, str.c_str(), str.size());
	return 1;
}

// set_string(self, name, var)
int NodeMetaRef::l_set_string(lua_State *L)
{
	NodeMetaRef *ref = checkobject(L, 1);
	std::string name = luaL_checkstring(L, 2);
	size_t len = 0;
	const char *s = lua_tolstring(L, 3, &len);
	std::string str(s, len);

	NodeMetadata *meta = getmeta(ref, !str.empty());
	if(meta == NULL || str == meta->getString(name))
		return 0;
	meta->setString(name, str);
	reportMetadataChange(ref);
	return 0;
}

// get_int(self, name)
int NodeMetaRef::l_get_int(lua_State *L)
{
	NodeMetaRef *ref = checkobject(L, 1);
	std::string name = lua_tostring(L, 2);

	NodeMetadata *meta = getmeta(ref, false);
	if(meta == NULL){
		lua_pushnumber(L, 0);
		return 1;
	}
	std::string str = meta->getString(name);
	lua_pushnumber(L, stoi(str));
	return 1;
}

// set_int(self, name, var)
int NodeMetaRef::l_set_int(lua_State *L)
{
	NodeMetaRef *ref = checkobject(L, 1);
	std::string name = lua_tostring(L, 2);
	int a = lua_tointeger(L, 3);
	std::string str = itos(a);

	NodeMetadata *meta = getmeta(ref, true);
	if(meta == NULL || str == meta->getString(name))
		return 0;
	meta->setString(name, str);
	reportMetadataChange(ref);
	return 0;
}

// get_float(self, name)
int NodeMetaRef::l_get_float(lua_State *L)
{
	NodeMetaRef *ref = checkobject(L, 1);
	std::string name = lua_tostring(L, 2);

	NodeMetadata *meta = getmeta(ref, false);
	if(meta == NULL){
		lua_pushnumber(L, 0);
		return 1;
	}
	std::string str = meta->getString(name);
	lua_pushnumber(L, stof(str));
	return 1;
}

// set_float(self, name, var)
int NodeMetaRef::l_set_float(lua_State *L)
{
	NodeMetaRef *ref = checkobject(L, 1);
	std::string name = lua_tostring(L, 2);
	float a = lua_tonumber(L, 3);
	std::string str = ftos(a);

	NodeMetadata *meta = getmeta(ref, true);
	if(meta == NULL || str == meta->getString(name))
		return 0;
	meta->setString(name, str);
	reportMetadataChange(ref);
	return 0;
}

// get_inventory(self)
int NodeMetaRef::l_get_inventory(lua_State *L)
{
	NodeMetaRef *ref = checkobject(L, 1);
	getmeta(ref, true);  // try to ensure the metadata exists
	InvRef::createNodeMeta(L, ref->m_p);
	return 1;
}

// to_table(self)
int NodeMetaRef::l_to_table(lua_State *L)
{
	NodeMetaRef *ref = checkobject(L, 1);

	NodeMetadata *meta = getmeta(ref, true);
	if(meta == NULL){
		lua_pushnil(L);
		return 1;
	}
	lua_newtable(L);
	// fields
	lua_newtable(L);
	{
		std::map<std::string, std::string> fields = meta->getStrings();
		for(std::map<std::string, std::string>::const_iterator
				i = fields.begin(); i != fields.end(); i++){
			const std::string &name = i->first;
			const std::string &value = i->second;
			lua_pushlstring(L, name.c_str(), name.size());
			lua_pushlstring(L, value.c_str(), value.size());
			lua_settable(L, -3);
		}
	}
	lua_setfield(L, -2, "fields");
	// inventory
	lua_newtable(L);
	Inventory *inv = meta->getInventory();
	if(inv){
		std::vector<const InventoryList*> lists = inv->getLists();
		for(std::vector<const InventoryList*>::const_iterator
				i = lists.begin(); i != lists.end(); i++){
			push_inventory_list(L, inv, (*i)->getName().c_str());
			lua_setfield(L, -2, (*i)->getName().c_str());
		}
	}
	lua_setfield(L, -2, "inventory");
	return 1;
}

// from_table(self, table)
int NodeMetaRef::l_from_table(lua_State *L)
{
	NodeMetaRef *ref = checkobject(L, 1);
	int base = 2;

	// clear old metadata first
	ref->m_env->getMap().removeNodeMetadata(ref->m_p);

	if(lua_isnil(L, base)){
		// No metadata
		lua_pushboolean(L, true);
		return 1;
	}

	// Create new metadata
	NodeMetadata *meta = getmeta(ref, true);
	if(meta == NULL){
		lua_pushboolean(L, false);
		return 1;
	}
	// Set fields
	lua_getfield(L, base, "fields");
	int fieldstable = lua_gettop(L);
	lua_pushnil(L);
	while(lua_next(L, fieldstable) != 0){
		// key at index -2 and value at index -1
		std::string name = lua_tostring(L, -2);
		size_t cl;
		const char *cs = lua_tolstring(L, -1, &cl);
		std::string value(cs, cl);
		meta->setString(name, value);
		lua_pop(L, 1); // removes value, keeps key for next iteration
	}
	// Set inventory
	Inventory *inv = meta->getInventory();
	lua_getfield(L, base, "inventory");
	int inventorytable = lua_gettop(L);
	lua_pushnil(L);
	while(lua_next(L, inventorytable) != 0){
		// key at index -2 and value at index -1
		std::string name = lua_tostring(L, -2);
		read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
		lua_pop(L, 1); // removes value, keeps key for next iteration
	}
	reportMetadataChange(ref);
	lua_pushboolean(L, true);
	return 1;
}


NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
	m_p(p),
	m_env(env)
{
}

NodeMetaRef::~NodeMetaRef()
{
}

// Creates an NodeMetaRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
{
	NodeMetaRef *o = new NodeMetaRef(p, env);
	//infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
	*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
	luaL_getmetatable(L, className);
	lua_setmetatable(L, -2);
}

void NodeMetaRef::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);  // hide metatable from Lua getmetatable()

	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);  // drop metatable

	luaL_openlib(L, 0, methods, 0);  // fill methodtable
	lua_pop(L, 1);  // drop methodtable

	// Cannot be created from Lua
	//lua_register(L, className, create_object);
}

const char NodeMetaRef::className[] = "NodeMetaRef";
const luaL_reg NodeMetaRef::methods[] = {
	luamethod(NodeMetaRef, get_string),
	luamethod(NodeMetaRef, set_string),
	luamethod(NodeMetaRef, get_int),
	luamethod(NodeMetaRef, set_int),
	luamethod(NodeMetaRef, get_float),
	luamethod(NodeMetaRef, set_float),
	luamethod(NodeMetaRef, get_inventory),
	luamethod(NodeMetaRef, to_table),
	luamethod(NodeMetaRef, from_table),
	{0,0}
};
span>):format(rwt.to_string(timenow), rwt.to_string(arroff, true)) depts = ("%s (%s)"):format(rwt.to_string(timenow+interval), rwt.to_string(arroff+interval, true)) else interval = 15 arrts = rwt.to_string(timenow) depts = rwt.to_string(timenow+interval) end --atc_send(string.format("A0B0WO%sD%dOCD1%sA1S%s", door, interval, (ret and "R" or ""), tostring(depspeed or "M"))) atc_send(("B0WO%s"):format(door)) local nxt = t[i][2] local intext_nxt = "" local nxtdisp = "" if F.stnlist[nxt] then intext_nxt = F.stnlist[nxt] and ("Next station: %s (Track: %s)"):format(F.stnlist[nxt], t[i][3] or "?") or "" nxtdisp = intext_nxt end local intext = ("%s\n%s\nArrival: %s\nDeparture: %s"):format(F.stnlist[stn] or stn, intext_nxt, arrts, depts) local outtext = F.lines[l].name or l if t[i].dest then chout = true outtext = outtext..": "..t[i].dest elseif t[i][4] then outtext = outtext..": "..(F.stnlist[t[i][4]] or t[i][4]) end t[i][5] = t[i][5] or (timenow-(F.depint[l] or 300)) -- note that the "average" time is weighted t[i][6] = t[i][6] and (t[i][6]+timenow-t[i][5]+30)/2 or (timenow-t[i][5]) t[i][5] = timenow atc_set_text_inside(intext) if chout then if type(chout) == "string" then atc_set_text_outside(chout) else atc_set_text_outside(outtext) end end S.trains[atc_id] = { v = 2, l = (get_line() or ""), s = stn, t = (trk or "N/A"), ts = os.time(), mode = "ARR", } schedule_in(interval, {intext = nxtdisp, depart = true}) break end end if (not stop) then atc_send((ret and "BBWR" or "").."A1S"..(depspeed or "M")) end elseif event.type == "schedule" then if atc_id then local msg = event.msg or {} if msg.intext then atc_set_text_inside(msg.intext) end atc_send("OCD1"..((ret and atc_arrow) and "B0WR" or "").."A1S"..(depspeed or "M")) S.trains[atc_id] = { v = 2, l = get_line() or "", s = stn, t = trk or "N/A", ts = os.time(), mode = "DEP", } end end end function F.tram2gen(stn,trk,ret) return F.stn2gen(stn,trk,"C",ret,true,8) end function F.disp2gen(stn) if not stn then return end if not F.stndet[stn] then return end local s = F.stndet[stn] local sn = F.stnlist[stn] or stn local d={{ {sn.."\nTrack Line"}, {(string.sub(sn, 28, 54) or "").." \nDestination"}, {" \nEstimated Arrival"} }} local c=2 local t={} for i,_ in pairs(s) do t[#t+1]=i end table.sort(t) for i = 1,#t,1 do local trkname = t[i] local trk = s[trkname] for j = 1,#trk,1 do local det = trk[j] local r = (c%4==0) and (c/4+1) or ((c-c%4)/4+1) if not d[r] then d[r]={{},{},{}} end d[r][1][#d[r][1]+1] = string.format("%-8s %s", tostring(trkname), F.lines[det[1]].name) d[r][2][#d[r][2]+1] = (F.lines[det[1]].ring and "Ring Line" or (F.stnlist[det[4]] or det[4] or "")) c=c+1 local earr = (det[5] or rwt.to_secs(rwt.now()))+(det[6] or F.depint[det[1]] or 600) d[r][3][#d[r][3]+1] = rwt.to_string(earr) end end for i = 1, #d, 1 do for j = 1, #d[i], 1 do digiline_send("d_"..tostring(i).."_"..tostring(j), table.concat(d[i][j],"\n")) end end end --function F.disp2gen() end function F.pdisp(stn, trk, dn) if not (stn and trk and dn) then return end if not F.stndet[stn] or not F.stndet[stn][trk] then return end if not S.pd[stn] then S.pd[stn] = { [trk] = {[dn] = 0} } end if not S.pd[stn][trk] then S.pd[stn][trk] = {[dn] = 0} end if not S.pd[stn][trk][dn] then S.pd[stn][trk][dn] = 0 end if not S.pdt[stn] then S.pdt[stn] = {} end if not S.pdt[stn][trk] then local t = {} for i = 1, #F.stndet[stn][trk], 1 do local v = F.stndet[stn][trk][i] t[#t+1] = ("Track: %s\n%s\nTo: %s\nNext: %s"):format( trk, F.lines[v[1]].name, F.stnlist[v[4] or "N/A"] or v[4] or "N/A", F.stnlist[v[2] or "N/A"] or v[2] or "N/A") end S.pdt[stn][trk] = t end local n = S.pd[stn][trk][dn] + 1 local t = S.pdt[stn][trk] digiline_send("track"..trk,t[n]) if n >= #t then n = 0 end S.pd[stn][trk][dn] = n end function F.dirsign(stn, trk, ln, arrow) if event.type == "init" or event.type == "punch" then if not (stn and trk and ln and arrow) then return end local t = F.stndet[stn] if not t then return end t = t[trk] if not t then return end for i = 1, #t, 1 do if t[i][1] == ln then digiline_send("lcd",("%s T. %s | %s | %s"):format(arrow, trk, F.lines[ln].name, F.stnlist[t[i][4]])) break end end end end function F.eval(expr, rettrue, retfalse) if expr then return rettrue else return retfalse end end function F.checkpoint(name,arrow,opp) if event.train then S.trains[atc_id] = "C "..name.." " if atc_arrow then S.trains[atc_id] = S.trains[atc_id]..arrow else S.trains[atc_id] = S.trains[atc_id]..opp end end end function F.atc (cmd, intext, outtext) if event.type == "train" then if atc_arrow then if cmd then atc_send (cmd) end if intext then atc_set_text_inside (intext) end if outtext then atc_set_text_outside (outtext) end end end end F.stnlist = { ["N/A"] = "N/A", Bts = "Berton St.", cg = "Colored Grasses", clockwise = "Clockwise", counterclockwise = "Counterclockwise", cras = "ARSE7's Shop", crbfost = "Station East", crbfsm = "Station St. Central", crbfso = "Station St. East", crbfsw = "Station St. West", crch = "Crossroads City Hall", crchs = "City Hall South", crmtrail = "Mountain Railway Terminus", crshelter1 = "Shelter I", crsfterm = "South Forest St. Terminal", crsmacker = "Smacker's Station", crwm = "West Mountains", elchateau = "Chateau d'Erstazi", elgp = "Greener Pastures", elsf = "Salt Factory", evo = "EVO", grsc = "Grassy Scarp", krasnograd = "Krasnograd-FTNT", mushroom = "Mushroom Land", neverbuild = "Neverbuild", nvbcentral = "Neverbuild Central", nvbold = "Old Terminus", nvboutskirts = "Neverbuild Outskirts", ["NRG-bplatz"] = "Berliner Platz", ["NRG-CW"] = "Dörfle", ["NRG-harbor"] = "Hafengebiet", ["NRG-krstr"] = "Krasnograder Straße", ["NRG-museum"] = "Museum", ["NRG-pek"] = "Pekinger Straße", ["NRG-south"] = "Südbahnhof", ["NRG-townhall"] = "Rathaus", ["NRG-townhall2"] = "Hintere Rathausstraße", ["NRG-yard"] = "Betriebshof", oc = "Ocean City", occh = "City Hall", occrt = "CRT Office", ocmushroom = "Mushroom Market", ocoutskirts = "Ocean City Outskirts", phsc = "Southern Crossing", phwest = "Personhood West", scc = "Silver Coast Central", scn = "Silver Coast North", scs = "Silver Coast South", ["SWALD-harbor"] = "Sakurawäldle Hafen", ["SWALD-south"] = "Sakurawäldle Süd", ["SWALD-stn"] = "Sakurawäldle", ["SWALD-west"] = "Sakurawäldle West", thecube = "The Cube", ["TSV-island"] = "West Island", ["TSV-mountain"] = "Mountain Cabin", ["TSV-pond"] = "Rexan Road", ["TSV-sharbor"] = "South Harbor", } for k, v in pairs{[1] = "Station",} do for _, i in pairs{6,} do F.stnlist[string.format("crc_%d_%d", k, i)] = string.format("%d %s St.", i, v) end end for k, v in pairs{[5] = "Fifth", [6] = "Sixth", [7] = "Seventh", [8] = "Eighth",} do for l, w in pairs{[1] = "First", [3] = "Third", [5] = "Fifth",} do F.stnlist[string.format("crs_%d_%d", k, l)] = string.format("%s St./%s Alley", v, w) end end F.lines = { ["AB"] = { name = "ATL-B Commuter", short = "ATL-B/C", ring = false, interval = 240, [1] = { {"crch","2"}, --[[ {"crwm","3"},]] {"scs","N1"}, {"scc","N1"}, {"scn","N1"}, {"cg","1"}, {"thecube","4"}, --[[{"phsc","N"},]] {"grsc", "4"}, {"phwest","1"} }, [2] = { {"phwest","1"}, {"grsc", "3"}, --[[{"phsc","S"},]] {"thecube","1"}, {"cg","2"}, {"scn","S1"}, {"scc","S1"}, {"scs","S1"}, --[[ {"crwm","4"}, ]] {"crch","2"} }, }, ["ABE"] = { name = "ATL-B Express", short = "ATL-B/E", ring = false, interval = 240, [1] = {{"crch","1"}, {"scc","N2"}, {"cg","1"}, {"thecube", "4"}, {"phwest", "2"}}, [2] = {{"phwest", "2"}, {"thecube", "1"}, {"cg", "2"}, {"scc", "S2"}, {"crch", "1"}}, }, ["AZ"] = { name = "ATL-Z", short = "ATL-Z", ring = false, interval = 480, [1] = { {"Bts","3"}, {"evo","N/A"}, {"scs","N1"}, {"scc","N1"}, {"scn","N1"}, {"oc","K1"}, --[[{"elgp","N"}, {"elsf","E"}, {"elchateau","S"}]] }, [2] = { --[[{"elchateau","S"}, {"elsf","W"}, {"elgp","S"},]] {"oc","K1"}, {"scn","S1"}, {"scc", "S1"}, {"scs","S1"}, {"evo","N/A"}, {"Bts","3"}, }, }, ["CRT1"] = { name = "1", ring = false, [1] = { {"crsmacker", "R3"}, --[[{"crch", "U1"},]] {"crchs", "W"}, {"cras", "N"} }, [2] = { {"cras", "N"}, {"crchs", "E"}, --[[{"crch", "U2"},]] {"crsmacker", "R3"} }, }, ["CRT4"] = { name = "4", ring = false, rtt = 120, interval = 60, [1] = { {"crch", "11", 0}, {"crsfterm", "1N", 0}, }, [2] = { {"crsfterm", "1N", 0}, {"crch", "11", 0}, }, }, ["CRT12"] = { name = "12", ring = false, [1] = {{"crch", "M"}, {"crwm", "3"}}, [2] = {{"crwm", "3"}, {"crch", "M"}}, }, ["CRT21"] = { name = "21", ring = false, rtt = 120, interval = 120, [1] = { {"crch", "12", 15}, {"crshelter1", "T", 75}, }, [2] = { {"crshelter1", "T", 75}, {"crch", "12", 15}, }, }, ["NRG1"] = { name = "1", ring = "counterclockwise", rtt = 120, interval = 150, [1] = { {"NRG-townhall","E",50}, {"NRG-south","N",90}, {"NRG-museum","S", 120}, }, }, ["NRG2"] = { name = "2", ring = false, rtt = 300, interval = 300, [1] = { {"NRG-harbor", "S", 35}, {"NRG-townhall2", "W", 70}, {"NRG-yard", "W", 145}, }, [2] = { {"NRG-yard", "W", 145}, {"NRG-museum", "S", 210}, {"NRG-townhall", "E", 255}, {"NRG-bplatz", "S", 295}, {"NRG-harbor", "S", 335}, }, }, ["NRG3"] = { name = "3", ring = false, rtt = 1200, interval = 300, [1] = { {"TSV-pond", "T", 40, dest = "New Roses Gardens\nvia Sakurawäldle, Krasnograd"}, {"TSV-sharbor", "S", 95}, {"TSV-island", "W", 145}, {"TSV-mountain", "W", 200}, {"SWALD-harbor", "W", 305, dest = "New Roses Gardens\nvia Krasnograd"}, {"SWALD-south", "W", 345}, {"SWALD-stn", "N", 385}, {"SWALD-west", "W", 415}, {"krasnograd", "2", 495, dest = "Betriebshof"}, {"NRG-pek", "2", 535}, {"NRG-CW", "S", 580}, {"NRG-krstr", "S", 610}, {"NRG-yard", "W", 640}, }, [2] = { {"NRG-yard", "W", 640, dest = "Tung Sha Tsuen\nvia Krasnograd, Sakurawäldle"}, {"NRG-krstr", "N", 670}, {"NRG-CW", "N", 700}, {"NRG-pek", "1", 740}, {"krasnograd", "1", 795, dest = "Tung Sha Tsuen\nvia Sakurawäldle"}, {"SWALD-west", "E", 875, dest = "Tung Sha Tsuen\nRexan Road"}, {"SWALD-stn", "S", 905}, {"SWALD-south", "E", 945}, {"SWALD-harbor", "E", 985}, {"TSV-mountain", "E", 1090, dest = "Rexan Road"}, {"TSV-island", "E", 1145}, {"TSV-sharbor", "N", 1195}, {"TSV-pond", "T", 1240}, }, }, } for k, v in pairs{["B"] = {6, 5, 3, 20},} do local t = {{"crc_1_"..v[1], "E", v[4]}} for i = 5, 8 do t[#t+1] = {string.format("crs_%d_%d", i, v[2]),"S", v[4] + 45 + (i-5)*30} end for i = 8, 5, -1 do t[#t+1] = {string.format("crs_%d_%d", i, v[3]),"N", v[4] + 175 + (8-i)*30} end F.lines["CRT1"..k] = { name = "1"..k, ring = "clockwise", rtt = 320, interval = 80, [1] = t } end for k, v in pairs{["A"] = {5}, ["B"]={6}, ["C"]={7}, ["D"]={8}} do local t = {} local u = {} local phase = ((v[1]-5)%2)*15 t[1] = {"crsfterm", (v[1]-4).."S", phase} for i = 1, 5, 2 do t[#t+1] = {string.format("crs_%d_%d",v[1],i), "E", 0} end for i = 5, 1, -2 do u[#u+1] = {string.format("crs_%d_%d",v[1],i), "W", 0} end u[#u+1] = {"crsfterm", (v[1]-4).."S", phase} F.lines["CRT3"..k] = { name = "3"..k, ring = false, rtt = 30, interval = 30, [1] = t, [2] = u, } end F.stndet = {} for i, l in pairs(F.lines) do for j = 1, #l, 1 do local dir = l[j] for k = 1, #dir, 1 do local s = dir[k][1] local t = dir[k][2] local o = dir[k][3] local dest = dir[k].dest if not F.stndet[s] then F.stndet[s] = {} end if not F.stndet[s][t] then F.stndet[s][t] = {} end local det = F.stndet[s][t] if k~=#dir or l.ring then local nxt = dir[k==#dir and 1 or k+1] det[#det+1] = {i, nxt[1], nxt[2], l.ring or dir[#dir][1], nil, nil, o, dest = dest} else local addent = true for m = 1, #l, 1 do addent = addent and (l[m][1]==dir[k]) end if addent then det[#det+1] = {i, nil, nil, s, nil, nil, o, dest = dest} end end end end end F.errlist = { ["incorrect_setup"] = "Incorrect LuaATC track setup!", ["runaway_train"] = "Runaway train found!", ["train_disappeared"] = "Train has disappeared!", ["unexpected_train"] = "Train is not expected to pass!", ["wrong_direction"] = "Train passed in wrong direction!", } F.depint = { ["AB"] = 90, ["AG"] = 120, ["CRT1"] = 30, } F.dpts = { ['CRT1'] = POS(1835,5,1000), ["CRT2"] = POS(1741,-1,1020), ['AG'] = POS(1399,4,2020), } function F.odd(ln) if event.type=='ext_int' then if atc_id then set_rc(event.message.rc) atc_send('SM') else