Skip to content

Commit

Permalink
LuaSynced: add Spring.SetProjectileTarget(number projectileID, [numbe…
Browse files Browse the repository at this point in the history
…r objectID, string objectType] | [number x, number y, number z]) --> boolean
  • Loading branch information
rt committed Mar 3, 2013
1 parent e562a7b commit ca61181
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 82 deletions.
103 changes: 81 additions & 22 deletions rts/Lua/LuaSyncedCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "Sim/Weapons/Weapon.h"
#include "Sim/Weapons/WeaponDefHandler.h"
#include "System/myMath.h"
#include "System/ObjectDependenceTypes.h"
#include "System/Log/ILog.h"
#include "System/Sync/HsiehHash.h"
#include "LuaHelper.h"
Expand Down Expand Up @@ -2663,13 +2664,11 @@ int LuaSyncedCtrl::SetFeatureCollisionVolumeData(lua_State* L)
int LuaSyncedCtrl::SetProjectileMoveControl(lua_State* L)
{
CProjectile* proj = ParseProjectile(L, __FUNCTION__, 1);
if (proj == NULL) {
return 0;
}

if (!proj->weapon && !proj->piece) {
if (proj == NULL)
return 0;
if (!proj->weapon && !proj->piece)
return 0;
}

proj->luaMoveCtrl = (lua_isboolean(L, 2) && lua_toboolean(L, 2));
return 0;
Expand All @@ -2678,9 +2677,9 @@ int LuaSyncedCtrl::SetProjectileMoveControl(lua_State* L)
int LuaSyncedCtrl::SetProjectilePosition(lua_State* L)
{
CProjectile* proj = ParseProjectile(L, __FUNCTION__, 1);
if (proj == NULL) {

if (proj == NULL)
return 0;
}

proj->pos.x = luaL_optfloat(L, 2, 0.0f);
proj->pos.y = luaL_optfloat(L, 3, 0.0f);
Expand All @@ -2692,9 +2691,9 @@ int LuaSyncedCtrl::SetProjectilePosition(lua_State* L)
int LuaSyncedCtrl::SetProjectileVelocity(lua_State* L)
{
CProjectile* proj = ParseProjectile(L, __FUNCTION__, 1);
if (proj == NULL) {

if (proj == NULL)
return 0;
}

proj->speed.x = luaL_optfloat(L, 2, 0.0f);
proj->speed.y = luaL_optfloat(L, 3, 0.0f);
Expand All @@ -2709,26 +2708,86 @@ int LuaSyncedCtrl::SetProjectileVelocity(lua_State* L)
int LuaSyncedCtrl::SetProjectileCollision(lua_State* L)
{
CProjectile* proj = ParseProjectile(L, __FUNCTION__, 1);
if (proj == NULL) {

if (proj == NULL)
return 0;
}

proj->Collision();
return 0;
}

int LuaSyncedCtrl::SetProjectileTarget(lua_State* L)
{
return 0; // TODO
CProjectile* pro = ParseProjectile(L, __FUNCTION__, 1);
CWeaponProjectile* wpro = NULL;

if (pro == NULL)
return 0;
if (!pro->weapon)
return 0;

wpro = static_cast<CWeaponProjectile*>(pro);

switch (lua_gettop(L)) {
case 3: {
const int id = luaL_checkint(L, 2);
const char* type = luaL_checkstring(L, 3);

if ((type == NULL) || ((*type) == 0))
return 0;

CWorldObject* oldTargetObject = wpro->GetTargetObject();
CWorldObject* newTargetObject = NULL;

switch (type[0]) {
case 'u': { newTargetObject = ParseUnit(L, __FUNCTION__, 2); } break;
case 'f': { newTargetObject = ParseFeature(L, __FUNCTION__, 2); } break;
case 'p': { newTargetObject = ParseProjectile(L, __FUNCTION__, 2); } break;
}

const DependenceType oldDepType =
(dynamic_cast< CSolidObject*>(oldTargetObject) != NULL)? DEPENDENCE_WEAPONTARGET:
(dynamic_cast<CWeaponProjectile*>(oldTargetObject) != NULL)? DEPENDENCE_INTERCEPTTARGET:
DEPENDENCE_NONE;
const DependenceType newDepType =
(dynamic_cast< CSolidObject*>(newTargetObject) != NULL)? DEPENDENCE_WEAPONTARGET:
(dynamic_cast<CWeaponProjectile*>(newTargetObject) != NULL)? DEPENDENCE_INTERCEPTTARGET:
DEPENDENCE_NONE;

if (oldTargetObject != NULL) {
wpro->DeleteDeathDependence(oldTargetObject, oldDepType);
wpro->SetTargetObject(NULL);
}
if (newTargetObject != NULL) {
wpro->AddDeathDependence(newTargetObject, newDepType);
wpro->SetTargetObject(newTargetObject);
}

assert(newTargetObject == NULL || newTargetObject->id == id);
lua_pushboolean(L, oldTargetObject != NULL || newTargetObject != NULL);
return 1;
} break;

case 4: {
if (wpro->GetTargetObject() == NULL) {
wpro->SetTargetPos(float3(luaL_checkfloat(L, 2), luaL_checkfloat(L, 3), luaL_checkfloat(L, 4)));
}

lua_pushboolean(L, wpro->GetTargetObject() == NULL);
return 1;
} break;
}

return 0;
}


int LuaSyncedCtrl::SetProjectileGravity(lua_State* L)
{
CProjectile* proj = ParseProjectile(L, __FUNCTION__, 1);
if (proj == NULL) {

if (proj == NULL)
return 0;
}

proj->mygravity = luaL_optfloat(L, 2, 0.0f);
return 0;
Expand All @@ -2737,9 +2796,9 @@ int LuaSyncedCtrl::SetProjectileGravity(lua_State* L)
int LuaSyncedCtrl::SetProjectileSpinAngle(lua_State* L)
{
CProjectile* proj = ParseProjectile(L, __FUNCTION__, 1);
if (proj == NULL || !proj->piece) {

if (proj == NULL || !proj->piece)
return 0;
}

CPieceProjectile* pproj = static_cast<CPieceProjectile*>(proj);
pproj->spinAngle = luaL_optfloat(L, 2, 0.0f);
Expand All @@ -2749,9 +2808,9 @@ int LuaSyncedCtrl::SetProjectileSpinAngle(lua_State* L)
int LuaSyncedCtrl::SetProjectileSpinSpeed(lua_State* L)
{
CProjectile* proj = ParseProjectile(L, __FUNCTION__, 1);
if (proj == NULL || !proj->piece) {

if (proj == NULL || !proj->piece)
return 0;
}

CPieceProjectile* pproj = static_cast<CPieceProjectile*>(proj);
pproj->spinSpeed = luaL_optfloat(L, 2, 0.0f);
Expand All @@ -2761,9 +2820,9 @@ int LuaSyncedCtrl::SetProjectileSpinSpeed(lua_State* L)
int LuaSyncedCtrl::SetProjectileSpinVec(lua_State* L)
{
CProjectile* proj = ParseProjectile(L, __FUNCTION__, 1);
if (proj == NULL || !proj->piece) {

if (proj == NULL || !proj->piece)
return 0;
}

CPieceProjectile* pproj = static_cast<CPieceProjectile*>(proj);
pproj->spinVec.x = luaL_optfloat(L, 2, 0.0f);
Expand All @@ -2776,9 +2835,9 @@ int LuaSyncedCtrl::SetProjectileSpinVec(lua_State* L)
int LuaSyncedCtrl::SetProjectileCEG(lua_State* L)
{
CProjectile* proj = ParseProjectile(L, __FUNCTION__, 1);
if (proj == NULL) {

if (proj == NULL)
return 0;
}

assert(proj->weapon || proj->piece);

Expand Down
2 changes: 1 addition & 1 deletion rts/Lua/LuaSyncedRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4491,7 +4491,7 @@ int LuaSyncedRead::GetProjectileTarget(lua_State* L)
return 0;

const CWeaponProjectile* wpro = static_cast<const CWeaponProjectile*>(pro);
const CWorldObject* wtgt = wpro->target;
const CWorldObject* wtgt = wpro->GetTargetObject();

if (wtgt == NULL)
return 0;
Expand Down
8 changes: 4 additions & 4 deletions rts/Lua/LuaUnsyncedCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1400,15 +1400,15 @@ static bool AddLightTrackingTarget(lua_State* L, GL::Light* light, bool trackEna
if (unit != NULL) {
if (trackEnable) {
if (light->GetTrackPosition() == NULL) {
light->AddDeathDependence(unit, CObject::DEPENDENCE_LIGHT);
light->AddDeathDependence(unit, DEPENDENCE_LIGHT);
light->SetTrackPosition(&unit->drawPos);
light->SetTrackDirection(&unit->speed); //! non-normalized
ret = true;
}
} else {
// assume <light> was tracking <unit>
if (light->GetTrackPosition() == &unit->drawPos) {
light->DeleteDeathDependence(unit, CObject::DEPENDENCE_LIGHT);
light->DeleteDeathDependence(unit, DEPENDENCE_LIGHT);
light->SetTrackPosition(NULL);
light->SetTrackDirection(NULL);
ret = true;
Expand All @@ -1425,15 +1425,15 @@ static bool AddLightTrackingTarget(lua_State* L, GL::Light* light, bool trackEna
if (proj != NULL) {
if (trackEnable) {
if (light->GetTrackPosition() == NULL) {
light->AddDeathDependence(proj, CObject::DEPENDENCE_LIGHT);
light->AddDeathDependence(proj, DEPENDENCE_LIGHT);
light->SetTrackPosition(&proj->drawPos);
light->SetTrackDirection(&proj->dir);
ret = true;
}
} else {
// assume <light> was tracking <proj>
if (light->GetTrackPosition() == &proj->drawPos) {
light->DeleteDeathDependence(proj, CObject::DEPENDENCE_LIGHT);
light->DeleteDeathDependence(proj, DEPENDENCE_LIGHT);
light->SetTrackPosition(NULL);
light->SetTrackDirection(NULL);
ret = true;
Expand Down
12 changes: 6 additions & 6 deletions rts/Sim/Misc/InterceptHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ void CInterceptHandler::Update(bool forced) {

const float3& pFlightPos = p->pos;
const float3& pImpactPos = p->pos + p->dir * impactDist;
const float3& pTargetPos = p->targetPos;
const float3& pTargetPos = p->GetTargetPos();

if ((pTargetPos - wPos).SqLength2D() < Square(wDef->coverageRange)) {
w->AddDeathDependence(p, CObject::DEPENDENCE_INTERCEPT);
w->AddDeathDependence(p, DEPENDENCE_INTERCEPT);
w->incomingProjectiles[p->id] = p;
continue; // 1
}
Expand All @@ -90,7 +90,7 @@ void CInterceptHandler::Update(bool forced) {
}

if ((pFlightPos - wPos).SqLength2D() < Square(wDef->coverageRange)) {
w->AddDeathDependence(p, CObject::DEPENDENCE_INTERCEPT);
w->AddDeathDependence(p, DEPENDENCE_INTERCEPT);
w->incomingProjectiles[p->id] = p;
continue; // 2
}
Expand All @@ -103,7 +103,7 @@ void CInterceptHandler::Update(bool forced) {
// area during transition from vertical to horizontal flight, so we
// perform an extra test (NOTE: assumes non-parabolic trajectory)
if (pTargetDir.dot(pImpactDir) >= 0.999f) {
w->AddDeathDependence(p, CObject::DEPENDENCE_INTERCEPT);
w->AddDeathDependence(p, DEPENDENCE_INTERCEPT);
w->incomingProjectiles[p->id] = p;
continue; // 3
}
Expand All @@ -115,7 +115,7 @@ void CInterceptHandler::Update(bool forced) {
const float3 pMinSeparationVec = wPos - pMinSeparationPos;

if (pMinSeparationVec.SqLength() < Square(wDef->coverageRange)) {
w->AddDeathDependence(p, CObject::DEPENDENCE_INTERCEPT);
w->AddDeathDependence(p, DEPENDENCE_INTERCEPT);
w->incomingProjectiles[p->id] = p;
continue; // 4
}
Expand All @@ -133,7 +133,7 @@ void CInterceptHandler::AddInterceptTarget(CWeaponProjectile* target, const floa
// if the target projectile dies in any way, we need to remove it
// (we cannot rely on any interceptor telling us, because they may
// die before the interceptable itself does)
AddDeathDependence(target, CObject::DEPENDENCE_INTERCEPTABLE);
AddDeathDependence(target, DEPENDENCE_INTERCEPTABLE);

Update(true);
}
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Projectiles/FlareProjectile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void CFlareProjectile::Update()
for (std::list<CMissileProjectile*>::iterator mi = owner->incomingMissiles.begin(); mi != owner->incomingMissiles.end(); ++mi) {
if (gs->randFloat() < owner->unitDef->flareEfficiency) {
CMissileProjectile* missile = *mi;
missile->target = this;
missile->SetTargetObject(this);
missile->AddDeathDependence(this, DEPENDENCE_DECOYTARGET);
}
}
Expand Down
31 changes: 23 additions & 8 deletions rts/Sim/Projectiles/WeaponProjectiles/WeaponProjectile.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ struct ProjectileParams {
float3 error;

// unit, feature or weapon projectile to intercept
// TODO: add Spring.{Get,Set}ProjectileTarget
CWorldObject* target;
CUnit* owner;

Expand Down Expand Up @@ -71,6 +70,26 @@ class CWeaponProjectile : public CProjectile

virtual void DrawOnMinimap(CVertexArray& lines, CVertexArray& points);

void DependentDied(CObject* o);
void PostLoad();

void SetTargetObject(CWorldObject* newTarget) {
if (newTarget != NULL) {
targetPos = newTarget->pos;
}

target = newTarget;
}

const CWorldObject* GetTargetObject() const { return target; }
CWorldObject* GetTargetObject() { return target; }

void SetStartPos(const float3& newStartPos) { startpos = newStartPos; }
void SetTargetPos(const float3& newTargetPos) { targetPos = newTargetPos; }

const float3& GetStartPos() const { return startpos; }
const float3& GetTargetPos() const { return targetPos; }

protected:
void UpdateInterception();
virtual void UpdateGroundBounce();
Expand All @@ -81,24 +100,20 @@ class CWeaponProjectile : public CProjectile
bool targeted;
const WeaponDef* weaponDef;

CWorldObject* target;
float3 targetPos;

unsigned int weaponDefID;
unsigned int cegID;

int colorTeam;

protected:
CWorldObject* target;

float3 startpos;
float3 targetPos;

int ttl;
int bounces;
bool keepBouncing;

public:
void DependentDied(CObject* o);
void PostLoad();
};

#endif /* WEAPON_PROJECTILE_H */
5 changes: 3 additions & 2 deletions rts/Sim/Weapons/PlasmaRepulser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ void CPlasmaRepulser::NewProjectile(CWeaponProjectile* p)
}

float3 dir = p->speed;
if (p->targetPos != ZeroVector) {
dir = p->targetPos - p->pos; // assume that it will travel roughly in the direction of the targetpos if it have one
if (p->GetTargetPos() != ZeroVector) {
// assume projectile will travel roughly in the direction of its targetpos
dir = p->GetTargetPos() - p->pos;
}

dir.y = 0.0f;
Expand Down
Loading

0 comments on commit ca61181

Please sign in to comment.