2 Commits
1.6.1 ... 1.6.2

Author SHA1 Message Date
mikx
11193f2472 (1.6.2) Level are server-wide. 2026-02-16 04:04:49 -05:00
mikx
fb96f17b55 (1.6.1) Readme Update 2026-02-15 05:58:33 -05:00
6 changed files with 117 additions and 164 deletions

View File

@@ -15,30 +15,48 @@ namespace MxValheim.EventSystem
return (level * 100); return (level * 100);
} }
public static void AddExperience(string playerid, int amount) public static float CalculateExpMultiplier(int level, Heightmap.Biome biome)
{ {
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_RequestProfile", playerid); Dictionary<int, float> biomeBase = new Dictionary<int, float>()
int level = Profiles.playerLevel; {
int exp = Profiles.playerExperience; { 1, 1.0f },
{ 2, 1.5f },
{ 8, 2.0f },
{ 4, 2.5f },
{ 16, 3.0f },
{ 256, 3.5f },
{ 32, 4.0f },
};
float rewardExpMulti = biomeBase[((int)biome)];
return rewardExpMulti;
}
public static void AddExperience(int amount)
{
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_RequestProfile", "Event");
int level = Profiles.serverLevel;
int exp = Profiles.serverExperience;
exp += amount; exp += amount;
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_ServerSetUserExperience", playerid, exp); ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_ServerSetExperience", exp);
exp = Profiles.playerExperience; exp = Profiles.serverExperience;
int maxexp = GetRequiredExpByLevel(level); int maxexp = GetRequiredExpByLevel(level);
UpdateProgressBar(playerid); UpdateProgressBar();
if (exp >= maxexp) if (exp >= maxexp)
{ {
exp -= maxexp; exp -= maxexp;
level++; level++;
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_ServerSetUserLevel", playerid, level); ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_ServerSetLevel", level);
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_ServerSetUserExperience", playerid, exp); ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_ServerSetExperience", exp);
UpdateProgressBar(playerid); UpdateProgressBar();
} }
} }
public static void UpdateProgressBar(string playerid) public static void UpdateProgressBar()
{ {
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_RequestProfile", playerid); ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_RequestProfile", "Event");
float expPercentage = ((float)Profiles.playerExperience / GetRequiredExpByLevel(Profiles.playerLevel)); float expPercentage = ((float)Profiles.serverExperience / GetRequiredExpByLevel(Profiles.serverLevel));
if (Clock.m_barFill != null) if (Clock.m_barFill != null)
{ {

View File

@@ -24,14 +24,7 @@ namespace MxValheim.EventSystem
{ {
static void Postfix(Player __instance) static void Postfix(Player __instance)
{ {
// Only trigger for the actual human player, not NPCs/Dummies
if (__instance == Player.m_localPlayer)
{
string pid = GetUserPlayerID(Player.m_localPlayer).ToString();
ZLog.Log($"[ProfileSystem] Spawning finished. Requesting profile for {pid}");
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_RequestProfile", pid);
}
} }
} }
@@ -40,10 +33,7 @@ namespace MxValheim.EventSystem
{ {
static void Prefix(RandEventSystem __instance) static void Prefix(RandEventSystem __instance)
{ {
// Use a small timer check so we don't spam the logic every single frame
// though for debugging, forcing these values is fine.
//__instance.m_eventIntervalMin = 10f;
//__instance.m_eventChance = 100f;
} }
} }
@@ -52,10 +42,47 @@ namespace MxValheim.EventSystem
{ {
static void Prefix(RandEventSystem __instance) static void Prefix(RandEventSystem __instance)
{ {
__instance.m_eventIntervalMin = 10f;
__instance.m_eventChance = 100f;
} }
} }
[HarmonyPatch(typeof(RandEventSystem), nameof(RandEventSystem.RPC_SetEvent))]
public static class RandEventEndPatch
{
private static readonly AccessTools.FieldRef<RandEventSystem, RandomEvent> ActiveEventField = AccessTools.FieldRefAccess<RandEventSystem, RandomEvent>("m_activeEvent");
static void Prefix(RandEventSystem __instance, string eventName)
{
RandomEvent currentRaid = ActiveEventField(__instance);
if (string.IsNullOrEmpty(eventName) && currentRaid != null)
{
Vector3 playerPos = Player.m_localPlayer.transform.position;
float distance = Vector3.Distance(playerPos, currentRaid.m_pos);
if (distance <= 96f)
{
Heightmap.Biome currentBiome = EnvMan.instance.GetCurrentBiome();
Reward.GiveItemStatic("Coins", Reward.CalculateRewardCoin(Profiles.serverLevel, currentBiome));
System.Random rand = new System.Random();
Reward.GiveItemStatic("Feathers", rand.Next(5, 20));
if (rand.Next(100) < 50)
{
Reward.GiveItemRandom(Reward.GetRewardPrefab(Profiles.serverLevel, "food"), 1);
}
if (rand.Next(100) < 25)
{
Reward.GiveItemRandom(Reward.GetRewardPrefab(Profiles.serverLevel, "heal"), 1);
}
if (rand.Next(100) < 10)
{
Reward.GiveItemRandom(Reward.GetRewardPrefab(Profiles.serverLevel, "metal"), rand.Next(5, 20));
}
}
}
}
}
/*
[HarmonyPatch(typeof(Hud), nameof(Hud.Update))] [HarmonyPatch(typeof(Hud), nameof(Hud.Update))]
public static class Hud_MonitorPatch public static class Hud_MonitorPatch
{ {
@@ -71,36 +98,14 @@ namespace MxValheim.EventSystem
// 2. Logic: Detection // 2. Logic: Detection
if (activeEvent != null && _lastKnownEvent == null) if (activeEvent != null && _lastKnownEvent == null)
{ {
Debug.Log($"MxEvent: HUD Heartbeat detected Raid Start: {activeEvent.m_name}");
_lastKnownEvent = activeEvent;
// Force HUD Creation and Display
//UpdateWave(true);
} }
else if (activeEvent == null && _lastKnownEvent != null) else if (activeEvent == null && _lastKnownEvent != null)
{ {
Debug.Log("MxEvent: HUD Heartbeat detected Raid End.");
string pid = GetUserPlayerID(Player.m_localPlayer).ToString();
Heightmap.Biome currentBiome = EnvMan.instance.GetCurrentBiome();
Reward.GiveItemStatic("Coins",Reward.CalculateRewardCoin(Profiles.playerLevel, currentBiome));
System.Random rand = new System.Random();
Reward.GiveItemStatic("Feathers", rand.Next(1,10));
if (rand.Next(100) < 50)
{
Reward.GiveItemRandom(Reward.GetRewardPrefab(Profiles.playerLevel, "food"), 1);
}
if (rand.Next(100) < 25)
{
Reward.GiveItemRandom(Reward.GetRewardPrefab(Profiles.playerLevel, "heal"), 1);
}
if (rand.Next(100) < 10)
{
Reward.GiveItemRandom(Reward.GetRewardPrefab(Profiles.playerLevel, "metal"), 1);
}
_lastKnownEvent = null;
} }
} }
} }*/
[HarmonyPatch(typeof(Character), nameof(Character.ApplyDamage))] [HarmonyPatch(typeof(Character), nameof(Character.ApplyDamage))]
public static class DeathNotifier public static class DeathNotifier
@@ -118,44 +123,25 @@ namespace MxValheim.EventSystem
// 2. Check if a raid is active // 2. Check if a raid is active
RandomEvent activeEvent = RandEventSystem.instance.GetActiveEvent(); RandomEvent activeEvent = RandEventSystem.instance.GetActiveEvent();
if (activeEvent == null) return; if (activeEvent == null) return;
Debug.Log("MxEvent: Confirmed Active Event!");
Vector3 playerPos = Player.m_localPlayer.transform.position; Vector3 playerPos = Player.m_localPlayer.transform.position;
string pid = GetUserPlayerID(Player.m_localPlayer).ToString();
Debug.Log($"EventSystem:ApplyDamage User {pid}");
float distance = Vector3.Distance(playerPos, activeEvent.m_pos); float distance = Vector3.Distance(playerPos, activeEvent.m_pos);
Debug.Log($"MxEvent: Distance: {distance}");
if (distance <= 96f) if (distance <= 96f)
{ {
int level = __instance.GetLevel(); int level = __instance.GetLevel();
int exp = int exp =
(level == 1) ? 2: (level == 1) ? 2 :
(level == 2) ? 3: (level == 2) ? 4 :
(level == 3) ? 4: (level == 3) ? 6 :
5; 6;
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_RequestProfile", "Event");
Experience.AddExperience(pid, exp); Heightmap.Biome currentBiome = EnvMan.instance.GetCurrentBiome();
int expFinal = (int)(exp * Experience.CalculateExpMultiplier(Profiles.serverLevel, currentBiome));
/* Send the update Experience.AddExperience(expFinal);
ZRoutedRpc.instance.InvokeRoutedRPC(
ZRoutedRpc.Everybody,
"RPC_UpdateRaidHUD",
$"PHASE: {CurrentPhase}",
KillsInCurrentPhase,
GetTargetForPhase(CurrentPhase)
);*/
} }
} }
} }
} }
// Hide HUD when raid ends
[HarmonyPatch(typeof(RandEventSystem), nameof(RandEventSystem.ResetRandomEvent))]
[HarmonyPostfix]
static void OnRaidEnd()
{
}
} }
} }

