Computer Science Canada

Flexible Arrays / Timers

Author:  tjmoore1993 [ Tue Jul 14, 2009 11:58 pm ]
Post subject:  Flexible Arrays / Timers

Hello, I haven't been on in a long time. I have gotten' ideas from my old game and wanted to improve on it. I plan on though remaking it, which by all means would really make a difference.

What I plan on doing is not ADDING DELAY! Delay stops the game, although it is a quick fix, so what I am doing is adding a timer system so I can have things working in the background.

So far, I've sketched a rough draft of how my timers will work.

Turing:
View.Set ("offscreenonly")

var Arrow : int := Pic.FileNew ("BULLET.bmp")
Pic.SetTransparentColour (Arrow, 3)

type OBJECT_BULLET :
    record
        X : int
        Y : int
    end record

var BULLET : flexible array 1 .. 0 of OBJECT_BULLET

type OBJECT_CHARACTER :
    record
        X : int
        Y : int
        TIME : int
        TIMEUPDATE : int
    end record

var CHARACTER : OBJECT_CHARACTER % This will be changed  for multiplayer purposes in the future

var KEY : array char of boolean
var DELAY : array 1 .. 2 of int := init (700, 20)

CHARACTER.X := 10
CHARACTER.Y := 100
CHARACTER.TIME := 0
CHARACTER.TIMEUPDATE := 0
const Velocity := 1

procedure TIMER (TYPE : int)
    CHARACTER.TIME := Time.Elapsed
    if CHARACTER.TIME > DELAY (TYPE) + CHARACTER.TIMEUPDATE then
        CHARACTER.TIMEUPDATE := CHARACTER.TIME
        CHARACTER.TIME -= CHARACTER.TIMEUPDATE
    end if
end TIMER

loop
    loop
        cls
        Input.KeyDown (KEY)
        if KEY (KEY_UP_ARROW) and CHARACTER.TIME - CHARACTER.TIMEUPDATE <= 0 then
            CHARACTER.TIME := Time.Elapsed
            CHARACTER.Y += 1
            Draw.Dot (CHARACTER.X, CHARACTER.Y, black)
            View.Update
        end if
        if KEY (KEY_UP_ARROW) and CHARACTER.TIME - CHARACTER.TIMEUPDATE >= 0 then
            TIMER (2)
        end if
        if KEY (KEY_DOWN_ARROW) and CHARACTER.TIME - CHARACTER.TIMEUPDATE <= 0 then
            CHARACTER.TIME := Time.Elapsed
            CHARACTER.Y -= 1
            Draw.Dot (CHARACTER.X, CHARACTER.Y, black)
            View.Update
        end if
        if KEY (KEY_DOWN_ARROW) and CHARACTER.TIME - CHARACTER.TIMEUPDATE >= 0 then
            TIMER (2)
        end if
        if KEY (KEY_ENTER) and CHARACTER.TIME - CHARACTER.TIMEUPDATE <= 0 then
            CHARACTER.TIME := Time.Elapsed
            new BULLET, upper (BULLET) + 1
            BULLET (upper (BULLET)).X := CHARACTER.X
            BULLET (upper (BULLET)).Y := CHARACTER.Y
            exit
        end if
        if KEY (KEY_ENTER) and CHARACTER.TIME - CHARACTER.TIMEUPDATE >= 0 then
            TIMER (1)
        end if
    end loop
    loop
        CHARACTER.TIME := Time.Elapsed
        cls
        Pic.Draw (Arrow, BULLET (upper (BULLET)).X, BULLET (upper (BULLET)).Y, picMerge)
        View.Update
        if BULLET (upper (BULLET)).X >= maxx then
            new BULLET, (upper (BULLET)) - 1
            exit
        else
            BULLET (upper (BULLET)).X += Velocity
        end if
    end loop
end loop


In this rough draft, I have demonstrated how my Timers will work.

I have setup 2 records for my CHARACTER (TIME and TIMEUPDATE), these 2 are used to calculate whether or not you as the player have exceeded the
delay or you are still waiting. By having a timer started when you press a key, there will be an if statement to check if TIME is greater then TIMEUPDATE + 1000.

Lost? Well, when you update your time you will never return to 0 so here's how it works, every time you reached your delay, TIMEUPDATE will be assigned TIME's value so that the timer can be used when numbers get higher then 1000.

Feel free to suggest any ways possible to get this smaller then it already is.

Author:  andrew. [ Wed Jul 15, 2009 10:33 am ]
Post subject:  RE:Flexible Arrays / Timers

