Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11-21-2008, 06:37 PM
Raeiphon Raeiphon is offline
I never asked for this.
Join Date: Jun 2005
Posts: 855
Raeiphon is on a distinguished road
Sorting numerical data to index

I'm terrible with this sort of stuff - not just in GS2 but in Python aswell.

I'm basically after a function that sifts through a list file (probably not the right term for gs2, but basically what I mean) and resorts the list so the index of said list matches to the contents being sorted in a ascending or descending order.

So basically, I feed it a list with the contents [11,24,55,9,17] and the variable to sort ascending, and it returns:

list[0] = 9
list[1] = 11
list[2] = 17
list[3] = 24
etc, etc

But not only that, I'd need the function to be able to do this and reassign two pieces of data, so each number would be linked to a string. (list[0] = "9, raeiphon" - picture a scoreboard as such) This would be easy if GS2 supported tuples, but I've been gone from the scripting scene for so long I can barely remember anything about it.

I'm not asking for a script that does exactly this. I'm asking for advice on what methods I should take to properly learn how to do this - what to read up on, what to think, what to sort. A script would be nice if it was properly commented and whatnot, but I doubt anyone would be willing to spare the time to do that, if it's as hard as I think it is.

There's probably a really simple way to do this too. -sigh-
__________________

I hope for nothing. I fear nothing. I am free.
Reply With Quote
  #2  
Old 11-21-2008, 06:47 PM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
Copy a quicksort implementation or something from wikipedia, port it to gscript, replace the comparison with one that first splits the number out of that string and only orders by it?
Reply With Quote
  #3  
Old 11-21-2008, 07:15 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Quote:
Originally Posted by Raeiphon View Post
I'm terrible with this sort of stuff - not just in GS2 but in Python aswell.

I'm basically after a function that sifts through a list file (probably not the right term for gs2, but basically what I mean) and resorts the list so the index of said list matches to the contents being sorted in a ascending or descending order.

So basically, I feed it a list with the contents [11,24,55,9,17] and the variable to sort ascending, and it returns:

list[0] = 9
list[1] = 11
list[2] = 17
list[3] = 24
etc, etc

But not only that, I'd need the function to be able to do this and reassign two pieces of data, so each number would be linked to a string. (list[0] = "9, raeiphon" - picture a scoreboard as such) This would be easy if GS2 supported tuples, but I've been gone from the scripting scene for so long I can barely remember anything about it.

I'm not asking for a script that does exactly this. I'm asking for advice on what methods I should take to properly learn how to do this - what to read up on, what to think, what to sort. A script would be nice if it was properly commented and whatnot, but I doubt anyone would be willing to spare the time to do that, if it's as hard as I think it is.

There's probably a really simple way to do this too. -sigh-
The best you can expect from this is O(n*log(n)).

PHP Code:
function sort_listinitial_list ) {
  
sorted_list = {}; // Sorted List
  // For every element in the initial list
  
for ( list_iteminitial_list ) {
    
// Find Index for new item
    
low 0;
    
high sorted_list.size();
    while ( 
low high ) {
      
mid low int( (high low) / );
      if ( 
sorted_list[mid] > list_item )
        
high mid;
      else if ( 
sorted_list[mid] < list_item )
        
low mid 1;
      else 
low high mid;
    }

    
// Add Item to appropriate index  
    
sorted_list.inserthighlist_item );
  }
  
// Return sorted list
  
return sorted_list;
}

function 
onCreated() {
  
initial_list = {11,24,55,9,17}; // To Sort
  
sorted_list sort_listinitial_list ); // output: {9,11,17,24,55}

If you want to create a mapped list... Have each list item such that key => value is a struct {key,value}. Namely, element[0] == key.

PHP Code:
function sort_listinitial_list ) {
  
sorted_list = {}; // Sorted List
  // For every element in the initial list
  
for ( list_iteminitial_list ) {
    
// Find Index for new item
    
low 0;
    
high sorted_list.size();
    while ( 
low high ) {
      
mid low int( (high low) / );
      if ( 
sorted_list[mid][0] > list_item[0] )
        
high mid;
      else if ( 
sorted_list[mid][0] < list_item[0] )
        
low mid 1;
      else 
low high mid;
    }
  
    
// Add Item to appropriate index  
    
sorted_list.inserthighlist_item );
  }
  
// Return sorted list
  
return sorted_list;
}

function 
onCreated() {
  
initial_list = {{11,"Foo"},{24,"Bar"},{55,"Bad"},{9,"Good"},{17,"Foo Foo"}}; // To Sort
  
sorted_list sort_listinitial_list ); // output: {{9,"Good"},{11,"Foo"},{17,"Foo Foo"},{24,"Bar"},{55,"Bad"}}

I actually am using a linear-search for my dictionary objects on my server, but a binary search on a sorted list is a lot more efficient... I was going to write this sometime this week anyways.

Hope this helps!
Reply With Quote
  #4  
Old 11-21-2008, 07:58 PM
Raeiphon Raeiphon is offline
I never asked for this.
Join Date: Jun 2005
Posts: 855
Raeiphon is on a distinguished road
That does it perfectly, Novo.

