diff options
| author | qwasdrizzel <145519042+qwasdrizzel@users.noreply.github.com> | 2026-03-16 21:44:26 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-16 21:44:26 -0500 |
| commit | ce739f6045ec72127491286ea3f3f21e537c1b55 (patch) | |
| tree | f33bd42a47c1b4a7b2153a7fb77127ee3b407db9 /Minecraft.World/MapItemSavedData.cpp | |
| parent | 255a18fe8e9b57377975f82e2b227afe2a12eda0 (diff) | |
| parent | 5a59f5d146b43811dde6a5a0245ee9875d7b5cd1 (diff) | |
Merge branch 'smartcmd:main' into main
Diffstat (limited to 'Minecraft.World/MapItemSavedData.cpp')
| -rw-r--r-- | Minecraft.World/MapItemSavedData.cpp | 110 |
1 files changed, 65 insertions, 45 deletions
diff --git a/Minecraft.World/MapItemSavedData.cpp b/Minecraft.World/MapItemSavedData.cpp index c06c3fac..358b9c7e 100644 --- a/Minecraft.World/MapItemSavedData.cpp +++ b/Minecraft.World/MapItemSavedData.cpp @@ -12,6 +12,28 @@ const int MapItemSavedData::END_PORTAL_DECORATION_KEY = -1; +namespace +{ + // Valid player icon slots in mapicons.png/additionalmapicons.png. + + // Keep to these slots so we never sample placeholder entries. + static const char PLAYER_MAP_ICON_SLOTS[] = { 0, 1, 2, 3, 8, 9, 10, 11 }; + + static char getRandomPlayerMapIcon(const shared_ptr<Player>& player) + { + // use seed bit shift random + unsigned int seed = static_cast<unsigned int>(player->entityId); + seed ^= static_cast<unsigned int>(player->getPlayerIndex() * 0x9E3779B9u); + seed ^= (seed >> 16); + seed *= 0x7FEB352Du; + seed ^= (seed >> 15); + seed *= 0x846CA68Bu; + seed ^= (seed >> 16); + + return PLAYER_MAP_ICON_SLOTS[seed % (sizeof(PLAYER_MAP_ICON_SLOTS) / sizeof(PLAYER_MAP_ICON_SLOTS[0]))]; + } +} + // 4J added entityId param MapItemSavedData::MapDecoration::MapDecoration(char img, char x, char y, char rot, int entityId, bool visible) { @@ -65,8 +87,8 @@ charArray MapItemSavedData::HoldingPlayer::nextUpdatePacket(shared_ptr<ItemInsta { sendPosTick = 4; - unsigned int playerDecorationsSize = (int)parent->decorations.size(); - unsigned int nonPlayerDecorationsSize = (int)parent->nonPlayerDecorations.size(); + unsigned int playerDecorationsSize = static_cast<int>(parent->decorations.size()); + unsigned int nonPlayerDecorationsSize = static_cast<int>(parent->nonPlayerDecorations.size()); charArray data = charArray( (playerDecorationsSize + nonPlayerDecorationsSize ) * DEC_PACKET_BYTES + 1); data[0] = 1; for (unsigned int i = 0; i < parent->decorations.size(); i++) @@ -74,7 +96,7 @@ charArray MapItemSavedData::HoldingPlayer::nextUpdatePacket(shared_ptr<ItemInsta MapDecoration *md = parent->decorations.at(i); #ifdef _LARGE_WORLDS data[i * DEC_PACKET_BYTES + 1] = (char) (md->img); - data[i * DEC_PACKET_BYTES + 8] = (char) (md->rot & 0xF); + data[i * DEC_PACKET_BYTES + 8] = static_cast<char>(md->rot & 0xF); #else data[i * DEC_PACKET_BYTES + 1] = (char) ((md->img << 4) | (md->rot & 0xF)); #endif @@ -92,7 +114,7 @@ charArray MapItemSavedData::HoldingPlayer::nextUpdatePacket(shared_ptr<ItemInsta MapDecoration *md = it.second; #ifdef _LARGE_WORLDS data[dataIndex * DEC_PACKET_BYTES + 1] = (char) (md->img); - data[dataIndex * DEC_PACKET_BYTES + 8] = (char) (md->rot & 0xF); + data[dataIndex * DEC_PACKET_BYTES + 8] = static_cast<char>(md->rot & 0xF); #else data[dataIndex * DEC_PACKET_BYTES + 1] = (char) ((md->img << 4) | (md->rot & 0xF)); #endif @@ -107,7 +129,7 @@ charArray MapItemSavedData::HoldingPlayer::nextUpdatePacket(shared_ptr<ItemInsta ++dataIndex; } bool thesame = !itemInstance->isFramed(); - if (lastSentDecorations.data == NULL || lastSentDecorations.length != data.length) + if (lastSentDecorations.data == nullptr || lastSentDecorations.length != data.length) { thesame = false; } @@ -125,7 +147,7 @@ charArray MapItemSavedData::HoldingPlayer::nextUpdatePacket(shared_ptr<ItemInsta if (!thesame) { - if( lastSentDecorations.data != NULL ) + if( lastSentDecorations.data != nullptr ) { delete[] lastSentDecorations.data; } @@ -148,8 +170,8 @@ charArray MapItemSavedData::HoldingPlayer::nextUpdatePacket(shared_ptr<ItemInsta charArray data = charArray(len + 3); data[0] = HEADER_COLOURS; - data[1] = (char) column; - data[2] = (char) min; + data[1] = static_cast<char>(column); + data[2] = static_cast<char>(min); for (unsigned int y = 0; y < data.length - 3; y++) { data[y + 3] = parent->colors[(y + min) * MapItem::IMAGE_WIDTH + column]; @@ -199,7 +221,7 @@ void MapItemSavedData::load(CompoundTag *tag) { byteArray newColors = tag->getByteArray(L"colors"); //4J - if(colors.data != NULL) + if(colors.data != nullptr) { delete[] colors.data; } @@ -236,7 +258,7 @@ void MapItemSavedData::tickCarriedBy(shared_ptr<Player> player, shared_ptr<ItemI { if (carriedByPlayers.find(player) == carriedByPlayers.end()) { - shared_ptr<HoldingPlayer> hp = shared_ptr<HoldingPlayer>( new HoldingPlayer(player, this ) ); + shared_ptr<HoldingPlayer> hp = std::make_shared<HoldingPlayer>(player, this); carriedByPlayers.insert( playerHoldingPlayerMapType::value_type(player, hp) ); carriedBy.push_back(hp); } @@ -303,17 +325,17 @@ void MapItemSavedData::tickCarriedBy(shared_ptr<Player> player, shared_ptr<ItemI float xd = (float) ( origX - x ) / (1 << scale); float yd = (float) ( origZ - z ) / (1 << scale); - char x = (char) (xd * 2 + 0.5); - char y = (char) (yd * 2 + 0.5); + char x = static_cast<char>(xd * 2 + 0.5); + char y = static_cast<char>(yd * 2 + 0.5); int size = MAP_SIZE - 1; #ifdef _LARGE_WORLDS if (xd < -size || yd < -size || xd > size || yd > size) { - if (xd <= -size) x = (byte) (size * 2 + 2.5); - if (yd <= -size) y = (byte) (size * 2 + 2.5); - if (xd >= size) x = (byte) (size * 2 + 1); - if (yd >= size) y = (byte) (size * 2 + 1); + if (xd <= -size) x = static_cast<byte>(size * 2 + 2.5); + if (yd <= -size) y = static_cast<byte>(size * 2 + 2.5); + if (xd >= size) x = static_cast<byte>(size * 2 + 1); + if (yd >= size) y = static_cast<byte>(size * 2 + 1); } #endif //decorations.push_back(new MapDecoration(4, x, y, 0)); @@ -332,25 +354,25 @@ void MapItemSavedData::tickCarriedBy(shared_ptr<Player> player, shared_ptr<ItemI if( nonPlayerDecorations.find( item->getFrame()->entityId ) == nonPlayerDecorations.end() ) { - float xd = (float) ( item->getFrame()->xTile - x ) / (1 << scale); - float yd = (float) ( item->getFrame()->zTile - z ) / (1 << scale); - char x = (char) (xd * 2 + 0.5); - char y = (char) (yd * 2 + 0.5); + float xd = static_cast<float>(item->getFrame()->xTile - x) / (1 << scale); + float yd = static_cast<float>(item->getFrame()->zTile - z) / (1 << scale); + char x = static_cast<char>(xd * 2 + 0.5); + char y = static_cast<char>(yd * 2 + 0.5); int size = MAP_SIZE - 1; - char rot = (char) ( (item->getFrame()->dir * 90) * 16 / 360); + char rot = static_cast<char>((item->getFrame()->dir * 90) * 16 / 360); if (dimension < 0) { - int s = (int) (playerLevel->getLevelData()->getDayTime() / 10); - rot = (char) ((s * s * 34187121 + s * 121) >> 15 & 15); + int s = static_cast<int>(playerLevel->getLevelData()->getDayTime() / 10); + rot = static_cast<char>((s * s * 34187121 + s * 121) >> 15 & 15); } #ifdef _LARGE_WORLDS if (xd < -size || yd < -size || xd > size || yd > size) { - if (xd <= -size) x = (byte) (size * 2 + 2.5); - if (yd <= -size) y = (byte) (size * 2 + 2.5); - if (xd >= size) x = (byte) (size * 2 + 1); - if (yd >= size) y = (byte) (size * 2 + 1); + if (xd <= -size) x = static_cast<byte>(size * 2 + 2.5); + if (yd <= -size) y = static_cast<byte>(size * 2 + 2.5); + if (xd >= size) x = static_cast<byte>(size * 2 + 1); + if (yd >= size) y = static_cast<byte>(size * 2 + 1); } #endif //decorations.push_back(new MapDecoration(7, x, y, 0)); @@ -399,12 +421,12 @@ void MapItemSavedData::tickCarriedBy(shared_ptr<Player> player, shared_ptr<ItemI PlayerList *players = MinecraftServer::getInstance()->getPlayerList(); for(auto& decorationPlayer : players->players) { - if(decorationPlayer!=NULL && decorationPlayer->dimension == this->dimension) + if(decorationPlayer!=nullptr && decorationPlayer->dimension == this->dimension) { - float xd = (float) (decorationPlayer->x - x) / (1 << scale); - float yd = (float) (decorationPlayer->z - z) / (1 << scale); - char x = (char) (xd * 2); - char y = (char) (yd * 2); + float xd = static_cast<float>(decorationPlayer->x - x) / (1 << scale); + float yd = static_cast<float>(decorationPlayer->z - z) / (1 << scale); + char x = static_cast<char>(xd * 2); + char y = static_cast<char>(yd * 2); int size = MAP_SIZE; // - 1; char rot; char imgIndex; @@ -414,33 +436,31 @@ void MapItemSavedData::tickCarriedBy(shared_ptr<Player> player, shared_ptr<ItemI #endif { - rot = (char) (decorationPlayer->yRot * 16 / 360 + 0.5); + rot = static_cast<char>(decorationPlayer->yRot * 16 / 360 + 0.5); if (dimension < 0) { - int s = (int) (playerLevel->getLevelData()->getDayTime() / 10); - rot = (char) ((s * s * 34187121 + s * 121) >> 15 & 15); + int s = static_cast<int>(playerLevel->getLevelData()->getDayTime() / 10); + rot = static_cast<char>((s * s * 34187121 + s * 121) >> 15 & 15); } // 4J Stu - As we have added new icons for players on a new row below // other icons used in Java we need to move our index to the next row - imgIndex = (int)decorationPlayer->getPlayerIndex(); - if(imgIndex>3) imgIndex += 4; + imgIndex = getRandomPlayerMapIcon(decorationPlayer); } #ifdef _LARGE_WORLDS else //if (abs(xd) < MAP_SIZE * 5 && abs(yd) < MAP_SIZE * 5) { // 4J Stu - As we have added new icons for players on a new row below // other icons used in Java we need to move our index to the next row - imgIndex = (int)decorationPlayer->getPlayerIndex(); - if(imgIndex>3) imgIndex += 4; + imgIndex = getRandomPlayerMapIcon(decorationPlayer); imgIndex += 16; // Add 16 to indicate that it's on the next texture rot = 0; size--; // Added to match the old adjusted size - if (xd <= -size) x = (byte) (size * 2 + 2.5); - if (yd <= -size) y = (byte) (size * 2 + 2.5); - if (xd >= size) x = (byte) (size * 2 + 1); - if (yd >= size) y = (byte) (size * 2 + 1); + if (xd <= -size) x = static_cast<byte>(size * 2 + 2.5); + if (yd <= -size) y = static_cast<byte>(size * 2 + 2.5); + if (xd >= size) x = static_cast<byte>(size * 2 + 1); + if (yd >= size) y = static_cast<byte>(size * 2 + 1); } #endif @@ -527,7 +547,7 @@ void MapItemSavedData::handleComplexItemData(charArray &data) #endif char x = data[i * DEC_PACKET_BYTES + 2]; char y = data[i * DEC_PACKET_BYTES + 3]; - int entityId = (((int)data[i * DEC_PACKET_BYTES + 4])&0xFF) | ( (((int)data[i * DEC_PACKET_BYTES + 5])&0xFF)<<8) | ((((int)data[i * DEC_PACKET_BYTES + 6])&0xFF)<<16) | ((((int)data[i * DEC_PACKET_BYTES + 7])&0x7F)<<24); + int entityId = (static_cast<int>(data[i * DEC_PACKET_BYTES + 4])&0xFF) | ( (static_cast<int>(data[i * DEC_PACKET_BYTES + 5])&0xFF)<<8) | ((static_cast<int>(data[i * DEC_PACKET_BYTES + 6])&0xFF)<<16) | ((static_cast<int>(data[i * DEC_PACKET_BYTES + 7])&0x7F)<<24); bool visible = (data[i * DEC_PACKET_BYTES + 7] & 0x80) != 0; decorations.push_back(new MapDecoration(img, x, y, rot, entityId, visible)); } @@ -545,7 +565,7 @@ shared_ptr<MapItemSavedData::HoldingPlayer> MapItemSavedData::getHoldingPlayer(s if (it == carriedByPlayers.end()) { - hp = shared_ptr<HoldingPlayer>( new HoldingPlayer(player, this) ); + hp = std::make_shared<HoldingPlayer>(player, this); carriedByPlayers[player] = hp; carriedBy.push_back(hp); } |
