Player Controlled Unit Problem

Started by Earlh21, October 19, 2013, 04:33:45 PM

Previous topic - Next topic

Earlh21

I'm trying to make a player unit. I want it to be able to be moved by the player, take ammo, and use one ammo to emit 3 AC.
I have accomplished two of these things:
Moving (I stole the script for it)
Taking ammo (Though once it's connected to something, it stays connected no matter how far I move it, I'm pretty sure it's the movement script)
But it won't emit. Here's the code:
Self CONST_AMMO GetUnitAttribute trace ->AmmoCount
<-AmmoCount if
CurrentCoords -3 AddCreeper
<-AmmoCount 1 sub ->AmmoCount
endif
#-------------------------------------------
<-ready if
0 GetMouseButtonDown if
GetMouseCell 1 QueueMove
false TraceStack ->ready
endif
endif

<-ready not if
GetMouseCell ->my ->mx
CurrentCoords ->sy ->sx
<-mx <-sx eq <-my <-sy eq and if
0 GetMouseButtonDown if
true TraceStack ->ready
endif
endif
endif

Can anyone help me?

J

Trace takes the item off the stack, so add 'dup' before the trace command or remove it.

Earlh21

#2
Ah, I fixed it, thanks.
Finished code, if anyone wants it.
# AC Unit.crpl
# Created on: 10/14/2013 5:38:17 PM
# ------------------------------------------
Self CONST_AMMO GetUnitAttribute ->AmmoCount
<-AmmoCount if
CurrentCoords -3 AddCreeper
<-AmmoCount 1 sub ->AmmoCount
Self CONST_AMMO <-AmmoCount SetUnitAttribute
endif
#-------------------------------------------
<-ready if
0 GetMouseButtonDown if
GetMouseCell 1 QueueMove
false TraceStack ->ready
endif
endif

<-ready not if
GetMouseCell ->my ->mx
CurrentCoords ->sy ->sx
<-mx <-sx eq <-my <-sy eq and if
0 GetMouseButtonDown if
true TraceStack ->ready
endif
endif
endif

eduran

I am trying to make a player controlled unit that can be moved in the same way as the build-in units. Here is what I've got so far:

# MoveableUnit.crpl
# Created on: 10/14/2013 10:29:46 AM
# ------------------------------------------
$debug:0
$unitSize:1 #currently only squares of uneven numbers are supported, i.e. 1x1, 3x3 etc
#0 means 1x1, 1 means 3x3 and so forth
$speed:1.0

once
if(<-debug)
ShowTraceLog
ClearTraceLog
else
HideTraceLog
endif
0 ->selected
0 ->moving
SetImage(Self "main" "CustomEmitter")
SetImageScale(Self "main" 1 1)
endonce

if(<-selected)
#this block handles input while the unit is selected
if(or(GetKeyDown("Space") GetMousebuttonDown(1)))
#deselect
@Deselect
else
#rotate selection marker
GetImageRotation(Self "marker") ->currentRotation
SetImageRotation(Self "marker" add(<-currentRotation -0.1))
#check for move orders
if(GetMouseButtonDown(0))
@Deselect
if(GetQueuedMoveCount)
AbortMove
Destroy(<-TargetUID 0)
endif
GetMouseCell ->MY ->MX
CreateUnit("CRPLCore" <-MX <-MY) ->TargetUID
SetUnitAttribute(<-TargetUID CONST_CREATEPZ 0)
SetImage(<-TargetUID "main" "CustomSporeTower")
QueueMove(<-MX <-MY <-speed)
1 ->moving
endif
endif
else
#this block is executed while the unit is not selected
if(GetMouseButtonDown(0))
CurrentX ->UX  CurrentY ->UY
GetMouseCell ->MY ->MX
and(gte(<-MX sub(<-UX <-unitSize)) lte(<-MX add(<-UX <-unitSize))) ->OnTargetX
and(gte(<-MY sub(<-UY <-unitSize)) lte(<-MY add(<-UY <-unitSize))) ->OnTargetY
if(and(<-OnTargetX <-OnTargetY))
#someone clicked on the unit
1 ->selected
SetImage(Self "marker" "Custom0")
SetImageScale(Self "marker" 1.5 1.5)
CreateUnit("CRPLCore" <-MX <-MY) ->PreviewUID
AddScriptToUnit(<-PreviewUID "Preview.crpl")
endif
endif
endif

if(<-moving)
if(not(GetQueuedMoveCount))
Destroy(<-TargetUID 0)
0 ->moving
endif
endif

:Deselect
#removes the rotating marker and the movement preview
0 ->selected
SetImage(Self "marker" "NONE")
Destroy(<-PreviewUID 0)

:destroyed
Destroy(<-PreviewUID 0)
Destroy(<-TargetUID 0)


# Preview.crpl
# Created on: 10/15/2013 12:26:52 PM
# ------------------------------------------

once
SetImage(Self "main" "CustomSporeTower")
SetUnitAttribute(Self CONST_TAKEMAPSPACE 0)
SetUnitAttribute(Self CONST_CREATEPZ 0)
endonce

GetMouseCell ->MY ->MX
SetCurrentCoords(<-MX <-MY)


It requires a marker image in slot "Custom0". For lack of something better I am using mortarbarrel2.png from the custom image repository. MovableUnit.crpl has to be attached to the CRPL tower you want to move and Preview.crpl has to be present in the project but not attached to anything.

What the script does:
- allows to select the unit by clicking on it and to deselect it by right-clicking/pressing space bar
- when selected a rotating marker is displayed to indicate that the unit is actually selected
- allows to move the unit by left-clicking while it is selected
- displays a movement target marker (the green footprint that is shown when you move one of the build-in units); for lack of one of those it currently uses the image of a spore tower
- can be selected while moving to redirect the unit

What it doesn't do right (yet):
1) lots of placeholder images
2) no line between the unit and the target marker
3) no way to select and redirect the unit during pause
4) unit can be shift-clicked to select in a group with other units but won't be part of any formation
5) no box selection; would be reasonably easy to implement but since it doesn't play nice with other units I didn't bother
6) keys to deselect the unit have to be specified within the script and could be different from what the player is using

Any suggestions on how to fix these? I guess 3 and 4 are impossible within CRPL  :(