Thanks a ton. I'll spend some time dissecting that script.

On a completely unrelated note, is it possible to call a string as a function - and on that train of thought, is it possible to use a string to mask an object so I can access that object's attributes?

Say foo = "calculate()", and I have a function of the same value. Is there any way of using foo to execute that function?

For the second one, say I had a string foo = "Command". This is in a class - and it's linked to a script with a gui element named Command. Is there some way of going foo.text (therefore linking Command.text)?
__________________

I hope for nothing. I fear nothing. I am free.
Reply With Quote
  #5  
Old 11-21-2008, 08:05 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
You can use ("string")() to call a function, and use ("string").attribute to access an object.
Reply With Quote
  #6  
Old 11-21-2008, 09:03 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by Crow View Post
You can use ("string")() to call a function, and use ("string").attribute to access an object.
You don't really need the paranthesises, but I guess that's a personal preference or something like that
__________________
Reply With Quote
  #7  
Old 11-21-2008, 09:21 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Quote:
Originally Posted by Chompy View Post
You don't really need the paranthesises, but I guess that's a personal preference or something like that
Hm. I think last time time I tried without it didn't work. Not sure. But I think the code reads a lot better with parenthesises ;D
Reply With Quote
  #8  
Old 11-21-2008, 09:56 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Chompy View Post
You don't really need the paranthesises, but I guess that's a personal preference or something like that
To do it without parenthesis implies that the attribute is part of the string itself.
__________________
Reply With Quote
  #9  
Old 11-21-2008, 10:52 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
I don't think you're ready to experiment with dynamic executional programming. In the majority of the cases, you don't need dynamic execution. Using them makes debugging harder and encourages unconventional hacks rather than well-thought-out solutions.
Reply With Quote
  #10  
Old 11-22-2008, 01:55 AM
Raeiphon Raeiphon is offline
I never asked for this.
Join Date: Jun 2005
Posts: 855
Raeiphon is on a distinguished road
Right, another question:

Does GS2 have some sort of semaphore I can use to tell a specific function to idle and wait until data from the server is received? I figured I could do this by setting a while loop which runs a 'idle' task until a onActionServerside -> onActionClientside bounce retrieves the data and updates the variable controlling the while loop, simultaneously ending the idling session and returning the function with the proper data.

Pretty rudimentary stuff, but I'm terrified of trying it out in fear that there's no anti-infinite loop control on the server end, and that ****ing around with this stuff will lag the server I'm currently on to ****. Normally in Python I'd just thread major functions or aspects and plonk semaphores whereever I needed them - I'm not sure if GS2 has the functionality with threading at all
__________________

I hope for nothing. I fear nothing. I am free.
Reply With Quote
  #11  
Old 11-22-2008, 02:57 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Raeiphon View Post
Right, another question:

Does GS2 have some sort of semaphore I can use to tell a specific function to idle and wait until data from the server is received? I figured I could do this by setting a while loop which runs a 'idle' task until a onActionServerside -> onActionClientside bounce retrieves the data and updates the variable controlling the while loop, simultaneously ending the idling session and returning the function with the proper data.

Pretty rudimentary stuff, but I'm terrified of trying it out in fear that there's no anti-infinite loop control on the server end, and that ****ing around with this stuff will lag the server I'm currently on to ****. Normally in Python I'd just thread major functions or aspects and plonk semaphores whereever I needed them - I'm not sure if GS2 has the functionality with threading at all
The object variable maxlooplimit prevents infinite loops on that object's script, the default loop limit is 10,000 but it can be changed.

And the function waitfor(object, eventname, time); may be what you're looking for. The function halts the function calling it and waits for the specified event on the specified object for the specified amount of time. If the time runs out then waitfor() returns false, otherwise returns true if the event it's waiting for happens.

I created a class for weapons that wraps triggerserver() and triggerclient() and allows functions to be called on the other side and wait for their return value. The script is in the Code Gallery.
Quote:
Originally Posted by Novo View Post
I don't think you're ready to experiment with dynamic executional programming. In the majority of the cases, you don't need dynamic execution. Using them makes debugging harder and encourages unconventional hacks rather than well-thought-out solutions.
What is this dynamic executional programming you're talking about?
__________________
Reply With Quote
  #12  
Old 11-22-2008, 05:22 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Apparently no one appreciates my quicksort implementation (and introsort, which has an upper bound complexity of O(n log n) and average complexity of O(n log n) because it switches from quicksort to heapsort based on a certain threshold):
http://forums.graalonline.com/forums...ad.php?t=81279

Or maybe I just scared off everyone with the first function in that thread for efficient string searching.
Reply With Quote
  #13  
Old 11-22-2008, 07:50 AM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Yea... WhiteDragon's code is marginally better than mine because I use an insertion (which has a O(n)... Making my code O(n^2) in worst case) while WhiteDragon's model uses qsort( which doesn't insert, but swaps values. No memory displacement ).

I think mine's easier to understand though, and for someone who isn't familiar with algorithms... Might be a start.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 12:29 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.