9
3

Rules.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <assert.h>
  17. #include "../../common/debug.h"
  18. #include "../../common/Log.h"
  19. #include "../../common/database.h"
  20. #include "Rules.h"
  21. extern RuleManager rule_manager;
  22. Rule::Rule() {
  23. category = 0;
  24. type = 0;
  25. strncpy(value, "", sizeof(value));
  26. strncpy(combined, "NONE", sizeof(combined));
  27. }
  28. Rule::Rule(int32 category, int32 type, const char *value, const char *combined) {
  29. this->category = category;
  30. this->type = type;
  31. strncpy(this->value, value, sizeof(this->value));
  32. strncpy(this->combined, combined, sizeof(this->combined));
  33. }
  34. Rule::Rule (Rule *rule_in) {
  35. category = rule_in->GetCategory();
  36. type = rule_in->GetType();
  37. strncpy(value, rule_in->GetValue(), sizeof(value));
  38. strncpy(combined, rule_in->GetCombined(), sizeof(combined));
  39. }
  40. Rule::~Rule() {
  41. }
  42. RuleSet::RuleSet() {
  43. id = 0;
  44. memset(name, 0, sizeof(name));
  45. m_rules.SetName("RuleSet::rules");
  46. }
  47. RuleSet::RuleSet(RuleSet *in_rule_set) {
  48. assert(in_rule_set);
  49. map<int32, map<int32, Rule *> > * in_rules = in_rule_set->GetRules();
  50. map<int32, map<int32, Rule *> >::iterator itr;
  51. map<int32, Rule *>::iterator itr2;
  52. Rule * rule;
  53. m_rules.SetName("RuleSet::rules");
  54. id = in_rule_set->GetID();
  55. strncpy(name, in_rule_set->GetName(), sizeof(name));
  56. for (itr = in_rules->begin(); itr != in_rules->end(); itr++) {
  57. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  58. rule = itr2->second;
  59. rules[rule->GetCategory()][rule->GetType()] = new Rule(rule);
  60. }
  61. }
  62. }
  63. RuleSet::~RuleSet() {
  64. ClearRules();
  65. }
  66. void RuleSet::CopyRulesInto(RuleSet *in_rule_set) {
  67. assert(in_rule_set);
  68. map<int32, map<int32, Rule *> > * in_rules = in_rule_set->GetRules();
  69. map<int32, map<int32, Rule *> >::iterator itr;
  70. map<int32, Rule *>::iterator itr2;
  71. Rule * rule;
  72. ClearRules();
  73. m_rules.writelock(__FUNCTION__, __LINE__);
  74. for (itr = in_rules->begin(); itr != in_rules->end(); itr++) {
  75. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  76. rule = itr2->second;
  77. rules[rule->GetCategory()][rule->GetType()] = new Rule(rule);
  78. }
  79. }
  80. m_rules.releasewritelock(__FUNCTION__, __LINE__);
  81. }
  82. void RuleSet::AddRule(Rule *rule) {
  83. int32 category, type;
  84. assert(rule);
  85. category = rule->GetCategory();
  86. type = rule->GetType();
  87. m_rules.writelock(__FUNCTION__, __LINE__);
  88. if (rules[category].count(type) == 0)
  89. rules[category][type] = rule;
  90. else
  91. rules[category][type]->SetValue(rule->GetValue());
  92. m_rules.releasewritelock(__FUNCTION__, __LINE__);
  93. }
  94. Rule * RuleSet::GetRule(int32 category, int32 type) {
  95. Rule *ret = 0;
  96. m_rules.readlock(__FUNCTION__, __LINE__);
  97. if (rules[category].count(type) > 0)
  98. ret = rules[category][type];
  99. m_rules.releasereadlock(__FUNCTION__, __LINE__);
  100. if (!ret)
  101. ret = rule_manager.GetBlankRule();
  102. LogWrite(RULESYS__DEBUG, 5, "Rules", "Rule: %s, Value: %s", ret->GetCombined(), ret->GetValue());
  103. return ret;
  104. }
  105. Rule * RuleSet::GetRule(const char *category, const char *type) {
  106. map<int32, map<int32, Rule *> >::iterator itr;
  107. map<int32, Rule *>::iterator itr2;
  108. char combined[256];
  109. Rule *ret = 0;
  110. snprintf(combined, sizeof(combined), "%s:%s", category, type);
  111. // Zero terminate ([max - 1] = 0) to prevent a warning/error
  112. combined[255] = 0;
  113. m_rules.readlock(__FUNCTION__, __LINE__);
  114. for (itr = rules.begin(); itr != rules.end(); itr++) {
  115. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  116. if (!strcmp(itr2->second->GetCombined(), combined)) {
  117. ret = itr2->second;
  118. break;
  119. }
  120. }
  121. }
  122. m_rules.releasereadlock(__FUNCTION__, __LINE__);
  123. return ret;
  124. }
  125. void RuleSet::ClearRules() {
  126. map<int32, map<int32, Rule *> >::iterator itr;
  127. map<int32, Rule *>::iterator itr2;
  128. m_rules.writelock(__FUNCTION__, __LINE__);
  129. for (itr = rules.begin(); itr != rules.end(); itr++) {
  130. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++)
  131. safe_delete(itr2->second);
  132. }
  133. rules.clear();
  134. m_rules.releasewritelock(__FUNCTION__, __LINE__);
  135. }
  136. RuleManager::RuleManager() {
  137. m_rule_sets.SetName("RuleManager::rule_sets");
  138. m_global_rule_set.SetName("RuleManager::global_rule_set");
  139. m_zone_rule_sets.SetName("RuleManager::zone_rule_sets");
  140. #define RULE_INIT(category, type, value) rules[category][type] = new Rule(category, type, value, #category ":" #type)
  141. /* CLIENT */
  142. RULE_INIT(R_Client, ShowWelcomeScreen, "0");
  143. RULE_INIT(R_Client, GroupSpellsTimer, "1000");
  144. /* FACTION */
  145. RULE_INIT(R_Faction, AllowFactionBasedCombat, "1");
  146. /* GUILD */
  147. RULE_INIT(R_Guild, MaxLevel, "50");
  148. RULE_INIT(R_Guild, MaxPlayers, "-1");
  149. /* PLAYER */
  150. RULE_INIT(R_Player, MaxLevel, "50");
  151. RULE_INIT(R_Player, MaxLevelOverrideStatus, "100");
  152. RULE_INIT(R_Player, VitalityAmount, ".5");
  153. RULE_INIT(R_Player, VitalityFrequency, "3600");
  154. RULE_INIT(R_Player, XPMultiplier, "1.0");
  155. RULE_INIT(R_Player, TSXPMultiplier, "1.0");
  156. RULE_INIT(R_Player, MaxAA, "320");
  157. RULE_INIT(R_Player, MaxClassAA, "100");
  158. RULE_INIT(R_Player, MaxSubclassAA, "100");
  159. RULE_INIT(R_Player, MaxShadowsAA, "70");
  160. RULE_INIT(R_Player, MaxHeroicAA, "50");
  161. RULE_INIT(R_Player, MaxTradeskillAA, "40");
  162. RULE_INIT(R_Player, MaxPrestigeAA, "25");
  163. RULE_INIT(R_Player, MaxTradeskillPrestigeAA, "25");
  164. RULE_INIT(R_Player, MinLastNameLevel, "20");
  165. RULE_INIT(R_Player, MaxLastNameLength, "20");
  166. RULE_INIT(R_Player, MinLastNameLength, "4");
  167. /* PVP */
  168. RULE_INIT(R_PVP, AllowPVP, "0");
  169. /* SPAWN */
  170. RULE_INIT(R_Spawn, SpeedMultiplier, "300"); // note: this value was 1280 until 6/1/2009, then was 600 til Sep 2009, when it became 300...?
  171. RULE_INIT(R_Spawn, SpeedRatio, "0"); // was 1280/7.5 and 600/7.5 until it became 300.
  172. /* TIMER */
  173. /* UI */
  174. RULE_INIT(R_UI, MaxWhoResults, "20");
  175. RULE_INIT(R_UI, MaxWhoOverrideStatus, "200");
  176. /* WORLD */
  177. RULE_INIT(R_World, DefaultStartingZoneID, "1");
  178. RULE_INIT(R_World, EnablePOIDiscovery, "0");
  179. RULE_INIT(R_World, GamblingTokenItemID, "2");
  180. RULE_INIT(R_World, GuildAutoJoin, "0");
  181. RULE_INIT(R_World, GuildAutoJoinID, "1");
  182. RULE_INIT(R_World, GuildAutoJoinDefaultRankID, "7");
  183. RULE_INIT(R_World, MaxPlayers, "-1");
  184. RULE_INIT(R_World, MaxPlayersOverrideStatus, "100");
  185. RULE_INIT(R_World, ServerLocked, "0");
  186. RULE_INIT(R_World, ServerLockedOverrideStatus, "10");
  187. RULE_INIT(R_World, SyncZonesWithLogin, "1");
  188. RULE_INIT(R_World, SyncEquipWithLogin, "1");
  189. RULE_INIT(R_World, UseBannedIPsTable, "0");
  190. RULE_INIT(R_World, LinkDeadTimer, "120000"); // default: 2 minutes
  191. RULE_INIT(R_World, RemoveDisconnectedClientsTimer, "30000"); // default: 30 seconds
  192. RULE_INIT(R_World, PlayerCampTimer, "20"); // default: 20 seconds
  193. RULE_INIT(R_World, GMCampTimer, "1"); // default: 1 second
  194. RULE_INIT(R_World, AutoAdminPlayers, "0"); // default: No
  195. RULE_INIT(R_World, AutoAdminGMs, "0"); // default: No
  196. RULE_INIT(R_World, AutoAdminStatusValue, "10"); // default: 10 (CSR)
  197. RULE_INIT(R_World, DuskTime, "20:00"); // default: 8pm
  198. RULE_INIT(R_World, DawnTime, "8:00"); // default: 8am
  199. RULE_INIT(R_World, ThreadedLoad, "0"); // default: no threaded loading
  200. RULE_INIT(R_World, TradeskillSuccessChance, "87.0"); // default: 87% chance of success while crafting
  201. RULE_INIT(R_World, TradeskillCritSuccessChance, "2.0"); // default: 2% chance of critical success while crafting
  202. RULE_INIT(R_World, TradeskillFailChance, "10.0"); // default: 10% chance of failure while crafting
  203. RULE_INIT(R_World, TradeskillCritFailChance, "1.0"); // default: 1% chance of critical failure while crafting
  204. RULE_INIT(R_World, TradeskillEventChance, "15.0"); // default: 15% chance of a tradeskill event while crafting
  205. RULE_INIT(R_World, EditorURL, "www.eq2emulator.net"); // default: www.eq2emulator.net
  206. RULE_INIT(R_World, EditorIncludeID, "0"); // default: 0 (0 = disabled, 1 = enabled)
  207. RULE_INIT(R_World, EditorOfficialServer, "0"); // default: 0 (0 = disabled, 1 = enabled)
  208. RULE_INIT(R_World, IRCEnabled, "0"); // default: 0 (0 = disabled, 1 = enabled)
  209. RULE_INIT(R_World, IRCGlobalEnabled, "0"); // default: 0 (0 = disabled, 1 = enabled)
  210. RULE_INIT(R_World, IRCAddress, "irc.eq2emulator.net"); // default: irc.eq2emulator.net
  211. RULE_INIT(R_World, IRCPort, "6667"); // default: 6667
  212. RULE_INIT(R_World, IRCChan, "#EQ2Emu"); // default: #EQ2Emu
  213. RULE_INIT(R_World, SavePaperdollImage, "1"); // default: true
  214. RULE_INIT(R_World, SaveHeadshotImage, "1"); // default: true
  215. RULE_INIT(R_World, SendPaperdollImagesToLogin, "1"); // default: true
  216. RULE_INIT(R_World, TreasureChestDisabled, "0"); // default: false
  217. //INSERT INTO `ruleset_details`(`id`, `ruleset_id`, `rule_category`, `rule_type`, `rule_value`, `description`) VALUES (NULL, '1', 'R_World', '', '', '')
  218. /* ZONE */
  219. RULE_INIT(R_Zone, MaxPlayers, "100");
  220. RULE_INIT(R_Zone, MinZoneLevelOverrideStatus, "1");
  221. RULE_INIT(R_Zone, MinZoneAccessOverrideStatus, "100");
  222. RULE_INIT(R_Zone, WeatherEnabled, "1"); // default: 1 (0 = disabled, 1 = enabled)
  223. RULE_INIT(R_Zone, WeatherType, "0"); // default: 1 (0 = normal, 1 = dynamic, 2 = random, 3 = chaotic)
  224. RULE_INIT(R_Zone, MinWeatherSeverity, "0.0"); // default: 0.0 or no weather
  225. RULE_INIT(R_Zone, MaxWeatherSeverity, "1.0"); // default: 1.0 or hard rain (range 0.0 - 1.0, rain starts at 0.75)
  226. RULE_INIT(R_Zone, WeatherChangeFrequency, "300"); // default: 5 minutes
  227. RULE_INIT(R_Zone, WeatherChangePerInterval, "0.02"); // default: 0.02 (slight changes)
  228. RULE_INIT(R_Zone, WeatherChangeChance, "20"); // default: 20% (in whole percents)
  229. RULE_INIT(R_Zone, WeatherDynamicMaxOffset, "0.08"); // default: 0.08 - dynamic weather changes can only change this max amount
  230. RULE_INIT(R_Zone, SpawnUpdateTimer, "50"); // default: 50ms - how often to check for spawn update sends
  231. RULE_INIT(R_Zone, CheckAttackNPC, "2000"); // default: 2 seconds, how often to for NPCs to attack eachother
  232. RULE_INIT(R_Zone, CheckAttackPlayer, "2000"); // default: 2 seconds, how often to check for NPCs to attack players
  233. RULE_INIT(R_Zone, HOTime, "10.0"); // default: 10 seconds, time to complete the HO wheel before it expires
  234. /* ZONE TIMERS */
  235. RULE_INIT(R_Zone, RegenTimer, "6000");
  236. RULE_INIT(R_Zone, ClientSaveTimer, "60000");
  237. RULE_INIT(R_Zone, DefaultZoneShutdownTimer, "300000");
  238. RULE_INIT(R_Zone, WeatherTimer, "60000"); // default: 1 minute
  239. RULE_INIT(R_Zone, SpawnDeleteTimer, "30000"); // default: 30 seconds, how long a spawn pointer is held onto after being removed from the world before deleting it
  240. #undef RULE_INIT
  241. }
  242. RuleManager::~RuleManager() {
  243. map<int32, map<int32, Rule *> >::iterator itr;
  244. map<int32, Rule *>::iterator itr2;
  245. for (itr = rules.begin(); itr != rules.end(); itr++) {
  246. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++)
  247. safe_delete(itr2->second);
  248. }
  249. ClearRuleSets();
  250. ClearZoneRuleSets();
  251. }
  252. void RuleManager::LoadCodedDefaultsIntoRuleSet(RuleSet *rule_set) {
  253. map<int32, map<int32, Rule *> >::iterator itr;
  254. map<int32, Rule *>::iterator itr2;
  255. assert(rule_set);
  256. for (itr = rules.begin(); itr != rules.end(); itr++) {
  257. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++)
  258. rule_set->AddRule(new Rule(itr2->second));
  259. }
  260. }
  261. bool RuleManager::AddRuleSet(RuleSet *rule_set) {
  262. bool ret = false;
  263. int32 id;
  264. assert(rule_set);
  265. id = rule_set->GetID();
  266. m_rule_sets.writelock(__FUNCTION__, __LINE__);
  267. if (rule_sets.count(id) == 0) {
  268. rule_sets[id] = rule_set;
  269. ret = true;
  270. }
  271. m_rule_sets.releasewritelock(__FUNCTION__, __LINE__);
  272. return ret;
  273. }
  274. int32 RuleManager::GetNumRuleSets() {
  275. int32 ret;
  276. m_rule_sets.readlock(__FUNCTION__, __LINE__);
  277. ret = rule_sets.size();
  278. m_rule_sets.releasereadlock(__FUNCTION__, __LINE__);
  279. return ret;
  280. }
  281. void RuleManager::ClearRuleSets() {
  282. map<int32, RuleSet *>::iterator itr;
  283. m_rule_sets.writelock(__FUNCTION__, __LINE__);
  284. for (itr = rule_sets.begin(); itr != rule_sets.end(); itr++)
  285. safe_delete(itr->second);
  286. rule_sets.clear();
  287. m_rule_sets.releasewritelock(__FUNCTION__, __LINE__);
  288. }
  289. bool RuleManager::SetGlobalRuleSet(int32 rule_set_id) {
  290. if (rule_sets.count(rule_set_id) == 0)
  291. return false;
  292. global_rule_set.CopyRulesInto(rule_sets[rule_set_id]);
  293. return true;
  294. }
  295. Rule * RuleManager::GetGlobalRule(int32 category, int32 type) {
  296. return global_rule_set.GetRule(category, type);
  297. }
  298. bool RuleManager::SetZoneRuleSet(int32 zone_id, int32 rule_set_id) {
  299. bool ret = true;
  300. RuleSet *rule_set;
  301. m_rule_sets.readlock(__FUNCTION__, __LINE__);
  302. if (rule_sets.count(rule_set_id) == 0)
  303. ret = false;
  304. rule_set = rule_sets[rule_set_id];
  305. if (ret) {
  306. m_zone_rule_sets.writelock(__FUNCTION__, __LINE__);
  307. zone_rule_sets[zone_id] = rule_set;
  308. m_zone_rule_sets.releasewritelock(__FUNCTION__, __LINE__);
  309. }
  310. m_rule_sets.releasereadlock(__FUNCTION__, __LINE__);
  311. return ret;
  312. }
  313. Rule * RuleManager::GetZoneRule(int32 zone_id, int32 category, int32 type) {
  314. Rule *ret = 0;
  315. /* we never want to return null so MAKE SURE the rule exists. if this assertion fails then the server admin must fix the problem */
  316. assert(rules.count(category) > 0);
  317. assert(rules[category].count(type) > 0);
  318. /* first try to get the zone rule */
  319. m_zone_rule_sets.readlock(__FUNCTION__, __LINE__);
  320. if (zone_rule_sets.count(zone_id) > 0)
  321. ret = zone_rule_sets[zone_id]->GetRule(category, type);
  322. m_zone_rule_sets.releasereadlock(__FUNCTION__, __LINE__);
  323. return ret ? ret : rules[category][type];
  324. }
  325. void RuleManager::ClearZoneRuleSets() {
  326. m_zone_rule_sets.writelock(__FUNCTION__, __LINE__);
  327. zone_rule_sets.clear();
  328. m_zone_rule_sets.releasewritelock(__FUNCTION__, __LINE__);
  329. }