RespawnTimer.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. namespace Server.MirEnvir
  2. {
  3. public class RespawnSave
  4. {
  5. public bool Spawned = false;
  6. public ulong NextSpawnTick = 0;
  7. public int RespawnIndex = 0;
  8. public RespawnSave() { }
  9. public RespawnSave(BinaryReader reader)
  10. {
  11. Spawned = reader.ReadBoolean();
  12. NextSpawnTick = reader.ReadUInt64();
  13. RespawnIndex = reader.ReadInt32();
  14. }
  15. public void Save(BinaryWriter writer)
  16. {
  17. writer.Write(Spawned);
  18. writer.Write(NextSpawnTick);
  19. writer.Write(RespawnIndex);
  20. }
  21. }
  22. public class RespawnTickOption
  23. {
  24. public int UserCount = 1;
  25. public double DelayLoss = 1.0;
  26. public RespawnTickOption() { }
  27. public RespawnTickOption(BinaryReader reader)
  28. {
  29. UserCount = reader.ReadInt32();
  30. DelayLoss = reader.ReadDouble();
  31. }
  32. public void Save(BinaryWriter writer)
  33. {
  34. writer.Write(UserCount);
  35. writer.Write(DelayLoss);
  36. }
  37. public override string ToString()
  38. {
  39. return string.Format("+{0} users", UserCount);
  40. }
  41. }
  42. public class RespawnTimer
  43. {
  44. protected static Envir Envir
  45. {
  46. get { return Envir.Main; }
  47. }
  48. public byte BaseSpawnRate = 20;//amount of minutes between respawnticks (with no bonus)
  49. public ulong CurrentTickcounter = 0; //counter used to respawn everything
  50. public long LastTick = 0; //what 'time' was the last tick?
  51. public int LastUsercount = 0; //stops it from having to check delay each time
  52. public long CurrentDelay = 0;
  53. public List<RespawnTickOption> Respawn = new List<RespawnTickOption>();
  54. public RespawnTimer()
  55. {
  56. RespawnTickOption Option = new RespawnTickOption { UserCount = 0, DelayLoss = 1.0 };
  57. Respawn.Add(Option);
  58. //LastTick = Envir.Time;
  59. }
  60. public RespawnTimer(BinaryReader reader)
  61. {
  62. BaseSpawnRate = reader.ReadByte();
  63. CurrentTickcounter = reader.ReadUInt64();
  64. LastTick = Envir.Time;
  65. Respawn.Clear();
  66. int Optioncount = reader.ReadInt32();
  67. for (int i = 0; i < Optioncount; i++)
  68. {
  69. RespawnTickOption Option = new RespawnTickOption(reader);
  70. Respawn.Add(Option);
  71. }
  72. CurrentDelay = (long)Math.Round((double)BaseSpawnRate * (double)60000);
  73. }
  74. public void Save(BinaryWriter writer)
  75. {
  76. writer.Write(BaseSpawnRate);
  77. writer.Write(CurrentTickcounter);
  78. writer.Write(Respawn.Count);
  79. foreach (RespawnTickOption Option in Respawn)
  80. Option.Save(writer);
  81. }
  82. public void Process()
  83. {
  84. //by always rechecking tickspeed we reduce the chance of having respawns get silly on situations where usercount goes up or down fast (like say after a server reboot)
  85. GetTickSpeed();
  86. if (Envir.Time >= (LastTick + CurrentDelay))
  87. {
  88. CurrentTickcounter++;
  89. if (CurrentTickcounter == long.MaxValue) //by using long instead of ulong here you basicaly have a huge safe zone on the respawn ticks of mobs
  90. {
  91. CurrentTickcounter = 0;
  92. }
  93. LastTick = Envir.Time;
  94. }
  95. }
  96. public void GetTickSpeed()
  97. {
  98. if (LastUsercount == Envir.PlayerCount) return;
  99. LastUsercount = Envir.PlayerCount;
  100. double bonus = 1.0;
  101. foreach (RespawnTickOption Option in Respawn)
  102. {
  103. if (Option.UserCount <= LastUsercount)
  104. bonus = Math.Min(bonus, Option.DelayLoss);
  105. }
  106. CurrentDelay = (long)Math.Round((BaseSpawnRate * 60000) * bonus);
  107. }
  108. }
  109. }