Widget.cpp 12 KB

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