aboutsummaryrefslogtreecommitdiff
path: root/advtrains_luaautomation/init.lua
blob: 0257aef17b1737307e7600d14940e9a5e4a3514d (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
-- advtrains_luaautomation/init.lua
-- Lua automation features for advtrains
-- Uses global table 'atlatc' (AdvTrains_LuaATC)

-- Boilerplate to support localized strings if intllib mod is installed.
if minetest.get_modpath("intllib") then
    atltrans = intllib.Getter()
else
    atltrans = function(s,a,...)a={a,...}return s:gsub("@(%d+)",function(n)return a[tonumber(n)]end)end
end

--Privilege
--Only trusted players should be enabled to build stuff which can break the server.

atlatc = { envs = {}}

minetest.register_privilege("atlatc", { description = "Player can place and modify LUA ATC components. Grant with care! Allows to execute bad LUA code.", give_to_singleplayer = false, default= false })

--assertt helper. error if a variable is not of a type
function assertt(var, typ)
	if type(var)~=typ then
		error("Assertion failed, variable has to be of type "..typ)
	end
end

local mp=minetest.get_modpath("advtrains_luaautomation")
if not mp then
	error("Mod name error: Mod folder is not named 'advtrains_luaautomation'!")
end
dofile(mp.."/environment.lua")
dofile(mp.."/interrupt.lua")
dofile(mp.."/active_common.lua")
dofile(mp.."/atc_rail.lua")
dofile(mp.."/operation_panel.lua")
dofile(mp.."/pcnaming.lua")
if mesecon then
	dofile(mp.."/p_mesecon_iface.lua")
end
dofile(mp.."/chatcmds.lua")


local filename=minetest.get_worldpath().."/advtrains_luaautomation"

function atlatc.load()
	local file, err = io.open(filename, "r")
	if not file then
		minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": "..(err or "Unknown Error"))
	else
		atprint("luaautomation reading file:",filename)
		local tbl = minetest.deserialize(file:read("*a"))
		if type(tbl) == "table" then
			if tbl.version==1 then
				for envname, data in pairs(tbl.envs) do
					atlatc.envs[envname]=atlatc.env_load(envname, data)
				end
				atlatc.active.load(tbl.active)
				atlatc.interrupt.load(tbl.interrupt)
				atlatc.pcnaming.load(tbl.pcnaming)
			end
		else
			minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": Not a table!")
		end
		file:close()
	end
	-- run init code of all environments
	atlatc.run_initcode()
end


atlatc.save = function()
	--versions:
	-- 1 - Initial save format.
	
	local envdata={}
	for envname, env in pairs(atlatc.envs) do
		envdata[envname]=env:save()
	end
	local save_tbl={
		version = 1,
		envs=envdata,
		active = atlatc.active.save(),
		interrupt = atlatc.interrupt.save(),
		pcnaming = atlatc.pcnaming.save(),
	}
	
	local datastr = minetest.serialize(save_tbl)
	if not datastr then
		minetest.log("error", " Failed to save advtrains_luaautomation save data to file "..filename..": Can't serialize!")
		return
	end
	local file, err = io.open(filename, "w")
	if err then
		minetest.log("error", " Failed to save advtrains_luaautomation save data to file "..filename..": "..(err or "Unknown Error"))
		return
	end
	file:write(datastr)
	file:close()
end


-- globalstep for step code
local timer, step_int=0, 2

function atlatc.mainloop_stepcode(dtime)
	timer=timer+dtime
	if timer>step_int then
		timer=0
		atlatc.run_stepcode()
	end
