Unit Casting;
{$Region Header}
{
=================================================================
 Script Name: Crome's Casting Unit
 Author: Crome969 AKA Jan Siems
 Version: 3
 Shard OSI / FS: OSI
 Revision Dates: 01\03\2013
 Public Release: 01\03\2013
 Purpose: Unit to handle Casting
 Credits: Endless Night & Trailmyx for Licence

=================================================================
 (c) 2005-2013 Crome696 - All Rights Reserved
  Please respect my property rights.. :)
=================================================================
 Disclaimer:  By downloading, copying, running or otherwise using
 this script, you accept the contents of this limited personal use
 licence agreement.
 This script is only authorized to be distributed on www.Scriptuo.com.
 If you purchase this script, or acquire it from any other source, it
 is not considered an authorized copy and should be deleted immediately.
 Crome969 AKA Jan Siems(Author) provides no warranty for use, function,
 or safe interaction with any software or hardware. User downloads and
 runs this script at his-her own risk. User shall hold Author and the
 distribution site www.scriptuo.com harmless for any loss of items,
 accounts, money, virtual currency or items associated with the use of
 this script. User shall hold the author harmless for any consequences
 that may result from the use of this script as intended.  Furthermore,
 you promise not to distribute or sell this script to anyone without
 the express permission of the author, Crome696 AKA Jan Siems.
 Violation of this agreement will result in one or more of the following:
 litigation, www.scriptuo.com account termination, bad karma for your
 next life, and most definitely causing a black mark placed on your soul.

 This Script will never become by any definition of the words "Abandon ware",
 this script will always remain the property of the author unless
 rights are transfered by auther in writing (not email Writing).

 I worked very hard to bring this script to you, so please use it
 responisibily and respectfully.  If you have any questions, please
 reach me at jsiems87@googlemail.com

 (c) 2005-2013 Crome696 - All Rights Reserved
 Please respect my property rights.. :)
===================================================================
 Additional Licence to Use Agreement: You are free to use this script for not
 for profit personal UO gameplay. You are *NOT* free to repost,
 modify and repost, post on other websites, post as part of a script
 or work you yourself have created, email or transfer to another
 person by any method, or otherwise distribute without written
 permision from the author. This Applies to the script as a whole and
 any given part of the script.
 This license to use can be changed at any time for any reason.
 If in doubt dont do it contact the author.

 Commercial usage of any kind requires written permission from the author.

 (c) 2005-2013 Crome696 - All Rights Reserved
 Please respect my property rights.. :)
===================================================================
===================================================================
 NOTE: My scripts or any derivative of them are *not* for use in other players script!
 EXCEPTION : This Unit can be used to handle & support your own Scripts. 
             Modifying this Unit is probhited without written permission from Author!
