BVB Source Codes

CRYENGINE Show ScriptBind_Vehicle.cpp Source code

Return Download CRYENGINE: download ScriptBind_Vehicle.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4.    -------------------------------------------------------------------------
  5.    $Id$
  6.    $DateTime$
  7.    Description: Script Binding for the Vehicle System
  8.  
  9.    -------------------------------------------------------------------------
  10.    History:
  11.    - 05:10:2004   12:05 : Created by Mathieu Pinard
  12.  
  13. *************************************************************************/
  14. #include "StdAfx.h"
  15. #include <IActionMapManager.h>
  16. #include <CryAnimation/ICryAnimation.h>
  17. #include <CryGame/IGameFramework.h>
  18. #include "IActorSystem.h"
  19. #include <vector>
  20. #include <CryRenderer/IRenderAuxGeom.h>
  21. #include <CryMath/Cry_GeoOverlap.h>
  22. #include <IGameRulesSystem.h>
  23.  
  24. #include "VehicleSystem.h"
  25. #include "Vehicle.h"
  26. #include "VehicleComponent.h"
  27. #include "VehicleSeat.h"
  28. #include "ScriptBind_Vehicle.h"
  29. #include "VehicleDamages.h"
  30. #include "IGameObject.h"
  31.  
  32. //------------------------------------------------------------------------
  33. // macro for retrieving vehicle and entity
  34. #undef GET_ENTITY
  35. #define GET_ENTITY IVehicle * pVehicle = GetVehicle(pH);      \
  36.   IEntity* pEntity = pVehicle ? pVehicle->GetEntity() : NULL; \
  37.   if (!pEntity) return pH->EndFunction();
  38.  
  39. //------------------------------------------------------------------------
  40. CScriptBind_Vehicle::CScriptBind_Vehicle(ISystem* pSystem, IGameFramework* pGameFW)
  41. {
  42.         m_pVehicleSystem = pGameFW->GetIVehicleSystem();
  43.  
  44.         Init(gEnv->pScriptSystem, gEnv->pSystem, 1);
  45.  
  46.         //CScriptableBase::Init(m_pSS, pSystem);
  47.         //SetGlobalName("vehicle");
  48.  
  49.         RegisterMethods();
  50. }
  51.  
  52. //------------------------------------------------------------------------
  53. void CScriptBind_Vehicle::RegisterMethods()
  54. {
  55. #undef SCRIPT_REG_CLASSNAME
  56. #define SCRIPT_REG_CLASSNAME &CScriptBind_Vehicle::
  57.  
  58.         SCRIPT_REG_TEMPLFUNC(IsInsideRadius, "pos, radius");
  59.  
  60.         SCRIPT_REG_TEMPLFUNC(MultiplyWithWorldTM, "pos");
  61.  
  62.         SCRIPT_REG_TEMPLFUNC(AddSeat, "params");
  63.  
  64.         SCRIPT_REG_TEMPLFUNC(HasHelper, "name");
  65.         SCRIPT_REG_TEMPLFUNC(GetHelperPos, "name, isVehicleSpace");
  66.         SCRIPT_REG_TEMPLFUNC(GetHelperDir, "name, isVehicleSpace");
  67.  
  68.         SCRIPT_REG_TEMPLFUNC(GetHelperWorldPos, "name");
  69.  
  70.         SCRIPT_REG_TEMPLFUNC(EnableMovement, "enable");
  71.         SCRIPT_REG_TEMPLFUNC(DisableEngine, "disable");
  72.  
  73.         SCRIPT_REG_TEMPLFUNC(OnHit, "targetId, shooterId, damage, position, radius, hitTypeId, explosion");
  74.         SCRIPT_REG_TEMPLFUNC(ProcessPassengerDamage, "passengerHandle, actorHealth, damage, pDamageClass, explosion");
  75.         SCRIPT_REG_TEMPLFUNC(Destroy, "");
  76.         SCRIPT_REG_TEMPLFUNC(IsDestroyed, "");
  77.  
  78.         SCRIPT_REG_TEMPLFUNC(IsUsable, "userId");
  79.         SCRIPT_REG_TEMPLFUNC(OnUsed, "userId, index");
  80.  
  81.         SCRIPT_REG_TEMPLFUNC(EnterVehicle, "actorId, seatIndex, isAnimationEnabled");
  82.         SCRIPT_REG_TEMPLFUNC(ChangeSeat, "actorId, seatIndex, isAnimationEnabled");
  83.         SCRIPT_REG_TEMPLFUNC(ExitVehicle, "actorId");
  84.  
  85.         SCRIPT_REG_TEMPLFUNC(GetComponentDamageRatio, "componentName");
  86.         SCRIPT_REG_TEMPLFUNC(OnSpawnComplete, "");
  87.  
  88.         SCRIPT_REG_TEMPLFUNC(GetSeatForPassenger, "passengerId");
  89. }
  90.  
  91. //------------------------------------------------------------------------
  92. void CScriptBind_Vehicle::AttachTo(IVehicle* pVehicle)
  93. {
  94.         IScriptTable* pScriptTable = pVehicle->GetEntity()->GetScriptTable();
  95.  
  96.         if (!pScriptTable)
  97.                 return;
  98.  
  99.         SmartScriptTable thisTable(gEnv->pScriptSystem);
  100.         thisTable->SetValue("vehicleId", ScriptHandle(pVehicle->GetEntityId()));
  101.         thisTable->Delegate(GetMethodsTable());
  102.         pScriptTable->SetValue("vehicle", thisTable);
  103.  
  104.         SmartScriptTable seatTable(gEnv->pScriptSystem);
  105.         pScriptTable->SetValue("Seats", seatTable);
  106. }
  107.  
  108. //------------------------------------------------------------------------
  109. CVehicle* CScriptBind_Vehicle::GetVehicle(IFunctionHandler* pH)
  110. {
  111.         ScriptHandle handle;
  112.         SmartScriptTable table;
  113.  
  114.         if (pH->GetSelf(table))
  115.         {
  116.                 if (table->GetValue("vehicleId", handle))
  117.                 {
  118.                         return (CVehicle*)m_pVehicleSystem->GetVehicle((EntityId)handle.n);
  119.                 }
  120.         }
  121.  
  122.         return 0;
  123. }
  124.  
  125. //------------------------------------------------------------------------
  126. CScriptBind_Vehicle::~CScriptBind_Vehicle()
  127. {
  128. }
  129.  
  130. //------------------------------------------------------------------------
  131. int CScriptBind_Vehicle::Reset(IFunctionHandler* pH)
  132. {
  133.         IVehicle* pVehicle = GetVehicle(pH);
  134.  
  135.         if (pVehicle)
  136.                 pVehicle->Reset(0);
  137.  
  138.         return pH->EndFunction();
  139. }
  140.  
  141. //------------------------------------------------------------------------
  142. int CScriptBind_Vehicle::IsInsideRadius(IFunctionHandler* pH, Vec3 pos, float radius)
  143. {
  144.         IVehicle* vehicle = GetVehicle(pH);
  145.  
  146.         if (!vehicle)
  147.                 return pH->EndFunction();
  148.  
  149.         AABB boundingBox;
  150.         IEntity* vehicleEntity = vehicle->GetEntity();
  151.         vehicleEntity->GetWorldBounds(boundingBox);
  152.  
  153.         Sphere sphere(pos, radius);
  154.         return pH->EndFunction(Overlap::Sphere_AABB(sphere, boundingBox));
  155. }
  156.  
  157. //------------------------------------------------------------------------
  158. int CScriptBind_Vehicle::MultiplyWithWorldTM(IFunctionHandler* pH, Vec3 pos)
  159. {
  160.         GET_ENTITY;
  161.  
  162.         return pH->EndFunction(pEntity->GetWorldTM() * pos);
  163. }
  164.  
  165. //------------------------------------------------------------------------
  166. int CScriptBind_Vehicle::AddSeat(IFunctionHandler* pH, SmartScriptTable paramsTable)
  167. {
  168.         IVehicle* pVehicle = GetVehicle(pH);
  169.         if (pVehicle)
  170.         {
  171.                 pVehicle->AddSeat(paramsTable);
  172.         }
  173.  
  174.         return pH->EndFunction();
  175. }
  176.  
  177. //------------------------------------------------------------------------
  178. int CScriptBind_Vehicle::HasHelper(IFunctionHandler* pH, const char* name)
  179. {
  180.         if (IVehicle* pVehicle = GetVehicle(pH))
  181.         {
  182.                 IVehicleHelper* pHelper = pVehicle->GetHelper(name);
  183.                 return pH->EndFunction(pHelper != NULL);
  184.         }
  185.  
  186.         return pH->EndFunction(false);
  187. }
  188.  
  189. //------------------------------------------------------------------------
  190. int CScriptBind_Vehicle::GetHelperPos(IFunctionHandler* pH, const char* name, bool isInVehicleSpace)
  191. {
  192.         if (IVehicle* pVehicle = GetVehicle(pH))
  193.         {
  194.                 if (IVehicleHelper* pHelper = pVehicle->GetHelper(name))
  195.                 {
  196.                         if (isInVehicleSpace)
  197.                                 return pH->EndFunction(pHelper->GetVehicleSpaceTranslation());
  198.                         else
  199.                                 return pH->EndFunction(pHelper->GetLocalSpaceTranslation());
  200.                 }
  201.         }
  202.  
  203.         return pH->EndFunction(Vec3(0.0f, 0.0f, 0.0f));
  204. }
  205.  
  206. //------------------------------------------------------------------------
  207. int CScriptBind_Vehicle::GetHelperDir(IFunctionHandler* pH, const char* name, bool isInVehicleSpace)
  208. {
  209.         if (IVehicle* pVehicle = GetVehicle(pH))
  210.         {
  211.                 if (IVehicleHelper* pHelper = pVehicle->GetHelper(name))
  212.                 {
  213.                         Matrix34 tm;
  214.                         if (isInVehicleSpace)
  215.                                 pHelper->GetVehicleTM(tm);
  216.                         else
  217.                                 tm = pHelper->GetLocalTM();
  218.  
  219.                         return pH->EndFunction(tm.TransformVector(FORWARD_DIRECTION));
  220.                 }
  221.         }
  222.  
  223.         return pH->EndFunction(Vec3(0.0f, 1.0f, 0.0f));
  224. }
  225.  
  226. //------------------------------------------------------------------------
  227. int CScriptBind_Vehicle::GetHelperWorldPos(IFunctionHandler* pH, const char* name)
  228. {
  229.         if (IVehicle* pVehicle = GetVehicle(pH))
  230.         {
  231.                 if (IVehicleHelper* pHelper = pVehicle->GetHelper(name))
  232.                 {
  233.                         return pH->EndFunction(pHelper->GetWorldSpaceTranslation());
  234.                 }
  235.         }
  236.  
  237.         return pH->EndFunction(Vec3(0.0f, 0.0f, 0.0f));
  238. }
  239.  
  240. //------------------------------------------------------------------------
  241. int CScriptBind_Vehicle::EnableMovement(IFunctionHandler* pH, bool enable)
  242. {
  243.         if (CVehicle* pVehicle = GetVehicle(pH))
  244.         {
  245.                 if (pVehicle->GetMovement())
  246.                         pVehicle->GetMovement()->EnableMovementProcessing(enable);
  247.         }
  248.  
  249.         return pH->EndFunction();
  250. }
  251.  
  252. //------------------------------------------------------------------------
  253. int CScriptBind_Vehicle::DisableEngine(IFunctionHandler* pH, bool disable)
  254. {
  255.         if (CVehicle* pVehicle = GetVehicle(pH))
  256.         {
  257.                 if (pVehicle->GetMovement())
  258.                         pVehicle->GetMovement()->DisableEngine(disable);
  259.         }
  260.  
  261.         return pH->EndFunction();
  262. }
  263.  
  264. //------------------------------------------------------------------------
  265. int CScriptBind_Vehicle::OnHit(IFunctionHandler* pH, ScriptHandle targetId, ScriptHandle shooterId, float damage, Vec3 position, float radius, int hitTypeId, bool explosion)
  266. {
  267.         if (CVehicle* pVehicle = GetVehicle(pH))
  268.         {
  269.                 HitInfo hitInfo;
  270.                 hitInfo.shooterId = (EntityId)shooterId.n;
  271.                 hitInfo.targetId = (EntityId)targetId.n;
  272.                 hitInfo.damage = damage;
  273.                 hitInfo.pos = position;
  274.                 hitInfo.radius = radius;
  275.                 hitInfo.type = hitTypeId;
  276.                 hitInfo.explosion = explosion;
  277.  
  278.                 pVehicle->OnHit(hitInfo);
  279.         }
  280.         return pH->EndFunction();
  281. }
  282.  
  283. //------------------------------------------------------------------------
  284. int CScriptBind_Vehicle::ProcessPassengerDamage(IFunctionHandler* pH, ScriptHandle passengerHandle, float actorHealth, float damage, int hitTypeId, bool explosion)
  285. {
  286.         CVehicle* pVehicle = GetVehicle(pH);
  287.         if (!pVehicle || !passengerHandle.n)
  288.                 return pH->EndFunction(0.0f);
  289.  
  290.         if (CVehicleSeat* pSeat = (CVehicleSeat*)pVehicle->GetSeatForPassenger((EntityId)passengerHandle.n))
  291.         {
  292.                 float newDamage = pSeat->ProcessPassengerDamage(actorHealth, damage, hitTypeId, explosion);
  293.                 return pH->EndFunction(newDamage);
  294.         }
  295.  
  296.         return pH->EndFunction(0.0f);
  297. }
  298.  
  299. //------------------------------------------------------------------------
  300. int CScriptBind_Vehicle::IsUsable(IFunctionHandler* pH, ScriptHandle userHandle)
  301. {
  302.         CVehicle* pVehicle = GetVehicle(pH);
  303.         if (!pVehicle || !userHandle.n)
  304.                 return pH->EndFunction(0);
  305.  
  306.         int ret = pVehicle->IsUsable((EntityId)userHandle.n);
  307.         return pH->EndFunction(ret);
  308. }
  309.  
  310. //------------------------------------------------------------------------
  311. int CScriptBind_Vehicle::OnUsed(IFunctionHandler* pH, ScriptHandle userHandle, int index)
  312. {
  313.         CVehicle* pVehicle = GetVehicle(pH);
  314.         if (!pVehicle || !userHandle.n)
  315.                 return pH->EndFunction(0);
  316.  
  317.         int ret = pVehicle->OnUsed((EntityId)userHandle.n, index);
  318.         return pH->EndFunction(ret);
  319. }
  320.  
  321. //------------------------------------------------------------------------
  322. int CScriptBind_Vehicle::EnterVehicle(IFunctionHandler* pH, ScriptHandle actorHandle, int seatId, bool isAnimationEnabled)
  323. {
  324.         CVehicle* pVehicle = GetVehicle(pH);
  325.         if (!pVehicle || !actorHandle.n)
  326.                 return pH->EndFunction(0);
  327.  
  328.         IVehicleSeat* pSeat = pVehicle->GetSeatById(seatId);
  329.  
  330.         bool ret = false;
  331.  
  332.         if (pSeat)
  333.                 ret = pSeat->Enter((EntityId)actorHandle.n, isAnimationEnabled);
  334.  
  335.         return pH->EndFunction(ret);
  336. }
  337.  
  338. //------------------------------------------------------------------------
  339. int CScriptBind_Vehicle::ChangeSeat(IFunctionHandler* pH, ScriptHandle actorHandle, int seatId, bool isAnimationEnabled)
  340. {
  341.         CVehicle* pVehicle = GetVehicle(pH);
  342.         if (!pVehicle || !actorHandle.n)
  343.                 return pH->EndFunction(0);
  344.  
  345.         bool ret = true;
  346.         pVehicle->ChangeSeat((EntityId)actorHandle.n, seatId, 0);
  347.  
  348.         return pH->EndFunction(ret);
  349. }
  350.  
  351. //------------------------------------------------------------------------
  352. int CScriptBind_Vehicle::ExitVehicle(IFunctionHandler* pH, ScriptHandle actorHandle)
  353. {
  354.         CVehicle* pVehicle = GetVehicle(pH);
  355.         if (!pVehicle || !actorHandle.n)
  356.                 return pH->EndFunction(0);
  357.  
  358.         if (IVehicleSeat* pSeat = pVehicle->GetSeatForPassenger((EntityId)actorHandle.n))
  359.         {
  360.                 bool ret = pSeat->Exit(true);
  361.                 return pH->EndFunction(ret);
  362.         }
  363.  
  364.         return pH->EndFunction(false);
  365. }
  366.  
  367. //------------------------------------------------------------------------
  368. int CScriptBind_Vehicle::GetComponentDamageRatio(IFunctionHandler* pH, const char* pComponentName)
  369. {
  370.         CVehicle* pVehicle = GetVehicle(pH);
  371.  
  372.         if (!pVehicle)
  373.                 return pH->EndFunction();
  374.  
  375.         if (IVehicleComponent* pComponent = pVehicle->GetComponent(pComponentName))
  376.                 return pH->EndFunction(pComponent->GetDamageRatio());
  377.  
  378.         return pH->EndFunction();
  379. }
  380.  
  381. //------------------------------------------------------------------------
  382. int CScriptBind_Vehicle::OnSpawnComplete(IFunctionHandler* pH)
  383. {
  384.         CVehicle* pVehicle = GetVehicle(pH);
  385.  
  386.         if (!pVehicle)
  387.                 return pH->EndFunction();
  388.  
  389.         pVehicle->OnSpawnComplete();
  390.  
  391.         return pH->EndFunction();
  392. }
  393.  
  394. //------------------------------------------------------------------------
  395. int CScriptBind_Vehicle::Destroy(IFunctionHandler* pH)
  396. {
  397.         CVehicle* pVehicle = GetVehicle(pH);
  398.  
  399.         if (!pVehicle)
  400.                 return pH->EndFunction();
  401.  
  402.         pVehicle->Destroy();
  403.  
  404.         return pH->EndFunction();
  405. }
  406.  
  407. //------------------------------------------------------------------------
  408. int CScriptBind_Vehicle::IsDestroyed(IFunctionHandler* pH)
  409. {
  410.         if (CVehicle* pVehicle = GetVehicle(pH))
  411.         {
  412.                 return pH->EndFunction(pVehicle->IsDestroyed());
  413.         }
  414.  
  415.         return pH->EndFunction(false);
  416. }
  417.  
  418. //------------------------------------------------------------------------
  419. int CScriptBind_Vehicle::GetSeatForPassenger(IFunctionHandler* pH, ScriptHandle passengerId)
  420. {
  421.         if (CVehicle* pVehicle = GetVehicle(pH))
  422.         {
  423.                 if (IVehicleSeat* pSeat = pVehicle->GetSeatForPassenger((EntityId)passengerId.n))
  424.                 {
  425.                         return pH->EndFunction(pSeat->GetSeatId());
  426.                 }
  427.         }
  428.  
  429.         return pH->EndFunction();
  430. }
  431.  
downloadScriptBind_Vehicle.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top