|
楼主 |
发表于 2009-1-8 10:08:35
|
显示全部楼层
[jass]
//*******************************************************************************
//* *
//* D A M A G E D E T E C T I O N *
//* Actual Code *
//* v1.06 *
//* *
//* By: Rising_Dusk *
//* *
//*******************************************************************************
library DamageSystem initializer DamageSystemInit
globals
//*********************************************************
//* These are the configuration constants for the system
//*
//* DummyUnitID: What the dummy unit raw ID is for your map
//* DamageTypeCount: The number of damage types declared in the system
//*
private constant integer DummyUnitID = 'hfoo'
private integer DamageTypeCount = 3
//*********************************************************
//* These are the constant globals for ease of use
//*
constant integer DAMAGE_TYPE_ATTACK = 0
constant integer DAMAGE_TYPE_SPELL = 1
constant integer DAMAGE_TYPE_EXTRA = 2
//* To add new constants, simply follow the naming convention and increment
//* the number accordingly. You shouldn't change or remove the first of the
//* damage types, since it is a special case, but you can change the others
//* however you'd like.
//*********************************************************
//* These are static constants used by the system and shouldn't be changed
//*
//* RunTrigger: The trigger that runs on the damage event
//* AddTrigger: The trigger that adds the damage event
//* Count: Counts how many registrations exist
//* Cache: The game cache used by the system
//*
//* Trg: The trigger array with acts/conds to fire
//* TrgCount: Arbitrary maximum of how many events will be
//* registered at once. This is only used during
//* initialization and making this arbitrarily large
//* (But under 8191) is not problematic.
//*
public trigger RunTrigger = CreateTrigger()
public trigger AddTrigger = CreateTrigger()
private integer Count = 0
private gamecache Cache = null
private trigger array Trg
private constant integer TrgCount = 256
//* Temporary variables used by the system
private unit DamageSource = null
private unit DamageTarget = null
private integer DamageType = 0
private real Damage = 0.
endglobals
//******************************************************************************
//******************************************************************************
private function H2I takes handle h returns integer
return h
return 0
endfunction
//******************************************************************************
//******************************************************************************
//* The new damage function used by the system
function UnitDamageTargetEx takes unit source, unit target, real damage, attacktype attackType, integer damageType, boolean ConsiderArmor returns boolean
local boolean b = false
set DamageType = damageType
set DamageSource = source
if ConsiderArmor then
set b = UnitDamageTarget(source, target, damage, false, false, attackType, DAMAGE_TYPE_NORMAL, null)
else
set b = UnitDamageTarget(source, target, damage, false, false, attackType, DAMAGE_TYPE_UNIVERSAL, null)
endif
if not b then
set DamageType = 0
set DamageSource = null
endif
return b
endfunction
//* The method by which one registers a trigger with the system
function TriggerRegisterDamageEvent takes trigger trg returns boolean
if trg == null then
return false
endif
set Trg[Count] = trg
call StoreInteger(Cache, I2S(H2I(trg)), SCOPE_PREFIX+"Index", Count)
set Count = Count + 1
return true
endfunction
//* The method by which one unregisters a trigger from the system
function TriggerUnregisterDamageEvent takes trigger trg returns boolean
local integer i = 0
if trg == null then
return false
endif
set i = GetStoredInteger(Cache, I2S(H2I(trg)), SCOPE_PREFIX+"Index")
if trg != Trg then
return false
endif
set Trg = Trg[Count]
call StoreInteger(Cache, I2S(H2I(Trg)), SCOPE_PREFIX+"Index", i)
set Count = Count - 1
return true
endfunction
//* Initialization shorthand to register a new damage type externally
function RegisterDamageType takes nothing returns integer
local integer i = DamageTypeCount
set DamageTypeCount = DamageTypeCount + 1
return i
endfunction
//******************************************************************************
//******************************************************************************
//* Wrappers for the system that can get inlined anyways
function GetTriggerDamageType takes nothing returns integer
return DamageType
endfunction
function GetTriggerDamageSource takes nothing returns unit
return DamageSource
endfunction
function GetTriggerDamageTarget takes nothing returns unit
return DamageTarget
endfunction
function GetTriggerDamage takes nothing returns real
return Damage
endfunction
//******************************************************************************
//******************************************************************************
private function RunConditions takes nothing returns boolean
//* The conditions for what must be true for damage detection to run
return GetEventDamage() >= 0.0001
endfunction
private function AddConditions takes nothing returns boolean
//* The conditions for registering a unit with the damage system
return true
endfunction
private function PreloadConditions takes unit u returns boolean
//* The conditions for preloading a unit to the damage system
return true
endfunction
//******************************************************************************
//******************************************************************************
private function Run takes nothing returns nothing
local unit u = GetEventDamageSource()
local unit s = DamageSource
local unit t = GetTriggerUnit()
local integer i = 0
local integer d = DamageType
local real r = GetEventDamage()
if DamageSource == null then
set d = DAMAGE_TYPE_ATTACK
set s = u
endif
loop
exitwhen i > Count
set Damage = r
set DamageTarget = t
set DamageSource = s
set DamageType = d
if IsTriggerEnabled(Trg) and TriggerEvaluate(Trg) then
call TriggerExecute(Trg)
endif
set i = i + 1
endloop
set Damage = 0.
set DamageTarget = null
set DamageSource = null
set DamageType = 0
set u = null
set s = null
set t = null
endfunction
private function Load takes nothing returns nothing
call TriggerRegisterUnitEvent(RunTrigger, GetEnteringUnit(), EVENT_UNIT_DAMAGED)
endfunction
//******************************************************************************
//******************************************************************************
private function PreloadUnits takes nothing returns boolean
if PreloadConditions(GetFilterUnit()) then
call TriggerRegisterUnitEvent(RunTrigger, GetFilterUnit(), EVENT_UNIT_DAMAGED)
endif
return false
endfunction
private function DamageSystemInit takes nothing returns nothing
local rect r = GetWorldBounds()
local region re = CreateRegion()
local boolexpr b = Condition(function PreloadUnits)
local group g = CreateGroup()
local integer i = 0
call FlushGameCache(InitGameCache("GameCache.x"))
set Cache = InitGameCache("GameCache.x")
set RunTrigger = CreateTrigger()
call TriggerAddAction(RunTrigger, function Run)
call TriggerAddCondition(RunTrigger, Condition(function RunConditions))
call GroupEnumUnitsInRect(g, r, b)
set AddTrigger = CreateTrigger()
call RegionAddRect(re, r)
call TriggerRegisterEnterRegion(AddTrigger, re, null)
call TriggerAddAction(AddTrigger, function Load)
call TriggerAddCondition(AddTrigger, Condition(function AddConditions))
loop
exitwhen i > TrgCount
set Trg = null
set i = i + 1
endloop
call RemoveRect(r)
call DestroyGroup(g)
call DestroyBoolExpr(b)
set re = null
set g = null
set b = null
set r = null
endfunction
endlibrary
[/jass]
一个伤害检测引擎 |
|