===================================================================
===================================================================
}
{$Endregion}
Interface
{$Region Custom Constants}
  Const CastInterruptMessages = '$0007A3A1|$0007AB68|$0007A521|$0007AB66|$0007AB72|$0007AB61';
  (* Cliloc Explanation
  $000F621E 
  $0007A521 = Location is Blocked
  $0007AB68 = The spell fizzles.
  $0007A3A1 = Your concentration is disturbed, thus ruining thy spell.
  $0007AB66 = More reagents are needed for this spell.
  $0007AB72 = You are already casting a spell.
  $0007AB61 = Insufficient mana for this spell.
  *)
{$Endregion}
{$Region Custom Variables}
  var CanCast:TDateTime;
{$Endregion}
{$Region Functions}
  {$Region ComputateFCR}
  Function ComputateFCR(FCR:Integer):Integer;
  {$Endregion}
  {$Region ComputateFC}
  Function ComputateFC(BaseDelay:Integer;FC:Integer;IsProthed:Boolean):Integer;
  {$Endregion}
  {$Region ComputateLMC}
    Function ComputateLMC(CurMana,LMC:Integer):Integer;
  {$Endregion}
  {$Region Cast2Object}
  Function Cast2Object(SpellName:String;TargetID,Delay,FC,FCR,LMC,ReqMana:Integer;IsProth:Boolean):Boolean;
  {$Endregion}
  {$Region Cast2Tile}
  Function Cast2Tile(SpellName:String;X,Y,Z,Delay,FC,FCR,LMC,ReqMana:Integer;IsProth:Boolean):Boolean;
  {$Endregion}
  {$Region Cast2Wait}
  Function Cast2Wait(SpellName:String;Delay,FC,FCR,LMC,ReqMana:Integer;IsProth:Boolean):Boolean;
  {$Endregion}
{$Endregion}
Implementation
{$Region Functions}
  {$Region ComputateFCR}
  Function ComputateFCR(FCR:Integer):Integer;
  begin
    Result := ( 1500 - ( FCR * 250 ) );
  end;
  {$Endregion}
  {$Region ComputateFC}
  Function ComputateFC(BaseDelay:Integer;FC:Integer;IsProthed:Boolean):Integer;
  begin
    Result := BaseDelay;
    if ( IsProthed = True )then begin
      if ( FC < 2 )then begin
        Result := (BaseDelay - ( ( FC - 2 ) *  250 ))
      end;
    end else begin
      Result := (BaseDelay - ( ( FC ) *  250 ));
    end;    
  end;
  {$Endregion}
  {$Region ComputateLMC}
    Function ComputateLMC(CurMana,LMC:Integer):Integer;
    begin
      If (LMC > 40)then begin
        LMC := 40;
      end;
      Result := (CurMana - (( CurMana * LMC ) / 100 ));
    end;
  {$Endregion}
  {$Region Cast2Object}
  Function Cast2Object(SpellName:String;TargetID,Delay,FC,FCR,LMC,ReqMana:Integer;IsProth:Boolean):Boolean;
  var vcast,vrecover,vlmc : Integer;
  var castbreak : Integer;
  var caststate : Boolean;  
  begin
    Result := False;
    caststate := False;
    vrecover := ComputateFCR(FCR);
    vcast := ComputateFC(Delay,FC,IsProth);
    vlmc := ComputateLMC(ReqMana,LMC);
    castbreak := 0;
    If (( Mana >= vlmc ) AND ( Now >= CanCast ))then begin
      Cast(SpellName);
      While ( TargetPresent = False )  do begin
        if ( WaitJournalLine( Now , CastInterruptMessages , 50 ) = True ) then begin
          exit;
        end;
      end;
      TargetToObject(TargetID);
      if (  WaitJournalLine( Now , CastInterruptMessages , 50 ) = True ) then begin
        exit;
      end;
      Result := True;
      CanCast := Now + MsToDateTime(vrecover);
    end; 
  end;
  {$Endregion}
  {$Region Cast2Tile}
   Function Cast2Tile(SpellName:String;X,Y,Z,Delay,FC,FCR,LMC,ReqMana:Integer;IsProth:Boolean):Boolean;
  var vcast,vrecover,vlmc : Integer;
  var castbreak : Integer;
  var caststate : Boolean;  
  begin
    Result := False;
    caststate := False;
    vrecover := ComputateFCR(FCR);
    vcast := ComputateFC(Delay,FC,IsProth);
    vlmc := ComputateLMC(ReqMana,LMC);
    castbreak := 0;
    If (( Mana >= vlmc ) AND ( Now >= CanCast ))then begin
      Cast(SpellName);
      While ( TargetPresent = False )  do begin
        if ( WaitJournalLine( Now , CastInterruptMessages , 50 ) = True ) then begin
          exit;
        end;
      end;
      TargetToXYZ(X,Y,Z);
      if (  WaitJournalLine( Now , CastInterruptMessages , 50 ) = True ) then begin
        exit;
      end;
      Result := True;
      CanCast := Now + MsToDateTime(vrecover); 
    end; 
  end;
  {$Endregion}
  {$Region Cast2Wait}
  Function Cast2Wait(SpellName:String;Delay,FC,FCR,LMC,ReqMana:Integer;IsProth:Boolean):Boolean;
  var vcast,vrecover,vlmc : Integer;
  var castbreak : Integer;
  var caststate : Boolean;
  begin
    Result := False;
    caststate := False;
    vrecover := ComputateFCR(FCR);
    vcast := ComputateFC(Delay,FC,IsProth);
    vlmc := ComputateLMC(ReqMana,LMC);
    castbreak := 0;
    If (( Mana >= vlmc ) AND ( Now >= CanCast ))then begin
      Cast(SpellName);
      while (( castbreak < vcast ) AND ( caststate = False ))do begin
        caststate := WaitJournalLine( Now , CastInterruptMessages , 50 );
        castbreak := castbreak + 50;   
      end;
      if (caststate = False ) then begin
        Result := True;
      end;
      CanCast := Now + MsToDateTime(vrecover); 
    end;
  end;
  {$Endregion}
{$Endregion}
end.