//****************************************************************************
//SPHERE by : Menasoft ©1997-2007
//www.sphereserver.net
// All SPHERE script files and formats are copyright Menasoft & Partners.
// This file may be freely edited for personal use, but may not be distributed
// in whole or in part, in any format without express written permission from
// Menasoft & Partners.  All donations and contributions
// become the property of Menasoft & Partners.
//****************************************************************************
// FILE LAST UPDATED: 14-March-2007
VERSION=0.56b

// ------------------------------------------------------
[FUNCTION f_townsys_recruitguy]
ref1 = <args>

If (<ref1.type> != t_town_town)
  return 1
elif ((<ref1.masteruid> != <uid>) && (<ref1.memberfromuid.<uid>.priv> != <def0.STONEPRIV_MEMBER>) && (!<isgm>))
  sysmessage @038a Only town members can recruit new players.
  return 1
elif (<argo.isItem>)
  sysmessage @038a You may only recruit players into the town.
  return 1
elif (!<argo.isPlayer>)
  sysmessage @038a You may only recruit players into the town.
  return 1
elif (<argo.flags>&<def0.statf_dead>)
  sysmessage @038A Only the living may be recruited.
  return 1
endif
If (<argo.memoryfindtype.<def0.memory_town>.uid>)
  If (<argo.memoryfindtype.<def0.memory_town>.link> == <ref1.uid>)
    sysmessage @038A They are already a townmember!
  else
    sysmessage @038A You can only recruit candidates who are not already in a town.
  endif
  return 1
endif

If (<ref1.memberfromuid.<argo.uid>.uid> != <argo.uid>)
  If ((<ref1.masteruid> == <uid>) || (<isgm>))
    If (<def0.TOWNSCONFIG_MAXMEMBERS>)
      If (<ref1.member.count <def0.STONEPRIV_MEMBER>> < <def0.TOWNSCONFIG_MAXMEMBERS>)
        f_townsys_addchartotown <ref1.uid>,<argo.uid>,1
      else
        sysmessage @038A There are already <dDEF0.TOWNSCONFIG_MAXMEMBERS> members. You cannot submit a member at this time.
      endif
    else
      f_townsys_addchartotown <ref1.uid>,<argo.uid>,1
    endif
  else
    If (<def0.TOWNSCONFIG_MAXCANDIDATES>)
      If (<ref1.member.count <def0.STONEPRIV_CANDIDATE>> < <def0.TOWNSCONFIG_MAXCANDIDATES>)
        f_townsys_addchartotown <ref1.uid>,<argo.uid>,0
      else
        sysmessage @038A There are already <dDEF0.TOWNSCONFIG_MAXCANDIDATES> candidates awaiting a decision. You cannot submit a candidate at this time.
      endif
    else
      f_townsys_addchartotown <ref1.uid>,<argo.uid>,0
    endif
else
  sysmessage @038A They are already a candidate.
endif

// ------------------------------------------------------
[FUNCTION f_townsys_addchartotown]
// no checks here, everything should be checked before calling this functions
ref1 = <argv[0]> // The town
ref2 = <argv[1]> // The char
local.addasmember = <argv[2]>

if (<local.addasmember>)
  ref1.JoinAsMember <ref2.uid>
else
  ref1.ApplyToJoin <ref2.uid>
endif

if (!<ref2.isevent.<def.TOWNSCONFIG_CHAREVENT_NAME>>)
  ref2.events +<def.TOWNSCONFIG_CHAREVENT_NAME>
endif
  
if (!<ref2.isdspeech.<def.TOWNSCFGSPEECH_NAME>>)
	ref2.dspeech +<def.TOWNSCFGSPEECH_NAME>
endif

// ------------------------------------------------------
[FUNCTION f_townsys_removecharfromtown]
// no checks here, everything should be checked before calling this functions
ref1 = <argv[0]> // The town
ref2 = <argv[1]> // The char

if (!<ref2.isevent.<def.TOWNSCONFIG_CHAREVENT_NAME>>)
  ref2.events -<def.TOWNSCONFIG_CHAREVENT_NAME>
endif

if (<ref2.isdspeech.<def.TOWNSCFGSPEECH_NAME>>)
  ref2.dspeech -<def.TOWNSCFGSPEECH_NAME>
endif

ref1.resign <ref2.uid>
f_townsys_sendsmsg <ref1.uid>,<ref2.name> has resigned from the town.

// ------------------------------------------------------
[FUNCTION f_townsys_isnameused]
RETURN <f_stonesys_isnameused <args>>

// ------------------------------------------------------
[FUNCTION f_townsys_isabbrevused]
RETURN <f_stonesys_isabbrevused <args>>

// ------------------------------------------------------
[FUNCTION f_townsys_isatwarwith]
local.mytown=<argv[0]>
local.othertown=<argv[1]>

If ((<uid.<local.mytown>.type>!=t_stone_town) || (<uid.<local.othertown>.type>!=t_stone_town))
  return 0
endif

If (<uid.<local.mytown>.guildfromuid.<local.othertown>.isenemy> && <uid.<local.othertown>.guildfromuid.<local.mytown>.isenemy>)
  return 1
endif

return 0

// ------------------------------------------------------
[FUNCTION f_townsys_candeclarewarwith]
local.mytown=<argv[0]>
local.othertown=<argv[1]>

If ((<uid.<local.mytown>.type>!=t_stone_town) || (<uid.<local.othertown>.type>!=t_stone_town))
  return 0
endif

