Author Topic: Location Script  (Read 5119 times)

0 Members and 1 Guest are viewing this topic.

Offline Falcon

  • VC:MP Developer
  • Wiseguy
  • *
  • Posts: 73
    • View Profile
Location Script
« on: September 30, 2007, 03:40:39 pm »
Location script using hash table instead of ini file.

Create and load hash table
Code: [Select]
!hmake -s area 2000
if ($exists(vcmp.area.txt)) { !hload area vcmp.area.txt }

Set max locations
Code: [Select]
var %a = 1
while ($hget(area,%a) != $null) {
   !inc %a
}
set %max_loc %a


Get Players Locations
Code: [Select]
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
Code: [Select]
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

Offline bakasan

  • VC:MP Developer
  • Made Man
  • *
  • Posts: 169
    • View Profile
Re: Location Script
« Reply #1 on: September 30, 2007, 04:25:49 pm »
nice work! 8)

Juppi

  • Guest
Re: Location Script
« Reply #2 on: September 30, 2007, 06:40:43 pm »
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...?

Offline bakasan

  • VC:MP Developer
  • Made Man
  • *
  • Posts: 169
    • View Profile
Re: Location Script
« Reply #3 on: November 25, 2007, 12:43:20 am »
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:
Get Players Locations
Code: [Select]
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:
Code: [Select]
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
  }
}

Offline bakasan

  • VC:MP Developer
  • Made Man
  • *
  • Posts: 169
    • View Profile
Re: Location Script
« Reply #4 on: November 25, 2007, 03:03:51 am »
here is my adaptation of falcons above code(only slightly different):

convert your vcmp.data.ini locations to a hash table and save it
Code: [Select]
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
Code: [Select]
!hmake -s area 1000
if ($exists(vcmp.area.hash)) { !hload area vcmp.area.hash }

to find an xy coordinate's location name:
Code: [Select]
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.