Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A nice commit. For details read comment.

• Gravel/sand falling fixed
• Added my own epic timer class thing.
• cleaned up some code
• qtcreator auto-fixing some whitespace in mineserver.cpp
• Added fastset.h (might be used in the future hopefully)
• Fixed player teleporting/moving
• added some protocol packet creation overloads for simplicity
  • Loading branch information...
commit 4f30aab7d75d3f7fe574f55dece03ea1659f81ab 1 parent b4c8fe5
Eximius authored committed
View
3  .gitignore
@@ -9,3 +9,6 @@ include/configure.h
# ignore our compile directory
bin
+
+# qmake .pro(ject) per platform config file
+src/mineserver.pro.user
View
19 include/protocol.h
@@ -146,6 +146,15 @@ class Protocol
return ret;
}
+ static Packet entityTeleport(int eid, const position& pos)
+ {
+ Packet ret;
+ ret << (int8_t)PACKET_ENTITY_TELEPORT << (int32_t)eid
+ << (int32_t)(pos.x * 32) << (int32_t)(pos.y * 32) << (int32_t)(pos.z * 32)
+ << (int8_t)pos.yaw << (int8_t)pos.pitch;
+ return ret;
+ }
+
static Packet entityHeadLook(int eid, int head_yaw)
{
Packet ret;
@@ -219,7 +228,7 @@ class Protocol
return ret;
}
- static Packet namedEntitySpawn(int eid, std::string nick, double x, double y, double z, int yaw, int pitch, int item)
+ static Packet namedEntitySpawn(int eid, const std::string& nick, double x, double y, double z, int yaw, int pitch, int item)
{
Packet ret;
ret << (int8_t)PACKET_NAMED_ENTITY_SPAWN << (int32_t)eid << nick
@@ -227,6 +236,14 @@ class Protocol
<< (int8_t)yaw << (int8_t)pitch << (int16_t)item << (int8_t)0 << (int8_t)0 << (int8_t)127;
return ret;
}
+ static Packet namedEntitySpawn(int eid, const std::string& nick, const position& pos, int item){
+ Packet ret;
+ ret << (int8_t)PACKET_NAMED_ENTITY_SPAWN << (int32_t)eid << nick
+ << (int32_t)(pos.x*32)<<(int32_t)(pos.y * 32) << (int32_t)(pos.z * 32)
+ << (int8_t)pos.yaw << (int8_t)pos.pitch << (int16_t)item
+ << (int8_t)0 << (int8_t)0 << (int8_t)127;
+ return ret;
+ }
static Packet addObject(int eid, int8_t object, double x, double y, double z, int objectData, int16_t speed_x = 0, int16_t speed_y = 0,int16_t speed_z = 0)
{
View
24 include/user.h
@@ -42,6 +42,8 @@
#include "packets.h"
#include "mineserver.h"
+//#include "chunkmap.h"
+
struct position
{
double x;
@@ -69,22 +71,33 @@ class User
//View distance in chunks -viewDistance <-> viewDistance
static const int viewDistance = 10;
+
+ std::string nick;
+ position pos;
+ int16_t health;
+
+ Item inv[45];
+ int16_t curItem;
+
+ /// updates this user's world (queues chunks, adds/removes entities)
+ void updateWorld();
+
+ /// chunks in use by user
+ //fastset<sChunk*> usedChunks;
+ /// chunk in which user last was during last updateWorld()
+ //sChunk* lastChunk;
+
uint8_t action;
bool waitForData;
uint32_t write_err_count;
bool logged;
bool muted;
bool dnd;
- int16_t health;
uint16_t timeUnderwater;
double fallDistance;
unsigned int UID;
- std::string nick;
std::string temp_nick;
- position pos;
vec curChunk;
- Item inv[45];
- int16_t curItem;
time_t healthtimeout;
Item inventoryHolding;
//Do we have an open _shared_ inventory?
@@ -123,6 +136,7 @@ class User
bool changeNick(std::string _nick);
void checkEnvironmentDamage();
+
bool updatePos(double x, double y, double z, double stance);
bool updatePosM(double x, double y, double z, size_t map, double stance);
/** Check if the user is standing on this block */
View
2  src/blocks/falling.cpp
@@ -169,7 +169,7 @@ void BlockFalling::applyPhysics(User* user, int32_t x, int16_t y, int32_t z, int
it->second->sendPacket(pkt);
//Add to physics loop
- ServerInstance->physics(map)->addFallSimulation(fallblock,vec(x, y+1, z), EID);
+ ServerInstance->physics(map)->addFallSimulation(fallblock,vec(x, y, z), EID);
this->notifyNeighbours(x, y + 1, z, map, "onNeighbourMove", user, fallblock, BLOCK_BOTTOM);
View
20 src/mineserver.cpp
@@ -367,7 +367,7 @@ Mineserver::Mineserver(int args, char **argarray)
m_physics.push_back(phy);
RedstoneSimulation* red = new RedstoneSimulation;
- red->map = n;
+ red->map = n;
m_redstone.push_back(red);
int k = m_config->iData((std::string(key) + ".") + (*it));
if ((uint32_t)k >= m_mapGenNames.size())
@@ -435,12 +435,12 @@ Mineserver::~Mineserver()
// Remove the PID file
unlink((config()->sData("system.pid_file")).c_str());
- #ifdef PROTOCOL_ENCRYPTION
+#ifdef PROTOCOL_ENCRYPTION
RSA_free(rsa);
X509_free(x);
pk->pkey.ptr = NULL;
EVP_PKEY_free(pk);
- #endif
+#endif
}
@@ -697,7 +697,7 @@ bool Mineserver::run()
{
// Send server time and keepalive
Packet pkt;
- pkt << Protocol::timeUpdate(m_map[0]->mapTime);
+ pkt << Protocol::timeUpdate(m_map[0]->mapTime);
pkt << Protocol::keepalive(0);
pkt << Protocol::playerlist();
(*User::all().begin())->sendAll(pkt);
@@ -723,9 +723,9 @@ bool Mineserver::run()
// Loop users
for (std::set<User*>::iterator it = users().begin(), it_end = users().end(); it != it_end;)
{
- // NOTE: iterators corrupt when you delete their objects, therefore we have to iterate in a special way - Justasic
- User *u = *it;
- ++it;
+ // NOTE: iterators corrupt when you delete their objects, therefore we have to iterate in a special way - Justasic
+ User *u = *it;
+ ++it;
// No data received in 30s, timeout
if (u->logged && timeNow - u->lastData > 30)
{
@@ -819,11 +819,11 @@ bool Mineserver::run()
client_write((*it));
}
}
- #ifdef WIN32
+#ifdef WIN32
closesocket(m_socketlisten);
- #else
+#else
close(m_socketlisten);
- #endif
+#endif
saveAll();
View
5 src/mineserver.pro
@@ -11,6 +11,8 @@ win32{
LIBS += -lnoise -leay32 -lssleay32 -lssl32 -lpdcurses -lz -levent -lWs2_32 -lwinmm -lpthread
INCLUDEPATH += $$(MINESERVER_WIN32_LIBS)/include
+
+ QMAKE_LFLAGS += -Wl,--large-address-aware
}
unix{
@@ -174,4 +176,5 @@ HEADERS += \
../include/cliScreen.h \
../include/chunkmap.h \
../include/chat.h \
- configure.h
+ configure.h \
+ ../include/stdtime.h
View
41 src/physics.cpp
@@ -224,20 +224,26 @@ bool Physics::updateFall()
for (int32_t simIt = listSize-1; simIt >= 0; simIt--)
{
- bool hitGround = false;
- double timeInSec = (microTime()-fallSimList[simIt].startTime)/1000000.0;
- fallSimList[simIt].ticks++;
+ Falling& f = fallSimList[simIt];
+
+ double timeInSec = (microTime()-f.startTime)/1000000.0;
+ f.ticks++;
const double gravity = 9.81;
double offset = 0.5*gravity*timeInSec*timeInSec;
- int blockOffset = fallSimList[simIt].pos.y() - fallSimList[simIt].lastY;
- if(blockOffset != (int)offset)
+ int blockOffset = f.pos.y() - f.lastY;
+ if(blockOffset != (int)offset) /// not necessary, doesn't optimize much
{
- for(int ypos = fallSimList[simIt].lastY-1; ypos >= fallSimList[simIt].lastY-((int)offset-blockOffset);ypos--)
+ int yStart = f.pos.y();
+ int x = f.pos.x(); int z = f.pos.z();
+
+ int ypos = f.lastY;
+
+ f.lastY = yStart-(int)offset;
+ for(; ypos >= f.lastY;ypos--)
{
uint8_t block, meta;
- ServerInstance->map(map)->getBlock(fallSimList[simIt].pos.x(),ypos,fallSimList[simIt].pos.z(), &block, &meta);
- fallSimList[simIt].lastY--;
+ ServerInstance->map(map)->getBlock(x,ypos,z, &block, &meta);
switch (block)
{
case BLOCK_AIR:
@@ -250,13 +256,13 @@ bool Physics::updateFall()
//If we hit ground
default:
{
- ServerInstance->map(map)->setBlock(fallSimList[simIt].pos.x(),ypos+1,fallSimList[simIt].pos.z(), fallSimList[simIt].block, 0);
- ServerInstance->map(map)->sendBlockChange(fallSimList[simIt].pos.x(),ypos+1,fallSimList[simIt].pos.z(), fallSimList[simIt].block, 0);
+ ServerInstance->map(map)->setBlock(x, ++ypos, z, f.block, 0);
+ ServerInstance->map(map)->sendBlockChange(x, ypos, z, f.block, 0);
//Despawn entity
- Packet pkt = Protocol::destroyEntity(fallSimList[simIt].EID);
- const int chunk_x = blockToChunk(fallSimList[simIt].pos.x());
- const int chunk_z = blockToChunk(fallSimList[simIt].pos.z());
+ Packet pkt = Protocol::destroyEntity(f.EID);
+ const int chunk_x = blockToChunk(x);
+ const int chunk_z = blockToChunk(z);
const ChunkMap::const_iterator it = ServerInstance->map(map)->chunks.find(Coords(chunk_x, chunk_z));
if (it != ServerInstance->map(map)->chunks.end())
{
@@ -264,16 +270,13 @@ bool Physics::updateFall()
}
//Erase from the simulation list
fallSimList.erase(fallSimList.begin()+simIt);
- hitGround = true;
- break;
+ goto breakout;
}
}
- if(hitGround)
- {
- break;
- }
}
}
+breakout:
+ continue;
}
return true;
}
View
166 src/user.cpp
@@ -176,8 +176,8 @@ User::~User()
if (inventoryHolding.getType() != -1)
{
ServerInstance->map(pos.map)->createPickupSpawn((int)pos.x, (int)pos.y, (int)pos.z,
- inventoryHolding.getType(), inventoryHolding.getCount(),
- inventoryHolding.getHealth(), this);
+ inventoryHolding.getType(), inventoryHolding.getCount(),
+ inventoryHolding.getHealth(), this);
inventoryHolding.setType(-1);
}
@@ -571,71 +571,33 @@ bool User::updatePos(double x, double y, double z, double stance)
Packet telePacket = Protocol::entityTeleport(UID, x, y, z, angleToByte(pos.yaw), angleToByte(pos.pitch));
newChunk->sendPacket(telePacket, this);
}
- else if (abs(newChunk->x - oldChunk->x) <= 1 && abs(newChunk->z - oldChunk->z) <= 1)
- {
-
+ else{
std::list<User*> toremove;
std::list<User*> toadd;
sChunk::userBoundary(oldChunk, toremove, newChunk, toadd);
- if (toremove.size())
- {
- Packet pkt = Protocol::destroyEntity(UID);
- std::list<User*>::iterator iter = toremove.begin(), end = toremove.end();
- for (; iter != end ; iter++)
- {
- (*iter)->buffer.addToWrite(pkt);
- }
- }
+ /// update this player's pos for others
- if (toadd.size())
- {
- Packet pkt = Protocol::namedEntitySpawn(UID, nick, x, y, z, angleToByte(pos.yaw), angleToByte(pos.pitch), curItem);
+ Packet dtPkt = Protocol::destroyEntity(UID);
+ for( User*& u : toremove){
+ u->buffer.addToWrite(dtPkt);
- std::list<User*>::iterator iter = toadd.begin(), end = toadd.end();
- for (; iter != end ; iter++)
- {
- if ((*iter) != this)
- {
- (*iter)->buffer.addToWrite(pkt);
- }
- }
+ this->buffer.addToWrite(Protocol::destroyEntity(u->UID));
}
- // TODO: Determine those who where present for both.
- Packet telePacket = Protocol::entityTeleport(UID, x, y, z, angleToByte(pos.yaw), angleToByte(pos.pitch));
- newChunk->sendPacket(telePacket, this);
-
- int chunkDiffX = newChunk->x - oldChunk->x;
- int chunkDiffZ = newChunk->z - oldChunk->z;
-
- // Send new chunk and clear old chunks
- for (int mapx = newChunk->x - viewDistance; mapx <= newChunk->x + viewDistance; mapx++)
- {
- for (int mapz = newChunk->z - viewDistance; mapz <= newChunk->z + viewDistance; mapz++)
- {
- if (!withinViewDistance((mapx - chunkDiffX), newChunk->x) || !withinViewDistance((mapz - chunkDiffZ), newChunk->z))
- {
- addRemoveQueue(mapx - chunkDiffX, mapz - chunkDiffZ);
- }
-
- // If this chunk wasn't in the view distance before
- // if (!withinViewDistance(chunkDiffX, oldChunk->x) || !withinViewDistance(chunkDiffZ, oldChunk->z))
- //{
+ Packet spawnPkt = Protocol::namedEntitySpawn(UID, nick, x, y, z, angleToByte(pos.yaw), angleToByte(pos.pitch), curItem);
+ for( User*& u : toadd){
+ u->buffer.addToWrite(spawnPkt);
- // This will remove the chunks from being removed if they were put to the remove queue.
- addQueue(mapx, mapz);
+ this->buffer.addToWrite(
+ Protocol::namedEntitySpawn(u->UID, u->nick, u->pos, u->curItem)
+ );
+ }
+ Packet tpPkt = Protocol::entityTeleport(UID, x, y, z, angleToByte(pos.yaw), angleToByte(pos.pitch));
- //}
- }
- }
- }
- else
- {
- std::set<User*> toRemove;
- std::set<User*> toAdd;
+ newChunk->sendPacket(tpPkt,this);
int chunkDiffX = newChunk->x - oldChunk->x;
int chunkDiffZ = newChunk->z - oldChunk->z;
@@ -646,81 +608,15 @@ bool User::updatePos(double x, double y, double z, double stance)
if (!withinViewDistance(chunkDiffX, oldChunk->x) || !withinViewDistance(chunkDiffZ, oldChunk->z))
{
addQueue(mapx, mapz);
- sChunk* chunk = ServerInstance->map(pos.map)->getChunk(mapx, mapz);
-
- if (chunk != NULL)
- {
- toAdd.insert(chunk->users.begin(), chunk->users.end());
- }
}
if (!withinViewDistance((mapx - chunkDiffX), newChunk->x) || !withinViewDistance((mapz - chunkDiffZ), newChunk->z))
{
addRemoveQueue(mapx - chunkDiffX, mapz - chunkDiffZ);
-
- sChunk* chunk = ServerInstance->map(pos.map)->getChunk((mapx - chunkDiffX), (mapz - chunkDiffZ));
-
- if (chunk != NULL)
- {
- toRemove.insert(chunk->users.begin(), chunk->users.end());
- }
}
}
}
- std::set<User*> toTeleport;
- std::set<User*>::iterator iter = toRemove.begin(), end = toRemove.end();
- for (; iter != end ; iter++)
- {
- std::set<User*>::iterator result = toAdd.find(*iter);
- if (result != toAdd.end())
- {
- toTeleport.insert(*iter);
- toAdd.erase(result);
-
-#ifdef _MSC_VER
- iter = toRemove.erase(iter);
-#else
- // TODO: Optimise
- toRemove.erase(iter);
- iter = toRemove.begin();
-#endif
- end = toRemove.end();
- if (iter == end)
- {
- break;
- }
- }
- }
-
- Packet destroyPkt = Protocol::destroyEntity(UID);
- Packet spawnPkt = Protocol::namedEntitySpawn(UID, nick, x, y, z, angleToByte(pos.yaw), angleToByte(pos.pitch), curItem);
- Packet telePacket = Protocol::entityTeleport(UID, x, y, z, angleToByte(pos.yaw), angleToByte(pos.pitch));
-
- toTeleport.erase(this);
- toAdd.erase(this);
- toRemove.erase(this);
-
- iter = toRemove.begin();
- end = toRemove.end();
- for (; iter != end ; iter++)
- {
- (*iter)->buffer.addToWrite(destroyPkt);
- }
-
- iter = toAdd.begin();
- end = toAdd.end();
- for (; iter != end ; iter++)
- {
- (*iter)->buffer.addToWrite(spawnPkt);
- }
-
- iter = toTeleport.begin();
- end = toTeleport.end();
- for (; iter != end ; iter++)
- {
- (*iter)->buffer.addToWrite(telePacket);
- }
}
@@ -834,7 +730,7 @@ bool User::checkOnBlock(int32_t x, int16_t y, int32_t z)
bool User::updateLook(float yaw, float pitch)
{
Packet pkt = Protocol::entityLook(UID, yaw, pitch);
- pkt << Protocol::entityHeadLook(UID, angleToByte(yaw));
+ pkt << Protocol::entityHeadLook(UID, angleToByte(yaw));
sChunk* chunk = ServerInstance->map(pos.map)->getChunk(blockToChunk((int32_t)pos.x), blockToChunk((int32_t)pos.z));
if (chunk != NULL)
@@ -1141,7 +1037,7 @@ class DistanceComparator
a.y() = 0;
b.y() = 0;
return vec::squareDistance(a, target) <
- vec::squareDistance(b, target);
+ vec::squareDistance(b, target);
}
};
@@ -1240,7 +1136,7 @@ void User::checkEnvironmentDamage()
/// louisdx: This makes no sense at the moment; type is not initialized!
//uint8_t type, meta;
uint8_t type = 0, meta = 0;
-
+
int16_t d = 0;
if (type == BLOCK_CACTUS && LOADBLOCK(pos.x, yVal, pos.z))
@@ -1522,7 +1418,7 @@ std::string User::generateDigest()
}
else
{
- carry = 0;
+ carry = 0;
}
md[i] = twocomp;
}
@@ -1530,15 +1426,15 @@ std::string User::generateDigest()
for (i = 0; i < 20; i++)
{
- if(i || md[i]>>4)
- {
- out += hex[(md[i]>>4)];
- }
- if(i || md[i]>>4 || md[i]&0xf)
- {
- out += hex[(md[i]&0xf)];
- }
- }
+ if(i || md[i]>>4)
+ {
+ out += hex[(md[i]>>4)];
+ }
+ if(i || md[i]>>4 || md[i]&0xf)
+ {
+ out += hex[(md[i]&0xf)];
+ }
+ }
return out;
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.