ScriptUO

Scripting Resources & Utilities => Stealth Client => Topic started by: smitty on December 07, 2017, 07:32:43 PM

Title: c# and multi-threading
Post by: smitty on December 07, 2017, 07:32:43 PM
im back with another question!
So i have been messing around with multi-threading with a hunter script for stealth. I have an autoHealer thread, and a monsterAttacker thread. Im having problems with the two not working together. If my character starts getting low on health, he will stop attacking the monster(I pause the monster attacker thread), but when i get healed back to full health and resume the monster attacker thread, i start attacking myself ( I have died many times haha). suggestions? Im thinkin multi-threading is not the way to go  :\'(
Title: Re: c# and multi-threading
Post by: BobOzarius on December 07, 2017, 07:35:32 PM
haha Multi threading is always the way to go. What you need to do is show code. You're just doing something incorrectly upon returning to the attacking thread. Show your work. Or the teachers can't help.
Title: Re: c# and multi-threading
Post by: smitty on December 07, 2017, 08:09:33 PM
okidoki
heres the main loop
Code: [Select]
            aHealer.Start();
            mTracker.Start();
            while(hunting)
            {
                //check weight
                if(player.Weight >= player.Maxweight - 10)
                {
                    var gold = Scanner.Find<Item>(typeof(items.gold), player.Backpack.Serial.Value, true);
                    if(gold.Count > 0)
                    {
                        Console.WriteLine("Sending gold...");
                        bos.useBag(gold[0]);
                    }
                }
                if(player.Hits < player.MaxHits)
                {
                    mTracker.Pause();
                    while(player.Hits < player.MaxHits)
                    {
                        //wait
                    }
                    mTracker.Resume();
                }
                if (myPet.Hits < (myPet.MaxHits - 5))
                {
                    mTracker.Pause();
                    while(myPet.Hits <  myPet.MaxHits)
                    {
                        //wait till pet is at fill health
                        player.Cast("heal");
                        player.Targeting.WaitForTarget(1500);
                        player.Targeting.TargetTo(myPet.Serial);

                    }
                    mTracker.Resume();
                }
                //check position
                //if(player.Location.X != startLocX && player.Location.Y != startLocY)
                //{
                //    player.Movement.MoveXY((ushort)startLocX, (ushort)startLocY, true, 1, true);
                //}
            } 

And heres the main part of my monster attacker thread
Code: [Select]
       private void attack(enemy item)
        {
            while(!_cancel && item.Hits > 0)
            {
                if (petKill && item.isAllKilled == false)
                {
                    if (Stealth.Client.ClientTargetResponsePresent() == true)
                        Stealth.Client.CancelTarget();
                    //uses pet
                    //Stealth.Client.SendTextToUO("All kill");
                    player.SendText("All kill", 0);
                    allKillRetical = true;
                    player.Targeting.WaitForTarget(1500);
                    player.Targeting.AutoTargetTo(item.Serial);
                    allKillRetical = false;
                    item.isAllKilled = true;
                }
                if (necroKill && item.isCorpsed == false)
                {
                    if (Stealth.Client.ClientTargetResponsePresent() == true)
                        Stealth.Client.CancelTarget();
                    player.Cast("corpse skin");
                    player.Targeting.WaitForTarget(1500);
                    player.Targeting.TargetTo(item.Serial);
                    item.isCorpsed = true;
                }
                if (mageryKill)
                {
                    if (Stealth.Client.ClientTargetResponsePresent() == true)
                        Stealth.Client.CancelTarget();
                    //uses eBolt
                    player.Cast("fireball");
                    player.Targeting.WaitForTarget(1500);
                    player.Targeting.TargetTo(item.Serial);
                }
            }
        }

and heres the main part of my heal thread

Code: [Select]
        public void HealOnce()
        {
           
            if (Stealth.Client.GetDeadStatus())
            {
                return;
            }
           
            //_ct = new CancellationTokenSource();
            if (player.Hits < player.MaxHits)
            {
                Console.WriteLine($"[Information] healonce");
                if (Stealth.Client.ClientTargetResponsePresent() == true)
                    Stealth.Client.CancelTarget();
            }
            else
            {
                //_sendConsoleMessage?.Invoke("No need in heals");
                return;
            }

            Stealth.Client.ClilocSpeech += Client_ClilocSpeech;

            //=================================================================================================================
            //if using magery
            if(useMageryHeal)
            {
                if(player.MaxMana > 5)
                {
                    player.Cast("heal");
                    player.Targeting.WaitForTarget(1500);
                    player.Targeting.TargetTo(targ.Serial);
                }
                else
                {
                    Console.WriteLine("[Error] Not enough mana!");
                    return;
                }

            }
            //==================================================================================================================
        }

hope this is enough!
Title: Re: c# and multi-threading
Post by: smitty on December 07, 2017, 09:29:10 PM
Ooooo! look at me learnin things!!! I think i have solved the problem with a Mutex. One of the coolest words i have ever heard!
Title: Re: c# and multi-threading
Post by: Crome969 on December 08, 2017, 01:00:13 AM