#ifndef XGsysSDLIncluded
#define XGsysSDLIncluded

#include "XGbase.j"

library XGsysSDL requires XGbase

function Xg_Reg_SDL takes trigger trg returns boolean//注册触发器
local integer i
if LoadBoolean(Xg_htb,GetHandleId(trg),StringHash("闪电链系统"))==true then
return true
endif
call SaveBoolean(Xg_htb,GetHandleId(trg),StringHash("闪电链系统"),true)
set i=LoadInteger(Xg_htb,StringHash("闪电链系统"),StringHash("已注册触发"))
call SaveTriggerHandle(Xg_htb,StringHash("闪电链系统"),i+1,trg)
call SaveInteger(Xg_htb,StringHash("闪电链系统"),StringHash("已注册触发"),i+1)
return false
endfunction
function Xg_SDLsource takes nothing returns unit //获取闪电链来源
return LoadUnitHandle(Xg_htb,GetHandleId(GetTriggeringTrigger())+StringHash("闪电链系统"),StringHash("闪电链来源"))
endfunction
function Xg_SDLunit takes nothing returns unit //获取闪电链单位
return LoadUnitHandle(Xg_htb,GetHandleId(GetTriggeringTrigger())+StringHash("闪电链系统"),StringHash("闪电链单位"))
endfunction
function Xg_SDLval takes nothing returns real //获取闪电链值
return LoadReal(Xg_htb,StringHash("闪电链系统-闪电链值"),GetHandleId(Xg_SDLunit()))
endfunction
function Xg_setSDLval takes real val returns nothing //设置闪电链值
call SaveReal(Xg_htb,StringHash("闪电链系统-闪电链值"),GetHandleId(Xg_SDLunit()),val)
endfunction
function Xg_ExcSysSDL takes unit us,unit ue,real val returns nothing//运行闪电链触发器
local integer i=1
local integer end = LoadInteger(Xg_htb,StringHash("闪电链系统"),StringHash("已注册触发"))
local integer ID=LoadInteger(Xg_htb,StringHash("闪电链系统"),StringHash("ID"))
local integer id
local trigger trg
loop
exitwhen i>end
set trg=LoadTriggerHandle(Xg_htb,StringHash("闪电链系统"),i)
if IsTriggerEnabled(trg)==true then
set id=GetHandleId(trg)+StringHash("闪电链系统")
call SaveUnitHandle(Xg_htb,id,StringHash("闪电链来源"),us)
call SaveUnitHandle(Xg_htb,id,StringHash("闪电链单位"),ue)
call SaveReal(Xg_htb,id,StringHash("闪电链值"),val)
call ConditionalTriggerExecute(trg)
endif
set trg=null
set i=i+1
endloop
endfunction
function Xg_SDLSH takes unit us,unit ue,real hp returns nothing//闪电链指定单位 (闪电链来源,闪电链单位,闪电链量)
local real rs=LoadReal(Xg_htb,GetHandleId(us),StringHash("闪电链提升")) //闪电链来源的提升闪电链量属性
local real re=LoadReal(Xg_htb,GetHandleId(us),StringHash("闪电链增幅")) //闪电链单位的闪电链效果增幅
local real val=0.
set val=hp*(1+rs)
set val=val*(1+re)
call SaveReal(Xg_htb,StringHash("闪电链系统-闪电链值"),GetHandleId(ue),val)
call Xg_ExcSysSDL(us,ue,val)
set val = LoadReal(Xg_htb,StringHash("闪电链系统-闪电链值"),GetHandleId(ue))
if val != 0 then
call UnitDamageTarget( us, ue, val, false, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_DIVINE, WEAPON_TYPE_WHOKNOWS )
endif
endfunction
function Xg_Trig_Group_2_Cont takes nothing returns boolean //敌人
  local unit u=LoadUnitHandle(Xg_htb,GetHandleId(GetExpiredTimer()),StringHash("单位"))
  local unit u2=LoadUnitHandle(Xg_htb,GetHandleId(GetExpiredTimer()),StringHash("单位2"))
  local group g=LoadGroupHandle(Xg_htb,GetHandleId(GetExpiredTimer()),StringHash("闪电链组"))
  local boolean b
  set b= IsUnitEnemy(GetFilterUnit(), GetOwningPlayer(u2)) == true and IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) == false and IsUnitAliveBJ(GetFilterUnit()) == true and GetFilterUnit() !=u and IsUnitInGroup(GetFilterUnit(), g)==false
  set u=null
  set g=null
  return b
endfunction
function Xg_Trig_Group_3_Cont takes nothing returns boolean //盟友
  local unit u=LoadUnitHandle(Xg_htb,GetHandleId(GetExpiredTimer()),StringHash("单位"))
  local unit u2=LoadUnitHandle(Xg_htb,GetHandleId(GetExpiredTimer()),StringHash("单位2"))
  local group g=LoadGroupHandle(Xg_htb,GetHandleId(GetExpiredTimer()),StringHash("闪电链组"))
  local boolean b
  set b= IsUnitAlly(GetFilterUnit(), GetOwningPlayer(u2)) == true and IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) == false and IsUnitAliveBJ(GetFilterUnit()) == true and GetFilterUnit() !=u and IsUnitInGroup(GetFilterUnit(), g)==false
  set u=null
  set g=null
  return b
