KWeatherMgr.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:3k
源码类别:

模拟服务器

开发平台:

C/C++

  1. #include "KCore.h"
  2. #include "KSubWorldSet.h"
  3. #include "KWeatherMgr.h"
  4. #ifdef _SERVER
  5. KWeatherMgr::KWeatherMgr()
  6. {
  7. m_bInWeather = false;
  8. m_nWeatherNum = 0;
  9. m_nFullOdds = 0;
  10. m_nCurWeather = WEATHERID_NOTHING;
  11. m_nHappenTimeMin = 300 * 20;
  12. m_nHappenTimeMax = 1000 * 20;
  13. m_nLastHappentime = g_SubWorldSet.GetGameTime();
  14. m_nNextHappentime = m_nLastHappentime + 500 * 20;
  15. m_nWeatherDuration = 0;
  16. }
  17. // IniFile:已经打开的.wor文件
  18. bool KWeatherMgr::InitFromIni(KIniFile &IniFile)
  19. {
  20. KIniFile IniFileWeather;
  21. // 打开游戏中所有天气定义的ini文件
  22. if(!IniFileWeather.Load("\settings\Weather\Weather.ini"))
  23. return false;
  24. IniFile.GetInteger("Weather", "WeatherNum", 0, &m_nWeatherNum);
  25. if(m_nWeatherNum > 4)
  26. m_nWeatherNum = 4;
  27. m_nFullOdds = 0;
  28. char cc[40];
  29. for(int i=0; i<m_nWeatherNum; i++)
  30. {
  31. sprintf(cc, "Weather%d",i);
  32. IniFile.GetInteger("Weather", cc, 0, &m_weatherInfo[i].m_weatherID);
  33. sprintf(cc, "Odds%d",i);
  34. IniFile.GetInteger("Weather", cc, 0, &m_weatherInfo[i].m_nOdds);
  35. m_nFullOdds += m_weatherInfo[i].m_nOdds;
  36. sprintf(cc, "Weather%.3d",i);
  37. IniFileWeather.GetInteger(cc, "LifeTimeMin", 200, &m_weatherInfo[i].m_nLifeTimeMin);
  38. IniFileWeather.GetInteger(cc, "LifeTimeMax", 600, &m_weatherInfo[i].m_nLifeTimeMax);
  39. m_weatherInfo[i].m_nLifeTimeMin *= 20;
  40. m_weatherInfo[i].m_nLifeTimeMax *= 20;
  41. }
  42. if(m_nFullOdds < 100)
  43. m_nFullOdds = 100;
  44. IniFile.GetInteger("Weather", "HappenTimeMin", 300, &m_nHappenTimeMin);
  45. IniFile.GetInteger("Weather", "HappenTimeMax", 1000, &m_nHappenTimeMax);
  46. if(m_nHappenTimeMax < m_nHappenTimeMin)
  47. {
  48. int n = m_nHappenTimeMax;
  49. m_nHappenTimeMax = m_nHappenTimeMin;
  50. m_nHappenTimeMin = n;
  51. }
  52. m_nHappenTimeMin *= 20;
  53. m_nHappenTimeMax *= 20;
  54. m_nNextHappentime = m_nLastHappentime + m_nHappenTimeMin + g_Random(m_nHappenTimeMax - m_nHappenTimeMin);
  55. return true;
  56. }
  57. int KWeatherMgr::Activate()
  58. {
  59. int tmCur = g_SubWorldSet.GetGameTime();
  60. int i;
  61. if(!m_bInWeather)
  62. {
  63. if(tmCur > m_nNextHappentime)
  64. {
  65. // 时间到,发生一次天气
  66. int nOdds = g_Random(m_nFullOdds);
  67. m_nWeatherDuration = 0;
  68. for(i=0; i<m_nWeatherNum; i++)
  69. {
  70. if(nOdds < m_weatherInfo[i].m_nOdds)
  71. {
  72. m_nCurWeather = m_weatherInfo[i].m_weatherID;
  73. m_nWeatherDuration = m_weatherInfo[i].m_nLifeTimeMin 
  74. + g_Random(m_weatherInfo[i].m_nLifeTimeMax - m_weatherInfo[i].m_nLifeTimeMin);
  75. break;
  76. }
  77. else
  78. nOdds -= m_weatherInfo[i].m_nOdds;
  79. }
  80. m_nLastHappentime = tmCur;
  81. m_nNextHappentime = m_nLastHappentime + m_nWeatherDuration + m_nHappenTimeMin + g_Random(m_nHappenTimeMax - m_nHappenTimeMin);
  82. m_bInWeather = true;
  83. }
  84. }
  85. else
  86. {
  87. if(tmCur - m_nLastHappentime > m_nWeatherDuration)
  88. {
  89. // 时间到,天气停止
  90. m_nCurWeather = WEATHERID_NOTHING;
  91. m_bInWeather = false;
  92. }
  93. }
  94. return m_nCurWeather;
  95. }
  96. #endif