|
 |
| Author |
Message |
|
phong1892
|
Posted: 19 Feb, 2012
|
|
Joined: 31 Mar, 2011 Posts: 128 Location: Hanoi, Vietnam
|
Hi, I want to created a new function like another OnCreate, OnDamage,... function called OnMissTarget(a function not a thread). A missile for example, when it pass an aircraft but doesn't collide with aircraft because of its high index in MaxSpeed and low index in TurnRate. I want OnMissTarget do some stuffs in this event. I've created a Projectile Class in /mymod/lua/mymod_projectiles.lua: Code: local SingleBeamProjectile = import('/lua/sim/defaultprojectiles.lua').SingleBeamProjectile
ATAMissile = Class(SingleBeamProjectile) { --# Note: Missile speed is 30dis/sec --# Calculate distance from projectile to its target GetDistanceToTarget = function(self) local mpos = self:GetPosition() local tpos = self:GetCurrentTargetPosition() local dist = VDist3(mpos, tpos) return math.floor(dist) end OnMissTarget = function(self) --# Initiate ApproachTimes = 0 --# Calculate DistanceOnCreate = distance from proj to target when projectile is created --# While not self:BeenDestroyed() and DistanceOnCreate > 0 and ApproachTimes ==0 do --# Update DistanceToTarget every 0.1 second --# While DistanceToTarget < 3 do ApproachTimes = 1 and break this while...do loop --# WaitSeconds(0.3) --# While ApproachTimes == 1 and DistanceToTarget > 3 do --# Do some stuffs here and then break the while...do loop --# break while...do loop end, } Is script and algorithm true?
|
|
| Top |
|
 |
|
phong1892
|
Posted: 19 Feb, 2012
|
|
Joined: 31 Mar, 2011 Posts: 128 Location: Hanoi, Vietnam
|
Here is my script with some change in algorithm: Code: local SingleBeamProjectile = import('/lua/sim/defaultprojectiles.lua').SingleBeamProjectile
ATAMissile = Class(SingleBeamProjectile) { --# Note: Missile speed is 30dis/sec and TurnRate is 120deg/sec GetDistanceToTarget = function(self) local mpos = self:GetPosition() local tpos = self:GetCurrentTargetPosition() local dist = VDist3(mpos, tpos) return math.floor(DistanceOnCreate) end OnMissTarget = function(self) local ApproachTimes = 0 local DistanceOnCreate = self:GetDistanceToTarget() while not self:BeenDestroyed() and DistanceOnCreate > 0 and ApproachTimes ==0 do mpos = self:GetPosition() tpos = self:GetCurrentTargetPosition() WaitSeconds(0.1) DistanceToTarget = math.floor(VDist3(mpos, tpos)) if DistanceToTarget < 3 then ApproachTimes = 1 WaitSeconds(0.3) if DistanceToTarget > 3 then self:SetTurnRate(10) WaitSeconds(0.3) self:Destroy() end end end end, } Haven't tested yet, could anyone give me an advice.Thank you
|
|
| Top |
|
 |
|
BulletMagnet
|
Posted: 19 Feb, 2012
|
|
Joined: 05 Oct, 2007 Posts: 16424 Location: camping near the biggest power-up
|
|
Since it's a function (and not a thread) you can't use WaitSeconds.
Also, trying to understand your algorithm... why do you want to destroy the projectile?
_________________
Nephylim wrote: But, an FA army in an FA environment just looks... right. Does anyone know how to use air transports? I cant get them to pick up troops.
|
|
| Top |
|
 |
|
phong1892
|
Posted: 20 Feb, 2012
|
|
Joined: 31 Mar, 2011 Posts: 128 Location: Hanoi, Vietnam
|
BulletMagnet wrote: Since it's a function (and not a thread) you can't use WaitSeconds.
Also, trying to understand your algorithm... why do you want to destroy the projectile? Ah hell, WaitSeconds can't be used in function. It's essential that I'd forgot. I've just wanted to do something like SetTurnRate or SetMaxSpeed,.etc... Destroy it is the easiest way to recognize that it missed its own target.
|
|
| Top |
|
 |
|
Domino
|
Posted: 20 Feb, 2012
|
|
Joined: 26 Feb, 2009 Posts: 2996
|
|
Hi,
i dont think this is viable, there is just to many variables involved to accuratly predict if the unit missed its target..
you could do a distance check upto a point and then start checking if the distance gets greater, but this is not really a good solution.
_________________ Domino. ______________
|
|
| Top |
|
 |
|
phong1892
|
Posted: 20 Feb, 2012
|
|
Joined: 31 Mar, 2011 Posts: 128 Location: Hanoi, Vietnam
|
After few minutes re-checking my script, I have successfully done my work. Here is my script: Code: OnCreate = function(self) SingleBeamProjectile.OnCreate(self) self:SetCollisionShape('Sphere', 0, 0, 0, 1) self:ForkThread(self.OnMissTarget) end,
GetDistanceToTarget = function(self) local mpos, tpos, dist = 0 mpos = self:GetPosition() tpos = self:GetCurrentTargetPosition() dist = math.floor(VDist3(mpos, tpos)) return dist end, OnMissTarget = function(self) local dist, ApproachTimes = 0 while not self:BeenDestroyed() and ApproachTimes ==0 do dist = self:GetDistanceToTarget() WaitSeconds(0.2) if dist < 4 then ApproachTimes = 1 LOG('Approached Target At Distance ', dist) WaitSeconds(0.3) --# Need to make sure that projectile can gain distance far enough. dist = self:GetDistanceToTarget() if dist > 4 then self:SetTurnRate(5) WaitSeconds(0.2) self:Destroy() end end end end, Any ideas?
|
|
| Top |
|
 |
 |
 |
|