Need Help with finding unit type

Started by Nicant, October 30, 2015, 05:47:02 PM

Previous topic - Next topic

Nicant

I need help with a unit i am making for my sleeper map that i am working on. I need help with having a unit check and see if there is an ore deposit (Not a siphon crystal) under it. Or really what i am trying to make is a unit that you can build on ore mines and when you give it ammo, it will make fake ore packets, then the ore packets go to another crpl unit which that unit make AC every time a fake packet goes inside of it. All i Really need help with is just the part where the core finds what unit is under it. Any help will be greatly appreciated.  :)

Here's what i got so far.
# OreHarvester.crpl
# Created on: 10/29/2015 5:08:40 PM
# ------------------------------------------
once
Self CONST_CREATEPZ 0 SetUnitAttribute
Self CONST_MAXAMMO 15 SetUnitAttribute
Self CONST_CONNECTABLE 1 SetUnitAttribute
Self CONST_CANREQUESTAMMO 1 SetUnitAttribute
Self CONST_REQUESTPACKETS 1 SetUnitAttribute
endonce

once
Self CONST_SHOWHEALTHBAR 1 SetUnitAttribute
Self CONST_ISBUILDING 1 SetUnitAttribute
Self CONST_BUILDCOST 50 SetUnitAttribute
endonce
Self CONST_HEALTH GetUnitAttribute
50 do
once
Self CONST_ISBUILDING 0 SetUnitAttribute
Self CONST_REQUESTPACKETS 0 SetUnitAttribute
endonce
Self CONST_AMMO GetUnitAttribute
15 do

CurrentCoords GetUnitAt ->UnitUID
<-UnitUID GetUnitType

#Not sure what to do next to have it know if there is an ore-deposit under it.
#For now i just have it emit anti creeper.

CurrentCoords -15 SetCreeper


loop
loop
endif





Or could i just do this?

# OreHarvester.crpl
# Created on: 10/29/2015 5:08:40 PM
# ------------------------------------------
once
Self CONST_CREATEPZ 0 SetUnitAttribute
Self CONST_MAXAMMO 15 SetUnitAttribute
Self CONST_CONNECTABLE 1 SetUnitAttribute
Self CONST_CANREQUESTAMMO 1 SetUnitAttribute
Self CONST_REQUESTPACKETS 1 SetUnitAttribute
endonce

once
Self CONST_SHOWHEALTHBAR 1 SetUnitAttribute
Self CONST_ISBUILDING 1 SetUnitAttribute
Self CONST_BUILDCOST 50 SetUnitAttribute
endonce
Self CONST_HEALTH GetUnitAttribute
50 do
once
Self CONST_ISBUILDING 0 SetUnitAttribute
Self CONST_REQUESTPACKETS 0 SetUnitAttribute
endonce
Self CONST_AMMO GetUnitAttribute
15 do

CurrentCoords GetUnitAt ->UnitUID
<-UnitUID GetUnitType ->Unit
<-UnitUID <-Unit eq

#Not sure what to do next to have it know if there is an ore-deposit under it.
#For now i just have it emit anti creeper.

CurrentCoords -15 SetCreeper


loop
loop
endif


CW4 hype!!

warren

Apologies for not reading through your code thoroughly. Is this what you are looking for?


#function call: takes coordinates return true iif ore deposit directly beneath.
CurrentCoords @detectOre if
... #ellipsis==insert code here.
endif

#this is a function: Do not stick inside another funtion.
:detectOre
  FALSE ->temp
  0 TRUE GetAllunitsInRange 0 do
    #uses TRUE ->temp instead of return true in order to unwind stack properly.
    GetUnitType "OREDEPOSIT" eq if TRUE ->temp endif
  loop
  <-temp


Nicant

#2
Quote from: warren on November 01, 2015, 05:21:56 AM
Apologies for not reading through your code thoroughly. Is this what you are looking for?


#function call: takes coordinates return true iif ore deposit directly beneath.
CurrentCoords @detectOre if
... #ellipsis==insert code here.
endif

#this is a function: Do not stick inside another funtion.
:detectOre
  FALSE ->temp
  0 TRUE GetAllunitsInRange 0 do
    #uses TRUE ->temp instead of return true in order to unwind stack properly.
    GetUnitType "OREDEPOSIT" eq if TRUE ->temp endif
  loop
  <-temp


Yes this is what i wanted but for some reason it does not work. IDK if it is the code i have or what. It builds and gets ammo properly, but does not emit any AC. I think it is the "do" code that is checking the ammo messing up. Here's what i did, Might me being a noob at CRPL.  :P

