ScriptUO

Scripting Resources & Utilities => Stealth Client => Stealth archive => Topic started by: mieza on January 14, 2015, 09:25:28 AM

Title: c# scripting problem
Post by: mieza on January 14, 2015, 09:25:28 AM
Code: [Select]
namespace firstMacro
{
    class Program
    {
        static DateTime lastBandage;
        static DateTime nextBandage;
        static double bandageDelay;

        static void Main(string[] args)
        {
            lastBandage = DateTime.Now;
            while (true)
            {
                int hp = Stealth.Script_GetSelfLife();
                int maxHp = Stealth.Script_GetSelfMaxLife();
                if (hp < maxHp)
                {
                    BandageSelf();
                }

            }
        }

        public static void BandageSelf()
        {
            bandageDelay = 5.0 + (0.5 * ((double)(120 - Stealth.Script_GetSelfDex()) / 10));
            nextBandage = lastBandage.AddSeconds(bandageDelay + 0.2); // + 0.2s is just for testing.

            if (nextBandage <= DateTime.Now)
            {
                Stealth.Script_SendTextToUO("[band");   // [band is a server command for bandaging.
                lastBandage = DateTime.Now;
            }
        }
        
    }
}


The problems are;
1- Stealth does'nt get hitpoints and any other stats instantly. (because of this, the script can't start to aid instantly)
2- After aiding successfuly, if hitpoints equals to maxhits, the script starts to wait the delay even if the character gets wounded in that time. (this could be related to script)

Stealth version : 6.0.0
Client version : 6.0.1.10
(if i want to update my stealth version, there becomes lots of problems, its the most stable version for me right now)



Title: Re: c# scripting problem
Post by: Crome969 on January 14, 2015, 10:16:04 AM
A Solution about API was posted here : http://www.scriptuo.com/index.php?topic=11628.msg107053#msg107053

API as it is currently still is a lot of slower (except in delphi) we are working on it..
Title: Re: c# scripting problem
Post by: mieza on January 14, 2015, 11:45:08 AM
Ty for reply. A small question about time-based events. Which way do u prefer in c# if you need to use timers? Can you give an example :)
Title: Re: c# scripting problem
Post by: Orich on February 14, 2015, 11:43:37 AM
Ty for reply. A small question about time-based events. Which way do u prefer in c# if you need to use timers? Can you give an example :)

There is nothing wrong with your method.  You could, however, use the Timer class to clean things up.

Code: [Select]


private Timer _btimer = new Timer(bandageDelay * 1000); // convert seconds to miliseconds
_btimer.AutoReset = true; // make sure it recycles itself
_btimer.Elapsed += new ElapsedEventHandler(bandageSelf);
_btimer.Start();


private static void bandageSelf(object sender, ElapsedEventArgs e)
{
   Stealth.Script_SendTextToUO("[band");
}

Since it looks like what you're doing is buffering bandage cooldowns, you'd need to add the delay checks you're using.

This was just to give you an example of using the class.
Title: Re: c# scripting problem
Post by: unisharp on March 22, 2015, 03:39:28 PM
This works with the latest version of Stealth, ScriptAPI.cs and ScriptDotNet2

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace firstMacro
{
    using ScriptDotNet2;
    using ScriptAPI;

    class Program
    {

        public static void BandageSelf(Item Bandage)
        {
            Bandage.Use();
            Stealth.Default.WaitTargetSelf();
            Console.WriteLine("Bandaging self...");
            Stealth.Default.WaitJournalLine(DateTime.Now, "You finish applying the bandages|You apply the bandages, but they barely help|You heal what little damage your patient had|You did not stay close enough to heal your patient|That is too far away", 15000);
        }

        static void Main(string[] args)
        {
            List<Item> Bandages = Find.FindItems(3617, Self.Backpack.ID);

            if (Bandages.Count == 0)
            {
                Console.WriteLine("No bandages found.");
                Environment.Exit(0);
            }

            Item Bandage = Bandages[0];


            while (Profile.IsConnected)
                if (Self.HP < Self.MaxHP)
                    BandageSelf(Bandage);
        }

    }
}

You can get the latest version of Stealth and ScriptDotNet2 here: http://www.scriptuo.com/index.php?topic=13054.msg108171#msg108171
Latest version of ScriptAPI.cs here: http://www.scriptuo.com/index.php?topic=13054.msg108167#msg108167

It started bandaging as soon as it took any damage and retries until full health.

EDIT: If anyone reads this and sees a way to shorten/improve on code please post so I learn! :)