View File

@@ -12,15 +12,13 @@ namespace MxValheim.EventSystem
{ {
public class ProfileRoot public class ProfileRoot
{ {
public string playerid { get; set; }
public string player_name { get; set; }
public int level { get; set; } public int level { get; set; }
public int experience { get; set; } public int experience { get; set; }
} }
public static bool userExists = false; public static bool userExists = false;
public static int playerLevel = 1; public static int serverLevel = 1;
public static int playerExperience = 0; public static int serverExperience = 0;
public static void ServerUserExists(string playerid) public static void ServerUserExists(string playerid)
@@ -28,9 +26,8 @@ namespace MxValheim.EventSystem
// Check to ensure this is actually the server running this // Check to ensure this is actually the server running this
ZNet zn = new ZNet(); ZNet zn = new ZNet();
if (!zn.IsDedicated()) return; if (!zn.IsDedicated()) return;
string profilePath = Path.Combine(MxValheimMod.internalDataEventSystemPath, $"{playerid}.json");
if (File.Exists(profilePath)) userExists = true; if (File.Exists(MxValheimMod.internalDataEventSystem)) userExists = true;
} }
public static void ServerReceiveCreateUser(long sender, string playerid, string playerName) public static void ServerReceiveCreateUser(long sender, string playerid, string playerName)
@@ -39,129 +36,80 @@ namespace MxValheim.EventSystem
ZNet zn = new ZNet(); ZNet zn = new ZNet();
if (!zn.IsDedicated()) return; if (!zn.IsDedicated()) return;
string profilePath = Path.Combine(MxValheimMod.internalDataEventSystemPath, $"{playerid}.json");
ProfileRoot pr = new ProfileRoot ProfileRoot pr = new ProfileRoot
{ {
playerid = playerid,
player_name = playerName,
level = 1, level = 1,
experience = 0 experience = 0
}; };
var raw = JsonConvert.SerializeObject(pr, Formatting.Indented); var raw = JsonConvert.SerializeObject(pr, Formatting.Indented);
File.WriteAllText(profilePath, raw); File.WriteAllText(MxValheimMod.internalDataEventSystem, raw);
} }
public static void ServerHandleProfileRequest(long sender, string playerid) public static void ServerHandleProfileRequest(long sender, string required)
{ {
// Check to ensure this is actually the server running this // Check to ensure this is actually the server running this
ZNet zn = new ZNet(); ZNet zn = new ZNet();
if (!zn.IsDedicated()) return; if (!zn.IsDedicated()) return;
ZNetPeer peer = ZNet.instance.GetPeer(sender); if (File.Exists(MxValheimMod.internalDataEventSystem))
string profilePath = Path.Combine(MxValheimMod.internalDataEventSystemPath, $"{playerid}.json");
if (File.Exists(profilePath))
{ {
string rawJson = File.ReadAllText(profilePath); string rawJson = File.ReadAllText(MxValheimMod.internalDataEventSystem);
// Send ONLY to the 'sender' who requested it
ZRoutedRpc.instance.InvokeRoutedRPC(sender, "RPC_ClientReceiveProfile", rawJson); ZRoutedRpc.instance.InvokeRoutedRPC(sender, "RPC_ClientReceiveProfile", rawJson);
} }
else else
{ {
ProfileRoot pr = new ProfileRoot ProfileRoot pr = new ProfileRoot
{ {
playerid = playerid,
player_name = peer.m_playerName,
level = 1, level = 1,
experience = 0 experience = 0
}; };
var raw = JsonConvert.SerializeObject(pr, Formatting.Indented); var raw = JsonConvert.SerializeObject(pr, Formatting.Indented);
File.WriteAllText(profilePath, raw); File.WriteAllText(MxValheimMod.internalDataEventSystem, raw);
} }
} }
public static void RequestCreateUser(string playerid, Player player) public static void LoadProfile(long sender, string json)
{
string playerName = player.GetHoverName();
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_ServerCreateUser", playerid, playerName);
}
public static void ClientLoadProfile(long sender, string json)
{ {
ProfileRoot loadedProfile = JsonConvert.DeserializeObject<ProfileRoot>(json); ProfileRoot loadedProfile = JsonConvert.DeserializeObject<ProfileRoot>(json);
playerLevel = loadedProfile.level; serverLevel = loadedProfile.level;
playerExperience = loadedProfile.experience; serverExperience = loadedProfile.experience;
//ZLog.Log($"[Client] Profile loaded! Level: {loadedProfile.level}");
} }
public static void CreateUser(string playerid, Player player) public static void ServerSetExperience(long sender, int exp)
{
// We only send the raw data; we let the server handle the file I/O
string playerName = player?.GetHoverName() ?? "Unknown";
// Invoke the RPC on the Server (0 is the server ID)
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_ServerCreateUser", playerid, playerName);
}
public static void ServerSetUserExperience(long sender, string playerid, int exp)
{ {
// Check to ensure this is actually the server running this // Check to ensure this is actually the server running this
ZNet zn = new ZNet(); ZNet zn = new ZNet();
if (!zn.IsDedicated()) return; if (!zn.IsDedicated()) return;
string profilePath = Path.Combine(MxValheimMod.internalDataEventSystemPath, $"{playerid}.json"); ProfileRoot pro = JsonConvert.DeserializeObject<ProfileRoot>(File.ReadAllText(MxValheimMod.internalDataEventSystem));
ProfileRoot pro = JsonConvert.DeserializeObject<ProfileRoot>(File.ReadAllText(profilePath));
ProfileRoot pr = new ProfileRoot ProfileRoot pr = new ProfileRoot
{ {
playerid = playerid,
player_name = pro.player_name,
level = pro.level, level = pro.level,
experience = exp experience = exp
}; };
var raw = JsonConvert.SerializeObject(pr, Formatting.Indented); var raw = JsonConvert.SerializeObject(pr, Formatting.Indented);
File.WriteAllText(profilePath, raw); File.WriteAllText(MxValheimMod.internalDataEventSystem, raw);
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_RequestProfile", playerid); ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_RequestProfile", "Event");
} }
public static void SetUserExperience(string playerid, int exp) public static void ServerSetLevel(long sender, int level)
{
string profilePath = Path.Combine(MxValheimMod.internalDataEventSystemPath, $"{playerid}.json");
ProfileRoot pro = JsonConvert.DeserializeObject<ProfileRoot>(File.ReadAllText(profilePath));
ProfileRoot pr = new ProfileRoot
{
playerid = playerid,
player_name = pro.player_name,
level = pro.level,
experience = exp
};
var raw = JsonConvert.SerializeObject(pr, Formatting.Indented);
File.WriteAllText(profilePath, raw);
}
public static void ServerSetUserLevel(long sender, string playerid, int level)
{ {
// Check to ensure this is actually the server running this // Check to ensure this is actually the server running this
ZNet zn = new ZNet(); ZNet zn = new ZNet();
if (!zn.IsDedicated()) return; if (!zn.IsDedicated()) return;
string profilePath = Path.Combine(MxValheimMod.internalDataEventSystemPath, $"{playerid}.json"); ProfileRoot pro = JsonConvert.DeserializeObject<ProfileRoot>(File.ReadAllText(MxValheimMod.internalDataEventSystem));
ProfileRoot pro = JsonConvert.DeserializeObject<ProfileRoot>(File.ReadAllText(profilePath));
ProfileRoot pr = new ProfileRoot ProfileRoot pr = new ProfileRoot
{ {
playerid = playerid,
player_name = pro.player_name,
level = level, level = level,
experience = pro.experience experience = pro.experience
}; };
var raw = JsonConvert.SerializeObject(pr, Formatting.Indented); var raw = JsonConvert.SerializeObject(pr, Formatting.Indented);
File.WriteAllText(profilePath, raw); File.WriteAllText(MxValheimMod.internalDataEventSystem, raw);
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_RequestProfile", playerid); ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_RequestProfile", "Event");
} }
} }
} }

