2017-08-02 23:06:11 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using FFXIVClassic.Common;
|
|
|
|
|
using FFXIVClassic_Map_Server.Actors;
|
|
|
|
|
using FFXIVClassic_Map_Server.packets.send.actor;
|
|
|
|
|
using FFXIVClassic_Map_Server.packets.send.actor.battle;
|
2017-08-26 04:08:26 +01:00
|
|
|
|
using FFXIVClassic_Map_Server.packets.send;
|
2017-08-26 17:39:28 +01:00
|
|
|
|
using FFXIVClassic_Map_Server.utils;
|
2017-08-26 04:08:26 +01:00
|
|
|
|
|
2017-08-02 23:06:11 +01:00
|
|
|
|
namespace FFXIVClassic_Map_Server.actors.chara.ai.state
|
|
|
|
|
{
|
|
|
|
|
class MagicState : State
|
|
|
|
|
{
|
|
|
|
|
|
2017-08-28 04:45:20 +01:00
|
|
|
|
private BattleCommand spell;
|
2017-08-25 03:52:43 +01:00
|
|
|
|
private Vector3 startPos;
|
2017-08-02 23:06:11 +01:00
|
|
|
|
|
|
|
|
|
public MagicState(Character owner, Character target, ushort spellId) :
|
|
|
|
|
base(owner, target)
|
|
|
|
|
{
|
2017-08-28 04:45:20 +01:00
|
|
|
|
this.startPos = owner.GetPosAsVector3();
|
2017-08-02 23:06:11 +01:00
|
|
|
|
this.startTime = DateTime.Now;
|
2017-08-29 01:15:12 +01:00
|
|
|
|
this.spell = Server.GetWorldManager().GetBattleCommand(spellId);
|
2017-08-28 04:45:20 +01:00
|
|
|
|
var returnCode = lua.LuaEngine.CallLuaBattleCommandFunction(owner, spell, "magic", "onMagicPrepare", owner, target, spell);
|
2017-08-02 23:06:11 +01:00
|
|
|
|
|
2018-02-15 13:20:46 -06:00
|
|
|
|
this.target = spell.GetMainTarget(owner, target);
|
|
|
|
|
|
|
|
|
|
if (returnCode == 0 && owner.CanCast(this.target, spell))
|
2017-08-02 23:06:11 +01:00
|
|
|
|
{
|
2017-08-26 04:08:26 +01:00
|
|
|
|
OnStart();
|
2017-08-25 03:52:43 +01:00
|
|
|
|
}
|
2017-08-28 04:45:20 +01:00
|
|
|
|
else
|
2017-08-30 00:14:14 +01:00
|
|
|
|
{
|
|
|
|
|
errorResult = new BattleAction(owner.actorId, 32553, 0);
|
2017-08-25 03:52:43 +01:00
|
|
|
|
interrupt = true;
|
2017-08-02 23:06:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnStart()
|
|
|
|
|
{
|
2017-08-28 04:45:20 +01:00
|
|
|
|
var returnCode = lua.LuaEngine.CallLuaBattleCommandFunction(owner, spell, "magic", "onMagicStart", owner, target, spell);
|
2017-08-02 23:06:11 +01:00
|
|
|
|
|
2017-08-25 03:52:43 +01:00
|
|
|
|
if (returnCode != 0)
|
|
|
|
|
{
|
|
|
|
|
interrupt = true;
|
2017-08-30 00:14:14 +01:00
|
|
|
|
errorResult = new BattleAction(target.actorId, (ushort)(returnCode == -1 ? 32553 : returnCode), 0, 0, 0, 1);
|
2017-08-25 03:52:43 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// todo: check within attack range
|
2017-08-26 17:39:28 +01:00
|
|
|
|
float[] baseCastDuration = { 1.0f, 0.25f };
|
2017-08-25 03:52:43 +01:00
|
|
|
|
|
2018-02-15 13:20:46 -06:00
|
|
|
|
//Check combo stuff here because combos can impact spell cast times
|
2017-08-26 17:39:28 +01:00
|
|
|
|
|
2018-02-15 13:20:46 -06:00
|
|
|
|
float spellSpeed = spell.castTimeMs;
|
|
|
|
|
|
|
|
|
|
//There are no positional spells, so just check onCombo, need to check first because certain spells change aoe type/accuracy
|
|
|
|
|
//If owner is a player and the spell being used is part of the current combo
|
|
|
|
|
if (spell.comboStep == 1 || ((owner is Player p) && (p.playerWork.comboNextCommandId[0] == spell.id || p.playerWork.comboNextCommandId[1] == spell.id)))
|
|
|
|
|
{
|
|
|
|
|
lua.LuaEngine.CallLuaBattleCommandFunction(owner, spell, "magic", "onCombo", owner, target, spell);
|
|
|
|
|
spell.isCombo = true;
|
|
|
|
|
}
|
|
|
|
|
if (!spell.IsInstantCast())
|
2017-08-25 03:52:43 +01:00
|
|
|
|
{
|
2018-02-15 13:20:46 -06:00
|
|
|
|
// command casting duration
|
|
|
|
|
if (owner is Player)
|
|
|
|
|
{
|
|
|
|
|
// todo: modify spellSpeed based on modifiers and stuff
|
|
|
|
|
((Player)owner).SendStartCastbar(spell.id, Utils.UnixTimeStampUTC(DateTime.Now.AddMilliseconds(spellSpeed)));
|
|
|
|
|
}
|
|
|
|
|
owner.SendChant(0xf, 0x0);
|
|
|
|
|
owner.DoBattleAction(spell.id, (uint) 0x6F000000 | spell.castType, new BattleAction(target.actorId, 30128, 1, 0, 1)); //You begin casting (6F000002: BLM, 6F000003: WHM, 0x6F000008: BRD)
|
2017-08-25 03:52:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-02 23:06:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Update(DateTime tick)
|
|
|
|
|
{
|
2017-08-25 03:52:43 +01:00
|
|
|
|
if (spell != null)
|
2017-08-02 23:06:11 +01:00
|
|
|
|
{
|
2017-08-25 03:52:43 +01:00
|
|
|
|
TryInterrupt();
|
2017-08-02 23:06:11 +01:00
|
|
|
|
|
2017-08-25 03:52:43 +01:00
|
|
|
|
if (interrupt)
|
|
|
|
|
{
|
|
|
|
|
OnInterrupt();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo: check weapon delay/haste etc and use that
|
2018-02-15 13:20:46 -06:00
|
|
|
|
var actualCastTime = spell.castTimeMs;
|
2017-08-25 03:52:43 +01:00
|
|
|
|
|
2018-02-15 13:20:46 -06:00
|
|
|
|
if ((tick - startTime).TotalMilliseconds >= spell.castTimeMs)
|
2017-08-25 03:52:43 +01:00
|
|
|
|
{
|
|
|
|
|
OnComplete();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2017-08-02 23:06:11 +01:00
|
|
|
|
}
|
2017-08-25 03:52:43 +01:00
|
|
|
|
return true;
|
2017-08-02 23:06:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnInterrupt()
|
|
|
|
|
{
|
|
|
|
|
// todo: send paralyzed/sleep message etc.
|
2017-08-28 21:45:01 -04:00
|
|
|
|
if (errorResult != null)
|
2017-08-25 03:52:43 +01:00
|
|
|
|
{
|
2017-08-29 22:27:32 -04:00
|
|
|
|
owner.SendChant(0, 0);
|
2017-08-30 00:14:14 +01:00
|
|
|
|
owner.DoBattleAction(spell.id, errorResult.animation, errorResult);
|
|
|
|
|
errorResult = null;
|
2017-08-25 03:52:43 +01:00
|
|
|
|
}
|
2017-08-02 23:06:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnComplete()
|
|
|
|
|
{
|
2018-02-15 13:20:46 -06:00
|
|
|
|
//How do combos/hitdirs work for aoe abilities or does that not matter for aoe?
|
|
|
|
|
HitDirection hitDir = owner.GetHitDirection(target);
|
|
|
|
|
bool hitTarget = false;
|
|
|
|
|
|
2017-08-31 05:56:43 +01:00
|
|
|
|
spell.targetFind.FindWithinArea(target, spell.validTarget, spell.aoeTarget);
|
2017-08-02 23:06:11 +01:00
|
|
|
|
isCompleted = true;
|
2017-08-26 04:08:26 +01:00
|
|
|
|
var targets = spell.targetFind.GetTargets();
|
2018-02-15 13:20:46 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<BattleAction> actions = new List<BattleAction>();
|
|
|
|
|
if (targets.Count > 0)
|
2017-08-25 03:52:43 +01:00
|
|
|
|
{
|
2018-02-15 13:20:46 -06:00
|
|
|
|
List<StatusEffect> effects = owner.statusEffects.GetStatusEffectsByFlag((uint)StatusEffectFlags.ActivateOnSpell);
|
|
|
|
|
|
|
|
|
|
//modify skill based on status effects
|
|
|
|
|
foreach (var effect in effects)
|
|
|
|
|
lua.LuaEngine.CallLuaStatusEffectFunction(owner, effect, "onWeaponSkill", owner, effect, spell);
|
|
|
|
|
|
|
|
|
|
//Now that combos and positionals bonuses are done, we can calculate hits/crits/etc and damage
|
|
|
|
|
foreach (var chara in targets)
|
|
|
|
|
{
|
|
|
|
|
for (int hitNum = 0; hitNum < spell.numHits; hitNum++)
|
|
|
|
|
{
|
|
|
|
|
var action = new BattleAction(chara.actorId, spell.worldMasterTextId, 0, 0, (byte) hitDir, (byte) hitNum);
|
|
|
|
|
lua.LuaEngine.CallLuaBattleCommandFunction(owner, spell, "magic", "onMagicFinish", owner, chara, spell, action);
|
|
|
|
|
|
|
|
|
|
//if hit type isn't evade or miss
|
|
|
|
|
if (action.hitType > HitType.Evade)
|
|
|
|
|
hitTarget = true;
|
|
|
|
|
|
|
|
|
|
actions.AddRange(action.GetAllActions());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//No targets hit, cast failed
|
|
|
|
|
actions.Add(new BattleAction(target.actorId, 30202, (uint) (0)));
|
2017-08-25 03:52:43 +01:00
|
|
|
|
}
|
2017-08-28 21:45:01 -04:00
|
|
|
|
|
2018-02-15 13:20:46 -06:00
|
|
|
|
|
2017-08-31 05:56:43 +01:00
|
|
|
|
// todo: this is fuckin stupid, probably only need *one* error packet, not an error for each action
|
2018-02-15 13:20:46 -06:00
|
|
|
|
BattleAction[] errors = (BattleAction[])actions.ToArray().Clone();
|
|
|
|
|
owner.OnCast(this, actions.ToArray(), spell, ref errors);
|
2017-08-29 00:33:23 -04:00
|
|
|
|
owner.DoBattleAction(spell.id, spell.battleAnimation, actions);
|
2018-02-15 13:20:46 -06:00
|
|
|
|
owner.statusEffects.RemoveStatusEffectsByFlags((uint)StatusEffectFlags.LoseOnCasting);
|
|
|
|
|
//Now that we know if we hit the target we can check if the combo continues
|
|
|
|
|
if (owner is Player player)
|
|
|
|
|
if (spell.isCombo && hitTarget)
|
|
|
|
|
player.SetCombos(spell.comboNextCommandId);
|
|
|
|
|
else
|
|
|
|
|
player.SetCombos();
|
2017-08-02 23:06:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void TryInterrupt()
|
|
|
|
|
{
|
2017-08-25 03:52:43 +01:00
|
|
|
|
if (interrupt)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-02-15 13:20:46 -06:00
|
|
|
|
if (owner.statusEffects.HasStatusEffectsByFlag((uint)StatusEffectFlags.PreventSpell))
|
2017-08-02 23:06:11 +01:00
|
|
|
|
{
|
|
|
|
|
// todo: sometimes paralyze can let you attack, get random percentage of actually letting you attack
|
2018-02-15 13:20:46 -06:00
|
|
|
|
var list = owner.statusEffects.GetStatusEffectsByFlag((uint)StatusEffectFlags.PreventSpell);
|
2017-08-02 23:06:11 +01:00
|
|
|
|
uint effectId = 0;
|
|
|
|
|
if (list.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
// todo: actually check proc rate/random chance of whatever effect
|
|
|
|
|
effectId = list[0].GetStatusEffectId();
|
|
|
|
|
}
|
|
|
|
|
interrupt = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-08-30 00:14:14 +01:00
|
|
|
|
|
|
|
|
|
if (HasMoved())
|
|
|
|
|
{
|
|
|
|
|
errorResult = new BattleAction(owner.actorId, 30211, 0);
|
|
|
|
|
errorResult.animation = 0x7F000002;
|
|
|
|
|
interrupt = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-25 03:52:43 +01:00
|
|
|
|
interrupt = !CanCast();
|
2017-08-02 23:06:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-25 03:52:43 +01:00
|
|
|
|
private bool CanCast()
|
2017-08-02 23:06:11 +01:00
|
|
|
|
{
|
2018-02-15 13:20:46 -06:00
|
|
|
|
return owner.CanCast(target, spell) && spell.IsValidMainTarget(owner, target) && !HasMoved();
|
2017-08-26 04:08:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool HasMoved()
|
|
|
|
|
{
|
2017-08-30 00:14:14 +01:00
|
|
|
|
return (owner.GetPosAsVector3() != startPos);
|
2017-08-02 23:06:11 +01:00
|
|
|
|
}
|
2017-08-26 17:39:28 +01:00
|
|
|
|
|
|
|
|
|
public override void Cleanup()
|
|
|
|
|
{
|
2017-08-31 05:56:43 +01:00
|
|
|
|
owner.SendChant(0, 0);
|
|
|
|
|
|
|
|
|
|
if (owner is Player)
|
|
|
|
|
{
|
2017-08-29 00:38:07 -04:00
|
|
|
|
((Player)owner).SendEndCastbar();
|
|
|
|
|
}
|
2018-02-15 13:20:46 -06:00
|
|
|
|
owner.aiContainer.UpdateLastActionTime(spell.animationDurationSeconds);
|
2017-08-26 17:39:28 +01:00
|
|
|
|
}
|
2017-08-28 04:45:20 +01:00
|
|
|
|
|
|
|
|
|
public BattleCommand GetSpell()
|
|
|
|
|
{
|
|
|
|
|
return spell;
|
|
|
|
|
}
|
2017-08-02 23:06:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|