Forward Object Moving

Started by wolkiewicz, July 28, 2014, 08:55:09 AM

Previous topic - Next topic

wolkiewicz

Hi
Now i have a question about Crpl moving options. Can I set crpl core to moving forward (where the image is rotated)?

ParkourPenguin

#1
EDIT: I realized that there was a significantly easier way of doing everything. If you're interested the original code, however, that's below in the spoiler.

To do this, you'd first need the angle that the image is rotated at- easy enough with GetImageRoation.

Next, you have to work on the speed of its movement, which gets a little more complicated. I'll just tell you that the speed in the x-direction is equal to the actual speed times the cosine of the angle found in GetImageRotation and the speed in the y-direction is the actual speed times the sine of that angle.

Finally, all you have to do is add those values to their previous respective values.

Here's some code below if you didn't follow what I wrote above:

$imageRotation:0.0
$speed:1.0
once
<-imageRotation pi mul 180 div ->imageRotation
self "main" <-imageRotation SetImageRotation
endonce

self CONST_PIXELCOORDX dup2 GetUnitAttribute add(<-speed mul(cos(<-imageRotation))) SetUnitAttribute
self CONST_PIXELCOORDY dup2 GetUnitAttribute add(<-speed mul(sin(<-imageRotation))) SetUnitAttribute


The speed is in pixels per frame and imageRotation is in degrees, by the way. I convert the rotation into radians in the once...endonce block.

Spoiler

$imageRotation:0.0
$speed:1.0
once
<-imageRotation pi mul 180 div ->imageRotation
self "main" <-imageRotation SetImageRotation
endonce

self "main" dup2 GetImagePositionX add(<-speed mul(cos(<-imageRotation))) SetImagePositionX
self "main" dup2 GetImagePositionY add(<-speed mul(sin(<-imageRotation))) SetImagePositionY

CurrentPixelCoords self "main" GetImagePositionY add swap self "main" GetImagePositionX add swap PixelToCell ->fy ->fx
if(CurrentX <-fx neq)
self "main" 4 CurrentX <-fx sub mul SetImagePositionX
<-fx SetCurrentX
endif
if(CurrentY <-fy neq)
self "main" 4 <-fy CurrentY sub mul SetImagePositionY
<-fy SetCurrentY
endif


Everything below the two lines of code where it sets the image position makes the CRPL core follow the closest grid cell the image is on. It's kind of weird how it works, which is why I didn't try to explain what's going on. If you don't want the CRPL core to follow it, then just comment out or remove everything from CurrentPixelCoords to the last endif.

If you are using the above code, it can get kind of glitchy if the CRPL core tries to go outside of the map boundaries, so try to not do that.
[close]
"Only a life lived for others is a life worthwhile."
-Albert Einstein

wolkiewicz

That is really complex. I tried to understand that script but i can't :/ I decide to use your script but... It don't work. Maybe i am not inteligent, I learning scripting. That is following script:
# Bomber.crpl
# Created on: 7/9/2014 10:38:06 AM by wolkiewicz
# ------------------------------------------

$range:8
$payload:20
$speed:1.5
$delay:30
$acceleration:0
$maxRotationSpeed:0.1
$imageRotation:0.0
#-------------------------------------------

once
SetUnitAttribute(self CONST_CREATEPZ FALSE)
SetUnitAttribute(self CONST_TAKEMAPSPACE FALSE)
SetUnitAttribute(Self CONST_SUPPORTSDIGITALIS FALSE)
SetUnitAttribute(Self CONST_NULLIFIERDAMAGES FALSE)
SetUnitAttribute(Self CONST_COUNTSFORVICTORY FALSE)
SetUnitAttribute(Self CONST_BEAMTARGET TRUE)
SetUnitAttribute(Self CONST_MAXHEALTH 3)
SetUnitAttribute(Self CONST_HEALTH 3)
Self "main" -0.02 SetImagePositionZ
   0 ->targetX
   0 ->targetY
endonce

@GetClosestUnit ->closestUnit