View File

@@ -15,11 +15,11 @@ namespace MxValheim.EventSystem
{ {
{ 1, 10 }, { 1, 10 },
{ 2, 20 }, { 2, 20 },
{ 4, 30 }, { 8, 30 },
{ 8, 40 }, { 4, 40 },
{ 16, 50 }, { 16, 50 },
{ 512, 60 }, { 256, 60 },
{ 64, 70 }, { 32, 70 },
}; };
int rewardCoin = biomeBase[((int)biome)] * level; int rewardCoin = biomeBase[((int)biome)] * level;

View File

@@ -28,7 +28,7 @@ public class MxValheimMod : BaseUnityPlugin
private const string ModGUID = "ovh.mxdev.mxvalheim"; private const string ModGUID = "ovh.mxdev.mxvalheim";
private const string ModName = "MxValheim"; private const string ModName = "MxValheim";
private const string ModVersion = "1.6.1"; private const string ModVersion = "1.6.2";
public static ConfigEntry<bool> Config_Locked; public static ConfigEntry<bool> Config_Locked;
public static ConfigEntry<int> Config_OreMultiplier; public static ConfigEntry<int> Config_OreMultiplier;
@@ -42,7 +42,7 @@ public class MxValheimMod : BaseUnityPlugin
public static string modPath = Path.Combine(Paths.PluginPath, "MxValheim"); public static string modPath = Path.Combine(Paths.PluginPath, "MxValheim");
public static string internalConfigsPath = Path.Combine(modPath, "Configs"); public static string internalConfigsPath = Path.Combine(modPath, "Configs");
public static string internalDataPath = Path.Combine(modPath, "Data"); public static string internalDataPath = Path.Combine(modPath, "Data");
public static string internalDataEventSystemPath = Path.Combine(internalDataPath, "EventSystem"); public static string internalDataEventSystem = Path.Combine(internalDataPath, "EventSystem.json");
public static string WeightConfigPath => Path.Combine(internalConfigsPath, "items_weight.json"); public static string WeightConfigPath => Path.Combine(internalConfigsPath, "items_weight.json");
public static string AutoDoorConfigPath => Path.Combine(internalConfigsPath, "auto_doors.json"); public static string AutoDoorConfigPath => Path.Combine(internalConfigsPath, "auto_doors.json");
public static DoorRoot CachedConfig; public static DoorRoot CachedConfig;
@@ -81,6 +81,8 @@ public class MxValheimMod : BaseUnityPlugin
public static readonly Queue<string> _doorQueueName = new Queue<string>(); public static readonly Queue<string> _doorQueueName = new Queue<string>();
public static float _doorTimer; public static float _doorTimer;
public static readonly Queue<RandomEvent> _serverEvent = new Queue<RandomEvent>();
void Awake() void Awake()
{ {
Instance = this; Instance = this;
@@ -139,10 +141,9 @@ public class MxValheimMod : BaseUnityPlugin
ZRoutedRpc.instance.Register<string, string>("RPC_ServerCreateUser", Profiles.ServerReceiveCreateUser); ZRoutedRpc.instance.Register<string, string>("RPC_ServerCreateUser", Profiles.ServerReceiveCreateUser);
ZRoutedRpc.instance.Register<string>("RPC_RequestProfile", Profiles.ServerHandleProfileRequest); ZRoutedRpc.instance.Register<string>("RPC_RequestProfile", Profiles.ServerHandleProfileRequest);
ZRoutedRpc.instance.Register<string>("RPC_ClientReceiveProfile", Profiles.ClientLoadProfile); ZRoutedRpc.instance.Register<string>("RPC_ClientReceiveProfile", Profiles.LoadProfile);
ZRoutedRpc.instance.Register<string>("RPC_UpdateProgressBar", Profiles.ClientLoadProfile); ZRoutedRpc.instance.Register<int>("RPC_ServerSetExperience", Profiles.ServerSetExperience);
ZRoutedRpc.instance.Register<string, int>("RPC_ServerSetUserExperience", Profiles.ServerSetUserExperience); ZRoutedRpc.instance.Register<int>("RPC_ServerSetLevel", Profiles.ServerSetLevel);
ZRoutedRpc.instance.Register<string, int>("RPC_ServerSetUserLevel", Profiles.ServerSetUserLevel);
} }
} }
} }
@@ -153,7 +154,7 @@ public class MxValheimMod : BaseUnityPlugin
if (Player.m_localPlayer != null) if (Player.m_localPlayer != null)
{ {
string pid = GetUserPlayerID(Player.m_localPlayer).ToString(); string pid = GetUserPlayerID(Player.m_localPlayer).ToString();
Experience.UpdateProgressBar(pid); Experience.UpdateProgressBar();
} }
// Only run if we are actually in the game world // Only run if we are actually in the game world
@@ -177,7 +178,7 @@ public class MxValheimMod : BaseUnityPlugin
if (initExpBar) if (initExpBar)
{ {
string pid = GetUserPlayerID(Player.m_localPlayer).ToString(); string pid = GetUserPlayerID(Player.m_localPlayer).ToString();
Experience.UpdateProgressBar(pid); Experience.UpdateProgressBar();
initExpBar = false; initExpBar = false;
} }
} }
@@ -188,7 +189,7 @@ public class MxValheimMod : BaseUnityPlugin
string timeString = clk.GetFormattedTime(); string timeString = clk.GetFormattedTime();
int day = EnvMan.instance.GetDay(); int day = EnvMan.instance.GetDay();
string pid = GetUserPlayerID(Player.m_localPlayer).ToString(); string pid = GetUserPlayerID(Player.m_localPlayer).ToString();
Clock.m_textComponent.text = $"<color=#32a852>{Localization.instance.Localize("$clock_string_rank")} {Profiles.playerLevel}</color> <color=#677074>|</color> <color=#32a852>{Localization.instance.Localize("$clock_string_day")} {day}</color> <color=#677074>|</color> <color=#d1954a>{timeString}</color>"; Clock.m_textComponent.text = $"<color=#32a852>{Localization.instance.Localize("$clock_string_rank")} {Profiles.serverLevel}</color> <color=#677074>|</color> <color=#32a852>{Localization.instance.Localize("$clock_string_day")} {day}</color> <color=#677074>|</color> <color=#d1954a>{timeString}</color>";
} }
} }

View File

@@ -1,4 +1,4 @@
![logo](https://mxdev.ovh/wp-content/uploads/2025/09/mxdev-1.png) ![logo](https://mxio.ovh/gaming/valheim/mxvalheim.png)
## MxValheim ## MxValheim
Official **MxValheim Server** Mod. Official **MxValheim Server** Mod.