# OreHarvester.crpl
# Created on: 10/29/2015 5:08:40 PM
# ------------------------------------------
once
Self CONST_CREATEPZ 0 SetUnitAttribute
Self CONST_MAXAMMO 15 SetUnitAttribute
Self CONST_CONNECTABLE 1 SetUnitAttribute
Self CONST_CANREQUESTAMMO 1 SetUnitAttribute
Self CONST_REQUESTPACKETS 1 SetUnitAttribute
endonce

once
Self CONST_SHOWHEALTHBAR 1 SetUnitAttribute
Self CONST_ISBUILDING 1 SetUnitAttribute
Self CONST_BUILDCOST 50 SetUnitAttribute
endonce
Self CONST_HEALTH GetUnitAttribute
50 do
once
Self CONST_ISBUILDING 0 SetUnitAttribute
Self CONST_REQUESTPACKETS 0 SetUnitAttribute
endonce
Self CONST_AMMO GetUnitAttribute
10 do


#function call: takes coordinates return true iif ore deposit directly beneath.
CurrentCoords @detectOre if
#ellipsis==insert code here.
CurrentCoords -15 SetCreeper
Self CONST_AMMO 0 SetUnitAttribute
endif
loop
loop
#this is a function: Do not stick inside another funtion.
:detectOre
  FALSE ->temp
  0 TRUE GetAllunitsInRange 0 do
    #uses TRUE ->temp instead of return true in order to unwind stack properly.
    GetUnitType "OREDEPOSIT" eq if TRUE ->temp endif
  loop
  <-temp
#Put this here to see if the code was going through the code above
CurrentCoords 15 SetCreeper














CW4 hype!!

warren

#3
Apologies for the late reply.

I do not know what went wrong, as I did not test that code. However, those do loops do look odd. The use of multiple once blocks is throwing me as well.

the first loop is going from 50 to HEALTH, which IIRC, is less than 1 while building. Do not understand what this loop is for. The second loop goes from 10 to ammo, which is really weird. I do not understand this either.

If I were to guess, you meant to use if statements where you used loops, and some of those once blocks should not exist at all.

edit:
Now that I think about it, structures stop building automatically. You can simply test if your structure is still building and you do not need to turn it off. You still need to test ammo, which should look roughly like:

self CONST_AMMO GetUnitAttribute self CONST_MAXAMMO GetUnitAttribute eq if

edit2:
You can also test code using trace logs:
ShowTraceLog #makes the log visible
"hello" trace #prints hello if the code reaches this command.


Nicant

#4
I did the trace and it said the IF at line 23 was taking from an empty stack. (Sorry for the super late reply :P )
Here is what i did with the code now. I am new to crpl and not really familiar with it. Also i used your code for the ammo.
# OreHrvester.crpl
# Created on: 10/29/2015 5:08:40 PM
# ------------------------------------------

ShowTraceLog
once
Self CONST_CREATEPZ 0 SetUnitAttribute
Self CONST_MAXAMMO 15 SetUnitAttribute
Self CONST_CONNECTABLE 1 SetUnitAttribute
Self CONST_CANREQUESTAMMO 1 SetUnitAttribute
Self CONST_REQUESTPACKETS 1 SetUnitAttribute
Self "main" "custom43" SetImage
Self CONST_SHOWHEALTHBAR 1 SetUnitAttribute
Self CONST_ISBUILDING 1 SetUnitAttribute
Self CONST_BUILDCOST 50 SetUnitAttribute
endonce

        Self CONST_AMMO GetUnitAttribute Self CONST_MAXAMMO GetUnitAttribute eq if

#function call: takes coordinates return true iif ore deposit directly beneath.
CurrentCoords @detectOre if trace #This line is 23
#ellipsis==insert code here.
@ProducePacket
Self CONST_AMMO 0 SetUnitAttribute trace
endif
loop
#this is a function: Do not stick inside another funtion.
:detectOre
  FALSE ->temp trace
  0 TRUE GetAllunitsInRange 0 do trace
    #uses TRUE ->temp instead of return true in order to unwind stack properly.
    GetUnitType "OREDEPOSIT" eq if TRUE ->temp endif trace

  <-temp
loop

:ProducePacket

self CONST_AMMO GetUnitAttribute ->AmmoAmt trace
<-AmmoAmt 1 sub ->AmmoNow trace
Self CONST_AMMO <-AmmoNow SetUnitAttribute trace


"CRPLCORE" CurrentX CurrentY CreateUnit ->Unit trace
<-Unit "OrePacket.crpl" AddScriptToUnit trace
<-Unit "main" "Custom9" SetImage trace

<-Unit CONST_ISDESTROYED 1 GetUnitAttribute trace
1 do trace
@ProducePacket
loop
CW4 hype!!

Nicant

WAIT NVM. XD Your code does work it was just me being an idiot.  :P  :P  :P  :P  :P LOL Working on the code now, I''ll post what i did once i am done. Don't look what i did up there i just couldn't think that day.
CW4 hype!!