gzdoom/wadsrc/static/zscript/actor_checks.txt
Christoph Oelckers ac86a535e7 - fixed: State labels were resolved in the calling function's context instead of the called function one's.
This could cause problems with functions that take states as parameters but use them to set them internally instead of passing them through the A_Jump interface back to the caller, like A_Chase or A_LookEx.
This required some quite significant refactoring because the entire state resolution logic had been baked into the compiler which turned out to be a major maintenance problem.
Fixed this by adding a new builtin type 'statelabel'. This is an opaque identifier representing a state, with the actual data either directly encoded into the number for single label state or an index into a state information table.
The state resolution is now the task of the called function as it should always have remained. Note, that this required giving back the 'action' qualifier to most state jumping functions.

- refactored most A_Jump checkers to a two stage setup with a pure checker that returns a boolean and a scripted A_Jump wrapper, for some simpler checks the checker function was entirely omitted and calculated inline in the A_Jump function. It is strongly recommended to use the boolean checkers unless using an inline function invocation in a state as they lead to vastly clearer code and offer more flexibility.

- let Min() and Max() use the OP_MIN and OP_MAX opcodes. Although these were present, these function were implemented using some grossly inefficient branching tests.
- the DECORATE 'state' cast kludge will now actually call ResolveState because a state label is not a state and needs conversion.
2016-11-14 14:12:27 +01:00

274 lines
No EOL
9.2 KiB
Text

