Question about possible code

Started by D0m0nik, March 21, 2016, 08:33:15 AM

Previous topic - Next topic

D0m0nik

Hi All

Just wondering if it is possible to write a code that would create a loose state if your energy went into the green? I would like to make a basic map like Johhny did as a kind of energy management tutorial but if your energy drops into the red you loose. Is this possible?

Thanks
Dom

Vanguard

#1
Hi Domonik.

Your question should better be placed into the "coder´s corner", which is a seperate forum dedicated to questions about CRPL. Maybe Karsten will move the thread?

Looking at the CRPL I do not see a function that CAN look at current energy of a player. BUT I already have a solution for your problem.

Here it goes:


# UNTESTED CODE, please look up in the CRPL reference the needed functions and correct accordingly , this is a kind of proof pseudocode
# The idea is to spawn an energy crystal at coordinates and as soon as it geets "eaten" by the player, you can do stuff.

once
# Coordinates to use, adjust accordingly to your map
3 3 ->xres ->yres
# Create a resourcepack, make it store energy and give it a value of ONE!
<-xres <-yres "RESOURCEPACK" Createunit ->UnitIDrespack
SetScriptVar(<-UnitIDrespack 0 "resourceType" "ENERGY")
SetScriptVar(<-UnitIDrespack 0 "amt" 1)
#Create a siphon on top of it, so the player can actually harvest the crystal.
<-xres <-yres "SIPHON" Createunit ->nullid
endonce

# Get a unit at coordinates and check the return value. if it is -1 it means there is no unit at these coordinates
# IF getunit cannot detect resourcepacks ( because they might not be units ) then you have to work around these issues by getting the energy amount left. Easy enough, just test it and adjust.
<-xres <-yres getunitat -1 eq if
# no unit was found at these coordinates, getunitat returns -1 and presto, game over.
Failmission
endif

# repeat each second.
30 delay


The player could simply deactivate the network attached to the Energycore, so just plant the Command node right besides it and use unitoverride to make it non-movable. Enjoy :)

CRPL can be incredibly fun and surprisingly powerful. Let us know, in the coder´s corner if possible, if you need any more help. There is a ton of talented scripters out here willing to help.

Edit: Thinking about it, it might be possible to just get the Command node and query it´s energy, if the command node behaves like any other unit, that should actually be easier than my workaround.

Looking at the unit properties page https://knucklecracker.com/wiki/doku.php?id=crpl:docs:getunitattribute though I can´t find a specific  value. CONST_AMMO might be working for the command node. This idea needs more testing.

D0m0nik

Would be good of Karsten could move this thread or should I just can repost?

If this is possible I will need a little help, ie code that I can just attach to a core, I have no knowledge of CRPL!

Vanguard

In theory, if the above code I posted worked, you could just attach that. However, if you are looking for working with CRPL, I highly recommend learning the basics of CRPL, so you can fix simple mistakes yourself. It´s really not hard and Coder´s corner can help you just fine.

D0m0nik


Vanguard

#5
I jsut tested that code and it is ( as expected ) garbage. Sometimes I just fail at the reverse notation thing.

Modified it and this one works ( still roughshod though ):


# detect energy left and loose if depleted.crpl
# Created on: 3/23/2016 12:24:45 AM
# ------------------------------------------

once
# Coordinates to use, adjust accordingly to your map
3 3 ->yres ->xres
# Create a resourcepack, make it store energy and give it a value of ONE!
CreateUnit("RESOURCEPACK" <-xres <-yres) ->uid
SetScriptVar(<-uid 0 "resourceType" "ENERGY") # Can be ORE, ENERGY or AETHER
SetScriptVar(<-uid 0 "amt" 1)
#Create a siphon on top of it, so the player can actually harvest the crystal.
CreateUnit("SIPHON" <-xres <-yres) ->uid
#SetUnitAttribute(<-uid "CONST_BUILDCOST" 1) # dont use this, itll destroy the siphon.
ShowTraceLog
endonce

<-xres <-yres getunitat ->unitfound

trace (<-unitfound)
<-unitfound -1 eq if
trace ("Unit destroyed, this means your energy ran out. You loose. Execute -failmission- here.")
# You will probably want to create and show a dialog first, in which you explain why the player lost.
#failmission
endif

30 delay


Note also: If you place units with "createunit", you will still have to pay the buildcost. If you want the siphon preplaced on the crystal, simply put them on the map via editor.

Top prevent the player from moving the command node, use this one: https://knucklecracker.com/wiki/doku.php?id=crpl:docs:setunitselectableoverride

GoodMorning

Could you use the attribute CONST_ISBUILDING to insta-build it?

Also, energy packs can have fractional energy stored.

Finally, is there some reason that you couldn't, say have a unit (Call it A) that manages this, by picking up CNs and building the Spihons under them? Then the whole arrangement can be shifted to follow it, either by a QueueMove command, or use of CONST_COORDX and CONST_COORDY.

This would need to track all CNs independently, and self-destruct when a CN returns to orbit.

You could also have a unit that drains energy at the same rate that it produces it, allowing you to provide energy packs. (Packs seem to be consumed in order of proximity to CNs, so the win/lose pack would be first to go.) The production might need to be provided by a hidden energy pack.

Any of these solutions will cause a cloud of green smoke as the pack is depleted.

Out of interest, has anyone made a battery unit? Pulls in power, makes an energy pack?
A narrative is a lightly-marked path to another reality.

Builder17

Energy packs are almost useless when your energy balance is good. Ore packs and aether packs are lot more useful.

GoodMorning

True. But what about if the energy balance is expected to fluctuate wildly?

This could be achieved by units stealing power, being long-period but high-drain when charging, high-strength Spore attacks, or other things. (Since packs are Creeper-proof, what about a world which gives you five minutes to set up, and then floods all but a random area with Creeper? A small area is protected, but you can draw upon the reserves you built in order to help/hinder your assault?)
A narrative is a lightly-marked path to another reality.

D0m0nik

If any of you guys think this idea is worth taking up feel free, the coding is best avoided for me at the mo!

Vanguard

Quote from: GoodMorning on March 24, 2016, 08:03:05 PM
Out of interest, has anyone made a battery unit? Pulls in power, makes an energy pack?
Well, it´s simple to code.

I made an unit which draws in energy and creates ore deposits in one of the two maps I have in the userspace. Could be easily modified to produce energycrystals.

There is also a map from another coder, which produces energy into a crystal based on the rate of creeper flowing through a hydroelectric damn. it´s a pretty good map, check it out. It was something with the Hoover damn if I remember correctly?

D0m0nik

Yeah I played that map, it was one of Tylers. Interesting idea and I also love real world maps, tend to have the best terrain.