end
span class="hl num">0; } PlayerControl( bool a_up, bool a_down, bool a_left, bool a_right, bool a_jump, bool a_aux1, bool a_sneak, bool a_LMB, bool a_RMB, float a_pitch, float a_yaw ) { up = a_up; down = a_down; left = a_left; right = a_right; jump = a_jump; aux1 = a_aux1; sneak = a_sneak; LMB = a_LMB; RMB = a_RMB; pitch = a_pitch; yaw = a_yaw; } bool up; bool down; bool left; bool right; bool jump; bool aux1; bool sneak; bool LMB; bool RMB; float pitch; float yaw; }; class Map; class IGameDef; struct CollisionInfo; class PlayerSAO; struct HudElement; class Environment; // IMPORTANT: // Do *not* perform an assignment or copy operation on a Player or // RemotePlayer object! This will copy the lock held for HUD synchronization class Player { public: Player(IGameDef *gamedef, const char *name); virtual ~Player() = 0; virtual void move(f32 dtime, Environment *env, f32 pos_max_d) {} virtual void move(f32 dtime, Environment *env, f32 pos_max_d, std::vector<CollisionInfo> *collision_info) {} v3f getSpeed() { return m_speed; } void setSpeed(v3f speed) { m_speed = speed; } v3f getPosition() { return m_position; } v3s16 getLightPosition() const; v3f getEyeOffset() { float eye_height = camera_barely_in_ceiling ? 1.5f : 1.625f; return v3f(0, BS * eye_height, 0); } v3f getEyePosition() { return m_position + getEyeOffset(); } virtual void setPosition(const v3f &position) { if (position != m_position) m_dirty = true; m_position = position; } void setPitch(f32 pitch) { if (pitch != m_pitch) m_dirty = true; m_pitch = pitch; } virtual void setYaw(f32 yaw) { if (yaw != m_yaw) m_dirty = true; m_yaw = yaw; } f32 getPitch() { return m_pitch; } f32 getYaw() { return m_yaw; } u16 getBreath() { return m_breath; } virtual void setBreath(u16 breath) { if (breath != m_breath) m_dirty = true; m_breath = breath; } f32 getRadPitch() { return -1.0 * m_pitch * core::DEGTORAD; } f32 getRadYaw() { return (m_yaw + 90.) * core::DEGTORAD; } const char *getName() const { return m_name; } aabb3f getCollisionbox() { return m_collisionbox; } u32 getFreeHudID() { size_t size = hud.size(); for (size_t i = 0; i != size; i++) { if (!hud[i]) return i; } return size; } void setHotbarItemcount(s32 hotbar_itemcount) { hud_hotbar_itemcount = hotbar_itemcount; } s32 getHotbarItemcount() { return hud_hotbar_itemcount; } void setHotbarImage(const std::string &name) { hud_hotbar_image = name; } std::string getHotbarImage() { return hud_hotbar_image; } void setHotbarSelectedImage(const std::string &name) { hud_hotbar_selected_image = name; } std::string getHotbarSelectedImage() { return hud_hotbar_selected_image; } void setSky(const video::SColor &bgcolor, const std::string &type, const std::vector<std::string> &params) { m_sky_bgcolor = bgcolor; m_sky_type = type; m_sky_params = params; } void getSky(video::SColor *bgcolor, std::string *type, std::vector<std::string> *params) { *bgcolor = m_sky_bgcolor; *type = m_sky_type; *params = m_sky_params; } void overrideDayNightRatio(bool do_override, float ratio) { m_day_night_ratio_do_override = do_override; m_day_night_ratio = ratio; } void getDayNightRatio(bool *do_override, float *ratio) { *do_override = m_day_night_ratio_do_override; *ratio = m_day_night_ratio; } void setLocalAnimations(v2s32 frames[4], float frame_speed) { for (int i = 0; i < 4; i++) local_animations[i] = frames[i]; local_animation_speed = frame_speed; } void getLocalAnimations(v2s32 *frames, float *frame_speed) { for (int i = 0; i < 4; i++) frames[i] = local_animations[i]; *frame_speed = local_animation_speed; } virtual bool isLocal() const { return false; } virtual PlayerSAO *getPlayerSAO() { return NULL; } virtual void setPlayerSAO(PlayerSAO *sao) { FATAL_ERROR("FIXME"); } /* serialize() writes a bunch of text that can contain any characters except a '\0', and such an ending that deSerialize stops reading exactly at the right point. */ void serialize(std::ostream &os); void deSerialize(std::istream &is, std::string playername); bool checkModified() const { return m_dirty || inventory.checkModified(); } void setModified(const bool x) { m_dirty = x; if (x == false) inventory.setModified(x); } // Use a function, if isDead can be defined by other conditions bool isDead() { return hp == 0; } bool got_teleported; bool touching_ground; // This oscillates so that the player jumps a bit above the surface bool in_liquid;