I didn't really read over your code thoroughly, but could you just do something like:
Turing:

loop
    if Time.Elapsed mod 10 = 0 then
        % Your code
    end if
end loop


That would be a 10 ms delay because your code would run every 10 ms.

Author:  DemonWasp [ Wed Jul 15, 2009 10:57 am ]
Post subject:  RE:Flexible Arrays / Timers

@andrew: Not necessarily, as that isn't guaranteed to be run every millisecond. Even if it was, Time.Elapsed has some error margins in its return.

@TJ: I ran that and didn't see anything (and yes, I did replace the image path with one that exists). What am I supposed to see?

In either case, the usual way of doing this kind of thing is to loop, determine how long it's been since you last dealt with input from a given player, and use that "interpolation" time to determine how much they've moved (or whatever else) since you last checked.

Author:  tjmoore1993 [ Wed Jul 15, 2009 11:34 am ]
Post subject:  Re: RE:Flexible Arrays / Timers

DemonWasp @ Wed Jul 15, 2009 10:57 am wrote:
@andrew: Not necessarily, as that isn't guaranteed to be run every millisecond. Even if it was, Time.Elapsed has some error margins in its return.

@TJ: I ran that and didn't see anything (and yes, I did replace the image path with one that exists). What am I supposed to see?

In either case, the usual way of doing this kind of thing is to loop, determine how long it's been since you last dealt with input from a given player, and use that "interpolation" time to determine how much they've moved (or whatever else) since you last checked.


The image was a bullet (arrow)... I am making another version of Fantastic Story, this one will include the job Pirate. The pirate is able to shoot rapid fire. So far, the AI for it is comming along, I am trying to figure out though how to make it so that monsters have their own characteristics efficiently.

Author:  DemonWasp [ Wed Jul 15, 2009 4:51 pm ]
Post subject:  RE:Flexible Arrays / Timers

For having multiple units that share a common set of characteristics (all basic orcs / kobolds / trolls have the same stats...), I would use a design like this:

Class 1 is MonsterDefinition, and it contains anything that you would find in a monster entry in a book. Does not contain its location or current status.

Class 2 is Monster, which contains a reference (not a copy!) of its MonsterDefinition, as well as its location, current status, and AI state.

Then, just keep a small library of MonsterDefinitions and you skip duplicating all that information.

Beyond that, I don't know what you mean by "efficiently".

Author:  andrew. [ Wed Jul 15, 2009 6:00 pm ]
Post subject:  Re: RE:Flexible Arrays / Timers

DemonWasp @ Wed Jul 15, 2009 10:57 am wrote:
@andrew: Not necessarily, as that isn't guaranteed to be run every millisecond. Even if it was, Time.Elapsed has some error margins in its return.
You could make it so it's in the range like this:
Turing:
if Time.Elapsed mod 10 > 8 or Time.Elapsed mod 10 < 2 then
   % blah blah blah
end if

That way it goes +/- 2.

But I agree, Time.Elapsed is inaccurate and it still may not work. It's just a simple and quick way of doing it. Also, if you are looking to be accurate and precise in your timing and stuff, why would you use Turing? Use a better language.

Author:  corriep [ Wed Jul 15, 2009 10:42 pm ]
Post subject:  Re: Flexible Arrays / Timers

I think what you are going after is Time.DelaySinceLast (10), it will give a 10 second delay from to last time it was called. ie if your calculations take 3 ms it will only delay 7 ms, if your calculations took 15 ms it will delay 0 ms.

I could be totally wrong though Confused

Author:  copthesaint [ Fri Jul 17, 2009 12:27 am ]
Post subject:  Re: Flexible Arrays / Timers

Lets make this very simple.
Turing:

var Timer : array 1 .. 2 of int
loop
    Timer (1) := Time.ElapsedCPU
    /*Your code*/
    loop
        Timer (2) := Time.ElapsedCPU
        exit when Timer (2) >= Timer (1) + 10
    end loop
end loop
[/syntax]

Author:  andrew. [ Fri Jul 17, 2009 3:30 pm ]
Post subject:  Re: Flexible Arrays / Timers

copthesaint @ Fri Jul 17, 2009 12:27 am wrote:
Lets make this very simple.
Turing:

var Timer : array 1 .. 2 of int
loop
    Timer (1) := Time.ElapsedCPU
    /*Your code*/
    loop
        Timer (2) := Time.ElapsedCPU
        exit when Timer (2) >= Timer (1) + 10
    end loop
end loop
That's prety much what I meant by my code although mine was simpler and not as good Smile


: