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.

Newcomers DBPro Corner / Seperate Counters in a UDT Array

Author
Message
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 2nd Nov 2011 22:23 Edited at: 2nd Nov 2011 22:28
If i want to make seperate counters for a UDT array, like for example

Type FactionType
Number ` What number the faction is
SpawnTime ` Set to 0 at first, every second it gets increased
EndType

This works fine once i add a time into it that sets this up to seconds and it will show me how many seconds whatever one of the 6 factions that exist were started at. At first they are all started together so they should all read the same and they do.

For a = 1 To MaxFactions (a constant set to 6)
Print "Faction ", Faction(a).Number, " spawn time ", Faction(a).SpawnTime
Next a

Works a charm.

Now if i want to add a little random decision making to them i would want them to make decisions every 5 - 8 seconds or so.
Thats each different faction to pick a random number, at a random time between 5 - 8 seconds.
I have tried so many different variations of this and none have worked.
If i try

This will add a different number of seconds to each of the factions in which to make a decision. But actually getting it to check through how long the seconds have passed since then either dont work or only work once because i cant reset it to a straightforward number that allows it to be reset properly.



Stuff like this simply wont work with the above. It still goes haywire and still makes them all pick the same decision number or no numbers at all, when i want them picking different numbers at different times.

I know exactly why the above does not work, this is only what i last tried. Trying to work out why the above does not work is pointless because it is obvious. I can see exactly where it has gone wrong, after a quick run through of it. Its merely the last thing i wrote. I am going wrong along the same lines as these. I am hoping the code points someone in the direction i was going, as i am not trying to fix the above code with an explenation, i know why its naff.

This is such a silly and simple problem about maniuptaing very simple numbers and variables but it certainly has me stumped.
The Wilderbeast
19
Years of Service
User Offline
Joined: 14th Nov 2005
Location: UK
Posted: 3rd Nov 2011 00:50 Edited at: 3rd Nov 2011 00:52


Something like that will work nicely. That probably won't compile, it's a while since I've used DBPro and I've forgotten which parts of my personal coding style are legal and which aren't xD


10% TGC Discount!
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 3rd Nov 2011 12:48 Edited at: 3rd Nov 2011 13:13
So..

What is handling the value of the STime property?

Have you been logging or printing the values of Decision = RND(99) + 1 to visually monitor what is happening?

You random method isn't the best. It may take for ever for you to pull a 20-30 (10%)result. Are you using the [Randomize] function? Do you know what it does?

The reason why your method isn't good for this type of use is because you want to control how often certain numbers occur; but your method lets the computer decide how often numbers generate; for example it could generate 31 to 100 for hours; even days depending on how long each interval is. Rnd; pretty much selects a sequencial number from an internal list; no matter how often the same number occurs in the list.

There is a technique that you should use which is often called random pools; something used to generate random occurances and control how many occurances are allowed. It is quite simple; you create a list of numbers in an array, and remove the number from the list as you aquire it. When the list runs out, create a new list.

Note: When you are ready to learn how to use Matrix1; there are more random generator functions made available to you when you install Matrix1 utils


Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 3rd Nov 2011 20:44
Quote: "What is handling the value of the STime property"

Its just a timer that starts with seconds and as all 6 at first start at the same time STime (spawn time) is effectivley the same as seconds, but it wont be when a faction or two is destroyed and then a new one takes its place at some time.

Quote: "Have you been logging or printing the values of Decision = RND(99) + 1 to visually monitor what is happening?"


Yes, it is working as expected, just too fast.

Quote: "You random method isn't the best. It may take for ever for you to pull a 20-30 (10%)result. Are you using the [Randomize] function? Do you know what it does?"