extend class Actor
{
//==========================================================================
//
// This file contains all the A_Jump* checker functions, all split up
// into an actual checker and a simple wrapper around ResolveState.
//
//==========================================================================
action state A_JumpIf(bool expression, statelabel label)
{
return expression? ResolveState(label) : null;
}
//==========================================================================
//
//
//
//==========================================================================
action state A_JumpIfHealthLower(int health, statelabel label, int ptr_selector = AAPTR_DEFAULT)
{
Actor aptr = GetPointer(ptr_selector);
return aptr && aptr.health < health? ResolveState(label) : null;
}
//==========================================================================
//
//
//
//==========================================================================
bool CheckIfCloser(Actor targ, float dist, bool noz = false)
{
if (!targ) return false;
return
(Distance2D(target) < dist && (noz ||
((pos.z > targ.pos.z && pos.z - targ.pos.z - targ.height < dist) ||
(pos.z <= targ.pos.z && targ.pos.z - pos.z - height < dist)
)
));
}
action state A_JumpIfCloser(float distance, statelabel label, bool noz = false)
{
Actor targ;
if (player == NULL)
{
targ = target;
}
else
{
// Does the player aim at something that can be shot?
targ = AimTarget();
}
return CheckIfCloser(targ, distance, noz)? ResolveState(label) : null;
}
action state A_JumpIfTracerCloser(float distance, statelabel label, bool noz = false)
{
return CheckIfCloser(tracer, distance, noz)? ResolveState(label) : null;
}
action state A_JumpIfMasterCloser(float distance, statelabel label, bool noz = false)
{
return CheckIfCloser(master, distance, noz)? ResolveState(label) : null;
}
//==========================================================================
//
//
//
//==========================================================================
action state A_JumpIfTargetOutsideMeleeRange(statelabel label)
{
return CheckMeleeRange()? null : ResolveState(label);
}
action state A_JumpIfTargetInsideMeleeRange(statelabel label)
{
return CheckMeleeRange()? ResolveState(label) : null;
}
//==========================================================================
//
//
//
//==========================================================================
native bool CheckInventory(class<Inventory> itemtype, int itemamount, int owner = AAPTR_DEFAULT);
action state A_JumpIfInventory(class<Inventory> itemtype, int itemamount, statelabel label, int owner = AAPTR_DEFAULT)
{
return CheckInventory(itemtype, itemamount, owner)? ResolveState(label) : null;
}
action state A_JumpIfInTargetInventory(class<Inventory> itemtype, int amount, statelabel label, int forward_ptr = AAPTR_DEFAULT)
{
if (target == null) return null;
return target.CheckInventory(itemtype, amount, forward_ptr)? ResolveState(label) : null;
}
//==========================================================================
//
//
//
//==========================================================================
native bool CheckArmorType(name Type, int amount = 1);
action state A_JumpIfArmorType(name Type, statelabel label, int amount = 1)
{
return CheckArmorType(Type, amount)? ResolveState(label) : null;
}
//==========================================================================
//
//
//
//==========================================================================
native bool CheckIfSeen();
native bool CheckSightOrRange(float distance, bool two_dimension = false);
native bool CheckRange(float distance, bool two_dimension = false);
action state A_CheckSight(statelabel label)
{
return CheckIfSeen()? ResolveState(label) : null;
}
action state A_CheckSightOrRange(float distance, statelabel label, bool two_dimension = false)
{
return CheckSightOrRange(distance, two_dimension)? ResolveState(label) : null;
}
action state A_CheckRange(float distance, statelabel label, bool two_dimension = false)
{
return CheckRange(distance, two_dimension)? ResolveState(label) : null;
}
//==========================================================================
//
//
//
//==========================================================================
action state A_CheckFloor(statelabel label)
{
return pos.z <= floorz? ResolveState(label) : null;
}
action state A_CheckCeiling(statelabel label)
{
return pos.z + height >= ceilingz? ResolveState(label) : null;
}
//==========================================================================
//
// since this is deprecated the checker is private.
//
//==========================================================================
private native bool CheckFlag(string flagname, int check_pointer = AAPTR_DEFAULT);
deprecated action state A_CheckFlag(string flagname, statelabel label, int check_pointer = AAPTR_DEFAULT)
{
return CheckFlag(flagname, check_pointer)? ResolveState(label) : null;
}
//==========================================================================
//
//
//
//==========================================================================
native bool PlayerSkinCheck();
action state A_PlayerSkinCheck(statelabel label)
{
return PlayerSkinCheck()? ResolveState(label) : null;
}
//==========================================================================
//
//
//
//==========================================================================
action state A_CheckSpecies(statelabel label, name species = 'none', int ptr = AAPTR_DEFAULT)
{
Actor aptr = GetPointer(ptr);
return aptr && aptr.GetSpecies() == species? ResolveState(label) : null;
}
//==========================================================================
//
//
//
//==========================================================================
native bool CheckLOF(int flags = 0, float range = 0, float minrange = 0, float angle = 0, float pitch = 0, float offsetheight = 0, float offsetwidth = 0, int ptr_target = AAPTR_DEFAULT, float offsetforward = 0);
native bool CheckIfTargetInLOS (float fov = 0, int flags = 0, float dist_max = 0, float dist_close = 0);
native bool CheckIfInTargetLOS (float fov = 0, int flags = 0, float dist_max = 0, float dist_close = 0);
native bool CheckProximity(class<Actor> classname, float distance, int count = 1, int flags = 0, int ptr = AAPTR_DEFAULT);
native bool CheckBlock(int flags = 0, int ptr = AAPTR_DEFAULT, float xofs = 0, float yofs = 0, float zofs = 0, float angle = 0);
action state A_CheckLOF(statelabel label, int flags = 0, float range = 0, float minrange = 0, float angle = 0, float pitch = 0, float offsetheight = 0, float offsetwidth = 0, int ptr_target = AAPTR_DEFAULT, float offsetforward = 0)
{
return CheckLOF(flags, range, minrange, angle, pitch, offsetheight, offsetwidth, ptr_target, offsetforward)? ResolveState(label) : null;
}
action state A_JumpIfTargetInLOS (statelabel label, float fov = 0, int flags = 0, float dist_max = 0, float dist_close = 0)
{
return CheckIfTargetInLOS(fov, flags, dist_max, dist_close)? ResolveState(label) : null;
}
action state A_JumpIfInTargetLOS (statelabel label, float fov = 0, int flags = 0, float dist_max = 0, float dist_close = 0)
{
return CheckIfInTargetLOS(fov, flags, dist_max, dist_close)? ResolveState(label) : null;
}
action state A_CheckProximity(statelabel label, class<Actor> classname, float distance, int count = 1, int flags = 0, int ptr = AAPTR_DEFAULT)
{
// This one was doing some weird stuff that needs to be preserved.
state jumpto = ResolveState(label);
if (!jumpto)
{
if (!(flags & (CPXF_SETTARGET | CPXF_SETMASTER | CPXF_SETTRACER)))
{
return null;
}
}
return CheckProximity(classname, distance, count, flags, ptr)? jumpto : null;
}
action state A_CheckBlock(statelabel label, int flags = 0, int ptr = AAPTR_DEFAULT, float xofs = 0, float yofs = 0, float zofs = 0, float angle = 0)
{
return CheckBlock(flags, ptr, xofs, yofs, zofs, angle)? ResolveState(label) : null;
}
//===========================================================================
// A_JumpIfHigherOrLower
//
// Jumps if a target, master, or tracer is higher or lower than the calling
// actor. Can also specify how much higher/lower the actor needs to be than
// itself. Can also take into account the height of the actor in question,
// depending on which it's checking. This means adding height of the
// calling actor's self if the pointer is higher, or height of the pointer
// if its lower.
//===========================================================================
action state A_JumpIfHigherOrLower(statelabel high, statelabel low, float offsethigh = 0, float offsetlow = 0, bool includeHeight = true, int ptr = AAPTR_TARGET)
{
Actor mobj = GetPointer(ptr);
if (mobj != null && mobj != self) //AAPTR_DEFAULT is completely useless in this regard.
{
if ((high) && (mobj.pos.z > ((includeHeight ? Height : 0) + pos.z + offsethigh)))
{
return ResolveState(high);
}
else if ((low) && (mobj.pos.z + (includeHeight ? mobj.Height : 0)) < (pos.z + offsetlow))
{
return ResolveState(low);
}
}
return null;
}
}