chat does not work .. why ?! work only in local.
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Data;
using System.Linq;
using System.Windows;
using OrbitReborn_Emulator.Specialized;
using OrbitReborn_Emulator.Storage;
using OrbitReborn_Emulator.Game.Sessions;
using OrbitReborn_Emulator.Communication;
using OrbitReborn_Emulator.Communication.Outgoing;
using OrbitReborn_Emulator.Util;
using OrbitReborn_Emulator.Game.Maps;
using OrbitReborn_Emulator.Game.Npcs;
namespace OrbitReborn_Emulator.Game.Event
{
public static class Spaceball
{
private static int mId;
private static bool mActive;
private static int mMMOScore;
private static int mEICScore;
private static int mVRUScore;
private static int mMoveToFirm;
private static int mBallSpeed;
private static int mDamageMMO;
private static int mDamageEIC;
private static int mDamageVRU;
private static int mMostDamage;
private static bool mIsMoving;
private static int mLastMoveToFirm;
private static int mLastBallSpeed;
private static Timer mPerformUpdate;
private static Timer mPathFinding;
private static bool mChangeBallSpeed;
private static bool mChangeMoveToFirm;
public static int BallSpeed
{
get
{
return mBallSpeed;
}
}
public static void Initialize()
{
mActive = false;
mMMOScore = 0;
mEICScore = 0;
mVRUScore = 0;
mMoveToFirm = 0;
mBallSpeed = 0;
}
public static void ShowHud(Session Session)
{
if (mActive)
{
Session.SendData(PacketComposer.Compose("n", "ssi|" + mMMOScore + "|" + mEICScore + "|" + mVRUScore + "|" + mBallSpeed + "|" + mMoveToFirm));
}
}
public static void SendHud()
{
SessionManager.BroadcastToUser(PacketComposer.Compose("n", "ssi|" + mMMOScore + "|" + mEICScore + "|" + mVRUScore + "|" + mBallSpeed +"|" + mMoveToFirm));
}
public static void UpdateScore(int FactionId)
{
if (FactionId == 1)
{
SessionManager.BroadcastToUser(PacketComposer.Compose("n", "ssc|"+FactionId+"|"+mMMOScore));
}
if (FactionId == 2)
{
SessionManager.BroadcastToUser(PacketComposer.Compose("n", "ssc|" + FactionId + "|" + mEICScore));
}
if (FactionId == 3)
{
SessionManager.BroadcastToUser(PacketComposer.Compose("n", "ssc|" + FactionId + "|" + mVRUScore));
}
}
public static void UpdateSpeed(int FactionId)
{
if (FactionId == 1)
{
SessionManager.BroadcastToUser(PacketComposer.Compose("n", "sss|" + FactionId + "|" + mBallSpeed));
}
if (FactionId == 2)
{
SessionManager.BroadcastToUser(PacketComposer.Compose("n", "sss|" + FactionId + "|" + mBallSpeed));
}
if (FactionId == 3)
{
SessionManager.BroadcastToUser(PacketComposer.Compose("n", "sss|" + FactionId + "|" + mBallSpeed));
}
if (FactionId == 0)
{
SessionManager.BroadcastToUser(PacketComposer.Compose("n", "sss|0|" + mBallSpeed));
}
}
public static void ResetBall(Npc Npc)
{
if (mPerformUpdate != null)
{
mPerformUpdate.Dispose();
TimerManager.TimerRunning -= 1;
mPerformUpdate = null;
}
// Try to retrieve Map information
MapInfo Info = MapInfoLoader.GetMapInfo(16);
// Load Map instance into the server memory if needed
if (!MapManager.InstanceIsLoadedForMap(Info.Id))
{
MapManager.TryLoadMapInstance(Info.Id);
}
MapInstance Instance = MapManager.GetInstanceByMapId(16);
if (Instance != null)
{
Instance.BroadcastMessage(PacketComposer.Compose("K", Npc.Id.ToString()));
MapActor Actor = Instance.GetActorByReferenceId(mId, MapActorType.AiBot);
if (Actor != null)
{
Instance.KickNpc(Actor.Id);
}
Npc = NpcManager.CreateNewInstance("Spaceball", 16, 210, 130, 442, 1000, 1000, 1000, 1000, 100);
Instance.AddNpcToMap(Npc);
mId = Npc.Id;
mPerformUpdate = new Timer(PerformUpdate, Npc, 0, 5000);
TimerManager.TimerRunning += 1;
}
}
public static void GiveReward(int FactionId)
{
List<Session> SessionUser = SessionManager.SessionsUser;
using (SqlDatabaseClient MySqlClient = SqlDatabaseManager.GetClient())
{
foreach (Session Session in SessionUser)
{
//Output.WriteLine("iterate");
if (Session.CharacterInfo.FactionId == FactionId)
{
string message = "Spaceball reward : + 10 kills.";
Session.SendData(PacketComposer.Compose("A", "STD|" + message));
Session.CharacterInfo.AddLog(MySqlClient, message);
Session.CharacterInfo.AddKillReward(MySqlClient, 10);
}
}
}
}
public static void CreateSpaceball()
{
if (mPerformUpdate != null)
{
mPerformUpdate.Dispose();
TimerManager.TimerRunning -= 1;
mPerformUpdate = null;
}
mActive = true;
SessionManager.BroadcastToUser(PacketComposer.Compose("n", "ssi|" + mMMOScore + "|" + mEICScore + "|" + mVRUScore + "|" + mBallSpeed + "|" + mMoveToFirm));
MapInfo Info = MapInfoLoader.GetMapInfo(16);
if (!MapManager.InstanceIsLoadedForMap(Info.Id))
{
MapManager.TryLoadMapInstance(Info.Id);
}
MapInstance Instance = MapManager.GetInstanceByMapId(16);
if (Instance != null)
{
Instance.Info.MaxUsers = 20;
Npc Npc = NpcManager.CreateNewInstance("Spaceball", 16, 210, 130, 442, 1000, 1000, 1000, 1000, 100);
Instance.AddNpcToMap(Npc);
mId = Npc.Id;
mPerformUpdate = new Timer(PerformUpdate, Npc, 0, 5000);
TimerManager.TimerRunning += 1;
//Instance.BroadcastMessage(PacketComposer.Compose("C", "-10|1|0||pointer|21000|13000|0|0|0|0|0|0"));
}
}
public static void StopSpaceball()
{
mActive = false;
if (mPerformUpdate != null)
{
mPerformUpdate.Dispose();
TimerManager.TimerRunning -= 1;
mPerformUpdate = null;
}
UpdateSpeed(0);
MapInstance Instance = MapManager.GetInstanceByMapId(16);
if (Instance != null)
{
Instance.Info.MaxUsers = 0;
MapActor Actor = Instance.GetActorByReferenceId(mId, MapActorType.AiBot);
if (Actor != null)
{
Instance.KickNpc(Actor.Id);
}
}
}
public static void DoDamage(int Damage, int FactionId)
{
if (FactionId == 1)
{
mDamageMMO += Damage;
}
if (FactionId == 2)
{
mDamageEIC += Damage;
}
if (FactionId == 3)
{
mDamageVRU += Damage;
}
//Output.WriteLine("dmg : " + Damage);
}
private static void PerformUpdate(object state)
{
Npc Npc = (Npc)state;
MapInstance Instance = MapManager.GetInstanceByMapId(16);
if (Instance == null)
{
return;
}
//Output.WriteLine("npc is moving : " + Npc.IsMoving);
int temp;
int bestfaction;
if (mDamageMMO > mDamageEIC)
{
temp = mDamageMMO;
bestfaction = 1;
}
else
{
temp = mDamageEIC;
bestfaction = 2;
}
if (mDamageVRU > temp)
{
temp = mDamageVRU;
bestfaction = 3;
}
if (mDamageVRU > 0 || mDamageMMO > 0 || mDamageEIC > 0)
{
mMostDamage = temp;
mMoveToFirm = bestfaction;
}
else
{
mMostDamage = 0;
mMoveToFirm = 0;
mBallSpeed = 0;
}
if (mMostDamage <= 10000)
{
mBallSpeed = 0;
}
if (mMostDamage > 8000)
{
mBallSpeed = 1;
}
if (mMostDamage > 4000)
{
mBallSpeed = 2;
}
if (mMostDamage > 1500)
{
mBallSpeed = 3;
}
//Output.WriteLine("ball speed : " + mBallSpeed);
if (mBallSpeed == 0 && mBallSpeed != mLastBallSpeed)
{
if (mPathFinding != null)
{
mPathFinding.Dispose();
TimerManager.TimerRunning -= 1;
mPathFinding = null;
}
Npc.IsMoving = false;
Instance.BroadcastMessage(MapShipMovementComposer.Compose(Npc.Id, Npc.LocX*100, Npc.LocY*100, 1000));
}
if (mMoveToFirm != mLastMoveToFirm)
{
if (mMoveToFirm == 1)
{
Npc.NewLocX = 20;
Npc.NewLocY = 130;
}
if (mMoveToFirm == 2)
{
Npc.NewLocX = 360;
Npc.NewLocY = 20;
}
if (mMoveToFirm == 3)
{
Npc.NewLocX = 360;
Npc.NewLocY = 250;
}
UpdateSpeed(mMoveToFirm);
mChangeMoveToFirm = true;
}
if (mBallSpeed > 0 && mBallSpeed != mLastBallSpeed && mMoveToFirm == mLastMoveToFirm)
{
mChangeBallSpeed = true;
}
if (mBallSpeed != mLastBallSpeed)
{
UpdateSpeed(mMoveToFirm);
}
if (mBallSpeed > 0 && !Npc.IsMoving)
{
if (mPathFinding != null)
{
mPathFinding.Dispose();
TimerManager.TimerRunning -= 1;
mPathFinding = null;
}
Npc.IsMoving = true;
Npc.TimeTaken = 1;
Npc.OldLocX = Npc.LocX;
Npc.OldLocY = Npc.LocY;
if (mMoveToFirm == 1)
{
Npc.NewLocX = 20;
Npc.NewLocY = 130;
}
if (mMoveToFirm == 2)
{
Npc.NewLocX = 360;
Npc.NewLocY = 20;
}
if (mMoveToFirm == 3)
{
Npc.NewLocX = 360;
Npc.NewLocY = 250;
}
int deltaX = Npc.LocX * 100 - Npc.NewLocX * 100;
int deltaY = Npc.LocY * 100 - Npc.NewLocY * 100;
double distance = (Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2)));
double timeTaken = (distance / (Npc.ShipSpeed * mBallSpeed)) * 1000;
timeTaken = timeTaken * 1.03;
Instance.BroadcastMessage(MapShipMovementComposer.Compose(Npc.Id, Npc.NewLocX * 100, Npc.NewLocY * 100, timeTaken));
mPathFinding = new Timer(PathFinding, Npc, 0, 235);
TimerManager.TimerRunning += 1;
}
mLastBallSpeed = mBallSpeed;
mLastMoveToFirm = mMoveToFirm;
//Output.WriteLine("most damage : " + mMostDamage + " from : " + mMoveToFirm);
//Output.WriteLine("npc id : " + Npc.Id);
mDamageMMO = 0;
mDamageEIC = 0;
mDamageVRU = 0;
mMostDamage = 0;
//mBallSpeed = 0;
}
private static void PathFinding(object state)
{
Npc Npc = (Npc)state;
//if (!mActive || mBallSpeed == 0)
//{
// if (mPathFinding != null)
// {
// mPathFinding.Dispose();
// TimerManager.TimerRunning -= 1;
// mPathFinding = null;
// }
//}
//Output.WriteLine("this is running");
if (!mActive || !Npc.IsMoving)
{
if (mPathFinding != null)
{
mPathFinding.Dispose();
TimerManager.TimerRunning -= 1;
mPathFinding = null;
}
return;
}
if (mChangeMoveToFirm)
{
MapInstance Instance = MapManager.GetInstanceByMapId(16);
if (Instance != null)
{
mChangeMoveToFirm = false;
int LocX = Npc.LocX * 100;
int LocY = Npc.LocY * 100;
int NewLocX = Npc.NewLocX * 100;
int NewLocY = Npc.NewLocY * 100;
int deltaX = LocX - NewLocX;
int deltaY = LocY - NewLocY;
double distance = (Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2)));
double timeTaken = (distance / (Npc.ShipSpeed * mBallSpeed) * 1000);
timeTaken = timeTaken * 1.03;
//Output.WriteLine("new ball speed !");
Instance.BroadcastMessage(MapShipMovementComposer.Compose(Npc.Id, NewLocX, NewLocY, timeTaken));
Npc.TimeTaken = 1;
Npc.OldLocX = Npc.LocX;
Npc.OldLocY = Npc.LocY;
}
}
if (mChangeBallSpeed)
{
MapInstance Instance = MapManager.GetInstanceByMapId(16);
if (Instance != null)
{
mChangeBallSpeed = false;
int LocX = Npc.LocX * 100;
int LocY = Npc.LocY * 100;
int NewLocX = Npc.NewLocX * 100;
int NewLocY = Npc.NewLocY * 100;
int deltaX = LocX - NewLocX;
int deltaY = LocY - NewLocY;
double distance = (Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2)));
double timeTaken = (distance / (Npc.ShipSpeed * mBallSpeed) * 1000);
timeTaken = timeTaken * 1.03;
//Output.WriteLine("new ball speed !");
Instance.BroadcastMessage(MapShipMovementComposer.Compose(Npc.Id, NewLocX, NewLocY, timeTaken));
Npc.TimeTaken = 1;
Npc.OldLocX = Npc.LocX;
Npc.OldLocY = Npc.LocY;
}
}
//Output.WriteLine("pathfinding still running bro.");
Npc.TimeTaken += 1;
Vector startpos = new Vector(Npc.OldLocX, Npc.OldLocY);
Vector endpos = new Vector(Npc.NewLocX, Npc.NewLocY);
Vector mov_vec = Vector.Subtract(endpos, startpos);
double mov_vec_len = Math.Sqrt(Math.Pow(mov_vec.X, 2) + Math.Pow(mov_vec.Y, 2));
Vector result = calculatePosition(ref startpos, ref endpos, (double)Npc.TimeTaken, ((double)Npc.ShipSpeed * mBallSpeed) / 416);
Vector result_vec = Vector.Subtract(startpos, result);
double result_mov_len = Math.Sqrt(Math.Pow(result_vec.X, 2) + Math.Pow(result_vec.Y, 2));
if (result_mov_len > mov_vec_len)
{
Npc.IsMoving = false;
if (mMoveToFirm == 1)
{
mMMOScore += 1;
UpdateScore(1);
}
if (mMoveToFirm == 2)
{
mEICScore += 1;
UpdateScore(2);
}
if (mMoveToFirm == 3)
{
mVRUScore += 1;
UpdateScore(3);
}
GiveReward(mMoveToFirm);
UpdateSpeed(0);
ResetBall(Npc);
//Output.WriteLine("is arrived.");
if (mPathFinding != null)
{
mPathFinding.Dispose();
TimerManager.TimerRunning -= 1;
mPathFinding = null;
}
return;
}
//Output.WriteLine(" " + Npc.Name + " " + Math.Round(result.X) + "/" + Math.Round(result.Y));
Npc.LocX = (int)Math.Round(result.X);
Npc.LocY = (int)Math.Round(result.Y);
//Session TargetUser2 = SessionManager.GetSessionByCharacterId(62);
//TargetUser2.SendData(PacketComposer.Compose("1", "-10|"+Npc.LocX+"00|"+Npc.LocY+"00"));
//if (Session.StoppedPlayer || Session.CharacterInfo.Destroy || Session.CharacterInfo.Disconnected)
//{
// if (Session.CharacterInfo.PathFinding != null)
// {
// Session.CharacterInfo.PathFinding.Dispose();
// TimerManager.TimerRunning -= 1;
// Session.CharacterInfo.PathFinding = null;
// }
//}
//Session.CharacterInfo.TmpActiveConfig = Session.CharacterInfo.ActiveConfig;
}
private static Vector calculatePosition(ref Vector start_pos, ref Vector end_pos, double time, double speed)
{
Vector mov_vec = Vector.Subtract(end_pos, start_pos);
mov_vec.Normalize();
Vector delta_vec = mov_vec * time * speed;
Vector result = Vector.Add(start_pos, delta_vec);
return result;
}
}
}