Whats missing is the other factors for what happens at the other outcomes. I just did 1 - 30 (which is effectivly 1 - 30%). I am using randomize timer() at the beginning of my program. It works fine because its just simple laws of averages which was all i am going for. For example on a roulette wheel (lets forget the 00 green stuff) there is a 50% chance it will land on red, 50% on black. Now if you spin 100 times it could land on red 90 times but it doesnt change the fact that black still has a 50% chance of occuring, it doesnt make red a 90% chance. This may not be a good technique for other problems and i would be interested in learning more about the random pools if you would be so kind as to show me, though what im using at the moment is fine for the little test program.

What i am struggling with is the timer. I want each faction to come up with a random number at a different time interval. What i had before i typed the above worked but what was happening was each faction was making a decision at the same time AND they were all picking the same random number. So i tried to break it up so they decide at a different time, and also they each choose a random number, but for some reason i cant seem to do it.

Also i would be interested to learn more about the matrix utils (i have them downloaded and ready to install) but for the right now i want to do this program all native dbpro as i do think its acheivable, but for the future i will definateley be learning more about ians utils, as while at the moment i understand not one command in them, nor do i know how to use them effectivly (i dont think there are any tutorials on the uses of them so people like me will never know what to use with what) the one thing i do know is that they are an invaluble contribution to the native DB pro structure.
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 4th Nov 2011 01:58
Quote: "Now if you spin 100 times it could land on red 90 times but it doesnt change the fact that black still has a 50% chance of occuring, it doesnt make red a 90% chance."


True. So as long as you are confident that it will not annoy most players.

Quote: "This may not be a good technique for other problems and i would be interested in learning more about the random pools if you would be so kind as to show me, though what im using at the moment is fine for the little test program. "


This code example extracts a number from a list of decisions. The random pool result never reoccurs until you start over; when this occurs you shuffle the decisions; or leave the decisions in the same order.

The other method creates a normal random number from a range; minimum to maximum. However, decisions re-occur.



Quote: "What i am struggling with is the timer. I want each faction to come up with a random number at a different time interval."


This should be a small matter of setting each factions start time differently; or changing the length of their intervals; thus they will have to decide at different times if one faction must wait longer than the other.

Quote: "Also i would be interested to learn more about the matrix utils (i have them downloaded and ready to install) but for the right now i want to do this program all native dbpro as i do think its acheivable, but for the future i will definateley be learning more about ians utils, as while at the moment i understand not one command in them"


There are loads more functions in the plugin; a bit like there are loads more functions in Microsoft Word, than in Notepad. It may be intimidating today; but as a minimum have a look at what functions exist and check what they do; they are there to make your life simpler, not more difficult.

You may not understand them today, but you will tomorrow; far better than re-inventing the wheel only to find out too late that the plugin could do the job for you. Even in the code above there are a number of lines where the functions in Matrix1 could have made the code simpler. I am sure if you read the Matrix1 help files even today, you will spot what I am talking about; and you could shorten your source code.

Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 4th Nov 2011 09:47
Chris, that code looks great but i am struggling to follow it as im not 100% sure whats its doing and why. Unfortunatley one of my curses is that i still struggle to read code and follow it in a logical way, i get lost a bit too quick. Usually why i look for snippets with more green in them than black and blue , but it does look interesting, i just dont know why some numbers are what they are and im getting lost in it a bit, sorry.
Im not sure if it matters but some of the decisions would be conditional for example repairing a tower but it would only do that if it had one and was damaged in the first place, if not then it would make that decision but do nothing, which in effect is what i would want it doing the majority of the time i guess as i dont want it making thousands of decisions all the time. Then again i would have no idea how to port that over to an RTS if i were to make one where the AI would be relying on making many actions per minute, so i suppose with my current method i would get stuck somewhere, it only would appear to work with text. But i am fascinated with making complex AI and it is something i would like to do, not only for any rts but also mobs in 3d fps games, it has been my dream for a long time to create mobs that would make decisions based on their surroundings that you can observe from a far or interrupt as a player.

