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 Discussion / Functions Help

Author
Message
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 18th Dec 2007 12:28
I've given a general title to this thread because I know I'm going to need it again .

Highest Number
How would I write a function to return the highest value from a list of parameters?

i.e. In this example Num=4.
Quote: "
Num = HiNum(1,2,3,4)

END

FUNCTION HiNum(n1,n2,n3,n4)
~
ENDFUNCTION Hi
"


demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 18th Dec 2007 12:49 Edited at: 18th Dec 2007 12:50
I would say something like this (although it may not be the most efficient way of doing it)



That should work, but obviously if you were inputting thousands of variables then it would take WAY too long, I guess you'd use an array for that instead or something so that you could use a for loop...

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 18th Dec 2007 14:37 Edited at: 18th Dec 2007 14:51
@DB (wow cool initials! )
Yeah that's what I was thinking, it doesn't look very nice does it lol

@Window
I like that idea but I was hoping for something that compared numbers in real-time instead of remembering which is the highest.
Your idea is probably a lot more efficient though.


It's probably best if I explain why I wanted this.
I want to make a gangster game and I like the nicknames that some gangsters get, so I want my characters to have nicknames based on their skills.
For example: a guy with high Strength and Weapon skills would be called "Scarface".

I was thinking of having template skill levels for these nicknames:
Quote: "
Scarface = Strength: 80, Weapons: 70, Intelligence: 50
Fingers = Strength: 50, Weapons: 70, Intelligence: 80
"

Then the player's skills would be compared to the templates and if it was close to one of them the character would get that nickname.

demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 18th Dec 2007 20:59
Well seeing as you're having to store those values in an array anyway, then why not just do my method but comparing within the array.

Like


If you only have a few variables that would probably be the easiest way, if not the most appealing...

jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 18th Dec 2007 21:36
Quote: "Write the numbers to an array. Depending on the array index, check if the next and/or last index has a higher value. If so, delete the current array index. Do that until only one number is left in the array."


Right Approach - Except for the Delete Array Index Each Iteration PArt. That will cost more CPU CYCLES than just having a local variable (Named MAX) set to zero. Loop thorugh the array - if the current element is greater then "MAX" than make MAX = Current Value in the array - finish loop. Then Delete Array.

Each System is built differently and only TGC could correctly answer which is more efficient. My Statement is based on a number of common system "design paradigms" for Array Type Stuff.

If DBPro Arrays are under the hood in a class - that doesn't allocate EACH element in a different memory allocation (optimized memory management) - Then won't matter. If DB Allocates one MEM block for Whole Array - and deleting an element means allocating a smaller memblock and copying all the data (less the deleted element) to the new memblock and then deleing the old memblock (allocated ram - not a DB memblock but I think you get me) anyway - that would likely take more cycles than necessary.

jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 18th Dec 2007 22:28
Quote: "But if you think deleting an element from a simple integer array (even if large) is slow"
I don't know how they work under the hood so I don't know.

Quote: "take less than 1 ms"
On a fast machine no doubt.


Quote: "Think about it, it only has to copy a few bytes or kbytes."
I agree - but my method should be as efficient - or more - because there isn't any under-the-hodd mem allocation happening every iteration - just once. That's all I'm saying. I worry about this kind of stuff - especially with "tiny functions" that might get called thousands of time in an algorythm. That's my reasoning.

And I don't know the best solution - I just know my solution would allocate a variable on the stack (MAX) and one "System mem" request before the loop and one "System Mem FRee" Call at the end.

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 19th Dec 2007 11:29 Edited at: 19th Dec 2007 11:30
I've made this basic (messy) example of what I want to do.

The problem is that the character will only get the nickname if his skills are near the exact template.
I want the character to get the nickname if his skills are proportioned in the same way as the template.

Quote: "
Scarface: Strength=80, Smarts=50, Weapons=70

Mickey "Scarface" Sorvito: Strength=95, Smarts=62, Weapons=84
"

How can I do that kind of "fuzzy" analysis.

demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 19th Dec 2007 14:10
compare the values of all of them. So for scarface you want strength>weapons>smarts... Unless you want to only accept values in a certain range...

"A West Texas girl, just like me"
-Bush
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 20th Dec 2007 08:34
find % of the primary stat (strength for scareface, weapons for fingers ie highest value skill=primary) for the 'base' template: strength=80, smart=50 which is ~62% of strength (smart/strength), weapons=70 which is ~87% of strength.

establish an acceptable variance range, say +/- 3% for a 6% total range. so if actual smarts falls within 59%-65% of actual strength, requirement is met, and if actual weapons falls within 84%-90% of actual strength second requirement is met, set name to scarface.

Quote: "
Scarface: Strength=80, Smarts=50, Weapons=70

Mickey "Scarface" Sorvito: Strength=95, Smarts=62, Weapons=84
"




and so on. So with Mickey Sorvito example we come up with strength >100$, smarts at 65% and weapons at 88% which meets all criteria checks and will set nick to scarface

Login to post a reply

Server time is: 2025-06-02 10:23:07
Your offset time is: 2025-06-02 10:23:07