9
3

Widget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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->SetCollector(IsCollector());
  113. new_spawn->SetMerchantID(merchant_id);
  114. new_spawn->SetMerchantType(merchant_type);
  115. new_spawn->SetMerchantLevelRange(GetMerchantMinLevel(), GetMerchantMaxLevel());
  116. new_spawn->SetPrimaryCommands(&primary_command_list);
  117. new_spawn->primary_command_list_id = primary_command_list_id;
  118. new_spawn->SetSecondaryCommands(&secondary_command_list);
  119. new_spawn->secondary_command_list_id = secondary_command_list_id;
  120. new_spawn->database_id = database_id;
  121. memcpy(&new_spawn->appearance, &appearance, sizeof(AppearanceData));
  122. new_spawn->SetWidgetID(widget_id);
  123. new_spawn->SetWidgetX(widget_x);
  124. new_spawn->SetWidgetY(widget_y);
  125. new_spawn->SetWidgetZ(widget_z);
  126. new_spawn->SetIncludeHeading(include_heading);
  127. new_spawn->SetIncludeLocation(include_location);
  128. new_spawn->SetOpenY(open_y);
  129. new_spawn->SetCloseY(close_y);
  130. new_spawn->SetOpenDuration(open_duration);
  131. if(GetOpenSound())
  132. new_spawn->SetOpenSound(GetOpenSound());
  133. if(GetCloseSound())
  134. new_spawn->SetCloseSound(GetCloseSound());
  135. new_spawn->SetOpenHeading(open_heading);
  136. new_spawn->SetClosedHeading(closed_heading);
  137. new_spawn->SetWidgetType(widget_type);
  138. new_spawn->SetActionSpawnID(action_spawn_id);
  139. new_spawn->SetLinkedSpawnID(linked_spawn_id);
  140. new_spawn->SetTransporterID(GetTransporterID());
  141. new_spawn->SetHouseID(GetHouseID());
  142. new_spawn->SetCloseX(GetCloseX());
  143. new_spawn->SetCloseZ(GetCloseZ());
  144. new_spawn->SetOpenX(GetOpenX());
  145. new_spawn->SetOpenZ(GetOpenZ());
  146. new_spawn->SetMultiFloorLift(multi_floor_lift);
  147. new_spawn->SetSoundsDisabled(IsSoundsDisabled());
  148. return new_spawn;
  149. }
  150. void Widget::SetIncludeLocation(bool val){
  151. include_location = val;
  152. }
  153. bool Widget::GetIncludeLocation(){
  154. return include_location;
  155. }
  156. void Widget::SetIncludeHeading(bool val){
  157. include_heading = val;
  158. }
  159. bool Widget::GetIncludeHeading(){
  160. return include_heading;
  161. }
  162. float Widget::GetOpenHeading(){
  163. return open_heading;
  164. }
  165. void Widget::SetOpenHeading(float val){
  166. open_heading = val;
  167. }
  168. float Widget::GetClosedHeading(){
  169. return closed_heading;
  170. }
  171. void Widget::SetClosedHeading(float val){
  172. closed_heading = val;
  173. }
  174. float Widget::GetOpenY(){
  175. return open_y;
  176. }
  177. void Widget::SetOpenY(float val){
  178. open_y = val;
  179. }
  180. float Widget::GetCloseY(){
  181. return close_y;
  182. }
  183. void Widget::SetCloseY(float val){
  184. close_y = val;
  185. }
  186. bool Widget::IsOpen(){
  187. std::lock_guard<std::mutex> lk(MWidgetMutex);
  188. bool widget_open = is_open;
  189. return widget_open;
  190. }
  191. int8 Widget::GetWidgetType(){
  192. return widget_type;
  193. }
  194. void Widget::SetWidgetType(int8 val){
  195. widget_type = val;
  196. }
  197. int32 Widget::GetActionSpawnID(){
  198. return action_spawn_id;
  199. }
  200. void Widget::SetActionSpawnID(int32 id){
  201. action_spawn_id = id;
  202. }
  203. int32 Widget::GetLinkedSpawnID(){
  204. return linked_spawn_id;
  205. }
  206. void Widget::SetLinkedSpawnID(int32 id){
  207. linked_spawn_id = id;
  208. }
  209. const char* Widget::GetOpenSound(){
  210. if(open_sound.length() > 0)
  211. return open_sound.c_str();
  212. else
  213. return 0;
  214. }
  215. void Widget::SetOpenSound(const char* name){
  216. open_sound = string(name);
  217. }
  218. const char* Widget::GetCloseSound(){
  219. if(close_sound.length() > 0)
  220. return close_sound.c_str();
  221. else
  222. return 0;
  223. }
  224. void Widget::SetCloseSound(const char* name){
  225. close_sound = string(name);
  226. }
  227. void Widget::HandleTimerUpdate(){
  228. if(widget_type == WIDGET_TYPE_LIFT)
  229. return; //This Widget is a lift, return.
  230. else if (widget_type == WIDGET_TYPE_DOOR && is_open)
  231. HandleUse(nullptr, "");
  232. }
  233. void Widget::OpenDoor(){
  234. std::lock_guard<std::mutex> lk(MWidgetMutex);
  235. if(GetOpenHeading() >= 0)
  236. SetHeading(GetOpenHeading());
  237. float openX = GetOpenX();
  238. float openY = GetOpenY();
  239. float openZ = GetOpenZ();
  240. if(openX != 0 || openY != 0 || openZ != 0 ) {
  241. float x = GetX();
  242. float y = GetY();
  243. float z = GetZ();
  244. if(openX != 0)
  245. x = openX;
  246. if(openY != 0)
  247. y = openY;
  248. if(openZ != 0)
  249. z = openZ;
  250. AddRunningLocation(x, y, z, 4);
  251. float diff = GetDistance(GetX(), GetY(), GetZ(), x, y, z);
  252. if(diff < 0)
  253. diff*=-1;
  254. GetZone()->AddWidgetTimer(this, diff / 4);
  255. }
  256. if (widget_type != WIDGET_TYPE_LIFT)
  257. SetActivityStatus(0);
  258. is_open = true;
  259. if(open_duration > 0)
  260. GetZone()->AddWidgetTimer(this, open_duration);
  261. GetZone()->SendSpawnChanges(this);
  262. }
  263. void Widget::CloseDoor(){
  264. std::lock_guard<std::mutex> lk(MWidgetMutex);
  265. if(GetClosedHeading() > 0)
  266. SetHeading(GetClosedHeading());
  267. else if(GetOpenHeading() >= 0)
  268. SetHeading(GetSpawnOrigHeading());
  269. if (widget_type != WIDGET_TYPE_LIFT)
  270. SetActivityStatus(64);
  271. if (GetCloseX() != 0 || GetCloseY() != 0 || GetCloseZ() != 0 || GetOpenX() != 0 || GetOpenY() != 0 || GetOpenZ() != 0) {
  272. float x = GetSpawnOrigX();
  273. float y = GetSpawnOrigY();
  274. float z = GetSpawnOrigZ();
  275. if (GetCloseX() != 0)
  276. x = GetCloseX();
  277. if (GetCloseY() != 0)
  278. y = GetCloseY();
  279. if (GetCloseZ() != 0)
  280. z = GetCloseZ();
  281. AddRunningLocation(x, y, z, 4);
  282. float diff = GetDistance(GetX(), GetY(), GetZ(), x, y, z);
  283. if (diff < 0)
  284. diff *= -1;
  285. GetZone()->AddWidgetTimer(this, diff / 4);
  286. }
  287. is_open = false;
  288. GetZone()->SendSpawnChanges(this);
  289. }
  290. void Widget::ProcessUse(Spawn* caller){
  291. if(widget_type == WIDGET_TYPE_LIFT && GetZone()->HasWidgetTimer(this)) //this door is a lift and in use, wait until it gets to the
  292. return;
  293. if (GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_USEDOOR, caller, "", is_open)) {
  294. // handled in lua, nothing to do here!
  295. }
  296. else
  297. {
  298. bool wasOpen = IsOpen();
  299. if(wasOpen) //close
  300. CloseDoor();
  301. else //open
  302. OpenDoor();
  303. bool isOpen = IsOpen();
  304. if(isOpen){
  305. if(GetOpenSound())
  306. GetZone()->PlaySoundFile(0, GetOpenSound(), widget_x, widget_y, widget_z);
  307. }
  308. else
  309. if(GetCloseSound())
  310. GetZone()->PlaySoundFile(0, GetCloseSound(), widget_x, widget_y, widget_z);
  311. }
  312. }
  313. void Widget::HandleUse(Client* client, string command, int8 overrideWidgetType){
  314. vector<TransportDestination*> destinations;
  315. //The following check disables the use of doors and other widgets if the player does not meet the quest requirements
  316. //If this is from a script ignore this check (client will be null)
  317. if (overrideWidgetType == 0xFF)
  318. overrideWidgetType = widget_type;
  319. if (client) {
  320. bool meets_quest_reqs = MeetsSpawnAccessRequirements(client->GetPlayer());
  321. if (!meets_quest_reqs && (GetQuestsRequiredOverride() & 2) == 0)
  322. return;
  323. else if (meets_quest_reqs && appearance.show_command_icon != 1)
  324. return;
  325. }
  326. if (client && GetTransporterID() > 0)
  327. {
  328. client->SetTemporaryTransportID(0);
  329. GetZone()->GetTransporters(&destinations, client, GetTransporterID());
  330. }
  331. if (destinations.size() && client)
  332. client->ProcessTeleport(this, &destinations, GetTransporterID());
  333. else if (overrideWidgetType == WIDGET_TYPE_DOOR || overrideWidgetType == WIDGET_TYPE_LIFT){
  334. Widget* widget = this;
  335. if (!action_spawn && action_spawn_id > 0){
  336. Spawn* spawn = GetZone()->GetSpawnByDatabaseID(action_spawn_id);
  337. if (spawn && spawn->IsWidget())
  338. action_spawn = (Widget*)spawn;
  339. }
  340. if (!linked_spawn && linked_spawn_id > 0){
  341. Spawn* spawn = GetZone()->GetSpawnByDatabaseID(linked_spawn_id);
  342. if (spawn && spawn->IsWidget())
  343. linked_spawn = (Widget*)spawn;
  344. }
  345. if (linked_spawn){
  346. widget = linked_spawn;
  347. ProcessUse(client ? client->GetPlayer() : nullptr); //fire the first door, then fire the linked door below
  348. }
  349. else if (action_spawn) {
  350. widget = action_spawn;
  351. if (!widget->linked_spawn && widget->linked_spawn_id > 0) {
  352. Spawn* spawn = GetZone()->GetSpawnByDatabaseID(widget->linked_spawn_id);
  353. if (spawn && spawn->IsWidget())
  354. widget->linked_spawn = (Widget*)spawn;
  355. }
  356. if (widget->linked_spawn)
  357. widget->linked_spawn->ProcessUse(client ? client->GetPlayer() : nullptr);
  358. }
  359. widget->ProcessUse(client ? client->GetPlayer() : nullptr);
  360. }
  361. else if (client && m_houseID > 0 && strncasecmp("access", command.c_str(), 6) == 0) {
  362. // Used a door to enter a house
  363. HouseZone* hz = world.GetHouseZone(m_houseID);
  364. PlayerHouse* ph = 0;
  365. if (hz)
  366. ph = world.GetPlayerHouseByHouseID(client->GetPlayer()->GetCharacterID(), hz->id);
  367. if (ph) {
  368. // if we aren't in our own house we should get the full list of houses we can visit
  369. if ( m_houseID && client->GetCurrentZone()->GetInstanceType() != Instance_Type::PERSONAL_HOUSE_INSTANCE )
  370. ClientPacketFunctions::SendHouseVisitWindow(client, world.GetAllPlayerHousesByHouseID(m_houseID));
  371. ClientPacketFunctions::SendBaseHouseWindow(client, hz, ph, 0);
  372. client->GetCurrentZone()->SendHouseItems(client);
  373. }
  374. else {
  375. if (hz)
  376. ClientPacketFunctions::SendHousePurchase(client, hz, 0);
  377. }
  378. }
  379. else if (client && strncasecmp("access", command.c_str(), 6) == 0 && GetZone()->GetInstanceType() > 0) {
  380. // Used a door within a house
  381. PlayerHouse* ph = world.GetPlayerHouseByInstanceID(GetZone()->GetInstanceID());
  382. if (ph) {
  383. HouseZone* hz = world.GetHouseZone(ph->house_id);
  384. if (hz) {
  385. int32 id = client->GetPlayer()->GetIDWithPlayerSpawn(this);
  386. if (m_houseID)
  387. ClientPacketFunctions::SendHouseVisitWindow(client, world.GetAllPlayerHousesByHouseID(m_houseID));
  388. ClientPacketFunctions::SendBaseHouseWindow(client, hz, ph, id);
  389. }
  390. }
  391. }
  392. else if (client && m_houseID > 0 && strncasecmp("visit", command.c_str(), 6) == 0) {
  393. ClientPacketFunctions::SendHousingList(client);
  394. ClientPacketFunctions::SendHouseVisitWindow(client, world.GetAllPlayerHousesByHouseID(m_houseID));
  395. }
  396. else if (client && command.length() > 0) {
  397. EntityCommand* entity_command = FindEntityCommand(command);
  398. if (entity_command)
  399. client->GetCurrentZone()->ProcessEntityCommand(entity_command, client->GetPlayer(), client->GetPlayer()->GetTarget());
  400. }
  401. }