As for the timer. I dont want the factions STime to be anything but when the faction was started, so i want to rule out changing each one to start at a different time. What i want to do is make a seperate timer (which i tried and for some reason it didnt work) which says for each faction, make a random number between 3 and 8 (this will be seconds), now after the factions random number of seconds make it make a decision, so faction 1 picks 4, faction 2 picks 6, after 4 seconds faction 1 picks a decision, performs it and resets its timer so therefore picks another number, 2 seconds later (because 6 seconds have elapsed) faction 2 picks his decision.
This seems so simple to me but i was unable to do it, they all either make the same decision at the same time or it worked for the first couple of seconds or so. As i must not have been resetting the other timer properly.
I dont know whether i need a universal timer that ticks away and resets after the last faction has made a decision or each faction has 2 timers etc etc, ive tried so much and not got the desired result.

Even if i break it down to the very simple, take away decisions, take away factions. Just making numbers 1 to 6 pick a random number at different intervals between 3 and 8 seconds is stumping me, and thats it at its very basic level.
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 4th Nov 2011 14:51 Edited at: 4th Nov 2011 14:54
Quote: "Chris, that code looks great but i am struggling to follow it as im not 100% sure whats its doing and why."


Keep it aside and we can discuss what it's doing later. The important thing to understand is what the AvailableDecisions() array contains, and why its random numbers use the full range of chances without recurrence.

You timer problem shouldn't be an issue if your faction count downs are different. Consider this;

Faction(1).DecisionTime = Timer() + 10 * 1000
Faction(2).DecisionTime = Timer() + 20 * 1000


These two variables represent decision schedules that your factions own. I have to use raw calculations for illustration, but you should create a function for all your complex calculations to make them easy to reuse and update.

In the above assignment:

Timer() is the current time.
10 represents seconds.
1000 represents 1000 milliseconds. 1000 milliseconds (ms) is equal to 1 second.

10 * 1000ms is 10,000ms which is equal to 10 seconds.

Add this to Timer(), and it sets the countdown clock to 10 seconds ahead of the current time.

For I = 1 to MaxFactions

Your for loop runs through all of your factions at the same time; however the following IF statement only performs the inner actions when their scheduled time has come or passed.

If Timer() => Faction(I).DecisionTime

At this point, the code in the If block is skipped because the current time is still not equal or ahead of the DecisionTime.

The code in the condition block runs only when the Faction's decision time has come or passed. Now you need to schedule a new decision time. How will you do this?

Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 4th Nov 2011 19:20 Edited at: 4th Nov 2011 20:02
I thought i had cracked it but i ended up doing exactly the same thing i had done previously but in a different way.

If i loop through each faction using
For a = 1 To MaxFactions
Then i add a decision counter to it, all that ends up happening is its wiped out on the next main loop. So at first everyone gets a nice random 5 - 9 seconds on the timer, but before any if statement goes off, it goes again. So its permanently adding either 5 - 8 seconds to the time it was spawned every time.
Adding a second counter in some way might be the solution but i already have one and its not working as intended so it cant be right.

The adding of a random amount of seconds to each faction MUST have some sort of if statement within itself so that it does not repeat the adding of numbers until of course it is that time. Ill try a flag statement of some sort and see if it works.

Edit: Ok i finally got it to work. I basically added 4 things to the UDT for a faction.
DecisionFlag ` 0 or 1, at first allows a random number to be added to DecisionCounter by checking first if its 0, if a number is added then it is flagged as 1.
DecisionTimer ` Which is a constant but resetable timer that increments every second in the main loop.
DecisionCounter ` This is a random number between 3-8, when the timer hits more than this in seconds it resets DecisionTimer and drops the flag (sets it to 0)
Decision ` This is the random number between 1 - 100 that only occurs when the flag is 1 AND the decisiontimer is more than the counter, once it has made the decision everyting gets reset.

It seems like an awful lot but it really is the only way i got it working. I would like to learn other ways though for now i am grateful that i can at least finish this part and move on to the next bit

Login to post a reply

Server time is: 2024-11-22 11:54:58
Your offset time is: 2024-11-22 11:54:58