[Release] Counter kill for CoEmu (Basic version)

04/30/2010 16:27 pro4never#1
Ok so I wrote this ages ago for Hellmouth but I figured I'd throw it out there for a BASIC example of how to do this kinda stuff.

I was tired and really didn't care to fine tune it at all but it works and it works quite well.. so enjoy.

For simplicity sake I made a new .cs file named Handlers>CounterKill.cs

Here's the entirety of it.

PHP Code:
using System;
using System.Collections;
using System.Collections.Generic;
using CoEmu_v2_GameServer.Connections;
using CoEmu_v2_GameServer.Entities;
using CoEmu_v2_GameServer.Structs;
using CoEmu_v2_GameServer.Packets;
using System.Threading;

namespace 
CoEmu_v2_GameServer.Handlers
{
    public 
partial class Handler
    
{
        public class 
CounterKill
        
{
            public static 
void CounterKillSwitch(ClientSocket CSocket)//simply toggled it on/off
            
{

                
#region counter kill
                
if (!CSocket.Client.CounterKillOn)//if it's off
                
{
                    if (
CSocket.Client.CurrentStam >= 100)//and player has 100 stam
                    
{                        
                        
CSocket.Client.CurrentStam -= 100;//remove the stam
                        
CSocket.Send(ConquerPacket.Status(CSocket2CSocket.Client.CurrentStamStruct.StatusTypes.Stamina)); //update client stam
                        
CSocket.Client.CounterKillOn true;//turn on counter kill bool
                        
CSocket.Client.CounterKillTime 10;//how often it can activate (once every 10 sec)
                        
CSocket.Client.CounterKillRate 5;// how often it activates (5 percent)
                        
CSocket.Client.LastCounterKill DateTime.Now;//time when last counter kill activated (now: cannot be used for 10 seconds)
                        
ConquerPacket.ToLocal(ConquerPacket.String(CSocket.Client.ID10"ScapegoatSwitchOpen"), CSocket.Client.XCSocket.Client.Y, (int)CSocket.Client.Map00);//display the counter kill on effect
                        
CSocket.Send(ConquerPacket.Chat(0"System"CSocket.Client.Name"CounterKill activated"Struct.ChatType.Top));//print msg to client. never bothered with the little flag effect in top corner
                        
return;
                    }
                    else
//not enough stam
                        
return;
                }
                else
//counter kill is already on
                
{
                    
CSocket.Client.CounterKillOn false;//turn it off
                    
CSocket.Client.LastCounterKill DateTime.Now;
                    
ConquerPacket.ToLocal(ConquerPacket.String(CSocket.Client.ID10"replacedisappear"), CSocket.Client.XCSocket.Client.Y, (int)CSocket.Client.Map00);//display counter kill off effect
                    
CSocket.Send(ConquerPacket.Chat(0"System"CSocket.Client.Name"CounterKill de-activated"Struct.ChatType.Top));//print msg to client, again no lil flag used. 
                    
return;
                }
                
#endregion
            
}
        }
    }

Note: I added a bunch of commenting for noobs to be able to understand the steps it goes through.

Now you will need some variable in your Entities>Character.cs file.

Code:
public bool CounterKillOn = false;
        public DateTime LastCounterKill;
        public int CounterKillRate = 0;
Now: You will need to handle how to turn it on/off when he skill is used by a player. I did mine directly in the PacketProcessor.cs file.

Search for
Code:
switch (AType)
and somewhere under the handling for melee, magic and bows add...

Code:
 case 37://counter kill
                                    {
                                        Handlers.Handler.CounterKill.CounterKillSwitch(CSocket);
                                        break;
                                    }


You will now need to handle how the actual skill activates. The way I did this was simply by going to any section where a player was attacked (player->player, monster->player, guard->player) and adding these checks...


Here's my code for Monsters attacking players (under monsters.cs in my source)

PHP Code:
if (Attacked.Client.CounterKillOn)
                        {
                            if (
Calculation.PercentSuccess(Attacked.Client.CounterKillRate))
                            {
                                if (
DateTime.Now Attacked.Client.LastCounterKill.AddSeconds(Attacked.Client.CounterKillTime))
                                {
                                    
int DamageReturn Nano.Rand.Next(Attacked.Client.MinAttackAttacked.Client.MaxAttack);
                                    
Calculation.doMonster(this, ((int)DamageReturn 2), 2Attacked);
                                    
Handlers.Handler.DoSpell(Attacked.Client.IDthis.IDthis.Xthis.YAttacked.Client.XAttacked.Client.Ythis.Map6003DamageReturn2);
                                    
Attacked.Client.LastCounterKill DateTime.Now;
                                    goto 
Jump;
                                }
                            }
                        } 

And for simplicity sake, here's mine from Damage.cs (dealing with Player->Players)
PHP Code:
 if (Attacked.CounterKillOn)
                {
                    if (
Calculation.PercentSuccess(Attacked.CounterKillRate))
                    {
                        if (
DateTime.Now Attacked.LastCounterKill.AddSeconds(Attacked.CounterKillTime))
                       {
                            
int DamageReturn Nano.Rand.Next(Attacked.MinAttackAttacked.MaxAttack);
                            
Handlers.Handler.DoSpell(Attacker.IDAttacker.IDAttacker.XAttacker.YAttacker.XAttacker.Y, (int)Attacker.Map6003Damage0);
                            
Calculation.doPlayer(Attacker.OwnerAttacker.Owner, (int)DamageReturn 22);
                            
Attacked.LastCounterKill DateTime.Now;                            
                            
Console.WriteLine("Counter Kill Activated: " Attacked.Name);
                            return 
0;
                        }
                    }
                } 

Anyways: It's all very simple to do but I figured I'd post it cause someone was asking about how to add some reborn skills such as counterkill/reflect so I thought I'd post mine (seeing as Counter Kill for coemu has never been released). There's alot of things that can be improved with this version but it works and that's all that mattered to me at the time.


Also: For fun you can make your gm invincibility command using something like this!

PHP Code:
 case "counter":
                            {
                                if (
Command[1] == "on")
                                {
                                    if (
CSocket.Client.isGM)
                                    {
                                        
CSocket.Client.CounterKillOn true;
                                        
CSocket.Client.CounterKillRate 100;
                                        
CSocket.Client.CounterKillTime 0;
                                        
CSocket.Send(ConquerPacket.Chat(0"SYSTEM"CSocket.Client.Name"Counter kill activated"Struct.ChatType.Top));
                                        
ConquerPacket.ToServer(ConquerPacket.Chat(0"SYSTEM""ALLUSERS"CSocket.Client.Name " Has become invincible! All attackers will be punished"Struct.ChatType.Center), 0);

                                    }
                                }
                                else
                                {
                                    
CSocket.Client.CounterKillOn false;
                                    
CSocket.Client.CounterKillRate 0;
                                    
CSocket.Client.CounterKillTime 1000;
                                    
CSocket.Send(ConquerPacket.Chat(0"SYSTEM"CSocket.Client.Name" Counter kill deactivated!"Struct.ChatType.Top));

                                }
                                break;
                            } 
This will allow you to counter 'every' attack (sometimes it doesn't due to the date time checks but w/e. It's loads of fun for gm's :P)
04/30/2010 16:43 QuickCo#2
awesome release :> +k good job
04/30/2010 17:17 sawickas#3
Thanks pro!
04/30/2010 21:06 copz1337#4
Pretty good example to learn how to do this sort of stuff. Thank you for sharing.
04/30/2010 22:13 Arcо#5
Nice release chris, keep up the good work.