local.mytown_count=<uid.<local.mytown>.member.count <def0.STONEPRIV_MEMBER>>
If (<uid.<local.mytown>.masteruid>)
  local.mytown_count += 1
endif

If (<local.mytown_count>==0)
  return 0
endif

local.othertown_count=<uid.<local.othertown>.member.count <def0.STONEPRIV_MEMBER>>
If (<uid.<local.othertown>.masteruid>)
  local.othertown_count += 1
endif

If (<local.othertown_count>==0)
  return 0
endif

// Order cannot declare war.
If ((<uid.<local.mytown>.align>==1) && (<uid.<local.mytown>.align>==<uid.<local.othertown>.align>))
  return 0
endif

return 1


// ------------------------------------------------------
[FUNCTION f_townsys_searchtownname]
local.currentuid=<argv[0]>	// Current town uid
local.textsearch=<argv[1]>	// Text to search in other town names
local.alltowns=0           	// Alltowns check

local.search_found=0
local.search_num=0
local.search_uid=<serv.townstones.<local.search_num>.uid>

If (<def0.TOWNSCONFIG_ALLOW_ALLTOWN_SEARCH>)
    If (!STRCMPI("<local.textsearch>","<def.TOWNSCONFIG_ALLOW_ALLTOWN_TEXT>"))
        local.alltowns=1
    endif
endif

while (<local.search_uid>)
  If (<local.currentuid>!=<local.search_uid>)
    If (<local.alltowns>)
      If (!<local.returndata>)
        local.returndata=<local.search_uid>
      else
        local.returndata .= ,<local.search_uid>
      endif
      local.search_found += 1
    elif (strregex(<local.textsearch>*,<uid.<local.search_uid>.name>))
      If (!<local.returndata>)
        local.returndata=<local.search_uid>
      else
        local.returndata .= ,<local.search_uid>
      endif
      local.search_found += 1
    endif
  endif
  
  local.search_num += 1
  local.search_uid=<serv.townstones.<local.search_num>.uid>
  If (<def0.TOWNSCONFIG_MAX_NSEARCH_RES>)
    If (<local.search_num> >= <def0.TOWNSCONFIG_MAX_NSEARCH_RES>)
      local.search_uid=0 // Break the loop
    endif
  endif
endwhile

If (!<local.search_found>)
  return ""
endif
return <local.returndata>


// ------------------------------------------------------
[FUNCTION f_townsys_listsplit]
local.townlist_count=<argv>
If (<local.townlist_count>)
  For x 0 <eval <local.townlist_count>-1>
    try local.townlist_data<dLOCAL.X>=<argv[<dLOCAL.X>]>
  endfor
endif


// ------------------------------------------------------
[FUNCTION f_townsys_declarewar]
local.firstuid=<argv[0]>
local.seconduid=<argv[1]>

If ((<uid.<local.firstuid>.type>!=t_stone_town) || (<uid.<local.seconduid>.type>!=t_stone_town))
  return 0
endif

ref1 = <local.firstuid>
ref1.declarewar <local.seconduid>

If (<argv[2]> == 1)
  f_townsys_sendsmsg <local.firstuid>,Your town is now at war with <uid.<local.seconduid>.name> <qval (strlen(<uid.<local.seconduid>.abbrev>)) ? [<uid.<local.seconduid>.abbrev>]:>
  f_townsys_sendsmsg <local.seconduid>,Your town is now at war with <uid.<local.firstuid>.name> <qval (strlen(<uid.<local.firstuid>.abbrev>)) ? [<uid.<local.firstuid>.abbrev>]:>
elseif (<argv[2]> == 2)
  f_townsys_sendsmsg <local.firstuid>,Your town has sent a war invitation to <uid.<local.seconduid>.name> <qval (strlen(<uid.<local.seconduid>.abbrev>)) ? [<uid.<local.seconduid>.abbrev>]:>
  f_townsys_sendsmsg <local.seconduid>,Your town has received a war invitation from <uid.<local.firstuid>.name> <qval (strlen(<uid.<local.firstuid>.abbrev>)) ? [<uid.<local.firstuid>.abbrev>]:>
endif

return 1


// ------------------------------------------------------
[FUNCTION f_townsys_declarepeace]
local.firstuid=<argv[0]>
local.seconduid=<argv[1]>

If ((<uid.<local.firstuid>.type>!=t_stone_town) || (<uid.<local.seconduid>.type>!=t_stone_town))
  return 0
endif

ref1 = <local.firstuid>
ref1.declarepeace <local.seconduid>

If (<argv[2]> != 0)
  f_townsys_sendsmsg <local.firstuid>,You are now at peace with <uid.<local.seconduid>.name> [<uid.<local.seconduid>.abbrev>]
  f_townsys_sendsmsg <local.seconduid>,You are now at peace with <uid.<local.firstuid>.name> [<uid.<local.firstuid>.abbrev>]
endif

return 1

// ------------------------------------------------------
[FUNCTION f_townsys_sendsmsg]
f_stonesys_sendsmsg <args>

// ------------------------------------------------------
[FUNCTION f_townsys_speech_sendsmsg]
f_stonesys_speech_sendsmsg <args>

// ------------------------------------------------------
[FUNCTION f_townsys_iswarring]
for ctown 0 <guild.count>
  If ((<guild.<local.ctown>.isenemy>) && (<uid.<guild.<local.ctown>.uid>.guildfromuid.<uid>.isenemy>))
    local.wars += 1
  endif
endfor

return <dlocal.wars>

[EOF]