Molay's troublesome questions

Started by Molay, May 30, 2013, 08:31:18 PM

Previous topic - Next topic

Molay

I noticed I open quite a few topics for my many questions, in an attempt to reduce the clutter of the forum, I'll post future questions here:

First question:

I'm having trouble to animate dynamically instantiated images, probably something related to the <-! operator, but going by the wiki, I don't see my mistake.

Here's the concerned piece of code:

:InitializeRig
do( <-rigAmount 0)
concat("rig" add(1 I)) dup ->!
SetImage(self concat("rig" add(1 I)) <-IMGrigCore)
SetImageColor(self concat("rig" add(1 I)) 125 60 60 0)
SetImageScale(self concat("rig" add(1 I)) 0.6 0.6)

concat("rigBar" add(1 I)) dup ->!
SetImage(self concat("rigBar" add(1 I)) <-IMGrigBar)
SetImageColor(self concat("rigBar" add(1 I)) 125 125 90 0)
SetImageScale(self concat("rigBar" add(1 I)) 0.9 0.9)

->!(concat("rigBarUp" add(1 I)) 1)
->!(concat("rigBarSize" add(1 I)) 1)
loop

:AnimateRig
do(add(<-rigDeployed 1) 1)
if(concat("rigBarUp" I) eq0)
sub(<-!(concat("rigBarSize" I)) 0.01) ->!(concat("rigBarSize" I))
if(<-!(concat("rigBarSize" I)) 0.65 lt)
->!(concat("rigBarUp" I) 1)
endif
else
add(<-!(concat("rigBarSize" I)) 0.01) ->!(concat("rigBarSize" I))
if(<-!(concat("rigBarSize" I)) 1.1 gt)
->!(concat("rigBarUp" I) 0)
endif
endif
SetImageScale(self concat("rigBar" I) <-!(concat("rigBarSize" I)) 1)
loop


Here's the whole thing if someone does fancy reading it all in a piece (not close to be finished ofc)

# OilDigger.crpl
# Created on: 5/30/2013 10:57:08 PM
# Created by: Molay
# ------------------------------------------

#Public variables
$rigAmount:3 #Using more than 7 could cause serious lag!! Edit DeployRig to avoid lag (up to 11 is fairly reliable, most of the time, though)
$rigDelay:30    #1200
$IMGmain:"custom5"
$IMGrigCore:"custom3"
$IMGrigBar:"custom4"
#Private variables
once
0 ->rigDeployed
0 ->canDeployRig
0 ->currentRigX
0 ->currentRigY
0 ->currentRigRotation
0 ->lagAvoid
HideTraceLog
endonce

#DeployRig

once
@InitializeMain
@InitializeRig
SetTimer0(30)
endonce

@AnimateRig

If(GetTimer0 eq0)
@deployRig
SetTimer0(64)
endif



:AnimateRig
do(add(<-rigDeployed 1) 1)
if(concat("rigBarUp" I) eq0)
sub(<-!(concat("rigBarSize" I)) 0.01) ->!(concat("rigBarSize" I))
if(<-!(concat("rigBarSize" I)) 0.65 lt)
->!(concat("rigBarUp" I) 1)
endif
else
add(<-!(concat("rigBarSize" I)) 0.01) ->!(concat("rigBarSize" I))
if(<-!(concat("rigBarSize" I)) 1.1 gt)
->!(concat("rigBarUp" I) 0)
endif
endif
SetImageScale(self concat("rigBar" I) <-!(concat("rigBarSize" I)) 1)
loop








:InitializeMain
SetImage(self "main" <-IMGmain)
SetImageColor(self "main" 230 190 40 255)
SetImageRotation(self "main" div(PI 4))

:InitializeRig
do( <-rigAmount 0)
concat("rig" add(1 I)) dup ->!
SetImage(self concat("rig" add(1 I)) <-IMGrigCore)
SetImageColor(self concat("rig" add(1 I)) 125 60 60 0)
SetImageScale(self concat("rig" add(1 I)) 0.6 0.6)

concat("rigBar" add(1 I)) dup ->!
SetImage(self concat("rigBar" add(1 I)) <-IMGrigBar)
SetImageColor(self concat("rigBar" add(1 I)) 125 125 90 0)
SetImageScale(self concat("rigBar" add(1 I)) 0.9 0.9)

->!(concat("rigBarUp" add(1 I)) 1)
->!(concat("rigBarSize" add(1 I)) 1)
loop

