// @name SimpleRP API
// @author: Motive
// @website: www.roleplayershub.com
// @email: trelorn@gmail.com
// ---------------------------------
// Needed vars:
// Array Size 12 boolean udg_IsAdmin Default: False
// Single string udg_ShortMapName Default: "SimpleRP API"
// Single string udg_prefix Default: "@"
// Single unittype udg_SpawnerType
globals
string udg_ShortMapName = "SimpleRP API"
string udg_prefix = "@"
boolean array udg_IsAdmin
unittype udg_SpawnerType
endglobals
// void RP_SendErrorMessageToPlayer(player p, string message)
// Sends an error message to a player.
function RP_SendErrorMessageToPlayer takes player p, string message returns nothing
local string send = "|cff0000" + message
call DisplayTextToPlayer(p, 0, 0, send)
endfunction
// bool RP_IsAdmin(player p)
// Returns true/false, depending on whether or not a player is an admin
function RP_IsAdmin takes player p returns boolean
local integer id = GetPlayerId(p)
return udg_IsAdmin[id]
endfunction
// int RP_FindString(string haystack, string needle)
// Returns the position of the needle in the haystack.
function RP_FindString takes string haystack, string needle, integer min returns integer
local integer hlength = StringLength(haystack)
local integer nlength = StringLength(needle)
local integer curpos = min
local integer result = -2
local string temp = ""
loop
set temp = SubString(haystack, curpos, curpos + nlength)
if temp == needle then
set result = curpos
else
if curpos == hlength then
set result = -1
endif
endif
set curpos = curpos + 1
exitwhen result != -2
endloop
return result
endfunction
// private bool RP_GetValidSelectedUnitsPair()
function RP_GetValidSelectedUnitsPair takes nothing returns boolean
if GetOwningPlayer(GetEnumUnit()) != GetTriggerPlayer() then
return false
endif
if IsUnitType(GetEnumUnit(), udg_SpawnerType) then
return false
endif
return true
endfunction
// group RP_GetValidSelectedUnits(plr)
function RP_GetValidSelectedUnits takes player plr returns group
local group units = GetUnitsOfPlayerMatching(plr, Condition(function RP_GetValidSelectedUnitsPair))
return units
endfunction
// private void RP_HandleUnitKillCommandPair()
function RP_HandleUnitKillCommandPair takes nothing returns nothing
call KillUnit(GetEnumUnit())
endfunction
// void RP_HandleUnitKillCommand(plr)
function RP_HandleUnitKillCommand takes player plr returns nothing
local group units = RP_GetValidSelectedUnits(plr)
call ForGroupBJ(units, function RP_HandleUnitKillCommandPair)
endfunction
// void RP_HandleUnitCommands(player plr, string message)
// Handles commands dealing with the "unit" command type
function RP_HandleUnitCommands takes player plr, string message returns nothing
// BEGIN PARAMETERS TEMPLATE
local string command
local integer array spaceLocations
local string array parameters
local string tstring
local integer counter = 0
local integer temp = 0
local integer tpos = 0
loop
set temp = RP_FindString(message, " ", tpos)
if temp > 0 then
set spaceLocations[counter] = temp
set counter = counter + 1
endif
set tpos = tpos + temp
exitwhen temp < 0
endloop
set temp = 0
set tpos = 0
loop
set tstring = SubString(message, tpos, spaceLocations[temp]-1)
set tpos = spaceLocations[temp]+1
set parameters[temp] = tstring
set temp = temp + 1
exitwhen temp >= counter
endloop
// END PAREMETERS TEMPLATE
if parameters[0] == "kill" then
endif
endfunction
// void RP_HandleChatMessage(player plr, string message)
// Handles every chat message input by the player.
function RP_HandleChatMessage takes player plr, string message returns nothing
local integer spaceLocation1
local string commandtype
local string firstChar = SubString(message, 1, 1)
// Check for command prefix: @
if firstChar != udg_prefix then
return
endif
// Get the space after the command prefix: @unit" " kill
// if we're missing a space, then it's not a valid command.
set spaceLocation1 = RP_FindString(message, " ", 0)
if spaceLocation1 < 0 then
call RP_SendErrorMessageToPlayer(plr, "Invalid command.")
return
endif
// Get the command type. Ex: @"unit" kill
// Terminate execution on calling the specific handler
set commandtype = SubString(message, 2, spaceLocation1 - 1)
if commandtype == "unit" then
call RP_HandleUnitCommands(plr, SubString(message, spaceLocation1 +1, StringLength(message)))
return
endif
// We're lacking a valid command type :(
call RP_SendErrorMessageToPlayer(plr, udg_ShortMapName + " has no \"" + commandtype + "\" command type.")
endfunction