Dark Beam Directions

Started by Edward, January 06, 2016, 04:32:23 PM

Previous topic - Next topic

Edward

Hello.
Coming about with a new problem. I used and edited some Dark Beam code from Dimava. It works fine, until I rotate the gun. The beam always shoots to the right. I see that the beam created has its own code and possibly its own start direction, but I can't seem to see where that could be. I'm wondering how would I go about transfering the gun's orientation to the beam?

Gun.crpl

# Gun.crpl
# Dark Beam Canon by Dimava
# Few edits by Edward
# Created on: 7/28/2014 1:07:13 AM
# ------------------------------------------

$RayLifetime:30
$RayConsume:10

once
   SetUnitAttribute(Self CONST_MAXAMMO 100)   SetUnitAttribute(Self CONST_MAXHEALTH 10)
   SetUnitAttribute(Self CONST_AMMO 0)
   SetUnitAttribute(Self CONST_SHOWAMMOBAR 1)
   SetUnitAttribute(Self CONST_CANREQUESTAMMO 1)
   SetUnitAttribute(Self CONST_REQUESTPACKETS 1)
   SetUnitAttribute(Self CONST_CONNECTABLE 1)
   SetUnitAttribute(Self CONST_CELLHEIGHT 5)
   SetUnitAttribute(Self CONST_CELLWIDTH 5)
   SetUnitAttribute(Self CONST_PACKETREQUESTDELAY 1)
   SetUnitAttribute(Self CONST_COUNTSFORVICTORY 0)
   SetUnitAttribute(Self CONST_NULLIFIERDAMAGES 0)
   SetUnitSelectableOverride(1)
   
   SetImage(Self "main" "Custom0")
   SetImageScale(Self "main" 2.25 dup )
   SetImagePositionZ(Self "main" -0.5 )
   1 ->on
   0 ->step
endonce
CurrentCoords ->y ->x

<-step 1 add ->step
1 ->h

   if (<-step <-RayLifetime gte)
      if (GetUnitAttribute(Self CONST_AMMO) <-RayConsume gte)
GetTerrain(<-x <-y) ->h
         CreateUnit("CRPLCORE" CurrentCoords) ->unit
         AddScriptToUnit(<-unit "Ray.crpl")
         SetScriptVar(<-unit "Ray.crpl" "Lifetime" <-RayLifetime)
         SetScriptVar(<-unit "Ray.crpl" "BaseHeight" <-h)
         SetUnitAttribute(Self CONST_AMMO GetUnitAttribute(Self CONST_AMMO) <-RayConsume sub)
         0 ->step
         if (<-on not)
            1 ->on
            SetImage(Self "main" "Custom1")
            PlaySound("Misc20")
         endif
      endif
   endif
if (<-step 35 gt)
         if (<-on)
            0 ->on                       
            SetImage(Self "main" "Custom0")
            PlaySound("Misc4")
         endif
   
endif


GetUnitAttribute(Self CONST_AMMO) ->amo
   if (<-amo <-RayConsume lte)
      SetUnitAttribute(Self CONST_HEALTH <-amo 10 mul <-RayConsume div 1 add)
      SetUnitAttribute(Self CONST_SHOWHEALTHBAR 1)
   else
      SetUnitAttribute(Self CONST_SHOWHEALTHBAR 0)
   endif


Ray.crpl

# Ray.crpl
# Dark Beam Ray by Dimava
# Few edits by Edward
# Created on: 7/28/2014 1:32:35 PM
# ------------------------------------------
$mx:1
$my:0
$pi:3.1416
$Lifetime:30
$BaseHeight:1
$damage:1

once
0 ->step
SetUnitAttribute(Self CONST_CREATEPZ 0)
SetUnitAttribute(Self CONST_NULLIFIERDAMAGES 0)
SetUnitAttribute(Self CONST_COUNTSFORVICTORY 0)
SetUnitAttribute(Self CONST_MAXHEALTH 0.0001)
SetUnitAttribute(Self CONST_DESTROYMODE 0)
   
SetImage(Self "main" "CustomEmitter")
SetImageScale(Self "main" 0.3 dup)
#   ShowTraceLog
endonce
CurrentCoords ->y ->x
1 ->mx 0 ->my
1 ->gogogo


if (<-step 1 eq)
@drawray
else
@ray

#   
endif

if (<-step <-Lifetime gt)
Destroy(Self 0)
endif

<-step 1 add ->step
:ray


while
<-gogogo
repeat



