Widget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 <boost/algorithm/string/predicate.hpp>
  17. #include <boost/algorithm/string.hpp>
  18. #include "Widget.h"
  19. #include "../common/ConfigReader.h"
  20. #include "Spells.h"
  21. #include "World.h"
  22. #include "../common/Log.h"
  23. #include "ClientPacketFunctions.h"
  24. #include "LuaInterface.h"
  25. extern World world;
  26. extern ConfigReader configReader;
  27. extern MasterSpellList master_spell_list;
  28. extern LuaInterface* lua_interface;
  29. Widget::Widget(){
  30. widget_id = 0;
  31. widget_x = 0;
  32. widget_y = 0;
  33. widget_z = 0;
  34. action_spawn = 0;
  35. action_spawn_id = 0;
  36. linked_spawn = 0;
  37. linked_spawn_id = 0;
  38. appearance.pos.state = 1;
  39. appearance.difficulty = 0;
  40. spawn_type = 2;
  41. appearance.activity_status = 64;
  42. include_location = true;
  43. include_heading = true;
  44. is_open = false;
  45. widget_type = 0;
  46. open_heading = -1;
  47. closed_heading = -1;
  48. open_y = 0;
  49. open_x = 0;
  50. open_z = 0;
  51. close_x = 0;
  52. close_z = 0;
  53. movement_index = 0;
  54. movement_interrupted = false;
  55. resume_movement = true;
  56. attack_resume_needed = false;
  57. multi_floor_lift = false;
  58. MMovementLoop.SetName("Widget::MMovementLoop");
  59. last_movement_update = Timer::GetCurrentTime2();
  60. }
  61. Widget::~Widget(){
  62. }
  63. int32 Widget::GetWidgetID(){
  64. return widget_id;
  65. }
  66. EQ2Packet* Widget::serialize(Player* player, int16 version){
  67. return spawn_serialize(player, version);
  68. }
  69. void Widget::SetWidgetID(int32 val){
  70. widget_id = val;
  71. }
  72. void Widget::SetWidgetX(float val){
  73. widget_x = val;
  74. }
  75. float Widget::GetWidgetX(){
  76. return widget_x;
  77. }
  78. void Widget::SetWidgetY(float val){
  79. widget_y = val;
  80. }
  81. float Widget::GetWidgetY(){
  82. return widget_y;
  83. }
  84. void Widget::SetWidgetZ(float val){
  85. widget_z = val;
  86. }
  87. float Widget::GetWidgetZ(){
  88. return widget_z;
  89. }
  90. void Widget::SetWidgetIcon(int8 val){
  91. appearance.icon = val;
  92. }
  93. void Widget::SetOpenDuration(int16 val){
  94. open_duration = val;
  95. }
  96. int16 Widget::GetOpenDuration(){
  97. return open_duration;
  98. }
  99. Widget* Widget::Copy(){
  100. Widget* new_spawn = new Widget();
  101. if(GetOpenY() > 0)
  102. appearance.pos.state = 0;
  103. if(GetSizeOffset() > 0){
  104. int8 offset = GetSizeOffset()+1;
  105. sint32 tmp_size = size + (rand()%offset - rand()%offset);
  106. if(tmp_size < 0)
  107. tmp_size = 1;
  108. else if(tmp_size >= 0xFFFF)
  109. tmp_size = 0xFFFF;
  110. new_spawn->size = (int16)tmp_size;
  111. }
  112. else
  113. new_spawn->size = size;
  114. new_spawn->SetCollector(IsCollector());
  115. new_spawn->SetMerchantID(merchant_id);
  116. new_spawn->SetMerchantType(merchant_type);
  117. new_spawn->SetMerchantLevelRange(GetMerchantMinLevel(), GetMerchantMaxLevel());
  118. new_spawn->SetPrimaryCommands(&primary_command_list);
  119. new_spawn->primary_command_list_id = primary_command_list_id;
  120. new_spawn->SetSecondaryCommands(&secondary_command_list);
  121. new_spawn->secondary_command_list_id = secondary_command_list_id;
  122. new_spawn->database_id = database_id;
  123. memcpy(&new_spawn->appearance, &appearance, sizeof(AppearanceData));
  124. new_spawn->SetWidgetID(widget_id);
  125. new_spawn->SetWidgetX(widget_x);
  126. new_spawn->SetWidgetY(widget_y);
  127. new_spawn->SetWidgetZ(widget_z);
  128. new_spawn->SetIncludeHeading(include_heading);
  129. new_spawn->SetIncludeLocation(include_location);
  130. new_spawn->SetOpenY(open_y);
  131. new_spawn->SetCloseY(close_y);
  132. new_spawn->SetOpenDuration(open_duration);
  133. if(GetOpenSound())
  134. new_spawn->SetOpenSound(GetOpenSound());
  135. if(GetCloseSound())
  136. new_spawn->SetCloseSound(GetCloseSound());
  137. new_spawn->SetOpenHeading(open_heading);
  138. new_spawn->SetClosedHeading(closed_heading);
  139. new_spawn->SetWidgetType(widget_type);
  140. new_spawn->SetActionSpawnID(action_spawn_id);
  141. new_spawn->SetLinkedSpawnID(linked_spawn_id);
  142. new_spawn->SetTransporterID(GetTransporterID());
  143. new_spawn->SetHouseID(GetHouseID());
  144. new_spawn->SetCloseX(GetCloseX());
  145. new_spawn->SetCloseZ(GetCloseZ());
  146. new_spawn->SetOpenX(GetOpenX());
  147. new_spawn->SetOpenZ(GetOpenZ());
  148. new_spawn->SetMultiFloorLift(multi_floor_lift);
  149. new_spawn->SetSoundsDisabled(IsSoundsDisabled());
  150. return new_spawn;
  151. }
  152. void Widget::SetIncludeLocation(bool val){
  153. include_location = val;
  154. }
  155. bool Widget::GetIncludeLocation(){
  156. return include_location;
  157. }
  158. void Widget::SetIncludeHeading(bool val){
  159. include_heading = val;
  160. }
  161. bool Widget::GetIncludeHeading(){
  162. return include_heading;
  163. }
  164. float Widget::GetOpenHeading(){
  165. return open_heading;
  166. }
  167. void Widget::SetOpenHeading(float val){
  168. open_heading = val;
  169. }
  170. float Widget::GetClosedHeading(){
  171. return closed_heading;
  172. }
  173. void Widget::SetClosedHeading(float val){
  174. closed_heading = val;
  175. }
  176. float Widget::GetOpenY(){
  177. return open_y;
  178. }
  179. void Widget::SetOpenY(float val){
  180. open_y = val;
  181. }
  182. float Widget::GetCloseY(){
  183. return close_y;
  184. }
  185. void Widget::SetCloseY(float val){
  186. close_y = val;
  187. }
  188. bool Widget::IsOpen(){
  189. std::lock_guard<std::mutex> lk(MWidgetMutex);
  190. bool widget_open = is_open;
  191. return widget_open;
  192. }
  193. int8 Widget::GetWidgetType(){
  194. return widget_type;
  195. }
  196. void Widget::SetWidgetType(int8 val){
  197. widget_type = val;
  198. }
  199. int32 Widget::GetActionSpawnID(){
  200. return action_spawn_id;
  201. }
  202. void Widget::SetActionSpawnID(int32 id){
  203. action_spawn_id = id;
  204. }
  205. int32 Widget::GetLinkedSpawnID(){
  206. return linked_spawn_id;
  207. }
  208. void Widget::SetLinkedSpawnID(int32 id){
  209. linked_spawn_id = id;
  210. }
  211. const char* Widget::GetOpenSound(){
  212. if(open_sound.length() > 0)
  213. return open_sound.c_str();
  214. else
  215. return 0;
  216. }
  217. void Widget::SetOpenSound(const char* name){
  218. open_sound = string(name);
  219. }
  220. const char* Widget::GetCloseSound(){
  221. if(close_sound.length() > 0)
  222. return close_sound.c_str();
  223. else
  224. return 0;
  225. }
  226. void Widget::SetCloseSound(const char* name){
  227. close_sound = string(name);
  228. }
  229. void Widget::HandleTimerUpdate(){
  230. if(widget_type == WIDGET_TYPE_LIFT)
  231. return; //This Widget is a lift, return.
  232. else if (widget_type == WIDGET_TYPE_DOOR && is_open)
  233. HandleUse(nullptr, "");
  234. }
  235. void Widget::OpenDoor(){
  236. std::lock_guard<std::mutex> lk(MWidgetMutex);
  237. if(GetOpenHeading() >= 0)
  238. SetHeading(GetOpenHeading());
  239. float openX = GetOpenX();
  240. float openY = GetOpenY();
  241. float openZ = GetOpenZ();
  242. if(openX != 0 || openY != 0 || openZ != 0 ) {
  243. float x = GetX();
  244. float y = GetY();
  245. float z = GetZ();
  246. if(openX != 0)
  247. x = openX;
  248. if(openY != 0)
  249. y = openY;
  250. if(openZ != 0)
  251. z = openZ;
  252. AddRunningLocation(x, y, z, 4);
  253. float diff = GetDistance(GetX(), GetY(), GetZ(), x, y, z);
  254. if(diff < 0)
  255. diff*=-1;
  256. GetZone()->AddWidgetTimer(this, diff / 4);
  257. }
  258. if (widget_type != WIDGET_TYPE_LIFT)
  259. SetActivityStatus(0);
  260. is_open = true;
  261. if(open_duration > 0)
  262. GetZone()->AddWidgetTimer(this, open_duration);
  263. GetZone()->SendSpawnChanges(this);
  264. }
  265. void Widget::CloseDoor(){
  266. std::lock_guard<std::mutex> lk(MWidgetMutex);
  267. if(GetClosedHeading() > 0)
  268. SetHeading(GetClosedHeading());
  269. else if(GetOpenHeading() >= 0)
  270. SetHeading(GetSpawnOrigHeading());
  271. if (widget_type != WIDGET_TYPE_LIFT)
  272. SetActivityStatus(64);
  273. if (GetCloseX() != 0 || GetCloseY() != 0 || GetCloseZ() != 0 || GetOpenX() != 0 || GetOpenY() != 0 || GetOpenZ() != 0) {
  274. float x = GetSpawnOrigX();
  275. float y = GetSpawnOrigY();
  276. float z = GetSpawnOrigZ();
  277. if (GetCloseX() != 0)
  278. x = GetCloseX();
  279. if (GetCloseY() != 0)
  280. y = GetCloseY();
  281. if (GetCloseZ() != 0)
  282. z = GetCloseZ();
  283. AddRunningLocation(x, y, z, 4);
  284. float diff = GetDistance(GetX(), GetY(), GetZ(), x, y, z);
  285. if (diff < 0)
  286. diff *= -1;
  287. GetZone()->AddWidgetTimer(this, diff / 4);
  288. }
  289. is_open = false;
  290. GetZone()->SendSpawnChanges(this);
  291. }
  292. void Widget::ProcessUse(Spawn* caller){
  293. if(widget_type == WIDGET_TYPE_LIFT && GetZone()->HasWidgetTimer(this)) //this door is a lift and in use, wait until it gets to the
  294. return;
  295. if (GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_USEDOOR, caller, "", is_open)) {
  296. // handled in lua, nothing to do here!
  297. }
  298. else
  299. {
  300. bool wasOpen = IsOpen();
  301. if(wasOpen) //close
  302. CloseDoor();
  303. else //open
  304. OpenDoor();
  305. bool isOpen = IsOpen();
  306. if(isOpen){
  307. if(GetOpenSound())
  308. GetZone()->PlaySoundFile(0, GetOpenSound(), widget_x, widget_y, widget_z);
  309. }
  310. else
  311. if(GetCloseSound())
  312. GetZone()->PlaySoundFile(0, GetCloseSound(), widget_x, widget_y, widget_z);
  313. }
  314. }
  315. void Widget::HandleUse(Client* client, string command, int8 overrideWidgetType){
  316. vector<TransportDestination*> destinations;
  317. //The following check disables the use of doors and other widgets if the player does not meet the quest requirements
  318. //If this is from a script ignore this check (client will be null)
  319. if (overrideWidgetType == 0xFF)
  320. overrideWidgetType = widget_type;
  321. if (client) {
  322. bool meets_quest_reqs = MeetsSpawnAccessRequirements(client->GetPlayer());
  323. if (!meets_quest_reqs && (GetQuestsRequiredOverride() & 2) == 0)
  324. return;
  325. else if (meets_quest_reqs && appearance.show_command_icon != 1)
  326. return;
  327. }
  328. std::string cmdlower(command);
  329. boost::algorithm::to_lower(cmdlower);
  330. if (client && GetTransporterID() > 0)
  331. {
  332. client->SetTemporaryTransportID(0);
  333. GetZone()->GetTransporters(&destinations, client, GetTransporterID());
  334. }
  335. bool skipHouseCommands = (cmdlower == "access" || cmdlower == "visit");
  336. if (!skipHouseCommands && destinations.size() && client)
  337. client->ProcessTeleport(this, &destinations, GetTransporterID());
  338. else if (!skipHouseCommands && (overrideWidgetType == WIDGET_TYPE_DOOR || overrideWidgetType == WIDGET_TYPE_LIFT)){
  339. Widget* widget = this;
  340. if (!action_spawn && action_spawn_id > 0){
  341. Spawn* spawn = GetZone()->GetSpawnByDatabaseID(action_spawn_id);
  342. if (spawn && spawn->IsWidget())
  343. action_spawn = (Widget*)spawn;
  344. }
  345. if (!linked_spawn && linked_spawn_id > 0){
  346. Spawn* spawn = GetZone()->GetSpawnByDatabaseID(linked_spawn_id);
  347. if (spawn && spawn->IsWidget())
  348. linked_spawn = (Widget*)spawn;
  349. }
  350. if (linked_spawn){
  351. widget = linked_spawn;
  352. ProcessUse(client ? client->GetPlayer() : nullptr); //fire the first door, then fire the linked door below
  353. }
  354. else if (action_spawn) {
  355. widget = action_spawn;
  356. if (!widget->linked_spawn && widget->linked_spawn_id > 0) {
  357. Spawn* spawn = GetZone()->GetSpawnByDatabaseID(widget->linked_spawn_id);
  358. if (spawn && spawn->IsWidget())
  359. widget->linked_spawn = (Widget*)spawn;
  360. }
  361. if (widget->linked_spawn)
  362. widget->linked_spawn->ProcessUse(client ? client->GetPlayer() : nullptr);
  363. }
  364. widget->ProcessUse(client ? client->GetPlayer() : nullptr);
  365. }
  366. else if (client && cmdlower == "access" && GetZone()->GetInstanceID() &&
  367. (GetZone()->GetInstanceType() == PERSONAL_HOUSE_INSTANCE || GetZone()->GetInstanceType() == GUILD_HOUSE_INSTANCE)) {
  368. // Used a door within a house
  369. PlayerHouse* ph = world.GetPlayerHouseByInstanceID(GetZone()->GetInstanceID());
  370. if (ph) {
  371. HouseZone* hz = world.GetHouseZone(ph->house_id);
  372. if (hz) {
  373. int32 id = client->GetPlayer()->GetIDWithPlayerSpawn(this);
  374. ClientPacketFunctions::SendHouseVisitWindow(client, world.GetAllPlayerHousesByHouseID(hz->id));
  375. ClientPacketFunctions::SendBaseHouseWindow(client, hz, ph, id);
  376. }
  377. }
  378. }
  379. else if (client && m_houseID > 0 && cmdlower == "access") {
  380. // Used a door to enter a house
  381. HouseZone* hz = nullptr;
  382. PlayerHouse* ph = nullptr;
  383. int32 id = 0;
  384. if(client->GetVersion() <= 546) {
  385. id = client->GetPlayer()->GetIDWithPlayerSpawn(this);
  386. ph = world.GetPlayerHouse(client, id, 0, &hz);
  387. }
  388. else {
  389. hz = world.GetHouseZone(m_houseID);
  390. if(hz) {
  391. ph = world.GetPlayerHouseByHouseID(client->GetPlayer()->GetCharacterID(), hz->id);
  392. }
  393. id = client->GetPlayer()->GetID();// reconsider?
  394. }
  395. if (ph && hz) {
  396. // if we aren't in our own house we should get the full list of houses we can visit
  397. if ( client->GetCurrentZone()->GetInstanceType() != Instance_Type::PERSONAL_HOUSE_INSTANCE && client->GetVersion() > 546 )
  398. ClientPacketFunctions::SendHouseVisitWindow(client, world.GetAllPlayerHousesByHouseID(hz->id));
  399. ClientPacketFunctions::SendBaseHouseWindow(client, hz, ph, id);
  400. client->GetCurrentZone()->SendHouseItems(client);
  401. }
  402. else {
  403. if (hz)
  404. ClientPacketFunctions::SendHousePurchase(client, hz, 0);
  405. }
  406. }
  407. else if (client && m_houseID > 0 && cmdlower == "visit") {
  408. HouseZone* hz = nullptr;
  409. int32 id = client->GetPlayer()->GetIDWithPlayerSpawn(this);
  410. PlayerHouse* ph = world.GetPlayerHouse(client, id, 0, &hz);
  411. ClientPacketFunctions::SendHousingList(client);
  412. if(hz != nullptr) {
  413. ClientPacketFunctions::SendHouseVisitWindow(client, world.GetAllPlayerHousesByHouseID(hz->id));
  414. }
  415. }
  416. else if (client && command.length() > 0) {
  417. EntityCommand* entity_command = FindEntityCommand(command);
  418. if (entity_command)
  419. client->GetCurrentZone()->ProcessEntityCommand(entity_command, client->GetPlayer(), client->GetPlayer()->GetTarget());
  420. }
  421. }