Procházet zdrojové kódy

player data will now be saved an loaded from save folder

Kolja Strohm před 2 roky
rodič
revize
74c4ba2226

+ 11 - 1
FactoryCraft/Dimension.cpp

@@ -254,7 +254,17 @@ void Dimension::removeOldChunks()
 	for (int i : removed)
 	{
 		Chunk* chunk = chunkList.get(i);
-		// TODO: save chunk
+		// TODO: save chunk in a seperate thread?
+		Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/" + getDimensionId() + "/";
+		filePath.appendHex(chunk->getCenter().x);
+		filePath += "_";
+		filePath.appendHex(chunk->getCenter().y);
+		filePath += ".chunk";
+		Datei d;
+		d.setDatei(filePath);
+		d.open(Datei::Style::schreiben);
+		chunk->save(&d);
+		d.close();
 		chunk->prepareRemove();
 		setChunk(0, chunk->getCenter());
 	}

+ 19 - 0
FactoryCraft/EntityType.cpp

@@ -34,6 +34,16 @@ void EntityType::loadSuperEntity(Entity* zEntity, Framework::StreamReader* zRead
 	if (zEntity->target)
 		delete zEntity->target;
 	zEntity->target = ActionTarget::load(zReader);
+	// load item skills
+	zEntity->skills.leeren();
+	int skillCount;
+	zReader->lese((char*)&skillCount, 4);
+	for (int i = 0; i < skillCount; i++)
+	{
+		int type;
+		zReader->lese((char*)&type, 4);
+		zEntity->skills.add(StaticRegistry<ItemType>::INSTANCE.zElement(type)->loadItemSkill(zReader));
+	}
 }
 
 void EntityType::saveSuperEntity(Entity* zEntity, Framework::StreamWriter* zWriter) const
@@ -58,6 +68,15 @@ void EntityType::saveSuperEntity(Entity* zEntity, Framework::StreamWriter* zWrit
 	zWriter->schreibe((char*)&zEntity->location.y, 4);
 	zWriter->schreibe((char*)&zEntity->location.z, 4);
 	ActionTarget::save(zEntity->target, zWriter);
+	// save item skills
+	int skillCount = zEntity->skills.getEintragAnzahl();
+	zWriter->schreibe((char*)&skillCount, 4);
+	for (ItemSkill* skill : zEntity->skills)
+	{
+		int type = skill->zSkillType()->getId();
+		zWriter->schreibe((char*)&type, 4);
+		skill->zSkillType()->saveItemSkill(skill, zWriter);
+	}
 }
 
 void EntityType::createSuperEntity(Entity* zEntity) const

+ 9 - 2
FactoryCraft/Game.cpp

@@ -7,6 +7,7 @@
 #include "AsynchronCall.h"
 #include "Entity.h"
 #include "AddEntityUpdate.h"
+#include "EntityRemovedUpdate.h"
 
 using namespace Framework;
 
@@ -307,11 +308,15 @@ void Game::thread()
 		{
 			if (!player->isOnline())
 			{
+				std::cout << "player " << player->zEntity()->getName() << " disconnected.\n";
 				Datei pFile;
 				pFile.setDatei(path + "/player/" + player->zEntity()->getName());
+				pFile.erstellen();
 				if (pFile.open(Datei::Style::schreiben))
 					PlayerEntityType::INSTANCE->saveEntity(player->zEntity(), &pFile);
+				pFile.close();
 				removed.add(index, 0);
+				this->requestWorldUpdate(new EntityRemovedUpdate(player->zEntity()->getId(), player->zEntity()->getCurrentDimensionId(), player->zEntity()->getPosition()));
 			}
 			index++;
 		}
@@ -345,7 +350,7 @@ void Game::thread()
 		if (tickCounter >= 1000)
 		{
 			std::cout << "Average Tick time: " << (totalTickTime / 1000) << "\n";
-			if ((totalTickTime / 1000) * 30 > 1)
+			if ((totalTickTime / 1000) * 20 > 1)
 			{
 				std::cout << "The game runns slower than normal.\n";
 			}
@@ -427,6 +432,7 @@ void Game::api(Framework::StreamReader* zRequest, GameClient* zOrigin)
 		}
 		if (target)
 			target->inventoryApi(zRequest, &response, zOrigin->zEntity());
+		break;
 	}
 	default:
 		std::cout << "received unknown api request in game with type " << (int)type << "\n";
@@ -450,7 +456,7 @@ void Game::sendMessage(NetworkMessage* zResponse, Entity* zTargetPlayer)
 {
 	for (auto client : *clients)
 	{
-		if (client->zEntity() == zTargetPlayer)
+		if (client->zEntity()->getId() == zTargetPlayer->getId())
 		{
 			client->sendResponse(zResponse);
 			break;
@@ -482,6 +488,7 @@ GameClient* Game::addPlayer(FCKlient* client, Framework::Text name)
 	cs.lock();
 	Datei pFile;
 	pFile.setDatei(path + "/player/" + name);
+	std::cout << "player " << name.getText() << " connected.\n";
 	Player* player;
 	bool isNew = 0;
 	if (!pFile.existiert() || !pFile.open(Datei::Style::lesen))

+ 10 - 0
FactoryCraft/Player.cpp

@@ -426,6 +426,13 @@ void PlayerEntityType::loadSuperEntity(Entity* zEntity, Framework::StreamReader*
 	if (!zPlayer)
 		throw "PlayerEntityType::loadSuperEntity was called with an entity witch is not an instance of Player";
 	zReader->lese((char*)&zPlayer->leftHandPosition, 4);
+	char len;
+	zReader->lese(&len, 1);
+	char* name = new char[(int)len + 1];
+	zReader->lese(name, (int)len);
+	name[(int)len] = 0;
+	zPlayer->name = name;
+	delete[] name;
 	EntityType::loadSuperEntity(zPlayer, zReader);
 }
 
@@ -435,6 +442,9 @@ void PlayerEntityType::saveSuperEntity(Entity* zEntity, Framework::StreamWriter*
 	if (!zPlayer)
 		throw "PlayerEntityType::saveSuperEntity was called with an entity witch is not an instance of Player";
 	zWriter->schreibe((char*)&zPlayer->leftHandPosition, 4);
+	char len = (char)textLength(zPlayer->getName());
+	zWriter->schreibe(&len, 1);
+	zWriter->schreibe(zPlayer->getName(), (int)len);
 	EntityType::saveSuperEntity(zEntity, zWriter);
 }