aboutsummaryrefslogtreecommitdiff
path: root/src/debug.h
blob: 3f269176abcf889cf7fe06fff820382efde4b6d0 (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
/*
Minetest-c55
Copyright (C) 2010 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 General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.

You should have received a copy of the GNU 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 DEBUG_HEADER
#define DEBUG_HEADER

#include <stdio.h>
#include <jmutex.h>
#include <jmutexautolock.h>
#include <iostream>
#include "common_irrlicht.h"
#include "threads.h"
#include "gettime.h"

/*
	Debug output
*/

#define DTIME (getTimestamp()+": ")

#define DEBUGSTREAM_COUNT 2

extern FILE *g_debugstreams[DEBUGSTREAM_COUNT];

extern void debugstreams_init(bool disable_stderr, const char *filename);
extern void debugstreams_deinit();

#define DEBUGPRINT(...)\
{\
	for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
	{\
		if(g_debugstreams[i] != NULL){\
			fprintf(g_debugstreams[i], __VA_ARGS__);\
			fflush(g_debugstreams[i]);\
		}\
	}\
}

class Debugbuf : public std::streambuf
{
public:
	Debugbuf(bool disable_stderr)
	{
		m_disable_stderr = disable_stderr;
	}

	int overflow(int c)
	{
		for(int i=0; i<DEBUGSTREAM_COUNT; i++)
		{
			if(g_debugstreams[i] == stderr && m_disable_stderr)
				continue;
			if(g_debugstreams[i] != NULL)
				fwrite(&c, 1, 1, g_debugstreams[i]);
			//TODO: Is this slow?
			fflush(g_debugstreams[i]);
		}
		
		return c;
	}
	int xsputn(const char *s, int n)
	{
		for(int i=0; i<DEBUGSTREAM_COUNT; i++)
		{
			if(g_debugstreams[i] == stderr && m_disable_stderr)
				continue;
			if(g_debugstreams[i] != NULL)
				fwrite(s, 1, n, g_debugstreams[i]);
			//TODO: Is this slow?
			fflush(g_debugstreams[i]);
		}

		return n;
	}
	
private:
	bool m_disable_stderr;
};

// This is used to redirect output to /dev/null
class Nullstream : public std::ostream {
public:
	Nullstream():
		std::ostream(0)
	{
	}
private:
};

extern Debugbuf debugbuf;
extern std::ostream dstream;
extern std::ostream dstream_no_stderr;
extern Nullstream dummyout;

/*
	Assert
*/

__NORETURN extern void assert_fail(
		const char *assertion, const char *file,
		unsigned int line, const char *function);

#define ASSERT(expr)\
	((expr)\
	? (void)(0)\
	: assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))

#define assert(expr) ASSERT(expr)

/*
	DebugStack
*/

#define DEBUG_STACK_SIZE 50
#define DEBUG_STACK_TEXT_SIZE 300

struct DebugStack
{
	DebugStack(threadid_t id);
	void print(FILE *file, bool everything);
	
	threadid_t threadid;
	char stack[DEBUG_STACK_SIZE][DEBUG_STACK_TEXT_SIZE];
	int stack_i; // Points to the lowest empty position
	int stack_max_i; // Highest i that was seen
};

extern core::map<threadid_t, DebugStack*> g_debug_stacks;
extern JMutex g_debug_stacks_mutex;

extern void debug_stacks_init();
extern void debug_stacks_print();

class DebugStacker
{
public:
	DebugStacker(const char *text);
	~DebugStacker();

private:
	DebugStack *m_stack;
	bool m_overflowed;
};

