Author Topic: helpfull function CalculateNearest  (Read 2702 times)

0 Members and 1 Guest are viewing this topic.

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
helpfull function CalculateNearest
« on: May 08, 2007, 07:59:18 am »
Script Name: function CalculateNearest
Script Description: calculates the nearest player next to the chosen ID, depending on some parameters
Original Author(s): Avarax, taken from Hexer mod
Core Version: 2.6.1, compatible with 2.6.0.


Code: [Select]
function CalculateNearest(ID,team,dead: byte): byte;
var X1,Y1,X2,Y2,temp: single;
    i: integer;
begin
  GetPlayerXY(ID,X1,Y1);
  temp:=999999
  result:=0;
  for i:=1 to 32 do
    If (i<>ID) and (((GetPlayerStat(i,'Alive')=true) and (dead = 0)) or ((GetPlayerStat(i,'Alive')=false) and (dead = 1)) or (dead>1)) and (((GetPlayerStat(i,'Team')=team) and (team>0)) or (team=0))  then
    begin
      GetPlayerXY(i,X2,Y2);
        If Distance(X1,Y1,X2,Y2) < temp then
            begin
              temp:=Distance(X1,Y1,X2,Y2);
              result:=i;
            end;
    end;
end;


Parameters:

function CalculateNearest(ID,team,dead: byte): byte;

ID:
Player ID whos coordinates are used to search nearest player

team:
1: Searches for nearest Alpha player
2: Searches for nearest Bravo player
3: Searches for nearest Charlie player
4: Searches for nearest Delta player
0: Searches for nearest player of any team

dead:
0: Searches for alive players only
1: Searches for dead players only
higher than 1: Searches for any players

Returned is the player ID of the nearest player. If no player matching the chosen parameters was found, the function will return 0 as result.
« Last Edit: May 08, 2007, 08:02:18 am by Avarax »
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline Hydro

  • Major(1)
  • Posts: 31
Re: helpfull function CalculateNearest
« Reply #1 on: May 10, 2007, 09:55:43 am »
Hm, I can't make scripts well and i need help.

It is my NetworkCore.pas:
Quote
function CalculateNearest(ID,team,dead: byte): byte;
var X1,Y1,X2,Y2,temp: single;
    i: integer;
begin
  GetPlayerXY(ID,X1,Y1);
  temp:=999999
  result:=0;
  for i:=1 to 32 do
    If (i<>ID) and (((GetPlayerStat(i,'Alive')=true) and (dead = 0)) or ((GetPlayerStat(i,'Alive')=false) and (dead = 1)) or (dead>1)) and (((GetPlayerStat(i,'Team')=team) and (team>0)) or (team=0))  then
    begin
      GetPlayerXY(i,X2,Y2);
        If Distance(X1,Y1,X2,Y2) < temp then
            begin
              temp:=Distance(X1,Y1,X2,Y2);
              result:=i;
            end;
    end;
end;

procedure OnPlayerSpeak(Name,Text: string);
begin
if (Text = '!near') then
CalculateNearest(NameToID(Name),1,2);
End;
Server version 2.6.0
In game it doesnt work. Can you help me?

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: helpfull function CalculateNearest
« Reply #2 on: May 10, 2007, 09:57:51 am »
It works, but you arent actually echoing it anywhere.... do WriteLn(inttostr(CalculateNearest(NameToID(Name),1,2)));

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: helpfull function CalculateNearest
« Reply #3 on: May 10, 2007, 10:41:49 am »
I think you are using an old prototype for the OnPlayerSpeak event. You should do something like this:
Code: [Select]
procedure OnPlayerSpeak(ID: byte; Text: string);
begin
    if Text = '!near' then
        SayToPlayer( ID, IDToName(CalculateNearest(ID, 1, 2)) );
end;
urraka

Offline Hydro

  • Major(1)
  • Posts: 31
Re: helpfull function CalculateNearest
« Reply #4 on: May 10, 2007, 10:48:47 am »
I think you are using an old prototype for the OnPlayerSpeak event. You should do something like this:
Code: [Select]
procedure OnPlayerSpeak(ID: byte; Text: string);
begin
    if Text = '!near' then
        SayToPlayer( ID, IDToName(CalculateNearest(ID, 1, 2)) );
end;
Yeah it works. Thx.