Widget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. }
  255. void Widget::CloseDoor(){
  256. if(GetClosedHeading() > 0)
  257. SetHeading(GetClosedHeading());
  258. else if(GetOpenHeading() >= 0)
  259. SetHeading(GetSpawnOrigHeading());
  260. if (widget_type != WIDGET_TYPE_LIFT)
  261. SetActivityStatus(64);
  262. if (GetCloseX() != 0 || GetCloseY() != 0 || GetCloseZ() != 0 || GetOpenX() != 0 || GetOpenY() != 0 || GetOpenZ() != 0) {
  263. float x = GetSpawnOrigX();
  264. float y = GetSpawnOrigY();
  265. float z = GetSpawnOrigZ();
  266. if (GetCloseX() != 0)
  267. x = GetCloseX();
  268. if (GetCloseY() != 0)
  269. y = GetCloseY();
  270. if (GetCloseZ() != 0)
  271. z = GetCloseZ();
  272. AddRunningLocation(x, y, z, 4);
  273. float diff = GetDistance(GetX(), GetY(), GetZ(), x, y, z);
  274. if (diff < 0)
  275. diff *= -1;
  276. GetZone()->AddWidgetTimer(this, diff / 4);
  277. }
  278. is_open = false;
  279. }
  280. void Widget::ProcessUse(){
  281. if(widget_type == WIDGET_TYPE_LIFT && GetZone()->HasWidgetTimer(this)) //this door is a lift and in use, wait until it gets to the
  282. return;
  283. if(is_open) //close
  284. CloseDoor();
  285. else //open
  286. OpenDoor();
  287. if(is_open){
  288. if(GetOpenSound())
  289. GetZone()->PlaySoundFile(0, GetOpenSound(), widget_x, widget_y, widget_z);
  290. }
  291. else
  292. if(GetCloseSound())
  293. GetZone()->PlaySoundFile(0, GetCloseSound(), widget_x, widget_y, widget_z);
  294. }
  295. void Widget::HandleUse(Client* client, string command){
  296. vector<TransportDestination*>* destinations = 0;
  297. //The following check disables the use of doors and other widgets if the player does not meet the quest requirements
  298. //If this is from a script ignore this check (client will be null)
  299. if (client) {
  300. bool meets_quest_reqs = MeetsSpawnAccessRequirements(client->GetPlayer());
  301. if (!meets_quest_reqs && (GetQuestsRequiredOverride() & 2) == 0)
  302. return;
  303. else if (meets_quest_reqs && appearance.show_command_icon != 1)
  304. return;
  305. }
  306. if (client && GetTransporterID() > 0)
  307. destinations = GetZone()->GetTransporters(GetTransporterID());
  308. if (destinations)
  309. client->ProcessTeleport(this, destinations, GetTransporterID());
  310. else if (widget_type == WIDGET_TYPE_DOOR || widget_type == WIDGET_TYPE_LIFT){
  311. Widget* widget = this;
  312. if (!action_spawn && action_spawn_id > 0){
  313. Spawn* spawn = GetZone()->GetSpawnByDatabaseID(action_spawn_id);
  314. if (spawn && spawn->IsWidget())
  315. action_spawn = (Widget*)spawn;
  316. }
  317. if (!linked_spawn && linked_spawn_id > 0){
  318. Spawn* spawn = GetZone()->GetSpawnByDatabaseID(linked_spawn_id);
  319. if (spawn && spawn->IsWidget())
  320. linked_spawn = (Widget*)spawn;
  321. }
  322. if (linked_spawn){
  323. widget = linked_spawn;
  324. ProcessUse(); //fire the first door, then fire the linked door below
  325. }
  326. else if (action_spawn) {
  327. widget = action_spawn;
  328. if (!widget->linked_spawn && widget->linked_spawn_id > 0) {
  329. Spawn* spawn = GetZone()->GetSpawnByDatabaseID(widget->linked_spawn_id);
  330. if (spawn && spawn->IsWidget())
  331. widget->linked_spawn = (Widget*)spawn;
  332. }
  333. if (widget->linked_spawn)
  334. widget->linked_spawn->ProcessUse();
  335. }
  336. widget->ProcessUse();
  337. }
  338. else if (client && m_houseID > 0 && strncasecmp("access", command.c_str(), 6) == 0) {
  339. // Used a door to enter a house
  340. HouseZone* hz = world.GetHouseZone(m_houseID);
  341. int32 id = client->GetPlayer()->GetIDWithPlayerSpawn(this);
  342. PlayerHouse* ph = 0;
  343. if (hz)
  344. ph = world.GetPlayerHouseByHouseID(client->GetPlayer()->GetCharacterID(), hz->id);
  345. if (ph) {
  346. ClientPacketFunctions::SendBaseHouseWindow(client, hz, ph, id);
  347. }
  348. else {
  349. if (hz)
  350. ClientPacketFunctions::SendHousePurchace(client, hz, id);
  351. }
  352. }
  353. else if (client && strncasecmp("access", command.c_str(), 6) == 0 && GetZone()->GetInstanceType() > 0) {
  354. // Used a door within a house
  355. PlayerHouse* ph = world.GetPlayerHouseByInstanceID(GetZone()->GetInstanceID());
  356. if (ph) {
  357. HouseZone* hz = world.GetHouseZone(ph->house_id);
  358. if (hz) {
  359. int32 id = client->GetPlayer()->GetIDWithPlayerSpawn(this);
  360. ClientPacketFunctions::SendBaseHouseWindow(client, hz, ph, id);
  361. }
  362. }
  363. }
  364. else if (client && m_houseID > 0 && strncasecmp("visit", command.c_str(), 6) == 0) {
  365. ClientPacketFunctions::SendHouseVisitWindow(client, world.GetAllPlayerHousesByHouseID(m_houseID));
  366. }
  367. else if (client && command.length() > 0) {
  368. EntityCommand* entity_command = FindEntityCommand(command);
  369. if (entity_command)
  370. client->GetCurrentZone()->ProcessEntityCommand(entity_command, client->GetPlayer(), client->GetPlayer()->GetTarget());
  371. }
  372. }