plugindeveloper.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. typedef Param7<EntityAI, string, float, float, bool, string, FindInventoryLocationType> DevSpawnItemParams;//(target, item_name, health, quantity, special, presetName, locationType );
  2. class PluginDeveloper extends PluginBase
  3. {
  4. protected bool m_IsWinHolding;
  5. protected int m_FeaturesMask;
  6. UIScriptedMenu m_ScriptConsole;
  7. static PluginDeveloper GetInstance()
  8. {
  9. return PluginDeveloper.Cast( GetPlugin( PluginDeveloper ) );
  10. }
  11. //! Set Player position at his cursor position in the world
  12. void TeleportAtCursor()
  13. {
  14. DeveloperTeleport.TeleportAtCursorEx();
  15. }
  16. //! Teleport player at position
  17. void Teleport(PlayerBase player, vector position)
  18. {
  19. DeveloperTeleport.SetPlayerPosition(player, position);
  20. }
  21. //! Set player direction
  22. void SetDirection(PlayerBase player, vector direction)
  23. {
  24. DeveloperTeleport.SetPlayerDirection(player, direction);
  25. }
  26. //! Enable / Disable Free camera (Fly mod)
  27. void ToggleFreeCameraBackPos()
  28. {
  29. DeveloperFreeCamera.FreeCameraToggle( PlayerBase.Cast( GetGame().GetPlayer() ), false );
  30. }
  31. //! Enable / Disable Free camera (Fly mod) - disable of camera will teleport player at current free camera position.
  32. void ToggleFreeCamera()
  33. {
  34. DeveloperFreeCamera.FreeCameraToggle( PlayerBase.Cast( GetGame().GetPlayer() ), true );
  35. }
  36. bool IsEnabledFreeCamera()
  37. {
  38. return DeveloperFreeCamera.IsFreeCameraEnabled();
  39. }
  40. // System Public Events
  41. void PluginDeveloper()
  42. {
  43. }
  44. override void OnInit()
  45. {
  46. super.OnInit();
  47. DeveloperFreeCamera.OnInit();
  48. }
  49. void OnRPC(PlayerBase player, int rpc_type, ParamsReadContext ctx)
  50. {
  51. #ifdef DIAG_DEVELOPER
  52. switch (rpc_type)
  53. {
  54. case ERPCs.DEV_RPC_SPAWN_ITEM_ON_GROUND:
  55. OnRPCSpawnEntityOnGround(player, ctx);
  56. break;
  57. case ERPCs.DEV_RPC_SPAWN_ITEM_ON_GROUND_PATTERN_GRID:
  58. OnRPCSpawnEntityOnGroundPatternGrid(player, ctx);
  59. break;
  60. case ERPCs.DEV_RPC_SPAWN_ITEM_ON_CURSOR:
  61. OnRPCSpawnEntityOnCursorDir(player, ctx);
  62. break;
  63. case ERPCs.DEV_RPC_SPAWN_ITEM_IN_INVENTORY:
  64. OnRPCSpawnEntity(player, ctx);
  65. break;
  66. case ERPCs.DEV_RPC_CLEAR_INV:
  67. OnRPCClearInventory(player);
  68. break;
  69. case ERPCs.DEV_RPC_SEND_SERVER_LOG:
  70. OnRPCServerLogRecieved(ctx);
  71. break;
  72. case ERPCs.RPC_SYNC_SCENE_OBJECT:
  73. OnRPCSyncSceneObject(ctx);
  74. break;
  75. case ERPCs.DEV_RPC_PLUGIN_DZCREATURE_DEBUG:
  76. OnRPCPluginDayzCreatureDebug(player, rpc_type, ctx);
  77. break;
  78. case ERPCs.DEV_RPC_SPAWN_PRESET:
  79. string presetName;
  80. EntityAI target;
  81. ctx.Read(presetName);
  82. ctx.Read(target);
  83. HandlePresetSpawn(player,presetName, target);
  84. break;
  85. case ERPCs.DEV_RPC_SET_TIME:
  86. HandleSetTime(player,ctx);
  87. break;
  88. }
  89. DeveloperTeleport.OnRPC(player, rpc_type, ctx);
  90. #endif
  91. }
  92. // Public API
  93. // Send Print to scripted console
  94. void PrintLogClient(string msg_log)
  95. {
  96. if ( g_Game.GetUIManager().IsMenuOpen(MENU_SCRIPTCONSOLE) )
  97. {
  98. Param1<string> msg_p = new Param1<string>(msg_log);
  99. CallMethod(CALL_ID_SCR_CNSL_ADD_PRINT, msg_p);
  100. }
  101. }
  102. // Server Log Synch: Server Side
  103. void SendServerLogToClient(string msg)
  104. {
  105. if ( GetGame() )
  106. {
  107. array<Man> players = new array<Man>;
  108. GetGame().GetPlayers( players );
  109. for ( int i = 0; i < players.Count(); ++i )
  110. {
  111. Param1<string> param = new Param1<string>( msg );
  112. Man player = players.Get(i);
  113. if ( player && player.HasNetworkID() )
  114. {
  115. player.RPCSingleParam(ERPCs.DEV_RPC_SEND_SERVER_LOG, param, true, player.GetIdentity());
  116. }
  117. }
  118. }
  119. }
  120. void OnRPCSyncSceneObject( ParamsReadContext ctx )
  121. {
  122. Param3<string, EntityAI, Param> par = new Param3<string, EntityAI, Param>( "", NULL, new Param );
  123. if ( ctx.Read( par ) )
  124. {
  125. PluginSceneManager module_scene_editor = PluginSceneManager.Cast( GetPlugin( PluginSceneManager ) );
  126. SceneData scene_data = module_scene_editor.GetLoadedScene();
  127. SceneObject scene_object = scene_data.GetSceneObjectByEntityAI( par.param2 );
  128. GetGame().GetCallQueue( CALL_CATEGORY_SYSTEM ).CallByName( scene_object, par.param1, par.param3 );
  129. }
  130. }
  131. void OnRPCPluginDayzCreatureDebug(PlayerBase player, int rpc_type, ParamsReadContext ctx)
  132. {
  133. PluginDayZCreatureAIDebug pluginDZCreatureDebug = PluginDayZCreatureAIDebug.Cast( GetPlugin( PluginDayZCreatureAIDebug ) );
  134. if (pluginDZCreatureDebug)
  135. {
  136. pluginDZCreatureDebug.OnRpc(player, rpc_type, ctx);
  137. }
  138. }
  139. void HandleSetTime(PlayerBase player, ParamsReadContext ctx)
  140. {
  141. #ifdef DEVELOPER
  142. Param5<int,int,int,int,int> p5 = new Param5<int,int,int,int,int>(0,0,0,0,0);
  143. if (ctx.Read(p5))
  144. {
  145. int year = p5.param1;
  146. int month = p5.param2;
  147. int day = p5.param3;
  148. int hour = p5.param4;
  149. int minute = p5.param5;
  150. GetGame().GetWorld().SetDate(year,month, day, hour, minute);
  151. }
  152. #endif
  153. }
  154. void HandlePresetSpawn(PlayerBase player, string presetName, EntityAI target)
  155. {
  156. #ifdef DEVELOPER
  157. PlayerBase targetPlayer = player;
  158. if (target)
  159. {
  160. targetPlayer = PlayerBase.Cast(target);
  161. }
  162. if (!targetPlayer)
  163. return;
  164. targetPlayer.m_PresetSpawned = true;
  165. string class_name = presetName + "_Preset";
  166. typename type = class_name.ToType();
  167. if (type)
  168. {
  169. PresetSpawnBase presetObj = PresetSpawnBase.Cast(type.Spawn());
  170. if (presetObj)
  171. {
  172. presetObj.Init(targetPlayer);
  173. }
  174. }
  175. #endif
  176. }
  177. // RPC Events
  178. // Server Log Synch: Client Side
  179. void OnRPCServerLogRecieved(ParamsReadContext ctx)
  180. {
  181. Param1<string> param = new Param1<string>( "" );
  182. if ( ctx.Read(param) && param.param1 != "" )
  183. {
  184. Debug.ReceivedLogMessageFromServer(param.param1);
  185. }
  186. }
  187. void OnRPCClearInventory(PlayerBase player)
  188. {
  189. ClearInventory(player);
  190. }
  191. // Client -> Server Spawning: Server Side
  192. void OnRPCSpawnEntityOnCursorDir(PlayerBase player, ParamsReadContext ctx)
  193. {
  194. Param7<string, float, float, float, bool, string, bool> p = new Param7<string, float, float, float, bool, string, bool>("", 0, 0, 0, false, "", false);
  195. if ( ctx.Read(p) )
  196. {
  197. SpawnEntityOnCursorDir(player, p.param1, p.param2, p.param3, p.param4, p.param5, p.param6, p.param7);
  198. }
  199. }
  200. void OnRPCSpawnEntityOnGround(PlayerBase player, ParamsReadContext ctx)
  201. {
  202. Param6<string, float, float, vector, bool, bool> p = new Param6<string, float, float, vector, bool, bool>("", 0, 0, "0 0 0", false, false);
  203. if ( ctx.Read(p) )
  204. {
  205. SpawnEntityOnGroundPos(player, p.param1, p.param2, p.param3, p.param4, p.param5, p.param6);
  206. }
  207. }
  208. void OnRPCSpawnEntityOnGroundPatternGrid(PlayerBase player, ParamsReadContext ctx)
  209. {
  210. auto p = new Param10<string,int, float, float, int, int, float, float, bool, bool>("",0,0,0,0,0,0,0, false, false);
  211. if ( ctx.Read(p) )
  212. {
  213. SpawnEntityOnGroundPatternGrid(player, p.param1, p.param2, p.param3, p.param4, p.param5, p.param6, p.param7, p.param8, p.param9, p.param10);
  214. }
  215. }
  216. void OnRPCSpawnEntity(PlayerBase player, ParamsReadContext ctx)
  217. {
  218. #ifdef DEVELOPER
  219. DevSpawnItemParams p = new DevSpawnItemParams(null,"", 0, 0, false, "", FindInventoryLocationType.ANY);
  220. if ( ctx.Read(p) )
  221. {
  222. EntityAI target = EntityAI.Cast(p.param1);
  223. PlayerBase playerTarget = PlayerBase.Cast(target);
  224. EntityAI ent = SpawnEntityInInventory( target, p.param2, p.param3, p.param4, p.param5, "", p.param7);
  225. if (playerTarget && p.param5)
  226. {
  227. if (playerTarget.m_PresetSpawned)
  228. {
  229. playerTarget.m_PresetSpawned = false;
  230. playerTarget.m_PresetItems.Clear();
  231. }
  232. if (ent)
  233. {
  234. playerTarget.m_PresetItems.Insert(ent);
  235. }
  236. }
  237. }
  238. #endif
  239. }
  240. void OnSetFreeCameraEvent( PlayerBase player, FreeDebugCamera camera )
  241. {
  242. DeveloperFreeCamera.OnSetFreeCameraEvent( player, camera );
  243. }
  244. void OnSpawnErrorReport (string name)
  245. {
  246. PrintString("PluginDeveloper.SpawnEntity() Warning- Cant spawn object: " + name);
  247. //DumpStack();
  248. PrintString("PluginDeveloper.SpawnEntity() Warning END");
  249. }
  250. void SetupSpawnedEntity(PlayerBase player, EntityAI entity, float health, float quantity = -1, bool special = false, string presetName = "")
  251. {
  252. #ifdef DEVELOPER
  253. if (presetName && player.m_PresetSpawned)//this is how we ascertain this is the first item being spawned from a new preset after a previous preset was already spawned in
  254. {
  255. player.m_PresetItems.Clear();
  256. player.m_PresetSpawned = false;//is set to 'true' elsewhere after all the items have been spawned in
  257. }
  258. if ( entity.IsInherited( PlayerBase ) )
  259. {
  260. PlayerBase plr = PlayerBase.Cast( entity );
  261. plr.OnSpawnedFromConsole();
  262. }
  263. else if ( entity.IsInherited(ItemBase) )
  264. {
  265. ItemBase item = ItemBase.Cast( entity );
  266. SetupSpawnedItem(item, health, quantity);
  267. }
  268. else if (entity.IsInherited(House))
  269. {
  270. entity.PlaceOnSurface();
  271. vector pos = entity.GetPosition();
  272. vector ori = GetGame().GetSurfaceOrientation(pos[0], pos[2]);
  273. entity.SetOrientation(ori);
  274. }
  275. if (presetName)
  276. {
  277. player.m_PresetItems.Insert(entity);
  278. }
  279. if ( special )
  280. {
  281. auto debugParams = DebugSpawnParams.WithPlayer(player);
  282. entity.OnDebugSpawnEx(debugParams);
  283. }
  284. #endif
  285. }
  286. void SpawnEntityOnGroundPatternGrid( PlayerBase player, string item_name, int count, float health, float quantity, int rows, int columns, float gapRow = 1, float gapColumn = 1, bool special= false, bool withPhysics = false)
  287. {
  288. if (!item_name)
  289. {
  290. return;
  291. }
  292. if ( GetGame().IsServer() )
  293. {
  294. float rowDist = 0;
  295. float columnDist = 0;
  296. vector playerPos = player.GetPosition();
  297. vector camDirForward = player.GetDirection();
  298. vector camDirRight = camDirForward.Perpend() * -1;
  299. int countLoop = 0;
  300. for (int i = 0; i < rows; i++)
  301. {
  302. vector posRow = playerPos + camDirForward * rowDist;
  303. vector offsetSide;
  304. columnDist = 0;
  305. for (int j = 0; j < columns; j++)
  306. {
  307. offsetSide = camDirRight * columnDist;
  308. vector placement = posRow + offsetSide;
  309. float hlth = health * MiscGameplayFunctions.GetTypeMaxGlobalHealth( item_name );
  310. EntityAI ent = SpawnEntityOnGroundPos(player, item_name, hlth, quantity, placement, special );
  311. ent.PlaceOnSurface();
  312. InventoryItem item;
  313. if (Class.CastTo(item, ent) && withPhysics)
  314. item.ThrowPhysically(null, "0 0 0");
  315. countLoop++;
  316. if (countLoop == count)
  317. {
  318. return;
  319. }
  320. columnDist += gapColumn;
  321. }
  322. rowDist += gapRow;
  323. }
  324. }
  325. else
  326. {
  327. auto params = new Param10<string, int, float, float, int, int, float, float, bool, bool>(item_name, count, health, quantity, rows, columns, gapRow, gapColumn, special, withPhysics);
  328. player.RPCSingleParam(ERPCs.DEV_RPC_SPAWN_ITEM_ON_GROUND_PATTERN_GRID, params, true);
  329. }
  330. }
  331. void SpawnItemOnCrosshair(notnull PlayerBase player, string itemName, float health, float quantity, float maxDist = 100, bool allowFreeflight = false, bool special = false, bool withPhysics = false)
  332. {
  333. vector from, to;
  334. if (allowFreeflight && FreeDebugCamera.GetInstance().IsActive())
  335. {
  336. from = FreeDebugCamera.GetInstance().GetPosition();
  337. to = from + FreeDebugCamera.GetInstance().GetDirection() * maxDist;
  338. }
  339. else
  340. {
  341. from = GetGame().GetCurrentCameraPosition();
  342. to = from + GetGame().GetCurrentCameraDirection() * maxDist;
  343. }
  344. float hitFraction;
  345. vector start, end;
  346. vector direction;
  347. vector hitPos, hitNormal;
  348. Object obj;
  349. PhxInteractionLayers hitMask = PhxInteractionLayers.BUILDING | PhxInteractionLayers.DOOR | PhxInteractionLayers.VEHICLE | PhxInteractionLayers.ROADWAY | PhxInteractionLayers.TERRAIN | PhxInteractionLayers.CHARACTER | PhxInteractionLayers.AI | PhxInteractionLayers.RAGDOLL | PhxInteractionLayers.RAGDOLL_NO_CHARACTER;
  350. DayZPhysics.RayCastBullet(from, to, hitMask, player, obj, hitPos, hitNormal, hitFraction);
  351. // something is hit
  352. if (hitPos != vector.Zero)
  353. {
  354. SpawnEntityOnGroundPos(player, itemName, health, quantity, hitPos, special, withPhysics);
  355. }
  356. }
  357. /**
  358. * @fn SpawnEntityOnGroundPos
  359. * @brief spawns entity on exact position
  360. * @param[in] health \p health of the item
  361. * @param[in] quantity \p quantity to set if item.HasQuantity() (-1 == set to max)
  362. * @param[in] pos \p position where to spawn
  363. * @return entity if ok, null otherwise
  364. **/
  365. EntityAI SpawnEntityOnGroundPos( PlayerBase player, string item_name, float health, float quantity, vector pos, bool special = false, bool withPhysics = false)
  366. {
  367. if ( GetGame().IsServer() )
  368. {
  369. EntityAI entity = player.SpawnEntityOnGroundPos(item_name, pos);
  370. if (entity)
  371. SetupSpawnedEntity(player, entity, health, quantity, special);
  372. else
  373. OnSpawnErrorReport(item_name);
  374. InventoryItem item;
  375. if (Class.CastTo(item, entity) && withPhysics)
  376. item.ThrowPhysically(null, "0 0 0");
  377. return entity;
  378. }
  379. else
  380. {
  381. Param6<string, float, float, vector, bool, bool> params = new Param6<string, float, float, vector, bool, bool>(item_name, health, quantity, pos, special, withPhysics);
  382. player.RPCSingleParam(ERPCs.DEV_RPC_SPAWN_ITEM_ON_GROUND, params, true);
  383. }
  384. return NULL;
  385. }
  386. /**
  387. * @fn SpawnEntityOnCursorDir
  388. * @brief spawns entity in direction of cursor at specified distance
  389. * @param[in] health \p health of the item
  390. * @param[in] quantity \p quantity to set if item.HasQuantity() (-1 == set to max)
  391. * @param[in] distance \p distance of the item from player
  392. * @return entity if ok, null otherwise
  393. **/
  394. EntityAI SpawnEntityOnCursorDir( PlayerBase player, string item_name, float quantity, float distance, float health = -1, bool special = false, string presetName = "", bool withPhysics = false)
  395. {
  396. if ( GetGame().IsServer() )
  397. {
  398. // Client -> Server Spawning: Server Side
  399. EntityAI entity = player.SpawnEntityOnGroundOnCursorDir(item_name, distance);
  400. if ( entity )
  401. {
  402. if ( !entity.IsBuilding() && health < 0 && entity.GetMaxHealth() > 0)//check for default (-1)
  403. {
  404. health = entity.GetMaxHealth();
  405. }
  406. SetupSpawnedEntity( player,entity, health, quantity, special, presetName );
  407. }
  408. else
  409. OnSpawnErrorReport( item_name );
  410. InventoryItem item;
  411. if (Class.CastTo(item, entity) && withPhysics)
  412. item.ThrowPhysically(null, "0 0 0");
  413. return entity;
  414. }
  415. else
  416. {
  417. // Client -> Server Spawning: Client Side
  418. Param7<string, float, float, float, bool, string, bool> params = new Param7<string, float, float, float, bool, string, bool>(item_name, quantity, distance, health, special, presetName, withPhysics);
  419. player.RPCSingleParam(ERPCs.DEV_RPC_SPAWN_ITEM_ON_CURSOR, params, true);
  420. }
  421. return NULL;
  422. }
  423. /**
  424. * @fn SpawnEntityInInventory
  425. * @brief tries to spawn item somewhere in inventory
  426. *
  427. * SpawnEntityInInventory queries future parent about free inventory location of the future item
  428. *
  429. * @param[in] health \p health of the item
  430. * @param[in] quantity \p quantity to set if item.HasQuantity() (-1 == set to max)
  431. * @return entity if ok, null otherwise
  432. **/
  433. EntityAI SpawnEntityInInventory( notnull EntityAI target, string className, float health, float quantity, bool special = false, string presetName = "", FindInventoryLocationType locationType = FindInventoryLocationType.ANY)
  434. {
  435. if (target.IsPlayer())
  436. {
  437. return SpawnEntityInPlayerInventory(PlayerBase.Cast(target), className, health, quantity, special, presetName, locationType);
  438. }
  439. if ( GetGame().IsServer() )
  440. {
  441. InventoryLocation il = new InventoryLocation;
  442. if (target.GetInventory() && target.GetInventory().FindFirstFreeLocationForNewEntity(className, FindInventoryLocationType.ANY, il))
  443. {
  444. EntityAI eai = SpawnEntity(className, il, ECE_IN_INVENTORY, RF_DEFAULT);
  445. if ( eai && eai.IsInherited(ItemBase) )
  446. {
  447. if ( health < 0 )//check for default (-1)
  448. {
  449. health = eai.GetMaxHealth();
  450. }
  451. ItemBase i = ItemBase.Cast( eai );
  452. SetupSpawnedItem(i, health, quantity);
  453. if ( special )
  454. {
  455. auto debugParams = DebugSpawnParams.WithPlayer(null);
  456. eai.OnDebugSpawnEx(debugParams);
  457. }
  458. }
  459. return eai;
  460. }
  461. }
  462. else
  463. {
  464. DevSpawnItemParams params = new DevSpawnItemParams(target, className, health, quantity, special, presetName, 0 );
  465. GetGame().GetPlayer().RPCSingleParam(ERPCs.DEV_RPC_SPAWN_ITEM_IN_INVENTORY, params, true, GetGame().GetPlayer().GetIdentity());
  466. }
  467. return null;
  468. }
  469. EntityAI SpawnEntityInPlayerInventory(PlayerBase player, string item_name, float health, float quantity, bool special = false, string presetName = "", FindInventoryLocationType locationType = FindInventoryLocationType.ANY)
  470. {
  471. if (player)
  472. {
  473. if (GetGame().IsServer())
  474. {
  475. if (locationType == FindInventoryLocationType.HANDS && player.GetItemInHands())
  476. {
  477. if (!GetGame().IsMultiplayer())
  478. player.DropItem(player.GetItemInHands());
  479. else
  480. player.ServerDropEntity(player.GetItemInHands());
  481. GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(SpawnEntityInPlayerInventory, 500, false, player, item_name, health, quantity, special, presetName, locationType);
  482. return null;
  483. }
  484. #ifdef DEVELOPER
  485. if (GetGame().IsKindOf(item_name, "Transport"))
  486. {
  487. EntityAI vehicle = SpawnEntityOnGroundPos(player, item_name, 1, quantity, player.GetPosition());
  488. auto debugParams = DebugSpawnParams.WithPlayer(player);
  489. vehicle.OnDebugSpawnEx(debugParams);
  490. if (GetGame().IsMultiplayer())
  491. {
  492. DayZPlayerSyncJunctures.SendGetInVehicle(player, vehicle);
  493. }
  494. else
  495. {
  496. player.SetGetInVehicleDebug(vehicle);
  497. }
  498. return vehicle;
  499. }
  500. #endif
  501. InventoryLocation il = new InventoryLocation;
  502. if (player.GetInventory() && player.GetInventory().FindFirstFreeLocationForNewEntity(item_name, locationType, il))
  503. {
  504. Weapon_Base wpn = Weapon_Base.Cast(il.GetParent());
  505. bool is_mag = il.GetSlot() == InventorySlots.MAGAZINE || il.GetSlot() == InventorySlots.MAGAZINE2 || il.GetSlot() == InventorySlots.MAGAZINE3;
  506. if (wpn && is_mag)
  507. {
  508. vector pos = player.GetPosition();
  509. EntityAI eai_gnd = SpawnEntityOnGroundPos(player, item_name, health, quantity, pos);
  510. Magazine mag_gnd = Magazine.Cast(eai_gnd);
  511. if (mag_gnd && player.GetWeaponManager().CanAttachMagazine(wpn, mag_gnd))
  512. {
  513. player.GetWeaponManager().AttachMagazine(mag_gnd);
  514. }
  515. return eai_gnd;
  516. }
  517. else
  518. {
  519. EntityAI eai = SpawnEntity(item_name, il, ECE_IN_INVENTORY, RF_DEFAULT);
  520. if ( eai && eai.IsInherited(ItemBase) )
  521. {
  522. if ( health < 0 )//check for default (-1)
  523. {
  524. health = eai.GetMaxHealth();
  525. }
  526. ItemBase i = ItemBase.Cast( eai );
  527. SetupSpawnedItem(i, health, quantity);
  528. if ( special )
  529. {
  530. auto debugParams2 = DebugSpawnParams.WithPlayer(player);
  531. eai.OnDebugSpawnEx(debugParams2);
  532. }
  533. }
  534. return eai;
  535. }
  536. }
  537. else
  538. OnSpawnErrorReport(item_name);
  539. return NULL;
  540. }
  541. else
  542. {
  543. // Client -> Server Spawning: Client Side
  544. DevSpawnItemParams params = new DevSpawnItemParams(player, item_name, health, quantity, special, presetName, locationType );
  545. player.RPCSingleParam(ERPCs.DEV_RPC_SPAWN_ITEM_IN_INVENTORY, params, true);
  546. }
  547. }
  548. return NULL;
  549. }
  550. /**
  551. * @fn SpawnEntityAsAttachment
  552. * @brief tries to spawn item as attachment
  553. * @param[in] health \p health of the item
  554. * @param[in] quantity \p quantity to set if item.HasQuantity() (-1 == set to max)
  555. * @return entity if ok, null otherwise
  556. **/
  557. EntityAI SpawnEntityAsAttachment (PlayerBase player, EntityAI parent, string att_name, float health, float quantity)
  558. {
  559. if ( GetGame().IsServer() )
  560. {
  561. Weapon_Base wpn = Weapon_Base.Cast(parent);
  562. if (wpn)
  563. {
  564. GameInventory inventory = parent.GetInventory();
  565. return inventory.CreateInInventory(att_name);
  566. /*
  567. vector pos = player.GetPosition();
  568. EntityAI eai_gnd = SpawnEntityOnGroundPos(player, att_name, health, quantity, pos);
  569. Magazine mag_gnd = Magazine.Cast(eai_gnd);
  570. if (mag_gnd && player.GetWeaponManager().CanAttachMagazine(wpn, mag_gnd))
  571. {
  572. player.GetWeaponManager().AttachMagazine(mag_gnd);
  573. }
  574. return eai_gnd;
  575. */
  576. }
  577. else
  578. {
  579. EntityAI eai = parent.GetInventory().CreateAttachment(att_name);
  580. if (eai)
  581. {
  582. if ( eai.IsInherited(ItemBase) )
  583. {
  584. ItemBase i = ItemBase.Cast( eai );
  585. SetupSpawnedItem(ItemBase.Cast( eai ), health, quantity);
  586. }
  587. return eai;
  588. }
  589. else
  590. OnSpawnErrorReport(att_name);
  591. }
  592. return NULL;
  593. }
  594. else
  595. {
  596. // Client -> Server Spawning: Client Side
  597. Param3<string, float, float> params = new Param3<string, float, float>(att_name, health, quantity);
  598. player.RPCSingleParam(ERPCs.DEV_RPC_SPAWN_ITEM_AS_ATTACHMENT, params, true);
  599. }
  600. return NULL;
  601. }
  602. EntityAI SpawnFromClipboard()
  603. {
  604. UIScriptedMenu menu_curr = GetGame().GetUIManager().GetMenu();
  605. if ( menu_curr == NULL )
  606. {
  607. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  608. if ( player )
  609. {
  610. if ( !GetGame().GetWorld().Is3rdPersonDisabled() )
  611. {
  612. player.SetIsInThirdPerson(!player.IsInThirdPerson());//this counters the effect of switching camera through pressing the 'V' key
  613. }
  614. vector pos_player = player.GetPosition();
  615. // Get item from clipboard
  616. string clipboard;
  617. GetGame().CopyFromClipboard(clipboard);
  618. if (!clipboard.Contains(","))
  619. {
  620. //single item
  621. if (DeveloperFreeCamera.IsFreeCameraEnabled())
  622. SpawnItemOnCrosshair(player, clipboard.Trim(), -1, 1, 40, true );
  623. else
  624. SpawnEntityOnCursorDir(player, clipboard.Trim(), 1, 1);
  625. }
  626. else
  627. {
  628. TStringArray items = new TStringArray;
  629. clipboard.Split( ",", items );
  630. foreach (string item:items)
  631. {
  632. if (DeveloperFreeCamera.IsFreeCameraEnabled())
  633. SpawnItemOnCrosshair(player, item.Trim(), -1, 1, 40, true );
  634. else
  635. SpawnEntityOnCursorDir(player, item.Trim(), 1, 1);
  636. }
  637. }
  638. }
  639. }
  640. return NULL;
  641. }
  642. // Clear Entity Inventory
  643. void ClearInventory(EntityAI entity)
  644. {
  645. if ( GetGame().IsServer() )
  646. {
  647. entity.ClearInventory();
  648. }
  649. else
  650. {
  651. Param1<int> params = new Param1<int>(0);
  652. entity.RPCSingleParam(ERPCs.DEV_RPC_CLEAR_INV, params, true);
  653. }
  654. }
  655. void ToggleHelpScreen()
  656. {
  657. if ( g_Game.GetUIManager().GetMenu() == NULL )
  658. {
  659. g_Game.GetUIManager().EnterScriptedMenu(MENU_HELP_SCREEN, NULL);
  660. }
  661. else if ( g_Game.GetUIManager().IsMenuOpen(MENU_HELP_SCREEN) )
  662. {
  663. g_Game.GetUIManager().Back();
  664. }
  665. }
  666. void ToggleScriptConsole()
  667. {
  668. if (GetGame() != null && !g_Game.IsLoading() && GetGame().GetMission())
  669. {
  670. if ( g_Game.GetUIManager().GetMenu() == NULL )
  671. {
  672. m_ScriptConsole = g_Game.GetUIManager().EnterScriptedMenu(MENU_SCRIPTCONSOLE, NULL);
  673. GetGame().GetMission().AddActiveInputExcludes({"menu"});
  674. }
  675. else if ( g_Game.GetUIManager().IsMenuOpen(MENU_SCRIPTCONSOLE) )
  676. {
  677. g_Game.GetUIManager().Back();
  678. GetGame().GetMission().RemoveActiveInputExcludes({"menu"},true);
  679. }
  680. }
  681. }
  682. // Mission Editor
  683. void ToggleMissionLoader()
  684. {
  685. if ( g_Game.GetUIManager().IsMenuOpen(MENU_MISSION_LOADER) )
  686. {
  687. g_Game.GetUIManager().Back();
  688. GetGame().GetMission().RemoveActiveInputExcludes({"menu"},true);
  689. return;
  690. }
  691. if ( g_Game.GetUIManager().GetMenu() )
  692. g_Game.GetUIManager().GetMenu().Close();
  693. g_Game.GetUIManager().EnterScriptedMenu(MENU_MISSION_LOADER, NULL);
  694. GetGame().GetMission().AddActiveInputExcludes({"menu"});
  695. }
  696. // Script Editor History
  697. private void ScriptHistoryNext()
  698. {
  699. // Console key press
  700. if ( g_Game.GetUIManager().IsMenuOpen(MENU_SCRIPTCONSOLE) )
  701. {
  702. CallMethod(CALL_ID_SCR_CNSL_HISTORY_NEXT, NULL);
  703. }
  704. }
  705. private void ScriptHistoryBack()
  706. {
  707. // Console key press
  708. if ( g_Game.GetUIManager().IsMenuOpen(MENU_SCRIPTCONSOLE) )
  709. {
  710. CallMethod(CALL_ID_SCR_CNSL_HISTORY_BACK, NULL);
  711. }
  712. }
  713. private bool IsIngame()
  714. {
  715. UIScriptedMenu menu_curr = GetGame().GetUIManager().GetMenu();
  716. if ( menu_curr == NULL )
  717. {
  718. return true;
  719. }
  720. return false;
  721. }
  722. private bool IsInConsole()
  723. {
  724. UIScriptedMenu menu_curr = GetGame().GetUIManager().GetMenu();
  725. if ( menu_curr != NULL && menu_curr.GetID() == MENU_SCRIPTCONSOLE )
  726. {
  727. return true;
  728. }
  729. return false;
  730. }
  731. // Tools
  732. int QuickSortPartition( TStringArray arr, int left, int right )
  733. {
  734. string pivot = arr.Get( left );
  735. int i = left;
  736. int j = right + 1;
  737. string temp;
  738. while ( true )
  739. {
  740. while ( true )
  741. {
  742. i++;
  743. if ( i > right || arr.Get(i) > pivot )
  744. {
  745. break;
  746. }
  747. }
  748. while ( true )
  749. {
  750. j--;
  751. if ( arr.Get(j) <= pivot )
  752. {
  753. break;
  754. }
  755. }
  756. if ( i >= j )
  757. {
  758. break;
  759. }
  760. temp = arr.Get( i );
  761. arr.Set( i, arr.Get(j) );
  762. arr.Set( j, temp );
  763. }
  764. temp = arr.Get( left );
  765. arr.Set( left, arr.Get(j) );
  766. arr.Set( j, temp );
  767. return j;
  768. }
  769. void QuickSort( TStringArray arr, int left, int right )
  770. {
  771. int j;
  772. if ( left < right )
  773. {
  774. j = QuickSortPartition( arr, left, right );
  775. QuickSort( arr, left, j - 1 );
  776. QuickSort( arr, j + 1, right );
  777. }
  778. }
  779. void SortStringArray( TStringArray arr )
  780. {
  781. QuickSort( arr, 0, arr.Count() - 1 );
  782. }
  783. void ResetGUI()
  784. {
  785. if ( GetGame() && GetGame().GetMission() )
  786. {
  787. GetGame().GetMission().ResetGUI();
  788. }
  789. }
  790. static void SetDeveloperItemClientEx(notnull Object entity, bool getFocus = false)
  791. {
  792. PlayerBase player = PlayerBase.Cast(GetGame().GetPlayer());
  793. if (getFocus)
  794. {
  795. GetGame().GetInput().ChangeGameFocus(1);
  796. GetGame().GetUIManager().ShowUICursor(true);
  797. }
  798. if (GetGame().IsMultiplayer())
  799. {
  800. GetGame().RPCSingleParam(GetGame().GetPlayer(), ERPCs.DEV_SET_DEV_ITEM, new Param1<Object>(entity), true, GetGame().GetPlayer().GetIdentity());
  801. }
  802. else
  803. {
  804. PluginItemDiagnostic mid2 = PluginItemDiagnostic.Cast(GetPlugin(PluginItemDiagnostic));
  805. mid2.RegisterDebugItem(entity, player);
  806. }
  807. #ifdef DEVELOPER
  808. SetDebugDeveloper_item(entity);
  809. #endif
  810. }
  811. void SetDeveloperItemClient()
  812. {
  813. #ifdef DEVELOPER
  814. Object entity;
  815. PlayerBase player = PlayerBase.Cast(GetGame().GetPlayer());
  816. FreeDebugCamera debugCam = FreeDebugCamera.GetInstance();
  817. if (debugCam && debugCam.GetCurrentCamera())
  818. {
  819. entity = debugCam.GetCrosshairObject();
  820. }
  821. else
  822. {
  823. if (player && player.GetActionManager())
  824. {
  825. ActionTarget at = player.GetActionManager().FindActionTarget();
  826. entity = at.GetObject();
  827. }
  828. }
  829. if (entity)
  830. {
  831. SetDeveloperItemClientEx(entity, true);
  832. }
  833. #endif
  834. }
  835. }