SetCreeper(<-x <-y 0)
SetCreeper(<-x <-my add <-y <-mx add 0)
SetCreeper(<-x <-my sub <-y <-mx sub 0)
   
     
GetTerrain(<-x <-y) ->h
if (<-BaseHeight <-h lt)
0 ->gogogo
endif

GetUnitAt(<-x <-y) ->unit
GetImageRotation(<-unit "main") 1000 mul asint ->rot
# if (<-unit Self neq)
if (<-rot 5497 eq)
<-mx <-my ->mx ->my
# SetImageRotation(Self "main" GetImageRotation(Self "main") 1.5708 add )
else
if (<-rot 785 eq)
<-mx <-my neg ->mx neg ->my
# # SetImageRotation(Self "main" GetImageRotation(Self "main") 1.5708 add )
# else
# Damage(<-unit 0.1)
# 0 ->gogogo
endif
endif
# endif
if (GetVoid(<-x <-y))
# 0 ->gogogo
endif
#step
<-x <-mx add ->x
<-y <-my add ->y


endwhile



:drawray
1 ->num
0 ->r
1 ->h
while
<-gogogo
repeat
<-num 1 add ->num
<-x <-mx add ->x
<-y <-my add ->y
SetCreeper(<-x <-y 0)
SetCreeper(<-x <-my add <-y <-mx add 0)
SetCreeper(<-x <-my sub <-y <-mx sub 0)

GetTerrain(<-x <-y) ->h
if (<-BaseHeight <-h lt)
0 ->gogogo
# SetTerrain(<-x <-y <-h 1 sub)
endif

GetUnitAt(<-x <-y) ->unit
GetImageRotation(<-unit "main") 1000 mul asint ->rot
if (GetUnitAttribute(<-unit CONST_ISLANDED))
if (<-rot 5497 eq)
<-mx <-my ->mx ->my
<-r not ->r
0 ->d
else
if (<-rot 785 eq)
<-mx <-my neg ->mx neg ->my
<-r not ->r
0 ->d
else
@Test_Unit_Type
endif
endif
endif
GetUnitAt(<-x <-my add <-y <-mx add) ->unit
if (GetUnitAttribute(<-unit CONST_ISLANDED))
@Test_Unit_Type
endif
GetUnitAt(<-x <-my sub <-y <-mx sub) ->unit
if (GetUnitAttribute(<-unit CONST_ISLANDED))
@Test_Unit_Type
endif
   
if (GetVoid(<-x <-y))
# 0 ->gogogo
else
if (<-d)
SetImage(Self <-num "Custom3")
SetImagePosition(Self <-num <-x CurrentX sub 8 mul CurrentY <-y sub 8 mul -0.1 )
if (<-r)
SetImageRotation(Self <-num <-pi 2 div )
endif
else
1 ->d
endif
endif
   
#step
endwhile

