Widget.cpp 11 KB

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