<-closestUnit neq0 if
<-closestUnit CONST_COORDX GetUnitAttribute ->targetX
<-closestUnit CONST_COORDY GetUnitAttribute ->targetY
else
-1 ->targetX -1 ->targetY
endif

once
QueueMove(<-targetX <-targetY <-speed)
endonce

@TargetRotation
@MoveForward

CurrentCoords <-range GetUnitCountInRange neq0 if
GetTimer0 eq0 if
PlaySound("Weapons4")
@CreateBomb
<-delay SetTimer0
endif
endif




:CreateBomb
"CRPLCore" CurrentX CurrentY CreateUnit ->unit
<-unit "Bomb.crpl" AddScriptToUnit
<-unit "Bomb.crpl" "targetX" <-targetX SetScriptVar
<-unit "Bomb.crpl" "targetY" <-targetY SetScriptVar
<-unit "Bomb.crpl" "payload" <-payload SetScriptVar
<-unit "main" "Custom2" SetImage
<-unit "main" 0 120 255 255 SetImageColor
<-unit "main" -0.01 SetImagePositionZ
#End


:GetClosestUnit
99999999 ->closestDistance
0 ->closestUnit
GetUnitsInRange(CurrentCoords 99999999)  ->unitCount
do (<-unitCount 0)
->unit
CurrentCoords <-unit CONST_COORDX GetUnitAttribute <-unit CONST_COORDY GetUnitAttribute Distance ->d
if (<-d lt(<-closestDistance))
GetUnitType(<-unit) ->ut
if ( not(<-ut eq ("POWERZONE") or (<-ut eq ("TECHARTIFACT")) or (<-ut eq("MESSAGEARTIFACT")) or (<-ut eq("SHIELDKEY"))) )
<-d ->closestDistance
<-unit ->closestUnit
endif
endif
loop
<-closestUnit
#End


:TargetRotation

<-targetX CurrentX sub ->deltaX
CurrentY <-targetY sub ->deltaY
<-deltaY <-deltaX atan2 ->desiredAngle

self "main" GetImageRotation <-desiredAngle ShortestAngle ->rot

<-rot <-maxRotationSpeed gt if
<-maxRotationSpeed ->rot
else
<-rot <-maxRotationSpeed -1 mul lt if
<-maxRotationSpeed -1 mul ->rot
endif
endif

<-rot abs 0.0001 lt if
1 ->onTarget
else
0 ->onTarget
endif

self "main" GetImageRotation <-rot add ->rot

self "main" <-rot SetImageRotation

<-onTarget
#End


:MoveForward

once
<-imageRotation pi mul 180 div ->imageRotation
self "main" <-imageRotation SetImageRotation
endonce


self "main" dup2 GetImagePositionX add(<-speed mul(cos(<-imageRotation))) SetImagePositionX
self "main" dup2 GetImagePositionY add(<-speed mul(sin(<-imageRotation))) SetImagePositionY

CurrentPixelCoords self "main" GetImagePositionY add swap self "main" GetImagePositionX add swap PixelToCell ->fy ->fx
if(CurrentX <-fx neq)
self "main" 4 CurrentX <-fx sub mul SetImagePositionX
<-fx SetCurrentX
endif
if(CurrentY <-fy neq)
self "main" 4 <-fy CurrentY sub mul SetImagePositionY
<-fy SetCurrentY
endif
#End

Some parts of script i get from chimera map.

ParkourPenguin

I modified the original version of my script a bit since I realized that there was a significantly easier way of doing everything. I also did a few other modifications to it: I removed the queue move command in the once...endonce since I felt like that was a bit unnecessary in relation to what the rest of the script was trying to do, I improved the :TargetRotation function a bit, and I removed some unused input variables that you still had lying around. If you don't want these changes to take effect, then just replace everything from :MoveForward down, and it should be fixed.
# Bomber.crpl
# Created on: 7/9/2014 10:38:06 AM by wolkiewicz
# ------------------------------------------

