A Position's Sector


Sometimes you'll need to find the sector of a given position. This can be done is MotS with FindSectorAtPos(), but there is no easy way to do it in JK. The cog below shows a way that it can be done.

First, this cog creates a ghost at the given position. Then all the adjoins are turned off. The cog loops through all of the sectors, and when the cog finds a sector in which the position is closer to the sector's center than one of the vertices, the cog creates a thing at the center of the sector.

Then the cog checks to see if the original ghost can see the ghost at the center of the sector. If it can, then this is the sector that the position is in. The adjoins are turned off so that one ghost can't see a ghost in another sector.

This cog is in hotkey form and uses the position of the player, but it can easily be modified for other purposes. The ghost template is included in most levels.

# findsector.cog
#
# Find the sector of the player when given only his position.
#
# [GBK + Seifer + SM]
#==============================================================#
symbols

message   activated

vector       pos                   local

template    temp=ghost      local

Int            posGhost           local
Int            secGhost           local
Int            i=0                    local
Int            e=0                   local

thing         player                local

end
#==============================================================#
code
#------------------------------------------------------------------------------
activated:
   player=jkGetLocalPlayer();
   pos=GetThingPos(player);

   posGhost=CreateThingAtPos(temp, 0, pos, '0 0 0');
   for(i=0; i < GetSectorCount(); i=i+1) SetSectorAdjoins(i, 0);

   for(i=0; i < GetSectorCount(); i=i+1)
   {
      for(e=0; e <= GetNumSectorVertices(i); e=e+1)
      {
         If(VectorDist(GetSectorCenter(i), GetSectorVertexPos(i, e)) >= VectorDist(GetSectorCenter(i), pos))
         {
            secGhost=CreateThingAtPos(temp, i, GetSectorCenter(i), '0 0 0');
            If(Haslos(secGhost, posGhost))
            {
               Printint(i);      // Print the sector that we found
               call turnoff;
               Return;
            }
            Destroything(secGhost);
         }
      }
   }

   turnoff:
      For(i=0; i <= GetSectorCount(); i=i+1) SetSectorAdjoins(i, 1);
      DestroyThing(posGhost);
      Destroything(secGhost);

Return;
#------------------------------------------------------------------------------
end

Continue