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.

AppGameKit Classic Chat / [code] MArray - Arrays Built From Memblocks

Author
Message
swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 2nd Jun 2014 21:35 Edited at: 17th Jul 2014 23:37
Dynamic lists. Resizes at multiples of 2. Probably preferable to this.



From my testing it looks like these are about 2 times slower than normal arrays with constant time operations. It grew to 5 times adding 1000 elements using the same style of array growth (I assume it\'s because the interpreted code to copy over values is slower than dim\'s. I have since made a slight adjustment to increase the copy speed). So I have to say, this seems like a pretty strong alternative in those times when you can\'t use arrays (and these won\'t run into problems with shrinking in size ).

Some Notes:
-You need to reassign pointers to memblocks from any functions that may resize the MArray. It\'s simply because resizing memblocks demands that you discard the old one. For example, add should be used as such
. It is not needed with functions like set, but for convention you can still write your code that way.
-I don\'t catch any bounds errors here. You will most likely receive a memblock offset error if you violate the bounds of your MArray. I decided to leave the code that way because AppGameKit doesn\'t have a standard for throwing your own errors.
-Adding to the end is usually O(1) because no shifting is needed. If order doesn\'t matter add to the end to max out efficiency.

Edit: Updated version. This example shows how you can mix floats and ints into the same array (if you are careful )


Just the functions.

Commands:
new_MArray()
delete_MArray(ID)
MArray_addEnd_float(ID, num#)
MArray_addEnd_int(ID, num)
MArray_add_float(ID, index, num#)
MArray_add_int(ID, index, num)
MArray_remove(ID, index)
MArray_set_float(ID, index, num#)
MArray_set_int(ID, index, num)
MArray_get_float(ID, index)
MArray_get_int(ID, index)
MArray_get_size(ID)
MArray_copy(ID)
...
MArray_space(ID) returns the number of indexes the current memblock can hold. Only important if you\'re counting memory space.
MArray_resize(ID) is called by add and remove. I\'d warn against playing with it outside of those commands. You can modify it to change the conditions under which the array resizes.
MArray_bubbleSort_float(ID)/MArray_bubbleSort_int(ID) only work if you have a homogeneous array when it comes to data type. You can use these functions but if your array is of considerable size find a better sort

Edit2: Strings!! The newest new command set -

Keep in mind that
-Strings must be converted into memblocks and recreated, so this is certainly slower than using normal strings.
-Strings are not automatically garbage collected, you have to delete them yourself or you'll run into memory leakage. I put in a MArray_remove_str() command which does call the cleanup command(MArray_delete_str), but if you plan on overwriting other strings, be sure to delete the old one

String Commands:
MArray_addEnd_str(ID, st$)
MArray_add_str(ID, index, st$)
MArray_set_str(ID, index, st$)
MArray_get_str(ID, index)
MArray_delete_str(ID, index)

IronGiant
AGK Bronze Backer
12
Years of Service
User Offline
Joined: 27th Feb 2012
Location: the great state of madness
Posted: 3rd Jun 2014 17:48
Thanks again for the code Swiss, Both this MemBlock one and the string array one, I was thinkin on this very subject just the other day. Nice to see useful code being posted on this forum. Will find this very useful I'm sure.

It's Bird! , It's Plane!, No its a rocket powered Squirrel holding some acorns and a smile!
swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 3rd Jun 2014 18:42 Edited at: 4th Jun 2014 20:47
Quote: "Thanks again for the code Swiss, Both this MemBlock one and the string array one, I was thinkin on this very subject just the other day. Nice to see useful code being posted on this forum. Will find this very useful I'm sure."




I was able to use OOP style coding to nest these and make a data structure for this,

I have to say it's a bit of a nuisance that pointers can change because you have to update ALL of them. But it isn't too hard to work with. Finding clever ways around it is rather easy

Edit: does AppGameKit have a limit on the number of parameters you can have in a function? Looks like it does to me my values have been getting scrambled when I use 12. It's the strangest thing. I print a variable before passing it in and after passing it in to find the numbers differ. When I reduce the parameter count (to 7) the problem disappears. That took hours to figure out! My solution was one of these MArrays. Quite handy

Attachments

Login to view attachments
Wilf
Valued Member
17
Years of Service
User Offline
Joined: 1st Jun 2006
Location: Gone to Unity.
Posted: 4th Jun 2014 17:55
Yep, AppGameKit has 9 arguments in a function max.

Twitter: @itanican
swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 4th Jun 2014 19:58 Edited at: 4th Jun 2014 22:43
Quote: "Yep, AppGameKit has 9 arguments in a function max."

Strange. That should really be documented under 'function.'

Edit: Grew my code above into a navmesh that can be searched using A*. I'm still optimizing so I'll post a runnable version soon. At the moment I can update my path every frame and draw it at about 1000 FPS. Unfortunately, I've made an error somewhere and I believe I've forgotten to delete an MArray. It slows down progressively as memory builds up I do need to test it out on a much larger map, but I still think I can squeeze a few more frames out, so it feels promising



Attachments

Login to view attachments
Blendman
10
Years of Service
User Offline
Joined: 17th Feb 2014
Location: Arkeos
Posted: 5th Jun 2014 11:56 Edited at: 5th Jun 2014 11:57
Hi

I don't know for the 9 parameters in a function too ^^. Thans for the information.

Quote: "Strange. That should really be documented under 'function.'"

In fact, I have search, and it's documented here :



http://www.appgamekit.com/documentation/principles/4_functions.htm



http://www.dracaena-studio.com
swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 5th Jun 2014 18:35
Oh you're right. That should be made much more clear It's also a strange limitation Perhaps AGKv2 will allow any number

swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 11th Jun 2014 21:29 Edited at: 11th Jun 2014 21:30
For those who are curious, I managed to optimize and get pretty good results.

But they aren't really acceptable (some paths it generates just aren't quite right) I found a paper on blending A* and the funnel algorithm together to create paths published in 2012. I'm going to give it a shot It'll take a lot of recoding but at least I have a basis to build off of.

Attachments

Login to view attachments
Wilf
Valued Member
17
Years of Service
User Offline
Joined: 1st Jun 2006
Location: Gone to Unity.
Posted: 12th Jun 2014 11:50
Yes, that could be super-useful!

Twitter: @itanican
swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 13th Jun 2014 00:20 Edited at: 13th Jun 2014 00:38
The article lead to success I just need to optimize. It's a bit slow In theory it shouldn't always be giving me the absolute shortest path, but so far it has every time (or really really close, like this picture) once I polish it up I'll post an exe file for people to play with.

This is the slowest path I could find. I'll have to see how the speed scales with size. I was hoping for closer to 1 millisecond for the calculation but like I said, I can touch up a few things.

Attachments

Login to view attachments
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 13th Jun 2014 15:35
Is this Tier 1? If so I will give you all three of my children in exchange for the code. It's official. Here are three beers to be going on with
swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 13th Jun 2014 18:00 Edited at: 13th Jun 2014 21:10
Quote: "Is this Tier 1? If so I will give you all three of my children in exchange for the code. It's official. Here are three beers to be going on with "

Yep Tier 1. Logically the code is pretty sound, but it looks hideous. My variables are essentially all indices because of the way the memblocks work. Once I clean up my code I'll try commenting it for clarity, but I might be better off explaining all of the steps with pseudo code Surprisingly, there isn't a single source out there that completely and entirely explains what's required I had to piece it together myself. But hey, we'll see

Edit: Well here's that .exe
Controls:
wasd - move end point
ijkl - move start point
t - toggle mesh visibility
r - swap start and end points

The numbers at the top don't all update except calculation duration if you don't have a path. I just tacked on the a star stuff so I could see where the majority of the processing was going. (The % is percentage of total time spent on a star if you're curious)

Attachments

Login to view attachments
swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 5th Jul 2014 22:30
Had a bit of a set back as many electronics here were damaged from a lightning strike. I'll get back to my pathfinding project soon (and probably make a real thread for it )

swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 17th Jul 2014 22:49
Wanted to pop in with a quick bump to say I'm expanding MArrays to support strings I've stopped using normal arrays entirely so it seemed fitting to put the last piece of the puzzle in

Login to post a reply

Server time is: 2024-05-17 03:09:55
Your offset time is: 2024-05-17 03:09:55