$range:8
$payload:20
$speed:1.5
$delay:30
$maxRotationSpeed:0.1
#-------------------------------------------

once
   SetUnitAttribute(self CONST_CREATEPZ FALSE)
   SetUnitAttribute(self CONST_TAKEMAPSPACE FALSE)
   SetUnitAttribute(Self CONST_SUPPORTSDIGITALIS FALSE)
   SetUnitAttribute(Self CONST_NULLIFIERDAMAGES FALSE)
   SetUnitAttribute(Self CONST_COUNTSFORVICTORY FALSE)
   SetUnitAttribute(Self CONST_BEAMTARGET TRUE)
   SetUnitAttribute(Self CONST_MAXHEALTH 3)
   SetUnitAttribute(Self CONST_HEALTH 3)
   Self "main" -0.02 SetImagePositionZ
    0 ->targetX
    0 ->targetY
endonce

@GetClosestUnit ->closestUnit


<-closestUnit neq0 if
<-closestUnit CONST_COORDX GetUnitAttribute ->targetX
<-closestUnit CONST_COORDY GetUnitAttribute ->targetY
else
-1 ->targetX -1 ->targetY
endif

@TargetRotation
@MoveForward

CurrentCoords <-range GetUnitCountInRange neq0 if
GetTimer0 eq0 if
PlaySound("Weapons4")
@CreateBomb
<-delay SetTimer0
endif
endif




:CreateBomb
"CRPLCore" CurrentX CurrentY CreateUnit ->unit
<-unit "Bomb.crpl" AddScriptToUnit
<-unit "Bomb.crpl" "targetX" <-targetX SetScriptVar
<-unit "Bomb.crpl" "targetY" <-targetY SetScriptVar
<-unit "Bomb.crpl" "payload" <-payload SetScriptVar
<-unit "main" "Custom2" SetImage
<-unit "main" 0 120 255 255 SetImageColor
<-unit "main" -0.01 SetImagePositionZ
#End


:GetClosestUnit
99999999 ->closestDistance
0 ->closestUnit
GetUnitsInRange(CurrentCoords 99999999)  ->unitCount
do (<-unitCount 0)
->unit
CurrentCoords <-unit CONST_COORDX GetUnitAttribute <-unit CONST_COORDY GetUnitAttribute Distance ->d
if (<-d lt(<-closestDistance))
GetUnitType(<-unit) ->ut
if ( not(<-ut eq ("POWERZONE") or (<-ut eq ("TECHARTIFACT")) or (<-ut eq("MESSAGEARTIFACT")) or (<-ut eq("SHIELDKEY"))) )
<-d ->closestDistance
<-unit ->closestUnit
endif
endif
loop
<-closestUnit
#End


:TargetRotation

<-targetX <-targetY CellToPixel ->tpY ->tpX
CurrentPixelCoords <-tpY sub neg swap <-tpX sub neg atan2 ->desiredAngle

self "main" GetImageRotation <-desiredAngle ShortestAngle ->rot

<-rot <-maxRotationSpeed gt if
   <-maxRotationSpeed ->rot
else
   <-rot <-maxRotationSpeed neg lt if
      <-maxRotationSpeed neg ->rot
   endif
endif

<-rot abs 0.001 lt if
1 ->onTarget
self "main" <-desiredAngle SetImageRotation
else
0 ->onTarget
self "main" GetImageRotation <-rot add ->rot
self "main" <-rot SetImageRotation
endif
#End


:MoveForward
self "main" GetImageRotation ->imageRotation
self CONST_PIXELCOORDX dup2 GetUnitAttribute add(<-speed mul(cos(<-imageRotation))) SetUnitAttribute
self CONST_PIXELCOORDY dup2 GetUnitAttribute add(<-speed mul(sin(<-imageRotation))) SetUnitAttribute
#End


By the way: you might want to look into eduran's flyer scripts. It may be a bit too advanced for what you're trying to do here, but if you're interested, click here to go to the topic.
"Only a life lived for others is a life worthwhile."
-Albert Einstein