[Help] Stucked at DateTime.Now

01/12/2012 09:13 S/W#1
Quote:
using System.Text;
using Emu.Game;
using Emu.ServerBase;
using Emu.Network.GamePackets;
using System;

namespace Emu
{
public class Monster
{
public static void Spawn(Client.GameState client, ushort mesh)
{
if (DateTime.Now.Minute == 35 && DateTime.Now.Second == 00)
{
Database.MonsterInformation monster = new Emu.Database.MonsterInformation();
monster.Boss = true;
monster.Hitpoints = 50000000;
monster.Level = 140;
monster.Mesh = 950;
monster.Name = "Titan";
monster.MaxAttack = 10500;
monster.AttackRange = 5;
monster.AttackType = 2;
monster.AttackSpeed = 1000;
monster.ViewRange = 2;
monster.MoveSpeed = 500;
monster.RunSpeed = 500;
monster.MinAttack = 59000;
Game.Entity entity = new Game.Entity(Game.EntityFlag.Monster, false, null);
entity.MapObjType = Game.MapObjectType.Monster;
entity.MonsterInfo = monster;
entity.MonsterInfo.Owner = entity;
entity.Name = "Titan";
entity.MinAttack = monster.MinAttack;
entity.MaxAttack = entity.MagicAttack = monster.MaxAttack;
entity.Hitpoints = entity.MaxHitpoints = monster.Hitpoints;
entity.Body = monster.Mesh;
entity.Level = monster.Level;
entity.Defence = 5000;
entity.season = true;
entity.anti_revive = true;
entity.X = 641;
entity.Y = 544;
entity.UID = 500002;
entity.MapID = 1002;
entity.SendUpdates = true;
client.Map.RemoveEntity(entity);
client.Map.AddEntity(entity);
Program.WriteLine("Done");

}
}
}
}
His code should spawn monster in time xx.35 but don't work.way?
Thanks for help
01/12/2012 09:17 Arco.#2
Erm, what I would do is make a thread, checking for that time periodically, then execute that once the time has come. And make a bool and so it doesn't keep trying to spawn the mob after it has already spawn.
01/12/2012 09:26 S/W#3
Quote:
Originally Posted by Arco. View Post
Erm, what I would do is make a thread, checking for that time periodically, then execute that once the time has come. And make a bool and so it doesn't keep trying to spawn the mob after it has already spawn.
If mob spawned client.Map.RemoveEntity(entity);
client.Map.AddEntity(entity);
Server remove it and add new one
Or it is not god?
#edit his code don't work way?
01/12/2012 10:05 I don't have a username#4
Don't use DateTime.Now.Second, because your thread/timer might not hit exactly at the second, rather use a boolean to check if it have been executed once.
01/12/2012 11:58 S/W#5
Any more ides way timer don't work?
01/12/2012 12:07 I don't have a username#6
How does your timer code look?
01/12/2012 12:12 S/W#7
System timer
Quote:
if (DateTime.Now.Hour == 20 && DateTime.Now.Minute == 00 && DateTime.Now.DayOfWeek == DayOfWeek.Saturday && DateTime.Now.Second == 00)
{
foreach (Client.GameState client in Kernel.GamePool.Values)
{
Program.WriteLine("Hello");

}
}
}
01/12/2012 12:14 I don't have a username#8
I mean the timer that executes your event.
01/12/2012 20:49 S/W#9
Still don't haw answer :(
01/12/2012 23:49 Arco.#10
Okay simple.
In program cs create a new variable,
public static Thread MonsterThread;
and then,
public static Boolean MonsterSpawned = false;

Then inside the main void, initialize it.
MonsterThread = new Thread(Monster.Spawn);
MonsterThread.Start();

Then the Monster.Spawn void do someshit like

public static void Spawn(GameClient Client, UInt16 Mesh)
{
if (Blah blah time check && !Program.MonsterSpawned)
{
All that spawn shit.
Program.MonsterSpawned = true;

//And if you plan on resetting your server once a day, then you can just abort the thread. Likee so,
Program.MonsterThread.Abort();
}
else
Thread.Sleep(500);
}
01/13/2012 02:48 pro4never#11
So wait.. you want to spawn a monster EVERY hour at 35 minutes?

It's easier if your source handles spawn times in a particular way but it's not too much harder to do it this way (note: albetros uses a 'spawndelay' int which could just be set on mob dying but that's beside the point).

Here's how I'd do it with a thread though... (simplest way really)

public class MonsterSpawnThread
{
public void Run()
{
while(true)//infinite loop. use break; or return; to exit the thread!
{
try
{
if(DateTime.Now.Minute == 33)
{
//Spawn monster Code
Thread.Sleep(60000);//sleep for one minute so that the thread CANNOT re-execute this code within the same minute
}
}
catch{}
Thread.Sleep(30000);//run thread every 30 seconds
}
}
}

Now when starting up server you have to start this thread.

new Thread(new MonsterSpawnThread().Run);

Simple stuff really.