use uo;
include "include/client";

// home base for blue flag
Const bluex := 6119;
Const bluey := 358;

// home base for red flag
Const redx  := 6038;
Const redy  := 358;

program logoff( who )

  print ( "Logoff: " + who.name );
  fixchr(who);
  setskills(who);

  if (who.cmdlevel >0 )
	return;
  endif

Broadcast( "Player " + who.name + " has departed");

endprogram

function setskills(who)
local skill, oldskill, team :=getobjproperty(who, "team" );

if (!team)
return;
endif

for(skill:=0; skill<=48; skill:=skill+1)
  oldskill := cint(Getobjproperty(who, "Skill"+skill));

  if (oldskill=0)
     SetRawSkill( who, skill, 0 );
     eraseobjproperty(who, "Skill"+skill);
  elseif(oldskill)
  SetRawSkill( who, skill, oldskill );
  eraseobjproperty(who, "Skill"+skill);
  endif


endfor

oldskill := Getobjproperty(who, "strength" );

if (oldskill)
who.setstr( oldskill );
who.hp :=oldskill;
eraseobjproperty(who, "strength" );
endif

oldskill := Getobjproperty(who, "dexterity" );

if (oldskill)
who.setdex( oldskill );
who.stamina :=oldskill;
eraseobjproperty(who, "dexterity" );
endif

oldskill := Getobjproperty(who, "intelligence" );

if (oldskill)
who.setint( oldskill );
who.mana :=oldskill;
eraseobjproperty(who, "intelligence" );
endif

local playerhand := GetEquipmentByLayer( who,  LAYER_HAND2 );
local theflag := playerhand.objtype;

if (team="BLUE")
SetGlobalProperty( "ctfblueteam", cint(GetGlobalProperty("ctfblueteam")-1) );
endif

if (team="RED")
SetGlobalProperty( "ctfredteam", cint(GetGlobalProperty("ctfredteam")-1) );
endif

if (theflag = 0x7112)
createItemAtLocation( bluex, bluey, -22, 0x7112, 1 );
endif
if (theflag = 0x7111)
createItemAtLocation( redx, redy, 44, 0x7111, 1 );
endif
eraseobjproperty(who, "ctfkilled");
eraseobjproperty(who, "team" );
eraseobjproperty(who, "ctfclass");
EraseObjProperty(who, "CTFLastHit");
removeall(who);
MoveCharacterToLocation( who, 1417, 1715, 20 );

endfunction

function removeall(who)

local i,item;

for(i:=1;i<=25;i:=i+1)
if ((i<>11) and (i<>16))
item := GetEquipmentByLayer( who, i );
destroyitem(item);
endif
endfor

item := CreateItemAtLocation( who.x, who.y, who.z, 0x0E75, 1 );
EquipItem( who, item );

endfunction


function fixchr(who)

local temp;

  temp := GetObjProperty(who,"realname");
  
if (temp)
	SetName (who,temp);
	EraseObjProperty(who,"realname");
endif

  temp := GetObjProperty(who,"origform");
if (temp)
	who.graphic := temp;
	fixcolor(who);
	EraseObjProperty(who,"origform");
	who.frozen := 0;
endif

// druid spell shapechange
temp := getobjproperty(who, "tn");
if (temp)
   who.name := temp;
   eraseobjproperty(who, "tn");
endif
temp := getobjproperty(who, "tc");
if (temp)
   who.color := temp;
   eraseobjproperty(who, "tc");
endif
temp := getobjproperty(who, "tg");
if (temp)
   who.graphic := temp;
   eraseobjproperty(who, "tg");
endif

endfunction

function fixcolor(who)

   local newcolor, racepicked := getobjproperty(who, "racename");

   if (racepicked="Elf")
	newcolor := 770;
   elseif (racepicked="Dark-Elf")
	newcolor := 33877;
   elseif (racepicked="Goblin")
	newcolor := 34186;
   elseif (racepicked="Barbarian")
	newcolor := 33804;
   elseif (racepicked="Dwarf")
	newcolor := 33888;
   else
	newcolor := who.truecolor;
   endif

   who.color := newcolor;

endfunction
