Skip to content

Commit 280946b

Browse files
committedJun 28, 2013
Dont write player files all the time
1 parent 9e100bc commit 280946b

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed
 

Diff for: ‎src/environment.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ void ServerEnvironment::serializePlayers(const std::string &savedir)
434434
//infostream<<"Found matching player, overwriting."<<std::endl;
435435

436436
// OK, found. Save player there.
437+
if(player->checkModified())
437438
{
438439
// Open file and serialize
439440
std::ofstream os(path.c_str(), std::ios_base::binary);
@@ -444,6 +445,8 @@ void ServerEnvironment::serializePlayers(const std::string &savedir)
444445
}
445446
player->serialize(os);
446447
saved_players.insert(player);
448+
} else {
449+
saved_players.insert(player);
447450
}
448451
}
449452

Diff for: ‎src/inventory.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,26 @@ InventoryList & InventoryList::operator = (const InventoryList &other)
562562
return *this;
563563
}
564564

565+
bool InventoryList::operator == (const InventoryList &other)
566+
{
567+
if(m_size != other.m_size)
568+
return false;
569+
if(m_width != other.m_width)
570+
return false;
571+
if(m_name != other.m_name)
572+
return false;
573+
for(u32 i=0; i<m_items.size(); i++)
574+
{
575+
ItemStack s1 = m_items[i];
576+
ItemStack s2 = other.m_items[i];
577+
if(s1.name != s2.name || s1.wear!= s2.wear || s1.count != s2.count ||
578+
s1.metadata != s2.metadata)
579+
return false;
580+
}
581+
582+
return true;
583+
}
584+
565585
const std::string &InventoryList::getName() const
566586
{
567587
return m_name;
@@ -855,6 +875,19 @@ Inventory & Inventory::operator = (const Inventory &other)
855875
return *this;
856876
}
857877

878+
bool Inventory::operator == (const Inventory &other)
879+
{
880+
if(m_lists.size() != other.m_lists.size())
881+
return false;
882+
883+
for(u32 i=0; i<m_lists.size(); i++)
884+
{
885+
if(m_lists[i] != other.m_lists[i])
886+
return false;
887+
}
888+
return true;
889+
}
890+
858891
void Inventory::serialize(std::ostream &os) const
859892
{
860893
for(u32 i=0; i<m_lists.size(); i++)

Diff for: ‎src/inventory.h

+2
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class InventoryList
183183

184184
InventoryList(const InventoryList &other);
185185
InventoryList & operator = (const InventoryList &other);
186+
bool operator == (const InventoryList &other);
186187

187188
const std::string &getName() const;
188189
u32 getSize() const;
@@ -258,6 +259,7 @@ class Inventory
258259
Inventory(IItemDefManager *itemdef);
259260
Inventory(const Inventory &other);
260261
Inventory & operator = (const Inventory &other);
262+
bool operator == (const Inventory &other);
261263

262264
void serialize(std::ostream &os) const;
263265
void deSerialize(std::istream &is);

Diff for: ‎src/player.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ Player::Player(IGameDef *gamedef):
4444
m_yaw(0),
4545
m_speed(0,0,0),
4646
m_position(0,0,0),
47-
m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.55,BS*0.30)
47+
m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.55,BS*0.30),
48+
m_last_pitch(0),
49+
m_last_yaw(0),
50+
m_last_pos(0,0,0),
51+
m_last_hp(PLAYER_MAX_HP),
52+
m_last_inventory(gamedef->idef())
4853
{
4954
updateName("<not set>");
5055
inventory.clear();
@@ -53,6 +58,7 @@ Player::Player(IGameDef *gamedef):
5358
craft->setWidth(3);
5459
inventory.addList("craftpreview", 1);
5560
inventory.addList("craftresult", 1);
61+
m_last_inventory = inventory;
5662

5763
// Can be redefined via Lua
5864
inventory_formspec = "size[8,7.5]"
@@ -224,6 +230,9 @@ void Player::deSerialize(std::istream &is, std::string playername)
224230
inventory.getList("craftresult")->changeItem(0, ItemStack());
225231
}
226232
}
233+
234+
// Set m_last_*
235+
checkModified();
227236
}
228237

229238
/*

Diff for: ‎src/player.h

+23
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ class Player
199199
void serialize(std::ostream &os);
200200
void deSerialize(std::istream &is, std::string playername);
201201

202+
bool checkModified()
203+
{
204+
if(m_last_hp != hp || m_last_pitch != m_pitch ||
205+
m_last_pos != m_position || m_last_yaw != m_yaw ||
206+
!(inventory == m_last_inventory))
207+
{
208+
m_last_hp = hp;
209+
m_last_pitch = m_pitch;
210+
m_last_pos = m_position;
211+
m_last_yaw = m_yaw;
212+
m_last_inventory = inventory;
213+
return true;
214+
} else {
215+
return false;
216+
}
217+
}
218+
202219
bool touching_ground;
203220
// This oscillates so that the player jumps a bit above the surface
204221
bool in_liquid;
@@ -262,6 +279,12 @@ class Player
262279
v3f m_speed;
263280
v3f m_position;
264281
core::aabbox3d<f32> m_collisionbox;
282+
283+
f32 m_last_pitch;
284+
f32 m_last_yaw;
285+
v3f m_last_pos;
286+
u16 m_last_hp;
287+
Inventory m_last_inventory;
265288
};
266289

267290

0 commit comments

Comments
 (0)
Please sign in to comment.