aboutsummaryrefslogtreecommitdiff
path: root/advtrains_luaautomation/atc_rail.lua
blob: b90baf8f2363bc624839fbeca4e3bec842820da5 (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
-- atc_rail.lua
-- registers and handles the ATC rail. Active component.
-- This is the only component that can interface with trains, so train interface goes here too.

--Using subtable
local r={}

function r.fire_event(pos, evtdata)
	
	local ph=minetest.pos_to_string(pos)
	local railtbl = atlatc.active.nodes[ph]
	
	if not railtbl then
		atwarn("LuaAutomation ATC interface rail at",ph,": Data not in memory! Please visit position and click 'Save'!")
		return
	end
	
	
	local arrowconn = railtbl.arrowconn
	if not arrowconn then
		atwarn("LuaAutomation ATC interface rail at",ph,": Incomplete Data! Please visit position and click 'Save'!")
		return
	end
	
	--prepare ingame API for ATC. Regenerate each time since pos needs to be known
	--If no train, then return false.
	local train_id=advtrains.detector.get(pos)
	local train, atc_arrow, tvel
	if train_id then train=advtrains.trains[train_id] end
	if train then 
		if not train.path then
			--we happened to get in between an invalidation step
			--delay
			atlatc.interrupt.add(0,pos,evtdata)
			return
		end
		for index, ppos in pairs(train.path) do
			if vector.equals(advtrains.round_vector_floor_y(ppos), pos) then
				atc_arrow =
						vector.equals(
								advtrains.dirCoordSet(pos, arrowconn),
								advtrains.round_vector_floor_y(train.path[index+train.movedir])
						)
			end
		end
		if atc_arrow==nil then
			atwarn("LuaAutomation ATC rail at", pos, ": Rail not on train's path! Can't determine arrow direction. Assuming +!")
			atc_arrow=true
		end
		tvel=train.velocity
	end
	local customfct={
		atc_send = function(cmd)
			if not train_id then return false end
			assertt(cmd, "string")
			advtrains.atc.train_reset_command(train_id)
			train.atc_command=cmd
			train.atc_arrow=atc_arrow
			return true
		end,
		set_line = function(line)
		   train.line = line
		   return true
		end,
		atc_reset = function(cmd)
			if not train_id then return false end
			assertt(cmd, "string")
			advtrains.atc.train_reset_command(train_id)
			return true
		end,
		atc_arrow = atc_arrow,
		atc_id = train_id,
		atc_speed = tvel,
		atc_set_text_outside = function(text)
			if not train_id then return false end
			if text then assertt(text, "string") end
			advtrains.trains[train_id].text_outside=text
			return true
		end,
		atc_set_text_inside = function(text)
			if not train_id then return false end
			if text then assertt(text, "string") end
			advtrains.trains[train_id].text_inside=text
			return true
		end,
	}
	
	atlatc.active.run_in_env(pos, evtdata, customfct)
	
end

advtrains.register_tracks("default", {
	nodename_prefix="advtrains_luaautomation:dtrack",
	texture_prefix="advtrains_dtrack_atc",
	models_prefix="advtrains_dtrack",
	models_suffix=".b3d",
	shared_texture="advtrains_dtrack_shared_atc.png",
	description=atltrans("LuaAutomation ATC Rail"),
	formats={},
	get_additional_definiton = function(def, preset, suffix, rotation)
		return {
			after_place_node = atlatc.active.after_place_node,
			after_dig_node = atlatc.active.after_dig_node,

			on_receive_fields = function(pos, ...)
				atlatc.active.on_receive_fields(pos, ...)
				
				--set arrowconn (for ATC)
				local ph=minetest.pos_to_string(pos)
				local _, conn1=advtrains.get_rail_info_at(pos, advtrains.all_tracktypes)
				atlatc.active.nodes[ph].arrowconn=conn1
			end,

			advtrains = {
				on_train_enter = function(pos, train_id)
					--do async. Event is fired in train steps
					atlatc.interrupt.add(0, pos, {type="train", train=true, id=train_id})
				end,
			},
			luaautomation = {
				fire_event=r.fire_event
			}
		}
	end
}, advtrains.trackpresets.t_30deg_straightonly)


atlatc.rail = r
etName(); private: std::string getPropertyPacket(); void sendPosition(bool do_interpolate, bool is_movement_end); std::string m_init_name; std::string m_init_state; bool m_registered; struct ObjectProperties m_prop; s16 m_hp; v3f m_velocity; v3f m_acceleration; float m_yaw; ItemGroupList m_armor_groups; bool m_properties_sent; float m_last_sent_yaw; v3f m_last_sent_position; v3f m_last_sent_velocity; float m_last_sent_position_timer; float m_last_sent_move_precision; bool m_armor_groups_sent; v2f m_animation_range; float m_animation_speed; float m_animation_blend; bool m_animation_sent; std::map<std::string, core::vector2d<v3f> > m_bone_position; bool m_bone_position_sent; int m_attachment_parent_id; std::string m_attachment_bone; v3f m_attachment_position; v3f m_attachment_rotation; bool m_attachment_sent; }; /* PlayerSAO needs some internals exposed. */ class PlayerSAO : public ServerActiveObject { public: PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_, const std::set<std::string> &privs, bool is_singleplayer); ~PlayerSAO(); u8 getType() const { return ACTIVEOBJECT_TYPE_PLAYER; } u8 getSendType() const { return ACTIVEOBJECT_TYPE_GENERIC; } std::string getDescription(); /* Active object <-> environment interface */ void addedToEnvironment(u32 dtime_s); void removingFromEnvironment(); bool isStaticAllowed() const; bool unlimitedTransferDistance() const; std::string getClientInitializationData(u16 protocol_version); std::string getStaticData(); bool isAttached(); void step(float dtime, bool send_recommended); void setBasePosition(const v3f &position); void setPos(v3f pos); void moveTo(v3f pos, bool continuous); /* Interaction interface */ int punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher, float time_from_last_punch); void rightClick(ServerActiveObject *clicker); s16 getHP() const; void setHP(s16 hp); void setArmorGroups(const ItemGroupList &armor_groups); void setAnimation(v2f frame_range, float frame_speed, float frame_blend); void setBonePosition(std::string bone, v3f position, v3f rotation); void setAttachment(int parent_id, std::string bone, v3f position, v3f rotation); ObjectProperties* accessObjectProperties(); void notifyObjectPropertiesModified(); /* Inventory interface */ Inventory* getInventory(); const Inventory* getInventory() const; InventoryLocation getInventoryLocation() const; void setInventoryModified(); std::string getWieldList() const; int getWieldIndex() const; void setWieldIndex(int i); /* PlayerSAO-specific */ void disconnected(); Player* getPlayer() { return m_player; } u16 getPeerID() const { return m_peer_id; } // Cheat prevention v3f getLastGoodPosition() const { return m_last_good_position; } float resetTimeFromLastPunch() { float r = m_time_from_last_punch; m_time_from_last_punch = 0.0; return r; } void noCheatDigStart(v3s16 p) { m_nocheat_dig_pos = p; m_nocheat_dig_time = 0; } v3s16 getNoCheatDigPos() { return m_nocheat_dig_pos; } float getNoCheatDigTime() { return m_nocheat_dig_time; } void noCheatDigEnd() { m_nocheat_dig_pos = v3s16(32767, 32767, 32767); } // Other void updatePrivileges(const std::set<std::string> &privs, bool is_singleplayer) { m_privs = privs; m_is_singleplayer = is_singleplayer; } private: std::string getPropertyPacket(); Player *m_player; u16 m_peer_id; Inventory *m_inventory; // Cheat prevention v3f m_last_good_position; float m_last_good_position_age; float m_time_from_last_punch; v3s16 m_nocheat_dig_pos; float m_nocheat_dig_time; int m_wield_index; bool m_position_not_sent; ItemGroupList m_armor_groups; bool m_armor_groups_sent; bool m_properties_sent; struct ObjectProperties m_prop; // Cached privileges for enforcement std::set<std::string> m_privs; bool m_is_singleplayer; v2f m_animation_range; float m_animation_speed; float m_animation_blend; bool m_animation_sent; std::map<std::string, core::vector2d<v3f> > m_bone_position; // Stores position and rotation for each bone name bool m_bone_position_sent; int m_attachment_parent_id; std::string m_attachment_bone; v3f m_attachment_position; v3f m_attachment_rotation; bool m_attachment_sent; public: // Some flags used by Server bool m_moved; bool m_inventory_not_sent; bool m_hp_not_sent; bool m_wielded_item_not_sent; }; #endif