Widget.cpp 12 KB

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