Beam powered unit Script

Started by SuperDan, May 13, 2014, 10:37:35 AM

Previous topic - Next topic

SuperDan

This is the CRPL script for a tower that runs a function(:whileBeaming) when beams fire at it and :whenBeamed when beams have fired at it for long enough
[code# beamPower.crpl V1.3
# Created on: 5/13/2014 2:25:06 PM


# Author:SuperDan (Please give credit)
once
#A few variables
$ExtraBeamsHelp:1
$FramesPerAction:30
$overloads:0
$overloadAmt:70
0 ->currentFrame
1 ->amtBeams
<-overloadAmt 0.142857 mul ->health
#A few constants
Self CONST_BEAMTARGET 1 SetUnitAttribute
Self CONST_MAXHEALTH <-health SetUnitAttribute
Self CONST_HEALRATE 0 SetUnitAttribute
Self CONST_HEALTH <-health SetUnitAttribute
<-overloads not if
Self CONST_DESTROYONDAMAGE 0 SetUnitAttribute
endif
<-overloads if
Self CONST_DESTROYONDAMAGE 1 SetUnitAttribute
endif
#clearTraceLog
#showTraceLog
endonce

#The following happens every frame
<-ExtraBeamsHelp if
#This happens if extra beams are supposed to help
#First, it calculates how many beams have fired at it(1 beam/frame = approx 0.0142857
sub(<-health Self CONST_HEALTH GetUnitAttribute) 0.0142857 div 1 round ->amtBeams
#[(1-health)/0.0142857]
<-amtBeams if
#This activates if amtBeams is non-zero i.e. beams are firing at the CRPL core
add(<-currentFrame <-amtBeams) ->currentFrame

#the script now does anything it should while beaming
<-amtBeams @whileBeaming

#resets health
Self CONST_HEALTH <-health SetUnitAttribute
endif
endif

<-ExtraBeamsHelp not if
#this happens if the amount of beams doesn't matter
Self CONST_HEALTH GetUnitAttribute 1 lt if
#If the health is less than the maximum, it assumes it was damaged by beams
<-currentFrame 1 add ->currentFrame

#as the amount of beams doesn't matter, it sets amtBeams to 1
1 ->amtBeams
#the script now does anything it is supposed to do "while beaming" with 1 beam
1 @whileBeaming

#The script now resets the health
Self CONST_HEALTH <-health SetUnitAttribute
endif
endif

<-currentFrame <-FramesPerAction gte if
#This script happens when the frames required are passed
0 ->currentFrame
<-amtBeams @whenBeamed
endif
# The CRPL core is now damaged between frames so the "if"s can activate

#--------------------------

:whenBeamed
#this function happens when the FramesPerBeam frames are complete
#default is create an(indestructible) anti-creeper spore which goes to random coords, but you can change this
->numBeams1

CurrentCoords RandCoords 100 -10 CreateSpore

:whileBeaming
#this function happens whenever beams fire at the tower
#default is create some AC proportional to the amount of beams firing at the tower
->numBeams2

CurrentCoords -1 <-numBeams2 mul SetCreeperNoLower

Changelog:
Spoiler

V1.3: Added $overloads and $overloadAmt to set a possible overload to destroy the core. Use :destroyed to make it do something
V1.2: Added :whileBeaming and made core indestructible(i.e. infinite beams wouldn't destroy it)
[close]

The variable $ExtraBeamsHelp can be set to 0 or 1- if it is 0 lots of beams are like a single beam, if it is 1, extra beams help.
The variable $FramesPerAction dictates how long the tower needs to charge up before it runs :whenBeamed
The variable $overloads can be set to 0 or 1- if it is 0 it can take infinitely many beams, if it is 1 it can overload and be destroyed.
The variable $overloadAmt can be set to the amount of beams required to overload the system.

This is my first finished script, so please leave some feedback.

I update the script in this post regularly, so don't be surprised if it changes.  :)

The script is heavily commented, but if you don't follow somewhere, feel free to ask. :)

There's the script and a screenshot of the effects attached.
For the screenshot, I should clarify that I have set my AC colour to yellow.

Flabort

Yay! someone else who uses yellow AC!

I like the function of the script. I think I will indeed steal it (with credit) for something else later.
My maps: Top scores: Sugarplum, Cryz Dal, Cryz Torri, Cryz Bohz (Click fetch scores, page courtesy of kwinse)

SuperDan

Quote from: Flabort on May 13, 2014, 11:07:34 PM
Yay! someone else who uses yellow AC!

I like the function of the script. I think I will indeed steal it (with credit) for something else later.
;D I have already made a map with three of those scripts and uploaded it to CS.
I do have a few ideas for improvements.

knucracker

One interesting edge case is that you can blow up the towers with the script attached.  So you might want to call SetUnitAttribute(Self CONST_DESTROYONDAMAGE false) or something along those lines.  Right now if you get too many Beams in range (like 70 of them) they can damage the tower more than a total of 1, which will cause the tower to destruct before it can run the script and heal itself.

Of you could leave it and some something spectacular in a :Destroyed callback :)

SuperDan

#4
Quote from: virgilw on May 14, 2014, 01:49:26 PM
One interesting edge case is that you can blow up the towers with the script attached.  So you might want to call SetUnitAttribute(Self CONST_DESTROYONDAMAGE false) or something along those lines.  Right now if you get too many Beams in range (like 70 of them) they can damage the tower more than a total of 1, which will cause the tower to destruct before it can run the script and heal itself.

Of you could leave it and some something spectacular in a :Destroyed callback :)
I fixed it by doing exactly that(Self CONST_DESTROYONDAMAGE 0 SetUnitAttribute), but that would be an interesting mechanic.  :)
I think I'm going to code a $overloads now...

EDIT: Done! It was actually 70.00007 beams, but I think that's a rounding error. See the screenshot attached for lots of beams firing.