ID:1269343
 
(See the best response by Kozuma3.)
Alright, so I've been thinking of something awesome, but the downside is that I will be forced into looping through a certain type of tile in a specified Z-layer.

I am pretty much new at this, so help on how to do this would be very much appreciated.

You could loop through every turf in the world and only check ones whose Z level is what you want, or if you don't want to do this every time you can keep a list of turfs for each Z level and search through that list.
Keeping lists of things you need to quick access to is always the best way to do this because it uses more memory but less CPU.

For example, every time a turf of the type you care about is created, you can add it to a list...

var/list/turfs_cared_about = list()
turf/New()
..()
turfs_cared_about += src
proc/lets_check()
for(var/turf/t in turfs_cared_about)
world << "found [t]"
Best response
Below allows you to store the turfs wanted in specific lists to their specified Z level.

var/global/Get_Turfs/Turf_Datum=new/Get_Turfs
var/global/List_of_turfs=list()

Get_Turfs
proc
Add_Turfs(atom/PATH,at)
. = list()
for(var/atom/item in block(locate(1,1,at),locate(world.maxx,world.maxy,at)))
if(istype(item,PATH))
. += item
global.List_of_turfs["[at]"] = .
Clear_Turfs(at)
List_of_turfs["[at]"] = list()

world
New()
..()
global.Turf_Datum.Add_Turfs(/turf/Grass,1) //Adds all the Grass turfs to z-level 1 list.
global.Turf_Datum.Add_Turfs(/turf/Grass,2) //Adds all the Grass turfs to z-level 2 list.
//global.Turf_Datum.Clear_Turfs(1) //Removes all the items in this z-level list.


//TEST BELOW (You'll need a map with 2 Z-levels)

mob/verb/Test_1() //10 grass turfs
for(var/atom/A in global.List_of_turfs["1"]) //Items in Z-level 1
world<<"[A.name]"
mob/verb/Test_2() //05 grass turfs
for(var/atom/A in global.List_of_turfs["2"]) //Items in Z-level 2
world<<"[A.name]"


turf
Grass
In response to Kozuma3
Why did you use a datum for those two procs?
In response to Albro1
Albro1 wrote:
Why did you use a datum for those two procs?

For future use, if anything is needed.
I end up with this code:

[code] set background = 1
var/global/list/tiles = list()

for(var/turf/space/T in world)
if(T.z == 1)
tiles.Add(T)
T.icon = 'poop.dmi'[/code]
It works just fine and doesn't seem to lag in the slightest, so I think I'm going to stick to the easy and small code here, but your help was appreciated. And I did get to learn how others would do this. So thank you.
Galactic Soldier wrote:
I have a few points to make:
  • You're using global completely wrong in some instances. You either use it to access the global scope, or you use it to make a variable outside of the global scope a global variable; not one already in the global scope.
  • Specifying the type argument in the new procedure after it's already in its prototype is also wrong and results in it being initiated slower.
  • I'm not sure if this is what he even asked for.
  • Using datums as a namespace in this context is a waste.
  • You're assigning a default return value for something by your implementation should return void

I made a better alternative based off your schematic:
var/zCycle/zCycle = new
>
> zCycle
> var
> global/turfs[world.maxz]
>
> proc
>
> AddTurfs(path, zIndex)
> // I guess you wanted it to return the added paths? Okay, I'll leave it.
> . = new/list
>
> for(var/atom/A in block(locate(1, 1, zIndex), locate(world.maxx, world.maxy, zIndex))) if(istype(A, path))
> . += A
>
> src.turfs[zIndex] |= .
>
> ClearTurfs(zIndex)
> src.turfs[zIndex] = new
>
> // Demo:
>
> world/New()
>
> global.zCycle.AddTurfs(/turf/grass, 1)
> global.zCycle.AddTurfs(/turf/grass, 2)
> ..()
>
> turf/grass
>
> mob/verb/Test_1()
>
> for(var/atom/A in global.zCycle.turfs[1])
> world << A


Doesn't work, please test it before posting.
After bench-marking both of them, I see no difference.

                        Profile results (total time)
Proc Name Self CPU Total CPU Real Time Calls
------------------------- --------- --------- --------- ---------
/mob/verb/Test_1 0.000 0.403 0.403 1
/Get_Turfs/proc/Add_Turfs 0.403 0.403 0.403 1

Profile results (total time)
Proc Name Self CPU Total CPU Real Time Calls
--------------------- --------- --------- --------- ---------
/mob/verb/Test_1 0.000 0.406 0.406 1
/zCycle/proc/AddTurfs 0.406 0.406 0.406 1
I'm doing this logically, by increasing the map_size and such.
There isn't a point calling it over and over 100,000 times.
I think you're doing something wrong ^^.



And I don't think listening to you would help me approve seeing as how you gave bad code right from the beginning.
50 - 70 ms across 100,000 calls isn't even worth even quibbling over, when the total magnitude of this is still measured in sub-one second for said 100,000 calls.

What would make more sense, if you are going to get pedantic, is to discuss maintainability, and readability.

Truthfully, though? Whatever! Really, you could do either solution and that would be just fine and dandy. OP can use either solution, quite happily.
Galactic Soldier wrote:
Sorry, I misread. But mine appendages the list, while yours creates an entire new one for no explicit reasoning. Of course mine would perform a little slower but it'd give intended results, unlike yours.

They do the exact same thing - are you sure you tested mine?

Galactic Soldier wrote:
Kozuma3 wrote:
Galactic Soldier wrote:
Sorry, I misread. But mine appendages the list, while yours creates an entire new one for no explicit reasoning. Of course mine would perform a little slower but it'd give intended results, unlike yours.

They do the exact same thing - are you sure you tested mine?

Haha, no it wouldn't. I use the | operator to appendage the lists. You create a whole new one each time Add_Turfs is called. Perhaps you didn't examine your code close enough; no wonder I thought it was silly.

Silly? That's your opinion, Please continue learning. ^^
Galactic Soldier wrote:
Kozuma3 wrote:
Galactic Soldier wrote:
Kozuma3 wrote:
Galactic Soldier wrote:
Sorry, I misread. But mine appendages the list, while yours creates an entire new one for no explicit reasoning. Of course mine would perform a little slower but it'd give intended results, unlike yours.

They do the exact same thing - are you sure you tested mine?

Haha, no it wouldn't. I use the | operator to appendage the lists. You create a whole new one each time Add_Turfs is called. Perhaps you didn't examine your code close enough; no wonder I thought it was silly.

Silly? That's your opinion, Please continue learning. ^^

It is silly. You included a clear list procedure when your list gets replaced instead of added onto every time you call the Add_Turfs() procedure. For what purpose would that be included in terms of its functionality? Absolutely none.

I guess you could remove it then ^^, it's commented out after all.


I gave you advice. Take it and learn from it.

Poor "advice" at that.

Can I ask you two to take this discussion offline or to your pager? This back and forth has long stopped being a useful thing for the topic it's in.
Galactic Soldier wrote:
It serves functionality in the code I provided, but not yours. Poor advice isn't advice that serves as an improvement.

Remove the simple comment on it then so you'll stop ranting.

I really think you should consult the DM Guide, you used things out of intended context like using the global modifier in the global scope. Absolutely redundant.

It helps for readability.

Anything else you want to go on about?
In response to Stephen001
Stephen001 wrote:
Can I ask you two to take this discussion offline or to your pager? This back and forth has long stopped being a useful thing for the topic it's in.

Will do.