Widget.cpp 12 KB

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