:DeployRig
0 ->currentRigX
0 ->lagAvoid
while <-currentRigX -15 gt and( <-currentRigX 15 lt) repeat
RandInt(-45 45) ->currentRigX
add(1 <-lagAvoid) ->lagAvoid
if(<-lagAvoid 300 eq)
break
endif
do(<-rigDeployed 0)
if( GetImagePositionX(self concat("rig" add(1 I))) sub(<-currentRigX 15) gte
and(GetImagePositionX(self concat("rig" add(1 I))) add(<-currentRigX 15) lte)
and(GetImagePositionY(self concat("rig" add(1 I))) sub(<-currentRigY 15) gte
and(GetImagePositionY(self concat("rig" add(1 I))) add(<-currentRigY 15) lte) ) )
0 ->currentRigX
break
endif
loop
endwhile
0 ->currentRigY
0 ->lagAvoid
while <-currentRigY -15 gt and( <-currentRigY 15 lt) repeat
RandInt(-45 45) ->currentRigY
add(1 <-lagAvoid) ->lagAvoid
if(<-lagAvoid 300 eq)
break
endif
do(<-rigDeployed 0)
if( GetImagePositionX(self concat("rig" add(1 I))) sub(<-currentRigX 15) gte
and(GetImagePositionX(self concat("rig" add(1 I))) add(<-currentRigX 15) lte)
and(GetImagePositionY(self concat("rig" add(1 I))) sub(<-currentRigY 15) gte
and(GetImagePositionY(self concat("rig" add(1 I))) add(<-currentRigY 15) lte) ) )
0 ->currentRigY
break
endif
loop
endwhile
mul(RandInt(1 30) RandFloat) ->currentRigRotation
add(<-rigDeployed 1) ->rigDeployed
SetImagePosition(Self concat("rig" <-rigDeployed) <-currentRigX <-currentRigY 0)
SetImagePosition(Self concat("rigBar" <-rigDeployed) <-currentRigX <-currentRigY 0.01)
SetImageColor(Self concat("rig" <-rigDeployed) 125 60 60 255)
SetImageColor(Self concat("rigBar" <-rigDeployed) 125 125 90 255)
SetImageRotation(self concat("rig" <-rigDeployed) <-currentRigRotation)
SetImageRotation(self concat("rigBar" <-rigDeployed) <-currentRigRotation)


Rescue me, please!

Molay

#1
I believe the wiki has a wrong notation for ->!
Should it not be:  n1 s1 -  ?
Anyways, this didn't solve my problem.

Edit: Definitely wrong notation on the wiki. Fixed.

Molay

Feeling terribly stupid.
After one night's sleep and a brief look, less than 2 minutes, at my code, I found the error  ::)
I missed the <-! in the if clause in :InitializeRig

Molay

New question:

I came to think of a list.
One of my cores will spawn multiple cores in it's lifetime, and i'd like to keep track of the UIDs in a list.
Is there a list function, or am I to create new variables dynamically for each new core, and count the amount to loop (like I did previously for creating images dynamically?)

Unless there is such a thing already, I would suggest following new commands, making big chunks of code more easy to handle and much easier to read and edit!

AddToList("listname" Value)   Notation: s1 s2 - .
GetList                                 Notation:   .      - [l1...l2]n1
GetListCount("listname")       Notation: s1      - n1


Thanks!

J

If you just want to know how many cores it has created and are still alive, you should extract the scripts out of my 'The pit' map, there I'll keep track of how many childs a core has and create a new one if one died. You can also use the reflection operator (how's that thing called): <-! and ->! If you need an example (map) for that; The Totem. You know how to extract scripts ;)

knucracker

Lists are something I might add to CRPL....  I've held off so far, though.
To add them means to support them as a fundamental data type.  That means the stack has to be able to hold a list along with the three types it currently holds (int, float, string).  All functions have to be able to permute between these types as well. 

Lists can of course contain any of the 4 data types themselves (ints, strings, floats, and lists).  That allows nested lists.  And of course there has to be a set of commands that can manipulate and iterate over lists.  All things that can be done....

As mentioned, you can currently keep track of a list of things using reflection.  Not ideal, but it works.

For functions that need to return a 'list' you can push all of the items to the stack, then push the number of items in the list.  That returns a 'list' of items via the stack and can be easily iterated over using a do loop.

Molay

@J
I might take some inspirations from your The Pit map for future coding. Was most useful to see how someone else does stuff! :)
Very interesting concept for a map, too^^ Thanks for the hint!

@Virgil
I didn't know how much work it would be to implement lists on your end. Sounds like quite a bit. In this case I wouldn't worry too much, maybe a post-release thing to simplify coding.
I've been using the <-! and ->! so far with success (after quite a bit of getting used to...), so it's working this way now. It's just a tad too fiddly for my taste. But the lists are more of a "comfort option" than anything really necessary at the moment I guess.

As to my scripts, they are done now and to my surprise, working^^
Expect a playable map soon™ :)



Molay

Note to self: Don't go overboard with animating little things. It will drop you to 10 fps and nobody will ever notice the animation anyways :/

I guess it's unlikely we get separate threads for each script?^^

Molay

Do the 256x256 maps tend to lag in general? Having turned off most of the animations for my crpl towers and even reduced the amount of them, the game still comes to a crawl. About 8 fps after about 40 minutes time :(

I guess there might be no map for my last scripts, will have to make something more lightweight, and probably a smaller map, too...

knucracker

There should be nothing intrinsic that makes a map run slower over time.  That said, more units means more work.  So if you build lots and lots of things the map will naturally slow down depending on the speed of the host system.  Without knowing any more details I can't really say more than that.  When I hear things like "8fps after 40 min" I immediately worry about a memory leak or something like that, though.  So I'd like to profile your map if you have a reproducible case where a map gets slower over time.

Molay

I do have plenty of units and massive terraforming going on at all times (15 terps or so?). Additionally, I have the scripts running. Not that many anymore though, since I destroyed some of my crpl-cores over time. I expected the game to speed up as I reduce the crpl-core count, but it only kept getting slower anyways. Also, music starts to randomly cut out when fps falls below 10fps.

I'll attach both files for you, the finalized map and the save about 42 minutes in. At the beginning I get about 29fps, now it's about 8fps.
So the map ran a tiny bit slower from the get go than I was used to, but I expected the scripts to be the reason. They probably are, anyways.

I'm not sure you have to worry about the base game. I have made a 200x200 map where I built at least as many units as on this one, and played for over 90 minutes, with fps never dropping below 28fps (which I consider very good considering the size of my base at the end). It had not CRPL-cores though.

Really didn't want you to worry while on vacation! It's probably just me coding something really inefficient! :)

Anyways, here the files: