Grid Based transportation that calculates distance and Energy used.

Started by jamesreg, Tue 29/01/2019 00:47:53

Previous topic - Next topic

jamesreg



In the picture above you will see a grid system with numbers donating 001-049
The grid is a star map the squares and numbers are Star Systems or Sectors.

They player will start at grid 001 in the start of the game but it will be open ended and they can choose any grid in any order to travel to along
the way.

I will have an energy source that is consumed when the player is traveling between grids.
I want the energy source consumed based on how far the grid you are at vs the grid you are traveling to.
If you do not have enough energy to make it you will get a message telling you so.

I also want the distance traveled to also have a timer to it to the further your traveling the longer the timer before you reach your destination.

To make things even more complicated I would like to have different speeds in which you could travel (8) Speeds in total and based on how fast your traveling it will deduct energy at a greater amount and shorten the timer to get there. This last part if it is to complicated about speed I can sacrifice and leave out if I have to but it be great if i can figure it out.

Anyway one want to help me get started figuring out what I need to do to make this work the way I want.




Jack

I did something almost exactly like this for my game Hidden Plains.

First thing is to define the distance and position variables as floats, to get access to high detail, not just an integer value for which grid position you're on. This will allow you to set a very exact speed, like 0.1 squares moved every frame.

Then you manage your position and resources consumed in repeatedly_execute(), running dependent on a boolean like IsWalking.

jamesreg

I have never worked with floats just Int and bools so far so ill have to read up on them in the manual.
I will not need to see anything moving the player will click on that grid and then switch back to adventure game format
where there will have a icon of the energy resource and a timer counting down the distance then when arrived it set your
new location and ill do other actions.

You have me researching how floats work now im not the best programmer in the world but im forcing myself to learn what I must to do this.

I was thinking an int is what I need to determine distance from current position and so on but was confused how ags handled negative numbers like if I am at position 20 and want to go back to position five it would be -5 im horrible at when I have to do math stuff in script

Jack

If you're doing an instant transition, which it sounds like you're doing, it might be better using ints for positions. You would then use the Pythagorean formula to calculate the distance between the two grid squares, and this will take care of the negative numbers too.

It's called the Pythagorean theorem, but that makes it sound complex when it's one of the most basic formulas out there.

In AGS-ese it goes a little something like this, using the x and y coords of the two grid squares:

Code: ags

Maths.Sqrt(Maths.Exp(X2 - X1) + Maths.Exp(Y2 - Y1))

Khris

It looks like the first column is upside down?
And what path is used here? Does the player move horizontally and vertically only or in a straight line to the target?

jamesreg

Quote from: Khris on Tue 29/01/2019 08:44:14
It looks like the first column is upside down?
And what path is used here? Does the player move horizontally and vertically only or in a straight line to the target?

Yeah the grid was just concept art I numbered it wrong.

You will be able to go anywhere at anytime on the map as long as you have enough fuel.

The way it will work is you click on your navigation star map menu and this grid will pop up of star sectors
You will click on any one of them you want and if you have enough fuel it will take you there.
It will calculate where you are from where you are going and figure out the amount of time and fuel it will take to get there.
No room change or anything like that will happen. It will merely deduct fuel, have a timer, change label on other gui about current location and set like a star system flag so other events will happen. Main thing i'm trying to figure out now is the deducting fuel and timer for various distances and ideally I would like it to work with 8 different travel speeds.

I figured I would need at least 4 Int for this
1) Current_Position
2) Destination
3) Fuel
4) Distance

I am trying to figure out from there what to do  plus do i need a code to tell it the absolute value if its a negative

Khris

So going from 009 to 040 is four over, three down.
Again, does the ship move in a beeline towards sector 040? Does it cross both 016 and 017?

jamesreg

Quote from: Khris on Tue 29/01/2019 14:25:52
So going from 009 to 040 is four over, three down.
Again, does the ship move in a beeline towards sector 040? Does it cross both 016 and 017?

I see what you are saying now.

I guess I had not really thought of it ideally you would take the shortest path there.
so going from 9 diagonal you would go 17,25,33 then 40 for 4 spaces.
if it went without diagonal moves all other possible moves would be 7 spaces.
It does not really matter me which way it is as long as some realistic difference is noticeable on fuel and travel time is made.

I guess I need another INT for speed as well if I am able to factor speed into this?

Thank you sorry I make things complicated in my answers sometimes. Many times I thought of going to another engine for one reason or another
but AGS community is the most amazing community in the world and so helpful. Even if one or two features is more advanced or some extra
bells and whistles in anything else the community here vs other places makes AGS the only real home to be at.

Jack

You really want to use an X, Y coordinate system for this. It's another variable for location but it will make things a lot simpler to work out.

Khris

Quote from: jamesreg on Tue 29/01/2019 14:53:17going from 9 diagonal you would go 17,25,33 then 40 for 4 spaces
That path seems kind of arbitrary, it could just as well be going one over first, then diagonally to the target.
I as player would expect the ship to take a predictable path, especially if you have sector events.

In general, 009 is @ (1, 1) and 040 is at (5, 4). The path vector is therefore (4, 3) with a length of exactly 5.0, which can be used to calculate fuel consumption. I'd probably use floats (which are just numbers with decimals, no need for the manual here really) to store the position in the first place.

The center of sector 009 is @ 1.5, 1.5
The center of sector 004 is @ 5.5, 4.5

Pick the bigger difference (here: deltaX, which is 4.0) and calculate intermediate positions. For instance incrementing X by 0.1 means incrementing Y by 0.075 each step. So the ship will enter sector 016 at 2.0, 1.875. And so on.

Fuel consumption and passed time per step can be easily calculated based on speed and a step's distance, here 0.125

In the example, after 40 steps, the ship has reached 5.5, 4.5.

jamesreg

Thanks

All of the suggestions has been a big help it was giving me a bit of a headache to figure but this will get me started pretty good.

SMF spam blocked by CleanTalk