• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[WIP] Octree

Status
Not open for further replies.
Level 33
Joined
Apr 24, 2012
Messages
5,113
This is a WIP to the vJASS Octree
JASS:
library Octree uses Table
    struct Octree extends array
        private Table root
        
        private static thistype temp
        private static thistype ic = 0
        private static thistype array r
        
        private static integer size
        private static integer rx
        private static integer ry
        private static integer rz
        
        private static method allocate takes nothing returns thistype
            local thistype this = r[0]
            if 0 == this then
                set this = ic + 1
                set ic = this
            else
                set r[0] = r[this]
            endif
            return this
        endmethod
        
        static method new takes nothing returns thistype
            local thistype this = allocate()
            set root = Table.create()
            set root[-1] = 1 // size
            set root[-2] = 0 // x
            set root[-3] = 0 // y
            set root[-4] = 0 // z
            return this
        endmethod
        
        method expand takes integer x, integer y, integer z returns nothing
            local Table troot
            local boolean xpos
            local boolean xneg
            local boolean ypos
            local boolean yneg
            local boolean zpos
            local boolean zneg
            local integer i
            local Table node
            if this == 0 then
                set this = temp
            endif
            set troot = root
            set size = troot[-1]
            set rx = x - troot[-2]
            set ry = y - troot[-3]
            set rz = z - troot[-4]
            
            set xpos = rx >= size
            set ypos = ry >= size
            set zpos = rz >= size
            
            set xneg = rx < -size
            set yneg = ry < -size
            set zneg = ry < -size
            
            if (xpos or ypos or zpos or xneg or yneg or zneg) then
                set node = Table.create()
                set node[-1] = size*2
                set node[-2] = 0
                set node[-3] = 0
                set node[-4] = 0
                set i = 0
                
                if rx >= 0 then
                    set node[-2] = troot[-2] + size
                endif
                if ry >= 0 then
                    set node[-3] = troot[-3] + size
                endif
                if rz >= 0 then
                    set node[-4] = troot[-4] + size
                endif
                
                if rx < 0 then
                    set node[-2] = troot[-2] - size
                    set i = i + 1
                endif
                if ry < 0 then
                    set node[-3] = troot[-3] - size
                    set i = i + 2
                endif
                if rz < 0 then
                    set node[-4] = troot[-4] - size
                    set i = i + 4
                endif
                
                set node[i] = node
                
                set troot[-1] = 0
                set troot[-2] = 0
                set troot[-3] = 0
                set troot[-4] = 0
                set root = node
                
                set temp = this
                call expand.execute(x, y, z)
            endif
        endmethod
        
        method get takes integer x, integer y, integer z returns integer
            local Table node
            local integer i
            call expand(x, y, z)
            set node = root
            set size = node[-1]
            set rx = x - node[-2]
            set ry = y - node[-3]
            set rz = z - node[-4]
            loop
                set size = size/2
                set i = 0
                
                if rx >= 0 then
                    set i = i + 1
                    set rx = rx - size
                else
                    set rx = rx + size
                endif
                if ry >= 0 then
                    set i = i + 2
                    set ry = ry - size
                else
                    set ry = ry + size
                endif
                if rz >= 0 then
                    set i = i + 4
                    set rz = rz - size
                else
                    set rz = rz + size
                endif
            endloop
            return 0
        endmethod
                
    endstruct
endlibrary
 
Level 7
Joined
Apr 5, 2011
Messages
245
set size = troot[-1]
set rx = x - troot[-2]
set ry = y - troot[-3]
set rz = z - troot[-4]

set xpos = rx >= size
set ypos = ry >= size
set zpos = rz >= size

set xneg = rx < -size
set yneg = ry < -size
set zneg = ry < -size

if (xpos or ypos or zpos or xneg or yneg or zneg) then
set node = Table.create()
set node[-1] = size*2
set node[-2] = 0
set node[-3] = 0
set node[-4] = 0
set i = 0

if rx >= 0 then
set node[-2] = troot[-2] + size
endif
if ry >= 0 then
set node[-3] = troot[-3] + size
endif
if rz >= 0 then
set node[-4] = troot[-4] + size
endif

if rx < 0 then
set node[-2] = troot[-2] - size
set i = i + 1
endif
if ry < 0 then
set node[-3] = troot[-3] - size
set i = i + 2
endif
if rz < 0 then
set node[-4] = troot[-4] - size
set i = i + 4
endif
These conditions procing are kinda lame
First, comparison x - troot[bla] > troot[bla2] is supposed to be slower than x > troot[bla] + troot[bla2] (Though the difference is very little, but there is no sense to go worse way)
Second, repetitive conditions. For example, rx >= 0, then rx < 0.
Or rx >= size => rx >= 0, you don't consider this.
Dunno about the other part, lazy to read, but this one looks too much fast-made.
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
I remember Warcraft's engine stops the execution of recursive functions(limit op?). It won't stop it if you put sleeps on it though.
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
May I ask what kind of recursion this is? I haven't been here for a long time and I'm afraid that terminologies might have changed. Thanks.

EDIT: Disregard my question, I found the answer.
 
This is not a submission, I can't graveyard arbitrary threads.

This is in the lab _because_ you don't want to work on it anymore or because you don't know how to complete it.

You can say it's a WiP that you're no longer willing to maintain or fix in the first post and someone might come, see what you did, and either continue from where you left off, use it as a reference or rewrite it from scratch.

It's a lab, no formal submissions are required here :p
 
Status
Not open for further replies.
Top