• Welcome to Vice City Multiplayer.
 

Location Script

Started by Falcon, September 30, 2007, 01:40:39 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Falcon

Location script using hash table instead of ini file.

Create and load hash table

!hmake -s area 2000
if ($exists(vcmp.area.txt)) { !hload area vcmp.area.txt }


Set max locations

var %a = 1
while ($hget(area,%a) != $null) {
   !inc %a
}
set %max_loc %a


Get Players Locations

alias vcmp.area {   
var %a = 1,%b = $hget(vcmp,pos.x. $+ $1),%c = $hget(vcmp,pos.y. $+ $1),%d
while (%a < %max_loc) {
    %d = $hget(area,%a)
    if ($inpoly(%b,%c, [ %d ] )) !return $hget(area,%a $+ .)
    !inc %a
}


Convert old ini to hash

alias convert {
  !hmake -s area 2000
  var %a,%b = 1,%c
  %a = AREA2
  while (%b <= $ini(vcmp.data.ini,%a,0)) {
    %c = $ini(vcmp.data.ini,%a,%b)
    !hadd area %b %c
    !hadd area %b $+ . $readini(vcmp.data.ini,%a,%c)
    !inc %b
  } 
  !hsave area vcmp.area.txt
}


Hope this is useful for some of you guys.

Falcon

bakasan


Juppi

nice script  :D

But how about that table size? I'm not an expert of hash tables, but isn't 2000 quite big size for a location table? couldn't a table of that size contain few million entries...?

bakasan

falcon rocks, of course.  i love this!  i will be adding it to the mirc echo i have been working on.  im pretty impressed with the performance difference hash makes over ini.

a quick note about this code:
just incase you cut and pasted this code only to find your script buggin, this bit:
Quote from: Falcon on September 30, 2007, 01:40:39 PM
Get Players Locations

alias vcmp.area {   
var %a = 1,%b = $hget(vcmp,pos.x. $+ $1),%c = $hget(vcmp,pos.y. $+ $1),%d
while (%a < %max_loc) {
    %d = $hget(area,%a)
    if ($inpoly(%b,%c, [ %d ] )) !return $hget(area,%a $+ .)
    !inc %a
}

was missing a } at the end to close the alias.  it should be this:

alias vcmp.area {   
  var %a = 1,%b = $hget(vcmp,pos.x. $+ $1),%c = $hget(vcmp,pos.y. $+ $1),%d
  while (%a < %max_loc) {
    %d = $hget(area,%a)
    if ($inpoly(%b,%c, [ %d ] )) !return $hget(area,%a $+ .)
    !inc %a
  }
}

bakasan

here is my adaptation of falcons above code(only slightly different):

convert your vcmp.data.ini locations to a hash table and save it

alias convert {
  !hmake -s areatemp 500
  var %b = 1, %c
  while (%b <= $ini(vcmp.data.ini,AREA2,0)) {
    %c = $ini(vcmp.data.ini,AREA2,%b)
    !hadd areatemp %b %c
    !hadd areatemp %b $+ . $readini(vcmp.data.ini,AREA2,%c)
    !inc %b
  }
  !hadd areatemp max_loc %b
  !hsave areatemp vcmp.area.hash
  !hfree -s areatemp
}


create then load the hash table on script start

!hmake -s area 1000
if ($exists(vcmp.area.hash)) { !hload area vcmp.area.hash }


to find an xy coordinate's location name:

alias vcmp.area {   
  if (($1) && ($2)) {
    var %a = 1
    while (%a < $hget(area,max_loc)) {
      if ($inpoly($1,$2, [ $hget(area,%a) ] )) !return $hget(area,%a $+ .)
      !inc %a
    }
  }
  !return Vice City
}


basically the same, except it gets rid of the "Set max locations" step and saves the max_loc value directly into the hash table at the time of conversion so it can be referenced from there.