#define DSTACK(...)\
	char __buf[DEBUG_STACK_TEXT_SIZE];\
	snprintf(__buf,\
			DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
	DebugStacker __debug_stacker(__buf);

/*
	Packet counter
*/

class PacketCounter
{
public:
	PacketCounter()
	{
	}

	void add(u16 command)
	{
		core::map<u16, u16>::Node *n = m_packets.find(command);
		if(n == NULL)
		{
			m_packets[command] = 1;
		}
		else
		{
			n->setValue(n->getValue()+1);
		}
	}

	void clear()
	{
		for(core::map<u16, u16>::Iterator
				i = m_packets.getIterator();
				i.atEnd() == false; i++)
		{
			i.getNode()->setValue(0);
		}
	}

	void print(std::ostream &o)
	{
		for(core::map<u16, u16>::Iterator
				i = m_packets.getIterator();
				i.atEnd() == false; i++)
		{
			o<<"cmd "<<i.getNode()->getKey()
					<<" count "<<i.getNode()->getValue()
					<<std::endl;
		}
	}

private:
	// command, count
	core::map<u16, u16> m_packets;
};


#endif // DEBUG_HEADER


> 1.1 there is one or more subset(s) that can be directly connected -> choose the first possibility 2.2 not -> choose the first one and orient straight 2. there's exactly 1 rail around -> choose and orient straight 3. there's no rail around -> set straight ]] local function istrackandbc(pos_p, conn) local tpos = pos_p local cnode=minetest.get_node(advtrains.dirCoordSet(tpos, conn.c)) if advtrains.is_track_and_drives_on(cnode.name, advtrains.all_tracktypes) then local cconns=advtrains.get_track_connections(cnode.name, cnode.param2) return advtrains.conn_matches_to(conn, cconns) end --try the same 1 node below tpos = {x=tpos.x, y=tpos.y-1, z=tpos.z} cnode=minetest.get_node(advtrains.dirCoordSet(tpos, conn.c)) if advtrains.is_track_and_drives_on(cnode.name, advtrains.all_tracktypes) then local cconns=advtrains.get_track_connections(cnode.name, cnode.param2) return advtrains.conn_matches_to(conn, cconns) end return false end function tp.find_already_connected(pos) local dnode=minetest.get_node(pos) local dconns=advtrains.get_track_connections(dnode.name, dnode.param2) local found_conn for connid, conn in ipairs(dconns) do if istrackandbc(pos, conn) then if found_conn then --we found one in previous iteration return true, true --signal that it's connected else found_conn = conn.c end end end return found_conn end function tp.rail_and_can_be_bent(originpos, conn) local pos=advtrains.dirCoordSet(originpos, conn) local newdir=(conn+8)%16 local node=minetest.get_node(pos) if not advtrains.is_track_and_drives_on(node.name, advtrains.all_tracktypes) then return false end local ndef=minetest.registered_nodes[node.name] local nnpref = ndef and ndef.at_nnpref if not nnpref then return false end local tr=tp.tracks[nnpref] if not tr then return false end if not tr.modify[node.name] then --we actually can use this rail, but only if it already points to the desired direction. if advtrains.is_track_and_drives_on(node.name, advtrains.all_tracktypes) then local cconns=advtrains.get_track_connections(node.name, node.param2) return advtrains.conn_matches_to(conn, cconns) end end --rail at other end? local adj1, adj2=tp.find_already_connected(pos) if adj1 and adj2 then return false--dont destroy existing track elseif adj1 and not adj2 then if tr.double_conn[adj1.."_"..newdir] then return true--if exists, connect new rail and old end end return false else if tr.single_conn[newdir] then--just rotate old rail to right orientation return true end return false end end function tp.bend_rail(originpos, conn) local pos=advtrains.dirCoordSet(originpos, conn) local newdir=advtrains.oppd(conn) local node=minetest.get_node(pos) local ndef=minetest.registered_nodes[node.name] local nnpref = ndef and ndef.at_nnpref if not nnpref then return false end local tr=tp.tracks[nnpref] if not tr then return false end --is rail already connected? no need to bend. local conns=advtrains.get_track_connections(node.name, node.param2) if advtrains.conn_matches_to(conn, conns) then return end --rail at other end? local adj1, adj2=tp.find_already_connected(pos) if adj1 and adj2 then return false--dont destroy existing track elseif adj1 and not adj2 then if tr.double_conn[adj1.."_"..newdir] then advtrains.ndb.swap_node(pos, tr.double_conn[adj1.."_"..newdir]) return true--if exists, connect new rail and old end end return false else if tr.single_conn[newdir] then--just rotate old rail to right orientation advtrains.ndb.swap_node(pos, tr.single_conn[newdir]) return true end return false end end function tp.placetrack(pos, nnpref, placer, itemstack, pointed_thing, yaw) --1. find all rails that are likely to be connected local tr=tp.tracks[nnpref] local p_rails={} local p_railpos={} for i=0,15 do if tp.rail_and_can_be_bent(pos, i, nnpref) then p_rails[#p_rails+1]=i p_railpos[i] = pos else local upos = {x=pos.x, y=pos.y-1, z=pos.z} if tp.rail_and_can_be_bent(upos, i, nnpref) then p_rails[#p_rails+1]=i p_railpos[i] = upos end end end -- try double_conn if #p_rails > 1 then --iterate subsets for k1, conn1 in ipairs(p_rails) do for k2, conn2 in ipairs(p_rails) do if k1~=k2 then local dconn1 = tr.double_conn_1 local dconn2 = tr.double_conn_2 if not (advtrains.yawToDirection(yaw, conn1, conn2) == conn1) then dconn1 = tr.double_conn_2 dconn2 = tr.double_conn_1 end -- Checks are made this way round so that dconn1 has priority (this will make arrows of atc rails -- point in the right direction) local using if (dconn2[conn1.."_"..conn2]) then using = dconn2[conn1.."_"..conn2] end if (dconn1[conn1.."_"..conn2]) then using = dconn1[conn1.."_"..conn2] end if using then -- has found a fitting rail in either direction -- if not, continue loop tp.bend_rail(p_railpos[conn1], conn1, nnpref) tp.bend_rail(p_railpos[conn2], conn2, nnpref) advtrains.ndb.swap_node(pos, using) local nname=using.name if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing) end return end end end end end -- try single_conn if #p_rails > 0 then for ix, p_rail in ipairs(p_rails) do local sconn1 = tr.single_conn_1 local sconn2 = tr.single_conn_2 if not (advtrains.yawToDirection(yaw, p_rail, (p_rail+8)%16) == p_rail) then sconn1 = tr.single_conn_2 sconn2 = tr.single_conn_1 end if sconn1[p_rail] then local using = sconn1[p_rail] tp.bend_rail(p_railpos[p_rail], p_rail, nnpref) advtrains.ndb.swap_node(pos, using) local nname=using.name if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing) end return end if sconn2[p_rail] then local using = sconn2[p_rail] tp.bend_rail(p_railpos[p_rail], p_rail, nnpref) advtrains.ndb.swap_node(pos, using) local nname=using.name if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing) end return end end end --use default minetest.set_node(pos, {name=nnpref.."_"..tr.default}) if minetest.registered_nodes[nnpref.."_"..tr.default] and minetest.registered_nodes[nnpref.."_"..tr.default].after_place_node then minetest.registered_nodes[nnpref.."_"..tr.default].after_place_node(pos, placer, itemstack, pointed_thing) end end function tp.register_track_placer(nnprefix, imgprefix, dispname) minetest.register_craftitem(":"..nnprefix.."_placer",{ description = dispname, inventory_image = imgprefix.."_placer.png", wield_image = imgprefix.."_placer.png", groups={advtrains_trackplacer=1, digtron_on_place=1}, on_place = function(itemstack, placer, pointed_thing) return advtrains.pcall(function() local name = placer:get_player_name() if not name then return itemstack, false end if pointed_thing.type=="node" then local pos=pointed_thing.above local upos=vector.subtract(pointed_thing.above, {x=0, y=1, z=0}) if not advtrains.check_track_protection(pos, name) then return itemstack, false end if minetest.registered_nodes[minetest.get_node(pos).name] and minetest.registered_nodes[minetest.get_node(pos).name].buildable_to and minetest.registered_nodes[minetest.get_node(upos).name] and minetest.registered_nodes[minetest.get_node(upos).name].walkable then -- minetest.chat_send_all(nnprefix) local yaw = placer:get_look_horizontal() tp.placetrack(pos, nnprefix, placer, itemstack, pointed_thing, yaw) if not advtrains.is_creative(name) then itemstack:take_item() end end end return itemstack, true end) end, }) end minetest.register_craftitem("advtrains:trackworker",{ description = attrans("Track Worker Tool\n\nLeft-click: change rail type (straight/curve/switch)\nRight-click: rotate rail/bumper/signal/etc."), groups = {cracky=1}, -- key=name, value=rating; rating=1..3. inventory_image = "advtrains_trackworker.png", wield_image = "advtrains_trackworker.png", stack_max = 1, on_place = function(itemstack, placer, pointed_thing) return advtrains.pcall(function() local name = placer:get_player_name() if not name then return end local has_aux1_down = placer:get_player_control().aux1 if pointed_thing.type=="node" then local pos=pointed_thing.under if not advtrains.check_track_protection(pos, name) then return end local node=minetest.get_node(pos) --if not advtrains.is_track_and_drives_on(minetest.get_node(pos).name, advtrains.all_tracktypes) then return end if advtrains.get_train_at_pos(pos) then return end if has_aux1_down then --feature: flip the node by 180° --i've always wanted this! advtrains.ndb.swap_node(pos, {name=node.name, param2=(node.param2+2)%4}) return end local nnprefix, suffix, rotation=string.match(node.name, "^(.+)_([^_]+)(_[^_]+)$") --atdebug(node.name.."\npattern recognizes:"..nnprefix.." / "..suffix.." / "..rotation) --atdebug("nntab: ",tp.tracks[nnprefix]) if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twrotate[suffix] then nnprefix, suffix=string.match(node.name, "^(.+)_([^_]+)$") rotation = "" if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twrotate[suffix] then minetest.chat_send_player(placer:get_player_name(), attrans("This node can't be rotated using the trackworker!")) return end end local modext=tp.tracks[nnprefix].twrotate[suffix] if rotation==modext[#modext] then --increase param2 advtrains.ndb.swap_node(pos, {name=nnprefix.."_"..suffix..modext[1], param2=(node.param2+1)%4}) return else local modpos for k,v in pairs(modext) do if v==rotation then modpos=k end end if not modpos then minetest.chat_send_player(placer:get_player_name(), attrans("This node can't be rotated using the trackworker!")) return end advtrains.ndb.swap_node(pos, {name=nnprefix.."_"..suffix..modext[modpos+1], param2=node.param2}) end end end) end, on_use=function(itemstack, user, pointed_thing) return advtrains.pcall(function() local name = user:get_player_name() if not name then return end if pointed_thing.type=="node" then local pos=pointed_thing.under local node=minetest.get_node(pos) if not advtrains.check_track_protection(pos, name) then return end --if not advtrains.is_track_and_drives_on(minetest.get_node(pos).name, advtrains.all_tracktypes) then return end if advtrains.get_train_at_pos(pos) then return end local nnprefix, suffix, rotation=string.match(node.name, "^(.+)_([^_]+)(_[^_]+)$") --atdebug(node.name.."\npattern recognizes:"..nodeprefix.." / "..railtype.." / "..rotation) if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twcycle[suffix] then nnprefix, suffix=string.match(node.name, "^(.+)_([^_]+)$") rotation = "" if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twcycle[suffix] then minetest.chat_send_player(user:get_player_name(), attrans("This node can't be changed using the trackworker!")) return end end local nextsuffix=tp.tracks[nnprefix].twcycle[suffix] advtrains.ndb.swap_node(pos, {name=nnprefix.."_"..nextsuffix..rotation, param2=node.param2}) else atprint(name, dump(tp.tracks)) end end) end, }) --putting into right place advtrains.trackplacer=tp