Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Help with random?

Author
Message
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 2nd Apr 2012 23:31 Edited at: 3rd Apr 2012 00:06
I'm making a game where a guy is stranded in a desert, and walks around finding water. The terrain is randomly generated, and i need to radnomly generate 10 water bottles and have them randomly placed aroumd the map. How can I make them always be just above the land that they spawn in? Or even how to randomly spawn them?

Terrain Generation


[modedit - added closing code tag]
Sergey K
20
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 3rd Apr 2012 00:29
erm.. soo many questions in one thread

if you already have automatic map generation, then make the bottles appear only when the map is created. make an event or something.
detect the height of the X,Z positions of the map, then place your bottles over there.

more 3d models .x/.obj and more foramts here:
[href]https://www.turbosquid.com/Search/Index.cfm?keyword=gogetax1&x=0&y=0[href]
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 3rd Apr 2012 03:01 Edited at: 3rd Apr 2012 03:05
the_s4rg3:

The same way you are keeping your camera above the terrain. You use GET TERRAIN GROUND HEIGHT. Like this:



Also, you should seed the random number generator at the start of your code.

So many games to code.......so little time.

Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 3rd Apr 2012 13:06 Edited at: 3rd Apr 2012 13:07
Quote: "Also, you should seed the random number generator at the start of your code."


If you want it to repeat the same pattern each load up.

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 3rd Apr 2012 16:59
Hi Pincho,

Actually, what I meant was he will need to use something like
at the start of his code. From his post, I take it he wants it to be different every time and I didn't see where he seeded the random generator.

So many games to code.......so little time.

Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 3rd Apr 2012 17:26
You should indent your code to make it easier to read. I made a load of cool modifications to your code but my computer crashed and I lost the code.

WARNING: The above comment may contain sarcasm.
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 3rd Apr 2012 22:49 Edited at: 3rd Apr 2012 22:50
Quote: "Actually, what I meant was he will need to use something like..."


randomize timer()

You don't use that anymore. DBPro does that for you. Typing that in actually makes the program less random. It's OK though, I only found out a few months ago.

the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 3rd Apr 2012 23:39
LBFN, if I put it before the Terrain Generation, it says that I "have run into a FUNCTION mid-program!"

Any help? When at the end, it does nothing.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 4th Apr 2012 01:47
the_s4rg3:

Put the PlaceBottles function at the end of your program. Sometime before your main loop, call it by simply coding
. You can re-call it as needed in your program (i.e. the player picks up all of the bottles) by calling it again the same way.

Pincho:
Never heard that one. I have always used that in every DBP program I have ever written (that needed to be randomized) and things were always random. I plan to test that out when I get home.

So many games to code.......so little time.

the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 10th Apr 2012 21:51
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 10th Apr 2012 23:25
UPDATE:
I made this to go with the function, but I am having minimal success. Please help?

- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Apr 2012 00:28
Don't forget to put subroutines and functions after your end statement. If you haven't got one then add one, e.g.:



Perhaps something like that is your problem? We'd need to see your modified code before being more helpful.
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 11th Apr 2012 23:04
Green Gandalf, what does that do? Also, here is the updated code:
sync rate 100
PlaceBottles()
Rem Title Screen
cls 0
load bitmap "images/Stranded_TitleScreen.jpg"
ink rgb(255,0,0),0 : set text font "arial" : set text size 14 : set text to bold : set text transparent
center text 320,420,"Developed by Shane Sullivan"
center text 320,434,"Copyright (c) 2012 Shane Sullivan"
wait key
Rem Make/Load Objects
Rem Textures
load image "images/sand_dunes.jpg",10
Rem 4 Corners of the Map
make object sphere 6, 100,100,100
make object sphere 7, 100,100,100
make object sphere 8, 100,100,100
make object sphere 9, 100,100,100
Rem Hide/Show 4 Corners of the Map
hide object 6
hide object 7
hide object 8
hide object 9
Rem Map Boundaries
make object box 2, 10,1300,1300
make object collision box 2, 0,0,0,10,1300,1300,1
make object box 3, 10,1300,1300
make object collision box 3, 0,0,0,10,1300,1300,1
make object box 4, 10,1300,1300
make object collision box 4, 0,0,0,10,1300,1300,1
make object box 5, 10,1300,1300
make object collision box 5, 0,0,0,10,1300,1300,1
Rem Food
make object cube 1,50

for x = 11 to 20
make object sphere 1,50
next x
if object exist (x + 1) = 21

endif