:Test_Unit_Type
if(GetUnitType(<-unit) eq("COMMANDNODE"))
Damage(<-unit <-damage 2 mul)
endif
if(GetUnitType(<-unit) eq("COLLECTOR"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("RELAY"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("REACTOR"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("OREMINE"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("SIPHON"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("TERP"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("GUPPY"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("GUPPYAIR"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("SHIELD"))
Damage(<-unit <-damage)
endif

if(GetUnitType(<-unit) eq("PULSECANNON"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("MORTAR"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("STRAFER"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("STRAFERAIR"))
Damage(<-unit <-damage 10 mul)
endif
if(GetUnitType(<-unit) eq("BOMBER"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("BOMBERAIR"))
Damage(<-unit <-damage 10 mul)
endif
if(GetUnitType(<-unit) eq("SPRAYER"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("NULLIFIER"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("BEAM"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("SNIPER"))
Damage(<-unit <-damage)
endif

if(GetUnitType(<-unit) eq("FORGE"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("BERTHA"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("THOR"))
Damage(<-unit <-damage)
endif

if(GetUnitType(<-unit) eq("AOO"))
Damage(<-unit <-damage)
endif

if(GetUnitType(<-unit) eq("TECHARTIFACT"))
Damage(<-unit <-damage 10 mul)
endif
if(GetUnitType(<-unit) eq("MESSAGEARTIFACT"))
Damage(<-unit <-damage 10 mul)
endif

if(GetUnitType(<-unit) eq("CRPLCORE"))
if (GetUnitAttribute(<-unit CONST_DESTROYMODE) neq(0))
Damage(<-unit <-damage)
endif
endif
if(GetUnitType(<-unit) eq("EMITTER"))
Damage(<-unit <-damage 10 mul)
endif
if(GetUnitType(<-unit) eq("SPORETOWER"))
Damage(<-unit <-damage 10 mul)
endif
if(GetUnitType(<-unit) eq("RUNNERNEST"))
Damage(<-unit <-damage 10 mul)
endif
if(GetUnitType(<-unit) eq("RUNNER"))
Damage(<-unit <-damage 10 mul)
endif
if(GetUnitType(<-unit) eq("AETOWER"))
Damage(<-unit <-damage)
endif
if(GetUnitType(<-unit) eq("INHIBITOR"))
Damage(<-unit <-damage)
endif


As you can see, my edits of the beam/ray was to stop at higher terrain rather than dig through it, and have a little more control over what to damage.

GameGibu

And this is why I don't think I should go into other people's scripts. I could get hopelessly lost and have no idea whether another person's script is fully bug-free, especially if the author does not provide documentation for complex functions.

I have so far written all my scripts from the ground up. Each person can have a slightly different formatting style and skill level.

As for your rotation problem, please describe how the beam is supposed to act so that I can better understand your problem and come up with the most direct solution.

-GameGibu
Bringing you Custom maps since 2971!
☆CRPL Master☆

Edward

Ok. By default, the gun is pointing to the right, and the beam travels to the right. Now, if I should turn the gun 90° so that it is pointing up, the beam still travels to the right. How shall I tell the beam to travel in the same direction as the gun is pointing?

Grayzzur

#3
It looks like the ray script has step variables to tell it which way to go... mx and my. So, you should just need to change it from the default (mx=1, my=0) to step in whichever direction you want. x=1 to the right, x=-1 to the left, y=1 for up, y=-1 for down, and zero the other variable.

I'd do it right after this line:
SetScriptVar(<-unit "Ray.crpl" "BaseHeight" <-h)
Add:
SetScriptVar(<-unit "Ray.crpl" "mx" <-mx)
SetScriptVar(<-unit "Ray.crpl" "my" <-my)

This is without diving in too deep or actually testing that script out. Give it a shot, though.


(Also looks like the original author made a pi variable, even though CRPL has a built-in PI constant. Oh well.)
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

GameGibu

#4
I was thinking more like

sin(<-GetImageRotation(<-CannonUID "main")) ->my
cos(<-GetImageRotation(<-CannonUID "main")) ->mx


Less if-else-endifs and nested ifs this way.

-GameGibu
Bringing you Custom maps since 2971!
☆CRPL Master☆

Edward

Ok. I added the SetScriptVars, and added the sin/cos parts into the Gun. I also removed the 1 ->mx 0 ->my from the beginning of the Ray. It still shot towards the right despite the gun pointing up. I took away the sin/cos part and added the values manually (using the $vars). +/-X worked for right and left, but when I put in +Y ($my:1), the beam shot down. When I put -Y ($my:-1), the beam shot to the right. Also noticed the images weren't turned when shooting down.

GameGibu

That most likely means that the mx and my are not being used the way you intended. I would suggest rewriting the code from the ground up, however you are using a script that is supposed to be compatible with some unknowns here.

In that case, try using ShowTraceLog and Trace(unique text) that puts out something different each step, because at this point the code may be significantly unintentionally messed up to the extent that parts that you think are being executed are not actually being executed.

I hope that advice helps. Good luck!
-GameGibu
Bringing you Custom maps since 2971!
☆CRPL Master☆

Karsten75

There are scripts in the Credits mission that rotates the little man image and the direction he's firing in.

Edward

Well, I would like build something like this from the ground up, but I'm having a tough time reading the stacked mathematical equations. Can't quite see (for example in SetCreeper) where the X coords finish and the Y coords start. I know I did manage a creeper bomb on my own, but I didn't need any special mathematical steps.

I tried loading the credits world, but it seemed to be protected because I just got a "basic world" (has happened before when looking for survival pods).

The mirrors work though, so maybe I could fake it in some way... But would be nice to have it working properly, if not for myself at least for others.

Builder17

Maybe you didn't changed Credits mission's save file's name ,
when you put it into world editor project?

Edward

Forgot that detail.

...

Still don't got it. :p

Builder17

#11
What you have done then?
Edit:You have scripts. Did you moved these from Credits editor folder into your project?

Edward

I got the scripts, just can't grasp them.