YALootBoxes/scripts/4_World/YALootBoxes_Lootbox.c
smthspec a84b6b1a2b Phase 2: structural/completeness fixes (correctness pass)
- config.cpp: fix script/config binding. Crates now inherit a shared
  YALootBoxes_Base config class (name-matches the script class) instead of
  relying on the non-binding simulationModule line. Base class changed from
  non-existent ContainerObject to vanilla Container_Base.
- Lootbox.c: same base class change; replace never-called GetActions
  override with the engine-correct SetActions()/AddAction().
- ActionOpen.c: BROKEN_BONE now uses player.SetBrokenLegs(eBrokenLegs.
  BROKEN_LEGS) instead of bogus GetHealthDamageManager().SetFracture();
  DecreaseHealth health-type arg empty-string changed to "Health" (3 sites).
- Config.c: restructure loot lists from a string-keyed map to a flat array
  of YALB_LootList{type,entries} for reliable JSON deserialization;
  GetLootListForType updated to linear scan; position made a ref array;
  document failEffects.chance as a weight, not a probability.
- loot_lists.json (configfiles.readme): match new array schema.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-19 21:49:37 +03:00

158 lines
No EOL
4.9 KiB
C

// ============================================================
// YALootBoxes_Lootbox.c
// The actual in-world lootbox entity.
// Lives in: scripts/4_World/YALootBoxes_Lootbox.c
//
// NOTE ON className: each entry in box_types.json should have a
// matching config.cpp class inheriting from YALootBoxes_Base
// (see config.cpp artifact). This script class provides the
// shared behavior for ALL of them.
// ============================================================
class YALootBoxes_Base extends Container_Base
{
protected bool m_IsLocked;
protected string m_BoxTypeId; // matches "type" in box_types.json
protected float m_SpawnTime; // world time (seconds) when spawned
protected float m_UnlockedTime; // world time when it became unlocked (-1 if still locked)
protected ref YALB_BoxTypeDef m_TypeDef; // cached definition
void YALootBoxes_Base()
{
m_IsLocked = true;
m_UnlockedTime = -1;
RegisterNetSyncVariableBool("m_IsLocked");
}
override void EEInit()
{
super.EEInit();
SetSynchDirty();
}
// Called by the manager right after spawning, before placing in world
void Init(string boxTypeId)
{
m_BoxTypeId = boxTypeId;
m_TypeDef = YALB_ConfigLoader.GetBoxTypeByType(boxTypeId);
m_IsLocked = true;
m_UnlockedTime = -1;
m_SpawnTime = GetGame().GetTime() / 1000.0;
if (!m_TypeDef)
{
YALB_Log("WARNING: Lootbox spawned with unknown type '" + boxTypeId + "'. Using fallback values.");
}
SetSynchDirty();
}
bool IsLocked()
{
return m_IsLocked;
}
string GetBoxTypeId()
{
return m_BoxTypeId;
}
YALB_BoxTypeDef GetTypeDef()
{
if (!m_TypeDef)
m_TypeDef = YALB_ConfigLoader.GetBoxTypeByType(m_BoxTypeId);
return m_TypeDef;
}
float GetSpawnTime()
{
return m_SpawnTime;
}
float GetUnlockedTime()
{
return m_UnlockedTime;
}
// How many minutes this box should live for, based on its current state
int GetDespawnMinutes()
{
if (m_TypeDef)
return m_TypeDef.despawnMinutes;
return YALB_ConfigLoader.MainConfig.DefaultDespawnMinutes;
}
// Returns true if (locked, never-opened) box has expired and should
// be removed/respawned elsewhere. Locked boxes use the SAME despawn
// timer as unlocked ones by design (keeps the loot table fresh even
// if nobody opens it) - manager checks this each cycle.
bool HasExpiredUnopened(float currentWorldTime)
{
if (!m_IsLocked) return false; // handled separately once opened
float ageMinutes = (currentWorldTime - m_SpawnTime) / 60.0;
return ageMinutes >= GetDespawnMinutes();
}
bool HasExpiredAfterOpening(float currentWorldTime)
{
if (m_IsLocked || m_UnlockedTime < 0) return false;
float ageMinutes = (currentWorldTime - m_UnlockedTime) / 60.0;
return ageMinutes >= GetDespawnMinutes();
}
// Called by YALB_OpenLootboxAction on a successful unlock roll.
// Converts this from a locked decoration into a real container
// populated with loot, then starts its post-open despawn clock.
void UnlockAndFill()
{
if (!m_IsLocked) return; // already unlocked, no-op
m_IsLocked = false;
m_UnlockedTime = GetGame().GetTime() / 1000.0;
SetSynchDirty();
PopulateLoot();
YALB_Log(string.Format("Lootbox '%1' (%2) unlocked and filled.", GetBoxTypeId(), GetPosition().ToString()));
}
protected void PopulateLoot()
{
array<ref YALB_LootEntry> lootList = YALB_ConfigLoader.GetLootListForType(m_BoxTypeId);
if (!lootList)
{
YALB_Log("No loot list found for type '" + m_BoxTypeId + "' - box will be empty.");
return;
}
foreach (YALB_LootEntry entry : lootList)
{
float roll = Math.RandomFloat01();
if (roll > entry.chance) continue;
int count = Math.RandomIntInclusive(entry.minCount, entry.maxCount);
for (int i = 0; i < count; i++)
{
GetInventory().CreateInInventory(entry.classname);
}
}
}
// Player-facing string used by the proximity message and the action menu
string GetRequiredItemsMessage()
{
if (!m_TypeDef) return "Locked (unknown requirements)";
return "Requires: " + m_TypeDef.GetRequiredItemsDisplayString();
}
// Hooks our custom hold-to-open action onto this object without
// needing to touch any vanilla cfgVehicles action arrays.
// NOTE: the engine-called override is SetActions()/AddAction(),
// NOT GetActions(out array<typename>) - the latter is never
// invoked by the action manager and silently registers nothing.
override void SetActions()
{
super.SetActions();
AddAction(YALB_ActionOpenLootbox);
}
};