endfunction
function Xg_timer_SDL takes nothing returns nothing
  local timer t=GetExpiredTimer()
  local integer tnum=LoadInteger(Xg_htb,GetHandleId(t),StringHash("弹射次数"))
  local real hw=LoadReal(Xg_htb,GetHandleId(t),StringHash("闪电链量"))
  local real rds=LoadReal(Xg_htb,GetHandleId(t),StringHash("递减参数"))
  local unit u=LoadUnitHandle(Xg_htb,GetHandleId(t),StringHash("单位"))
  local unit u2=null
  local group g
  local group sdg=LoadGroupHandle(Xg_htb,GetHandleId(t),StringHash("闪电链组"))
  local location p1=GetUnitLoc(u)
  set g=XG_GetUnitsInRangeOfLocMatching(LoadReal(Xg_htb,GetHandleId(t),StringHash("影响范围")), p1, Condition(function Xg_Trig_Group_2_Cont)  )
  if CountUnitsInGroup(g)==0 then
  call DestroyGroup(g)
   if LoadBoolean(Xg_htb,GetHandleId(t),StringHash("弹射友军"))==true then
     set g=XG_GetUnitsInRangeOfLocMatching(LoadReal(Xg_htb,GetHandleId(t),StringHash("影响范围")), p1, Condition(function Xg_Trig_Group_3_Cont))
   endif
  endif
  call RemoveLocation(p1)
  set p1=null
  set u2 = GroupPickRandomUnit(g)
  call DestroyGroup(g)
  set g=null
  if tnum <=0 or u2==null or hw<1 then //判断无弹射次数+无弹射单位+闪电链量过低
  call FlushChildHashtable(Xg_htb,GetHandleId(t))
  call DestroyTimer(t)
  set t=null
  set sdg=null
  return
  endif
  if sdg!=null then
   call GroupAddUnit(sdg, u2)
  endif


call XG_DestroyLtByTimer( 0.75, AddLightningEx(LoadStr(Xg_htb,GetHandleId(t),StringHash("闪电特效")), false, GetUnitX(u), GetUnitY(u), GetUnitFlyHeight(u) ,GetUnitX(u2), GetUnitY(u2), GetUnitFlyHeight(u2)) )

set hw=hw*(1+rds) //闪电链递减
call Xg_SDLSH(LoadUnitHandle(Xg_htb,GetHandleId(t),StringHash("单位2")),u2,hw)

call SaveUnitHandle(Xg_htb,GetHandleId(t),StringHash("单位"),u2)
call SaveReal(Xg_htb,GetHandleId(t),StringHash("闪电链量"),hw)
call SaveInteger(Xg_htb,GetHandleId(t),StringHash("弹射次数"),tnum-1)
  set t=null
  set u2=null
  set sdg=null
endfunction
  //闪电链 :来源单位,指定单位,递减参数,弹射次数,闪电链量,影响范围,是否弹友军,是否重复弹射
function Xg_SDL takes unit us,unit ue,real rds,integer tnum,real hw,real ran,boolean tyj,boolean cfts,string mainlight,string minorlight returns nothing
local timer t

if hw <=0  or tnum<=0 then
return
endif

call XG_DestroyLtByTimer( 0.75, AddLightningEx(mainlight, false, GetUnitX(us), GetUnitY(us), GetUnitFlyHeight(us) ,GetUnitX(ue), GetUnitY(ue), GetUnitFlyHeight(ue)) )
call Xg_SDLSH(us,ue,hw)
if tnum <= 1 then
return
endif
set t=CreateTimer()
call SaveInteger(Xg_htb,GetHandleId(t),StringHash("弹射次数"),tnum-1)
call SaveReal(Xg_htb,GetHandleId(t),StringHash("闪电链量"),hw)
call SaveReal(Xg_htb,GetHandleId(t),StringHash("递减参数"),rds)
call SaveReal(Xg_htb,GetHandleId(t),StringHash("影响范围"),ran)
call SaveBoolean(Xg_htb,GetHandleId(t),StringHash("弹射友军"),tyj)
call SaveStr(Xg_htb,GetHandleId(t),StringHash("闪电特效"),minorlight)
if cfts==false then
call SaveGroupHandle(Xg_htb,GetHandleId(t),StringHash("闪电链组"),CreateGroup())
endif
call SaveUnitHandle(Xg_htb,GetHandleId(t),StringHash("单位"),ue)
call SaveUnitHandle(Xg_htb,GetHandleId(t),StringHash("单位2"),us)
call TimerStart(t,0.2,true,function Xg_timer_SDL)
set t=null
endfunction

endlibrary

#endif
