The island - An RTS game

New to FreeBASIC? Post your questions here.
Post Reply
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

The island - An RTS game

Post by ITomi »

Hello everybody!

I started making a small RTS game a few months ago, and it's ready about in 50%. In the game the player can leads a small group of men in an island (that's why the title of the game is: "The island" (or: "The settlers of the island"; not decided yet)), start out a harbour.
The computer can leads 1, 2 or 3 groups, depending on settings. My game looks like the old good Settlers 2, as the following picture shows:
Image
But some programming things are not too good yet, e.g. if a unit (worker and warrior) has subimages, I try set it depending on its direction as follows:

Code: Select all

function setsprite(...) as any ptr
if worker(number).direction>=338 and worker(number).direction<=22 then
                worker(number).sprite=workerdownright(worker(number).subimage)
elseif worker(number).direction>22 and worker(number).direction<=67 then
                worker(number).sprite=workerupright(worker(number).subimage)
elseif ...
return worker(number).sprite
end function
and then:

Code: Select all

unitsprite=setsprite(...)
put ground,(worker(i).xplace,worker(i).yplace),unitsprite,trans
But sometimes the program not shows the subimages, so the workers are became invisible. What's wrong with my technique and which is the easiest way to show subimages of a sprite?
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: The island - An RTS game

Post by fxm »

The first condition:
( worker(number).direction>=338 and worker(number).direction<=22 )
is never verified.
Perhaps replace the "and" by a "or"?

By using "Select Case As Const worker(number).direction" would induce a more compact syntax:
See at KeyPgSelectcase.
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: The island - An RTS game

Post by ITomi »

You are right, fxm!
I was inattentive with that condition, but I've replaced by an "or", as you adviced.

And, it seems that, finally I've found a better way to change subimages of a sprite:

Code: Select all

dim shared workergoes(7,3) as any ptr
workergoes(0,0)=workerright(0)
workergoes(0,1)=workerright(1)
workergoes(0,2)=workerright(2)
workergoes(0,3)=workerright(3)
workergoes(1,0)=workerrightup(0)
workergoes(1,1)=workerrightup(1)
workergoes(1,2)=workerrightup(2)
workergoes(1,3)=workerrightup(3)
(... The subimages in counter-clockwise direction ...)

Code: Select all

workergoes(7,0)=workerrightdown(0)
workergoes(7,1)=workerrightdown(1)
workergoes(7,2)=workerrightdown(2)
workergoes(7,3)=workerrightdown(3)
and later:

Code: Select all

if worker(number).subimgtimer>=5 then
     worker(number).subimgtimer=0
     if worker(number).subimage>=3 then
                worker(number).subimage=0
     else
                worker(number).subimage+=1
     end if
else
    worker(number).subimgtimer+=1
end if

dim itssprite as ubyte
itssprite=int(worker(number).direction*8/360)
if itssprite>7 then
   itssprite=0
end if
worker(number).sprite=workergoes(itssprite,worker(number).subimage)
But I have more a question: can I scaling a sprite in FreeBasic? For example, I want a smaller mountain than its original size, when it containes less rocks because of mining.
I have to draw its smaller subimages or there are possibility to scaling?
Last edited by ITomi on Mar 31, 2016 8:55, edited 1 time in total.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: The island - An RTS game

Post by Tourist Trap »

ITomi wrote: I have to draw its smaller subimages or there are possibility to scaling?
Hello Itomi.
You can easily scale your sprites. Look at second program in first post of this topic: http://www.freebasic.net/forum/viewtopi ... 15&t=24370.

For fast tool I think you will have to search for MultiputV2 from DJ Peters in the forum.
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: The island - An RTS game

Post by ITomi »

Tourist Trap wrote:
ITomi wrote: I have to draw its smaller subimages or there are possibility to scaling?
Hello Itomi.
You can easily scale your sprites. Look at second program in first post of this topic: http://www.freebasic.net/forum/viewtopi ... 15&t=24370.
Huhhh... it seems to be complicated a bit, at first sight...
Tourist Trap wrote: For fast tool I think you will have to search for MultiputV2 from DJ Peters in the forum.
OK and thanks. That is a drawing tool, like Paint?
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: The island - An RTS game

Post by Tourist Trap »

ITomi wrote: Huhhh... it seems to be complicated a bit, at first sight...
You'll need only this part for sprite reduction:

Code: Select all

sub ReduceXY(byval Img as fb.IMAGE ptr, _
             byval XScaleFactor as double=0.5, _
             byval YScaleFactor as double=0.5)
    for x as long = 0 to (imgW - 1)
        for y as long = 0 to (imgH - 1)
            pSet (500 + x*XScaleFactor, 120 + y*YScaleFactor), point(x,y,Img)
        next y       
    next x
end sub
MultiputV2 is simply a function (very big) that replace put with scaling options, and is very fast. But I've never tried it.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: The island - An RTS game

Post by leopardpm »

and not only does it do scaling, but MultiPut also rotates... pretty nifty function
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: The island - An RTS game

Post by leopardpm »

ITomi,
I love RTS games! Could you post your source code so we could play with it? It looks like you are making good progress so far

I think I recognize the little worker in your screenshot as a Human Peon from Warcraft I or II - am I right? I ripped those sprites years ago... loved Warcraft RTS!
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: The island - An RTS game

Post by ITomi »

Hi leopardpm!

My RTS game is not ready completely yet, but as soon as possible, I will publish the program and its source code too, as in case of my previous and first FB game, on my web page.
Several features are still missing from it, e.g. the soldiers and battles, the trading, precise pathfinding for the units... but maybe the main things already done: the player and the computer can build roads and buildings, workers do his own tasks automatically (build, chop trees, quarry stone)...
I had to do these things by myself, because I don't know any RTS game written in FreeBasic.
In any case, if you also make RTS with FreeBasic, I will help you as best I can, although the source code are in Hungarian but I can explain it in English too.
And yes, the worker are from Warcraft II; great game!
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: The island - An RTS game

Post by leopardpm »

I completely suggest referring to Amit Patel's web site:
http://www.redblobgames.com/
for help with things, ESPECIALLY for writing the A* pathfinding routine... his tutorial is the best! Also has alot of different topics which relate to your type of game.

I am trying to do a different type of pathfinding since I will have 1000s of units and need it super speedy... here is discussion regarding Heatmap pathfinding: http://www.freebasic.net/forum/viewtopi ... 49#p217912
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: The island - An RTS game

Post by ITomi »

Oh, thanks, leopardpm; I didn't know these.
I try to make own pathfinding algorithms, but it's not easy. Till now, it is the best what I rewrote from Game Maker to FreeBasic:
https://www.youtube.com/watch?v=wHenegMSN_U
It manipulate with directions and collisions. Not perfect, but not too bad. :-)
Another thing, what I solved in FreeBasic with difficulty, the computing of distance between two square areas. It needs for me to collision detection.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: The island - An RTS game

Post by Tourist Trap »

MultiPut [destination],[xmidpos],[ymidpos], source,[xScale],[yScale],[Trans]
Link -----> http://www.freebasic.net/forum/viewtopi ... =7&t=24479
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: The island - An RTS game

Post by leopardpm »

ITomi wrote:I try to make own pathfinding algorithms, but it's not easy.
home rolled ones usually end up being a version of floodfill, which is very slow... I really suggest A*, it is pretty simple once you understand it.
Till now, it is the best what I rewrote from Game Maker to FreeBasic:

https://www.youtube.com/watch?v=wHenegMSN_U
It manipulate with directions and collisions. Not perfect, but not too bad. :-)
I will take a look! Always like demos!

Another thing, what I solved in FreeBasic with difficulty, the computing of distance between two square areas. It needs for me to collision detection.
do you calculate distance from the centers of the areas, or the closest edges?

search the forums for 'collision', there are some really good ones here
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: The island - An RTS game

Post by leopardpm »

it looks like your gamemaker demo is using collisions to determine 'walkability' - while this is a viable way of doing things, and sometimes even necessary depending on the type of map, etc... but using collisions for pathfinding is not good. In the case of a standard tile map game you already have a graph structure in place for pathfinding, and 'walkability' is as easy as adding a flag to each map cell to determine if it is walkable...
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: The island - An RTS game

Post by leopardpm »

ITomi wrote:Another thing, what I solved in FreeBasic with difficulty, the computing of distance between two square areas. It needs for me to collision detection.
I was just thinking about it, and, I am confused... why exactly do you need 'collision detection'?
Post Reply