Rem Color Objects
Rem 4 Corners of the Map
color object 6, rgb(255,0,0)
color object 7, rgb(0,255,0)
color object 8, rgb(0,0,255)
color object 9, rgb(122,122,0)
Rem Map boundaries
texture object 2, 10
texture object 3, 10
texture object 4, 10
texture object 5, 10
Rem Position/Rotate Objects
Rem 4 Corners of the Map
position object 6, 0,300,0
position object 7, 1300,300,0
position object 8, 0,300,1300
position object 9, 1300,300,1300
Rem Map Boundaries
position object 2, 650,300,0
yrotate object 2, 90
position object 3, 1300,300,650
position object 4, 0,300,650
position object 5, 650,300,1300
yrotate object 5, 90
Rem Car







Rem Terrain Generation
`Terrain scale
xscale#=5
yscale#=1
zscale#=5

`Height map size in pixels
is=255


smooth=10 `Sets the smoothness of the terrain (1=least smooth)
detail=100 `Sets the detail of the terrain (1=least detailed)

Rndmap(is,smooth,detail)

Maketextures(is)
Maketerrain(1,xscale#,yscale#,zscale#)

px#=(get terrain x size(1)*xscale#)/2
pz#=(get terrain z size(1)*zscale#)/2
py#=get terrain ground height(1,px#,pz#)
position camera px#,py#+5,pz#

Do
gosub controlcamera
loop

controlcamera:
`Little bit of code just to move the camera about
px#=Camera position x()
pz#=Camera position z()
py#=Camera position y()
pang#=Camera angle y()

speed#=curvevalue((upkey()-downkey()),speed#,15)

AngleY# = WrapValue(AngleY#+(rightkey()-leftkey())*5)
pang#=CurveAngle(AngleY#,camera angle y(),15)

px#=newxvalue(px#,pang#,speed#)
pz#=newzvalue(pz#,pang#,speed#)
py#=get terrain ground height(1,px#,pz#)
position camera px#,py#+20,pz#
yrotate camera pang#
return

function Rndmap(is,smooth,detail)
dim col(is,is)
dim newcol(is,is)

`Quick random high level wash
for x=0 to is
for y=0 to is
col(x,y)=rnd(30)+200
next y
next x

`leaves the outer edges high but lower's the middle
for x=9 to is-9
for y=9 to is-9
col(x,y)=rnd(80)+80
next y
next x

`Adds in random boxes and circles of different heights
for d=1 to detail
col=rnd(80)+80
if rnd(1)=1
rad=rnd(20)+1
rx=rnd((is-rad*4)-8)+rad*2+4
ry=rnd((is-rad*4)-8)+rad*2+4
for r=0 to rad
for ang=1 to 360
col(int(rx+sin(ang)*r),int(ry+cos(ang)*r))=col
next ang
next r
else
xs=rnd(20)+1
ys=rnd(20)+1
rx=rnd((is-xs*4)-8)+xs*2+4
ry=rnd((is-ys*4)-8)+ys*2+4
for x=rx-xs to rx+xs
for y=ry-ys to ry+ys
col(x,y)=col
next y
next x
endif
next d

`gives final random wash
for x=0 to is
for y=0 to is
col(x,y)=col(x,y)+(rnd(40)-20)
if col(x,y)>255 then col(x,y)=255
next y
next x

`Smooths out the texture
for s=1 to smooth
for x=2 to is-2
for y=2 to is-2
tcol=0
av=0
for sx=-2 to 2
for sy=-2 to 2
inc av
inc tcol,col(x+sx,y+sy)
next sy
next sx
newcol(x,y)=tcol/av
next y
next x
if s<5
for x=3 to is-3
for y=3 to is-3
col(x,y)=newcol(x,y)
next y
next x
endif
next s

`Draws image
lock pixels
for x=0 to is
for y=0 to is
dot x,y,rgb(newcol(x,y),newcol(x,y),newcol(x,y))
next y
next x
unlock pixels

`Saves image
get image 5000,0,0,is,is
if file exist("heightmap.jpg") then delete file "heightmap.jpg"
save image "heightmap.jpg",5000
delete image 5000
cls 0
undim col(0)

`The code below is commented out purely as i'm using the data in this array later on
`to generate the textures. It would usually be left in.
`undim newcol(0)

endfunction

function Maketerrain(tn,xscale#,yscale#,zscale#)

make object terrain tn
set terrain heightmap tn ,"heightmap.jpg"
set terrain scale tn ,xscale#,yscale#,zscale#
set terrain split tn ,16
set terrain tiling tn ,16
set terrain light tn ,1,-1,0,1,1,1,0.5
set terrain texture tn ,1000,2000
build terrain tn
update terrain tn

endfunction

Function Maketextures(is)

`Set to 0 for grass, 1 for sand
map=1

`Makes a random terrain texture
lock pixels
for x=0 to is
for y=0 to is
col#=newcol(x,y)
if col#>200 then col#=150
inc col#,rnd(20)-20
if map=0
dot x,y,rgb(0,col#,0)
else
dot x,y,rgb(col#,int(col#/1.1),int(col#/2))
endif
next y
next x
unlock pixels
get image 1000,0,0,is,is

cls 0

`Makes a random detail map
lock pixels
for x=0 to 200
for y=0 to 200
col#=rnd(40)+100+map*30
dot x,y,rgb(col#,col#,col#)
next y
next x
unlock pixels

get image 2000,0,0,200,200,1
cls 0
endfunction

Rem Food generation
function Placebottles()
for i = 11 to 20
if object exist(i + 99 ) = 0
make object cylinder i + 99,15
endif
x# = rnd(900) + 150 : z# = rnd(900) + 150
y# = (get terrain ground height(1,x#,z#) ) + object size y(i + 99) / 2
position object i + 99,x#,y#,z#
show object i + 99
next i
endfunction

- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Apr 2012 11:39
The end statement stops your program from running into the function declarations at runtime. It's not needed in all cases but it's good practice to have a definite end to the program even when it isn't essential.

I'll look at your code later today if someone else hasn't done so already by then.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 12th Apr 2012 13:05
Think about the structure of your code. You've basically set it up like this:

Place The Bottles
Display the titlescreen
Load the objects
Generate the terrain
Main game loop

Have a look at that structure and see where it's gone wrong...

"here is a an expression to remember - He who has the gold makes the rules."
Paypal disagreed!
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 12th Apr 2012 17:02
You can place all of your code into a snippet box when posting by clicking the CODE button, pasting your code and then clicking the CODE button again.

Mobiius is correct; your code could use some organizing. I would suggest you save the title screen bitmap to an image. That way, you can display it while the objects / textures / etc. are loading and you are initializing everything. Once the game is over, you can display it again.

You also could make a subroutine or function to include all of the object creation / texturing / positioning. Then you simply call it early in your code. It would help to neaten things up a little.

You are placing the bottles before the terrain even has been created. Placing the bottles should come after you have everything else in place. Right before your main do - loop would be a good place.

I assume you actually have indented your code and posting on the forum like that removed it all. If you haven't indented your code, you should. This makes it more readable and will likely get more people to help you with it.

You need an END command right after your do - loop, as GG suggested.

I noticed you have a SYNC rate, but you did not turn manual SYNCing on, nor do I see any SYNC commands in your code.

So many games to code.......so little time.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th Apr 2012 02:40
Quote: "You can place all of your code into a snippet box when posting by clicking the CODE button, pasting your code and then clicking the CODE button again."


Better still when your code is long is to attach the file - much easier for people to grab the code that way.

Sorry I haven't had time to look at this today. I'll try to look at it tomorrow. In the meantime you will probably find it helpful to follow the advice of the previous two posters.
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 13th Apr 2012 23:12
UPDATE: Got the bottles to spawn just like I wanted them, but now it's spawning me near a weird sphere that I had no idea was created.

Code Update (and I do know how to do a code snippet, I just forgot the other code button, sorry!):



- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th Apr 2012 23:46 Edited at: 14th Apr 2012 00:01
What do you mean? This is what I get when I move the camera a bit with the arrow keys:



Edit Sorry. Just moved the camera the other way and I think I see what you mean. Why don't you simply hide your spheres one at a time till the offender disappears? Then you'll know.

Attachments

Login to view attachments
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 17th Apr 2012 22:59
Gandlaf, later on I'm going to add boundaries for the edges of the map. I respawn outside the map at a sphere. The sphere itself is not a problem, it's the spawn location.

- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Apr 2012 14:31
I assumed you didn't know which sphere when you said:

Quote: "but now it's spawning me near a weird sphere that I had no idea was created"


When you say "me" do you mean the main camera, i.e. camera 0?

As far as I can see you haven't initialised the main camera so it's starting position probably depends on the position of the last object created which I guess is the sphere you're talking about.

You need to take explicit control of the main camera as follows.

Make sure that somewhere near the start of your code you put



and at a suitable point before your main loop



or something like that (with appropriate values of course).
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 18th Apr 2012 23:01
Gandalf: I need to start the camera at ground height. Since the terrain is ranodmly generated, if I set a spawn it won't work unless i happen to spawn right on a hill.

- s4rg3
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 18th Apr 2012 23:03
UPDATED CODE



- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Apr 2012 01:13 Edited at: 19th Apr 2012 01:52
Quote: "if I set a spawn it won't work unless i happen to spawn right on a hill."


What do you mean and why don't you just do what I suggested? You can choose where the main camera starts. At the moment you seem to be letting the system use the DBPro default position which depends on where you last created an object. You need to use autocam off as I've already told you.

Edit Your new code doesn't compile. I think you've used "col" for two different things - a variable and an array.

Edit2 Weird. Your old code (which I compiled and ran on my laptop) won't compile on my main machine - and has the same conflicting use of "col". I think I'd better revisit this in the morning when I'm wide awake.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Apr 2012 17:23
OK. Tried your code on my laptop again and it compiles fine (haven't checked the other machine yet - perhaps there's a compiler setting I've changed somewhere).

Anyway, as I've said already, just putting autocam off at the start of the code seems to fix things. Did you try?

the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 20th Apr 2012 23:06
I made a camera 1 then changed all of the camera commands to be towards camera 1. That fixed my problem.



- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Apr 2012 01:25
What was the point of that?

Anyway, glad you solved your problem.
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 23rd Apr 2012 23:30
Thanks! I need to somehow collect the spheres with the camera, however, and have no idea how I can do it.

UPDATED CODE



- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Apr 2012 15:59
Do you mean something like this?

Move the camera using the mouse to turn and up/down keys to move back and forth.

The spheres are collected when the camera passes close enough. This demo has 9 spheres to collect.

the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 26th Apr 2012 23:10
Green Gandalf: That's exactly what I was trying to do, but just the camera collecting my spheres. I'm not sure exactly how you got that to work, could you explain it to me?

- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Apr 2012 00:25
The basic idea is very simple. Every loop check the camera's position against the position of every sphere still remaining. If you find one that is close enough hide it, i.e. collect it.

I also added an extra step to the code to avoid wasting time searching through spheres that had already been collected. When I found a match what I actually did was hide the last one in the list and put the matching sphere in that sphere's position. That meant that I initially checked the positions of object numbers 2 to 10, then 2 to 9, 2 to 8 and so on till all had been found. That extra step wasn't needed with such small numbers of spheres but might be helpful if you had 10000.

The rest of the code is just the usual distance calculation except that I used the square of distance rather than distance itself - just avoids an unnecessary square root.

Hope that helps.
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 30th Apr 2012 23:09
How would you do that? Something like this?



- s4rg3
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 30th Apr 2012 23:37
Ah, I think I'm closer to the problem. Here's my updated code:
It still won't collect any spheres, though. took the code you used and changed it to use my variables. It worked on yours, but doesn't seem to on mine.



- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st May 2012 01:46 Edited at: 1st May 2012 01:48
Did you remember to declare px#, py# and pz# as globals? Otherwise the function will assume they are local to the function and all have values of zero.

Edit And check the other variables used in your function too.
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 1st May 2012 23:11
Variables, Check.
Collection, not working. I can't find where it says which object is considered lastsphere, only things involving lastsphere.



- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd May 2012 00:07
It's somewhere else in the code - or should be.

Have a look at my snippet again. It's there.
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 2nd May 2012 23:32
IO looked, and I didn't have global on lastsphere or spherecount.

After adding it, nothing happened. Here's my updated code, but I'm baffled as to why it won't work.



- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd May 2012 23:39
Are these the correct values for your particular application. They were for my demo but you need to modify my code so it's correct for yours.
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 3rd May 2012 23:01 Edited at: 3rd May 2012 23:12
I did use the correct values, and have checked it as well. It still will not collect the spheres.

EDIT: I may have found the problem. Take a look at this, it says that my object numbers of the spheres are all (ob + 99). Could that be it?



- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 4th May 2012 00:43
Almost certainly. You obviously need to edit the object numbers in my snippet so it corresponds to the ones you used.

Which is precisely what I was suggesting in my previous message.
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 7th May 2012 23:15
Here's my code, no idea why it won't work.



- s4rg3
the_s4rg3
12
Years of Service
User Offline
Joined: 28th Mar 2012
Location:
Posted: 11th May 2012 23:22 Edited at: 11th May 2012 23:24
Anyone help me? I've tried something else, here it is. I need to finish this VERY SOON and have no idea how to.

Updated Code


- s4rg3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th May 2012 00:47
What are these two lines supposed to do?

Login to post a reply

Server time is: 2024-05-02 06:39:19
Your offset time is: 2024-05-02 06:39:19