mirror of
https://git.neolegacy.dev/neoStudiosLCE/neoLegacy.git
synced 2026-11-19 12:04:15 +01:00
Co-authored-by: DrPerkyLegit <116128211+DrPerkyLegit@users.noreply.github.com> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Anaël <60509176+Bakoubak@users.noreply.github.com> Reviewed-on: https://git.neolegacy.dev/neoStudiosLCE/neoLegacy/pulls/4205
424 lines
15 KiB
C++
424 lines
15 KiB
C++
#include <stdafx.h>
|
|
|
|
#include "DynamicLighting.h"
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
|
|
#include "./GameRenderer.h"
|
|
#include "./MultiPlayerLocalPlayer.h"
|
|
#include "./MultiPlayerLevel.h"
|
|
#include "../Minecraft.World/Tile.h"
|
|
#include "../Minecraft.World/ItemEntity.h"
|
|
#include "../Minecraft.World/Creeper.h"
|
|
|
|
int64_t DynamicLighting::lastUpdate = 0; //static define
|
|
std::mutex DynamicLighting::dynamicEntityLightSourcesMutex; //static define
|
|
std::vector<Entity*> DynamicLighting::dynamicEntityLightSources; //static define
|
|
|
|
std::mutex DynamicLighting::dynamicCompiledLightSourcesMutex; //static define
|
|
std::vector<DynamicLighting::DynamicLightSource> DynamicLighting::dynamicCompiledLightSources; //static define
|
|
std::atomic<bool> DynamicLighting::dynamicLightingEnabled(false); //static define
|
|
|
|
enum eDynamicLightSourceFlags {
|
|
NONE = 0,
|
|
FLICKER_SMALL = 1 << 0,
|
|
FLICKER_MEDIUM = 1 << 1,
|
|
FLICKER_LARGE = 1 << 2,
|
|
WATER_SENSITIVE = 1 << 4, // forces parser to use split luminance , dry / wet
|
|
AGEABLE = 1 << 5, // forces parser to use split luminance, low, high
|
|
};
|
|
|
|
std::unordered_map<int, std::pair<int, int>> DynamicLighting::dynamicEntitySourceDefines = { //static define
|
|
{ eTYPE_BLAZE, {(eDynamicLightSourceFlags::WATER_SENSITIVE | eDynamicLightSourceFlags::FLICKER_MEDIUM), ((10) << 16 || (4))}},
|
|
{ eTYPE_LAVASLIME, {(eDynamicLightSourceFlags::AGEABLE | eDynamicLightSourceFlags::FLICKER_SMALL), ((8) << 16 || (11))}},
|
|
|
|
{ eTYPE_DRAGON_FIREBALL, {(eDynamicLightSourceFlags::FLICKER_SMALL), 14}},
|
|
{ eTYPE_CREEPER, {eDynamicLightSourceFlags::FLICKER_SMALL, 15}},
|
|
{ eTYPE_WITHER_SKULL, {(eDynamicLightSourceFlags::NONE), 12}},
|
|
{ eTYPE_LARGE_FIREBALL, {(eDynamicLightSourceFlags::WATER_SENSITIVE | eDynamicLightSourceFlags::FLICKER_SMALL), ((14) << 16 || (0))}},
|
|
{ eTYPE_SMALL_FIREBALL, {(eDynamicLightSourceFlags::WATER_SENSITIVE | eDynamicLightSourceFlags::FLICKER_SMALL), ((12) << 16 || (0))}},
|
|
};
|
|
|
|
std::unordered_map<int, std::pair<int, int>> DynamicLighting::dynamicItemSourceDefines = { //static define
|
|
{ Item::blaze_powder_Id, {eDynamicLightSourceFlags::NONE, 10} },
|
|
{ Item::blaze_rod_Id, {eDynamicLightSourceFlags::NONE, 10} },
|
|
|
|
{ Item::fire_charge_Id, {eDynamicLightSourceFlags::WATER_SENSITIVE, ((10) << 16 || (0))} },
|
|
{ Item::glowstone_dust_Id, {eDynamicLightSourceFlags::NONE, 10} },
|
|
{ Item::nether_star_Id, {eDynamicLightSourceFlags::NONE, 8} },
|
|
{ Item::prismarine_crystals_Id, {eDynamicLightSourceFlags::NONE, 10} },
|
|
|
|
{ Item::lava_bucket_Id, {eDynamicLightSourceFlags::WATER_SENSITIVE | eDynamicLightSourceFlags::FLICKER_SMALL, 15} },
|
|
};
|
|
|
|
|
|
void DynamicLighting::addDynamicEntityLightSource(Entity* entity) {
|
|
if (entity == nullptr || !dynamicLightingEnabled.load(std::memory_order_relaxed)) return;
|
|
|
|
std::lock_guard<std::mutex> sourceLock(dynamicEntityLightSourcesMutex);
|
|
if (std::find(dynamicEntityLightSources.begin(), dynamicEntityLightSources.end(), entity) == dynamicEntityLightSources.end())
|
|
{
|
|
dynamicEntityLightSources.push_back(entity);
|
|
}
|
|
}
|
|
|
|
void DynamicLighting::removeDynamicEntityLightSource(Entity* entity) {
|
|
if (entity == nullptr || !dynamicLightingEnabled.load(std::memory_order_relaxed)) return;
|
|
|
|
std::lock_guard<std::mutex> sourceLock(dynamicEntityLightSourcesMutex);
|
|
auto source = std::find(dynamicEntityLightSources.begin(), dynamicEntityLightSources.end(), entity);
|
|
if (source == dynamicEntityLightSources.end()) return;
|
|
|
|
std::lock_guard<std::mutex> compiledLock(dynamicCompiledLightSourcesMutex);
|
|
dynamicEntityLightSources.erase(source);
|
|
dynamicCompiledLightSources.erase(
|
|
std::remove_if(dynamicCompiledLightSources.begin(), dynamicCompiledLightSources.end(),
|
|
[entity](const DynamicLightSource& source) { return source.entity == entity; }),
|
|
dynamicCompiledLightSources.end());
|
|
}
|
|
|
|
int DynamicLighting::getLightmapWithDynamicLight(int x, int y, int z, int lightMap) {
|
|
if (!dynamicLightingEnabled.load(std::memory_order_relaxed)) return lightMap;
|
|
return getLightmapWithDynamicLight(getDynamicLuminance(x, y, z), lightMap);
|
|
}
|
|
|
|
int DynamicLighting::getLightmapWithDynamicLight(Entity* entity, int lightMap) {
|
|
if (entity == nullptr || !dynamicLightingEnabled.load(std::memory_order_relaxed)) return lightMap;
|
|
|
|
int posLuminance = getDynamicLuminance((int)entity->x, (int)entity->y, (int)entity->z);
|
|
int entityLuminance = getFlickeredLuminance(entity, getEntityLuminance(entity), System::currentTimeMillis());
|
|
|
|
return getLightmapWithDynamicLight(max(posLuminance, entityLuminance), lightMap);
|
|
}
|
|
|
|
int DynamicLighting::getLightmapWithDynamicLight(double dynamicLuminance, int lightMap) {
|
|
if (!dynamicLightingEnabled.load(std::memory_order_relaxed)) return lightMap;
|
|
|
|
if (dynamicLuminance > 0) {
|
|
const int skyLevel = (lightMap >> 20) & 0xF;
|
|
auto mc = Minecraft::GetInstance();
|
|
if (skyLevel == 15 && mc != nullptr && mc->level != nullptr
|
|
&& mc->level->getSkyDarken(1.0f) < 0.3f) {
|
|
return lightMap;
|
|
}
|
|
|
|
// lightmap is (skyLevel << 20 | blockLevel << 4)
|
|
|
|
// Get vanilla block light level.
|
|
int blockLevel = (lightMap >> 4) & 0xF;
|
|
if (dynamicLuminance > blockLevel) {
|
|
// Equivalent to a << 4 bitshift with a little quirk: this one ensure more precision (more decimals are saved).
|
|
int luminance = (int)(dynamicLuminance * 16.0);
|
|
lightMap &= 0xfff00000;
|
|
lightMap |= luminance & 0x000fffff;
|
|
}
|
|
}
|
|
|
|
return lightMap;
|
|
}
|
|
|
|
int DynamicLighting::getEntityLuminance(Entity* entity) {
|
|
if (!dynamicLightingEnabled.load(std::memory_order_relaxed) || entity == nullptr || !entity->isAlive()) return 0;
|
|
|
|
if (entity->GetType() == eTYPE_ITEMENTITY) {
|
|
ItemEntity* itemEntity = static_cast<ItemEntity*>(entity);
|
|
return getItemLuminance(itemEntity->getItem());
|
|
}
|
|
|
|
if (entity->isOnFire()) return 15;
|
|
|
|
if (entity->GetType() == eTYPE_CREEPER) {
|
|
Creeper* creeper = static_cast<Creeper*>(entity);
|
|
const double swell = Mth::clamp(creeper->getSwelling(1.0f), 0.0f, 1.0f);
|
|
return static_cast<int>(std::ceil(swell * 15.0));
|
|
}
|
|
|
|
int luminance = 0;
|
|
auto it = dynamicEntitySourceDefines.find(entity->GetType());
|
|
if (it != dynamicEntitySourceDefines.end()) {
|
|
luminance = it->second.second;
|
|
}
|
|
|
|
if (entity->GetType() & BIT_LIVING_ENTITY) {
|
|
LivingEntity* livingEntity = dynamic_cast<LivingEntity*>(entity);
|
|
if (livingEntity != nullptr) {
|
|
auto heldItem = livingEntity->getCarriedItem();
|
|
if (heldItem != nullptr) {
|
|
int heldLuminance = getItemLuminance(heldItem);
|
|
|
|
if (heldLuminance > luminance) {
|
|
luminance = heldLuminance;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return luminance;
|
|
}
|
|
|
|
int DynamicLighting::getItemFlickerRange(shared_ptr<ItemInstance> item) {
|
|
if (item == nullptr) return 0;
|
|
|
|
auto it = dynamicItemSourceDefines.find(item->id);
|
|
if (it == dynamicItemSourceDefines.end()) return 0;
|
|
|
|
const int flags = it->second.first;
|
|
if (flags & eDynamicLightSourceFlags::FLICKER_LARGE) return 3;
|
|
if (flags & eDynamicLightSourceFlags::FLICKER_MEDIUM) return 2;
|
|
if (flags & eDynamicLightSourceFlags::FLICKER_SMALL) return 1;
|
|
return 0;
|
|
}
|
|
|
|
int DynamicLighting::getEntityFlickerRange(Entity* entity) {
|
|
if (entity == nullptr) return 0;
|
|
if (entity->isOnFire()) return 1;
|
|
|
|
if (entity->GetType() == eTYPE_ITEMENTITY) {
|
|
return getItemFlickerRange(static_cast<ItemEntity*>(entity)->getItem());
|
|
}
|
|
|
|
int luminance = 0;
|
|
int flags = eDynamicLightSourceFlags::NONE;
|
|
auto it = dynamicEntitySourceDefines.find(entity->GetType());
|
|
if (it != dynamicEntitySourceDefines.end()) {
|
|
flags = it->second.first;
|
|
luminance = it->second.second;
|
|
}
|
|
|
|
if (entity->GetType() & BIT_LIVING_ENTITY) {
|
|
LivingEntity* livingEntity = dynamic_cast<LivingEntity*>(entity);
|
|
if (livingEntity != nullptr) {
|
|
shared_ptr<ItemInstance> heldItem = livingEntity->getCarriedItem();
|
|
int heldLuminance = getItemLuminance(heldItem);
|
|
if (heldLuminance > luminance) {
|
|
return getItemFlickerRange(heldItem);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (flags & eDynamicLightSourceFlags::FLICKER_LARGE) return 3;
|
|
if (flags & eDynamicLightSourceFlags::FLICKER_MEDIUM) return 2;
|
|
if (flags & eDynamicLightSourceFlags::FLICKER_SMALL) return 1;
|
|
return 0;
|
|
}
|
|
|
|
int DynamicLighting::getFlickeredLuminance(Entity* entity, int luminance, int64_t now) {
|
|
const int range = getEntityFlickerRange(entity);
|
|
if (luminance <= 0 || range == 0) return luminance;
|
|
|
|
uint64_t sample = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(entity));
|
|
sample ^= static_cast<uint64_t>(now / 250) + 0x9e3779b97f4a7c15ULL;
|
|
sample ^= sample >> 30;
|
|
sample *= 0xbf58476d1ce4e5b9ULL;
|
|
sample ^= sample >> 27;
|
|
sample *= 0x94d049bb133111ebULL;
|
|
sample ^= sample >> 31;
|
|
|
|
const int offset = static_cast<int>(sample % (range * 2 + 1)) - range;
|
|
return Mth::clamp(luminance + offset, 0, 15);
|
|
}
|
|
|
|
int DynamicLighting::getItemLuminance(shared_ptr<ItemInstance> item) {
|
|
if (item == nullptr) return 0;
|
|
|
|
int luminance = 0;
|
|
auto it = dynamicItemSourceDefines.find(item->id);
|
|
if (it != dynamicItemSourceDefines.end()) {
|
|
luminance = it->second.second;
|
|
} else {
|
|
if (item->id >= 0 && item->id < Tile::TILE_NUM_COUNT) {
|
|
int heldLuminance = Tile::lightEmission[item->id];
|
|
|
|
if (heldLuminance > luminance) {
|
|
luminance = heldLuminance;
|
|
}
|
|
}
|
|
}
|
|
|
|
return luminance;
|
|
}
|
|
|
|
|
|
int DynamicLighting::getDynamicLuminance(int x, int y, int z) {
|
|
if (!dynamicLightingEnabled.load(std::memory_order_relaxed)) return 0;
|
|
|
|
int result = 0;
|
|
{
|
|
std::lock_guard<std::mutex> _lock(dynamicCompiledLightSourcesMutex);
|
|
for (DynamicLightSource& source : dynamicCompiledLightSources) {
|
|
result = maxDynamicLuminance(x, y, z, source, result);
|
|
}
|
|
}
|
|
|
|
return Mth::clamp(result, 0, 15);
|
|
}
|
|
|
|
int DynamicLighting::maxDynamicLuminance(int x, int y, int z, DynamicLightSource& source, double currentLuminance) {
|
|
if (source.entity != nullptr && source.luminance > 0) {
|
|
double dx = x - source.x + 0.5;
|
|
double dy = y - source.y + 0.5;
|
|
double dz = z - source.z + 0.5;
|
|
double distanceSquared = dx * dx + dy * dy + dz * dz;
|
|
|
|
if (distanceSquared <= MAX_RADIUS * MAX_RADIUS) {
|
|
double distance = Mth::sqrt(distanceSquared);
|
|
double lightLevel = source.luminance * (1.0 - distance / MAX_RADIUS);
|
|
if (lightLevel > currentLuminance) {
|
|
currentLuminance = lightLevel;
|
|
}
|
|
}
|
|
}
|
|
|
|
return currentLuminance;
|
|
}
|
|
|
|
|
|
void DynamicLighting::markTilesDirty(MultiPlayerLevel* level, int x, int y, int z, int luminance) {
|
|
if (luminance <= 0) return;
|
|
|
|
const int radius = (luminance + 1) / 2;
|
|
const int minY = y - radius < 0 ? 0 : y - radius;
|
|
const int maxY = y + radius > Level::maxBuildHeight - 1 ? Level::maxBuildHeight - 1 : y + radius;
|
|
if (minY > maxY) return;
|
|
|
|
level->setTilesDirty(x - radius, minY, z - radius, x + radius, maxY, z + radius);
|
|
}
|
|
|
|
void DynamicLighting::updateAll(GameRenderer* renderer) {
|
|
auto mc = Minecraft::GetInstance();
|
|
if (mc == nullptr) return;
|
|
|
|
const bool enabled = mc->localplayers[0] != nullptr
|
|
&& app.GetGameSettings(mc->localplayers[0]->GetXboxPad(), eGameSetting_DynamicLighting_Toggle) != 0;
|
|
const bool wasEnabled = dynamicLightingEnabled.exchange(enabled, std::memory_order_relaxed);
|
|
|
|
static std::vector<DynamicLightSource> previousSources;
|
|
static std::vector<DynamicLightSource> nextSources;
|
|
static std::vector<Entity*> includedEntities;
|
|
static MultiPlayerLevel* previousLevel = nullptr;
|
|
MultiPlayerLevel* level = mc->level;
|
|
|
|
if (!enabled) {
|
|
if (wasEnabled || previousLevel != nullptr) {
|
|
std::lock_guard<std::mutex> sourceLock(dynamicEntityLightSourcesMutex);
|
|
std::lock_guard<std::mutex> compiledLock(dynamicCompiledLightSourcesMutex);
|
|
std::vector<Entity*>().swap(dynamicEntityLightSources);
|
|
std::vector<DynamicLightSource>().swap(dynamicCompiledLightSources);
|
|
std::vector<DynamicLightSource>().swap(previousSources);
|
|
std::vector<DynamicLightSource>().swap(nextSources);
|
|
std::vector<Entity*>().swap(includedEntities);
|
|
previousLevel = nullptr;
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (previousLevel != level) {
|
|
std::lock_guard<std::mutex> sourceLock(dynamicEntityLightSourcesMutex);
|
|
std::lock_guard<std::mutex> compiledLock(dynamicCompiledLightSourcesMutex);
|
|
dynamicEntityLightSources.clear();
|
|
dynamicCompiledLightSources.clear();
|
|
previousSources.clear();
|
|
nextSources.clear();
|
|
includedEntities.clear();
|
|
|
|
if (level != nullptr) {
|
|
EnterCriticalSection(&level->m_entitiesCS);
|
|
for (const shared_ptr<Entity>& entity : level->entities) {
|
|
if (entity != nullptr
|
|
&& ((entity->GetType() & BIT_LIVING_ENTITY) || entity->GetType() == eTYPE_ITEMENTITY)) {
|
|
dynamicEntityLightSources.push_back(entity.get());
|
|
}
|
|
}
|
|
LeaveCriticalSection(&level->m_entitiesCS);
|
|
}
|
|
|
|
previousLevel = level;
|
|
}
|
|
|
|
int64_t now = System::currentTimeMillis();
|
|
if (now >= lastUpdate + 50) {
|
|
lastUpdate = now;
|
|
previousSources.reserve(16);
|
|
nextSources.reserve(16);
|
|
includedEntities.reserve(16);
|
|
|
|
nextSources.clear();
|
|
includedEntities.clear();
|
|
|
|
{
|
|
std::lock_guard<std::mutex> sourceLock(dynamicEntityLightSourcesMutex);
|
|
if (enabled && level != nullptr && mc->player != nullptr) {
|
|
for (int i = 0; i < mc->level->players.size(); ++i) {
|
|
shared_ptr<Player> player = mc->level->players[i];
|
|
if (!player || !player->inventory) continue;
|
|
|
|
includedEntities.push_back(player.get());
|
|
int luminance = getFlickeredLuminance(player.get(), getEntityLuminance(player.get()), now);
|
|
if (luminance <= 0) continue;
|
|
|
|
DynamicLightSource source = {
|
|
player.get(),
|
|
static_cast<double>(luminance),
|
|
player->x,
|
|
player->y + player->getEyeHeight(),
|
|
player->z
|
|
};
|
|
nextSources.push_back(source);
|
|
}
|
|
}
|
|
|
|
if (enabled && level != nullptr) {
|
|
for (Entity* entity : dynamicEntityLightSources) {
|
|
if (entity == nullptr || !entity->isAlive() || std::find(includedEntities.begin(), includedEntities.end(), entity) != includedEntities.end()) continue;
|
|
includedEntities.push_back(entity);
|
|
|
|
int luminance = getFlickeredLuminance(entity, getEntityLuminance(entity), now);
|
|
if (luminance <= 0) continue;
|
|
|
|
DynamicLightSource source = {
|
|
entity,
|
|
static_cast<double>(luminance),
|
|
entity->x,
|
|
entity->y + entity->getEyeHeight(),
|
|
entity->z
|
|
};
|
|
nextSources.push_back(source);
|
|
}
|
|
}
|
|
|
|
std::lock_guard<std::mutex> compiledLock(dynamicCompiledLightSourcesMutex);
|
|
dynamicCompiledLightSources.assign(nextSources.begin(), nextSources.end());
|
|
}
|
|
|
|
if (level == nullptr || previousLevel != level) {
|
|
previousSources.clear();
|
|
}
|
|
previousLevel = level;
|
|
|
|
for (const DynamicLightSource& oldSource : previousSources) {
|
|
auto current = std::find_if(nextSources.begin(), nextSources.end(),
|
|
[&oldSource](const DynamicLightSource& source) { return source.entity == oldSource.entity; });
|
|
if (current == nextSources.end()
|
|
|| Mth::floor(oldSource.x) != Mth::floor(current->x)
|
|
|| Mth::floor(oldSource.y) != Mth::floor(current->y)
|
|
|| Mth::floor(oldSource.z) != Mth::floor(current->z)
|
|
|| oldSource.luminance != current->luminance) {
|
|
markTilesDirty(level, Mth::floor(oldSource.x), Mth::floor(oldSource.y), Mth::floor(oldSource.z), static_cast<int>(oldSource.luminance));
|
|
}
|
|
}
|
|
|
|
for (const DynamicLightSource& newSource : nextSources) {
|
|
auto previous = std::find_if(previousSources.begin(), previousSources.end(),
|
|
[&newSource](const DynamicLightSource& source) { return source.entity == newSource.entity; });
|
|
if (previous == previousSources.end()
|
|
|| Mth::floor(previous->x) != Mth::floor(newSource.x)
|
|
|| Mth::floor(previous->y) != Mth::floor(newSource.y)
|
|
|| Mth::floor(previous->z) != Mth::floor(newSource.z)
|
|
|| previous->luminance != newSource.luminance) {
|
|
markTilesDirty(level, Mth::floor(newSource.x), Mth::floor(newSource.y), Mth::floor(newSource.z), static_cast<int>(newSource.luminance));
|
|
}
|
|
}
|
|
previousSources.assign(nextSources.begin(), nextSources.end());
|
|
}
|
|
} |