aboutsummaryrefslogtreecommitdiff
path: root/src/database-leveldb.h
blob: 5408f4ce6cdf124071d9a4d0e066667a1426c337 (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
/*
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.
*/

#ifndef DATABASE_LEVELDB_HEADER
#define DATABASE_LEVELDB_HEADER

#include "config.h"

#if USE_LEVELDB

#include "database.h"
#include "leveldb/db.h"
#include <string>

class ServerMap;

class Database_LevelDB : public Database
{
public:
	Database_LevelDB(ServerMap *map, std::string savedir);
	virtual void beginSave();
	virtual void endSave();
        virtual void saveBlock(MapBlock *block);
        virtual MapBlock* loadBlock(v3s16 blockpos);
        virtual void listAllLoadableBlocks(std::list<v3s16> &dst);
        virtual int Initialized(void);
	~Database_LevelDB();
private:
	ServerMap *srvmap;
	leveldb::DB* m_database;
};
#endif
#endif
a id='n169' href='#n169'>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
--atc.lua
--registers and controls the ATC system

local atc={}
-- ATC persistence table. advtrains.atc is created by init.lua when it loads the save file.
atc.controllers = {}
function atc.load_data(data)
	atc.controllers = data and data.controllers or {}
end
function atc.save_data()
	return {controllers = atc.controllers}
end
--contents: {command="...", arrowconn=0-15 where arrow points}

--call from advtrains.detector subprogram

function atc.trigger_controller_train_enter(pos, train_id)
	atc.send_command(pos)
end

--general

function atc.send_command(pos)
	local pts=minetest.pos_to_string(pos)
	if atc.controllers[pts] then
		--atprint("Called send_command at "..pts)
		local train_id = advtrains.detector.on_node[pts]
		if train_id then
			if advtrains.trains[train_id] then
				--atprint("send_command inside if: "..sid(train_id))
				atc.train_reset_command(train_id)
				local arrowconn=atc.controllers[pts].arrowconn
				local train=advtrains.trains[train_id]
				for index, ppos in pairs(train.path) do
					if vector.equals(advtrains.round_vector_floor_y(ppos), pos) then
						advtrains.trains[train_id].atc_arrow =
								vector.equals(
										advtrains.dirCoordSet(pos, arrowconn),
										advtrains.round_vector_floor_y(train.path[index+train.movedir])
								)
						advtrains.trains[train_id].atc_command=atc.controllers[pts].command
						atprint("Sending ATC Command: "..atc.controllers[pts].command)
					end
				end
			end
		end
	end
	return false
end

function atc.train_reset_command(train_id)
	advtrains.trains[train_id].atc_command=nil
	advtrains.trains[train_id].atc_delay=0
	advtrains.trains[train_id].atc_brake_target=nil
	advtrains.trains[train_id].atc_wait_finish=nil
	advtrains.trains[train_id].atc_arrow=nil
end

--nodes
local idxtrans={static=1, mesecon=2, digiline=3}
local apn_func=function(pos, node)
	advtrains.ndb.update(pos, node)
	local meta=minetest.get_meta(pos)
	if meta then
		meta:set_string("infotext", "ATC controller, unconfigured.")
		meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta))
	end
end

advtrains.register_tracks("default", {
	nodename_prefix="advtrains:dtrack_atc",
	texture_prefix="advtrains_dtrack_atc",
	models_prefix="advtrains_dtrack_detector",
	models_suffix=".b3d",
	shared_texture="advtrains_dtrack_rail_atc.png",
	description="ATC controller",
	formats={},
	get_additional_definiton = function(def, preset, suffix, rotation)
		return {
			after_place_node=apn_func,
			after_dig_node=function(pos)
				advtrains.invalidate_all_paths()
				advtrains.ndb.clear(pos)
				local pts=minetest.pos_to_string(pos)
				atc.controllers[pts]=nil
			end,
			on_receive_fields = function(pos, formname, fields, player)
				if minetest.is_protected(pos, player:get_player_name()) then
					minetest.chat_send_player(player:get_player_name(), "This position is protected!")
					return
				end
				local meta=minetest.get_meta(pos)
				if meta then
					if not fields.save then 
						--maybe only the dropdown changed
						if fields.mode then
							meta:set_string("mode", idxtrans[fields.mode])
							meta:set_string("infotext", "ATC controller, mode "..fields.mode.."\n"..( fields.mode=="digiline" and "Channel: "..meta:get_string("channel") or "Command: "..meta:get_string("command") ) )
							meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta))
						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)
					meta:set_string("infotext", "ATC controller, mode "..fields.mode.."\n"..( fields.mode=="digiline" and "Channel: "..meta:get_string("channel") or "Command: "..meta:get_string("command") ) )
					meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta))
					
					local pts=minetest.pos_to_string(pos)
					local _, conn1=advtrains.get_rail_info_at(pos, advtrains.all_tracktypes)
					atc.controllers[pts]={command=fields.command, arrowconn=conn1}
					atc.send_command(pos)
				end
			end,
		}
	end
}, advtrains.trackpresets.t_30deg_straightonly)


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;Command;"..minetest.formspec_escape(command).."]"
		if tonumber(mode)==2 then
			formspec=formspec.."field[0.5,3;7,1;command_on;Command (on);"..minetest.formspec_escape(command_on).."]"
		end
	else
		formspec=formspec.."field[0.5,1.5;7,1;channel;Digiline channel;"..minetest.formspec_escape(channel).."]"
	end
	return formspec.."button_exit[0.5,4.5;7,1;save;Save]"
end

--from trainlogic.lua train step
local matchptn={
	["SM"]=function(id, train)
		train.tarvelocity=train.max_speed
		return 2
	end,
	["S([0-9]+)"]=function(id, train, match)
		train.tarvelocity=tonumber(match)
		return #match+1
	end,
	["B([0-9]+)"]=function(id, train, match)
		if train.velocity>tonumber(match) then
			train.atc_brake_target=tonumber(match)
			if train.tarvelocity>train.atc_brake_target then
				train.tarvelocity=train.atc_brake_target
			end
		end
		return #match+1
	end,
	["W"]=function(id, train)
		train.atc_wait_finish=true
		return 1
	end,
	["D([0-9]+)"]=function(id, train, match)
		train.atc_delay=tonumber(match)
		return #match+1
	end,
	["R"]=function(id, train)
		if train.velocity<=0 then
			train.movedir=train.movedir*-1
			train.atc_arrow = not train.atc_arrow
		else
			minetest.chat_send_all("ATC Reverse command warning: didn't reverse train!")
		end
		return 1
	end,
	["O([LRC])"]=function(id, train, match)
		local tt={L=-1, R=1, C=0}
		local arr=train.atc_arrow and 1 or -1
		train.door_open = tt[match]*arr*train.movedir
		return 2
	end,
}

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
	--conditional statement?
	local is_cond, cond_applies
	local cond, rest=string.match(command, "^I([%+%-])(.+)$")
	if cond then
		is_cond=true
		if cond=="+" then
			cond_applies=train.atc_arrow
		end
		if cond=="-" then
			cond_applies=not train.atc_arrow