summaryrefslogtreecommitdiffstats
path: root/source/cSignEntity.cpp
blob: 883da223ee8b3d62818ad0629bd7e315eb4c2cbe (plain) (blame)
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
#include "cSignEntity.h"
#include "cMCLogger.h"

#include "cPlayer.h"
#include "cClientHandle.h"
#include "cWorld.h"
#include "cChunk.h"
#include "cRoot.h"

#include "packets/cPacket_UpdateSign.h"

#include <json/json.h>

cSignEntity::cSignEntity(ENUM_BLOCK_ID a_BlockType, int a_X, int a_Y, int a_Z)
	: cBlockEntity(a_BlockType, a_X, a_Y, a_Z)
{
}

cSignEntity::~cSignEntity()
{
}

// It don't do anything when 'used'
void cSignEntity::UsedBy( cPlayer & a_Player )
{
	(void)a_Player;
}

void cSignEntity::SetLines( const std::string & a_Line1, const std::string & a_Line2, const std::string & a_Line3, const std::string & a_Line4 )
{
	m_Line[0] = a_Line1;
	m_Line[1] = a_Line2;
	m_Line[2] = a_Line3;
	m_Line[3] = a_Line4;
}

void cSignEntity::SetLine( int a_Index, std::string a_Line )
{
	if( a_Index < 4 && a_Index > -1 )
	{
		m_Line[a_Index] = a_Line;
	}
}

std::string cSignEntity::GetLine( int a_Index )
{
	if( a_Index < 4 && a_Index > -1 )
	{
		return m_Line[a_Index];
	}
	return "";
}

void cSignEntity::SendTo( cClientHandle* a_Client )
{
	cPacket_UpdateSign Sign;
	Sign.m_PosX = m_PosX;
	Sign.m_PosY = (short)m_PosY;
	Sign.m_PosZ = m_PosZ;
	Sign.m_Line1 = m_Line[0];
	Sign.m_Line2 = m_Line[1];
	Sign.m_Line3 = m_Line[2];
	Sign.m_Line4 = m_Line[3];

	if( a_Client ) a_Client->Send( Sign );
	else // broadcast of a_Client == 0
	{
		cWorld* World = cRoot::Get()->GetWorld();
		cChunk* Chunk = World->GetChunkOfBlock( m_PosX, m_PosY, m_PosZ );
		Chunk->Broadcast( Sign );
	}
}

void cSignEntity::WriteToFile(FILE* a_File)
{
	fwrite( &m_BlockType, sizeof( ENUM_BLOCK_ID ), 1, a_File );
	fwrite( &m_PosX, sizeof( int ), 1, a_File );
	fwrite( &m_PosY, sizeof( int ), 1, a_File );
	fwrite( &m_PosZ, sizeof( int ), 1, a_File );

	for( int i = 0; i < 4; i++ )
	{
		short Size = (short)m_Line[i].size();
		fwrite( &Size, sizeof(short), 1, a_File );
		fwrite( m_Line[i].c_str(), Size * sizeof(char), 1, a_File );
	}
}

bool cSignEntity::LoadFromFile(FILE* a_File)
{
	if( fread( &m_PosX, sizeof(int), 1, a_File) != 1 ) { LOGERROR("ERROR READING SIGN FROM FILE"); return false; }
	if( fread( &m_PosY, sizeof(int), 1, a_File) != 1 ) { LOGERROR("ERROR READING SIGN FROM FILE"); return false; }
	if( fread( &m_PosZ, sizeof(int), 1, a_File) != 1 ) { LOGERROR("ERROR READING SIGN FROM FILE"); return false; }

	for( int i = 0; i < 4; i++ )
	{
		short Size = 0;
		if( fread( &Size, sizeof(short), 1, a_File) != 1 ) { LOGERROR("ERROR READING SIGN FROM FILE"); return false; }
		if( Size > 0 )
		{
			char* c_Str = new char[Size];
			if( fread( c_Str, Size * sizeof(char), 1, a_File) != 1 ) { LOGERROR("ERROR READING SIGN FROM FILE"); delete [] c_Str; return false; }
			m_Line[i].assign( c_Str, Size );
			delete [] c_Str;
		}
		LOG("Line %i: %s", i+1, m_Line[i].c_str() );
	}

	return true;
}

bool cSignEntity::LoadFromJson( const Json::Value & a_Value )
{
	m_PosX = a_Value.get("x", 0).asInt();
	m_PosY = a_Value.get("y", 0).asInt();
	m_PosZ = a_Value.get("z", 0).asInt();

	m_Line[0] = a_Value.get("Line1", "").asString();
	m_Line[1] = a_Value.get("Line2", "").asString();
	m_Line[2] = a_Value.get("Line3", "").asString();
	m_Line[3] = a_Value.get("Line4", "").asString();

	return true;
}

void cSignEntity::SaveToJson( Json::Value & a_Value )
{
	a_Value["x"] = m_PosX;
	a_Value["y"] = m_PosY;
	a_Value["z"] = m_PosZ;

	a_Value["Line1"] = m_Line[0];
	a_Value["Line2"] = m_Line[1];
	a_Value["Line3"] = m_Line[2];
	a_Value["Line4"] = m_Line[3];
}