Reworked GameTime task manager.

This commit is contained in:
MobiusDevelopment
2022-07-16 22:11:41 +00:00
parent beb1aa0b77
commit b4b76176de
406 changed files with 7608 additions and 5919 deletions

View File

@ -224,6 +224,9 @@ public class GameServer
printSection("ThreadPool");
ThreadPool.init();
// Start game time task manager early
GameTimeTaskManager.getInstance();
printSection("IdManager");
IdManager.getInstance();
if (!IdManager.hasInitialized())
@ -232,7 +235,6 @@ public class GameServer
throw new Exception("Could not initialize the ID factory!");
}
// load script engines
printSection("Scripting Engine");
EventDispatcher.getInstance();
ScriptEngineManager.getInstance();
@ -241,8 +243,6 @@ public class GameServer
TelnetServer.getInstance();
printSection("World");
// Start game time task manager early.
GameTimeTaskManager.getInstance();
World.getInstance();
MapRegionManager.getInstance();
ZoneManager.getInstance();

View File

@ -48,6 +48,7 @@ import org.l2jmobius.gameserver.network.serverpackets.ServerClose;
import org.l2jmobius.gameserver.network.serverpackets.SystemMessage;
import org.l2jmobius.gameserver.network.telnet.TelnetServer;
import org.l2jmobius.gameserver.taskmanager.GameTimeTaskManager;
import org.l2jmobius.gameserver.taskmanager.MovementTaskManager;
import org.l2jmobius.gameserver.util.Broadcast;
/**
@ -144,10 +145,21 @@ public class Shutdown extends Thread
}
// ensure all services are stopped
try
{
GameTimeTaskManager.getInstance().stopTimer();
LOGGER.info("Game Time Controller: Timer stopped(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
MovementTaskManager.getInstance().interrupt();
LOGGER.info("Movement Task Manager: Thread interruped(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
}
catch (Throwable t)
{
// ignore
}
try
{
GameTimeTaskManager.getInstance().interrupt();
LOGGER.info("Game Time Task Manager: Thread interruped(" + tc.getEstimatedTimeAndRestartCounter() + "ms).");
}
catch (Throwable t)
{

View File

@ -165,6 +165,7 @@ import org.l2jmobius.gameserver.network.serverpackets.UserInfo;
import org.l2jmobius.gameserver.taskmanager.AttackStanceTaskManager;
import org.l2jmobius.gameserver.taskmanager.CreatureSeeTaskManager;
import org.l2jmobius.gameserver.taskmanager.GameTimeTaskManager;
import org.l2jmobius.gameserver.taskmanager.MovementTaskManager;
import org.l2jmobius.gameserver.util.Broadcast;
import org.l2jmobius.gameserver.util.Util;
@ -2771,7 +2772,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
case MAGIC_ATTACK:
case MAGIC_ATTACK_SPEED:
case MAGICAL_DEFENCE:
case HIT_AT_NIGHT:
{
info.addComponentType(UserInfoType.STATS);
break;
@ -3598,9 +3598,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
// Set the Creature _move object to MoveData object
_move = m;
// Add the Creature to movingObjects of the GameTimeTaskManager
// The GameTimeTaskManager manage objects movement
GameTimeTaskManager.getInstance().registerMovingObject(this);
// Add the Creature to moving objects of the MovementTaskManager.
// The MovementTaskManager manages object movement.
MovementTaskManager.getInstance().registerMovingObject(this);
// Create a task to notify the AI that Creature arrives at a check point of the movement
if ((ticksToMove * GameTimeTaskManager.MILLIS_IN_TICK) > 3000)
@ -3677,9 +3677,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
// Set the Creature _move object to MoveData object
_move = m;
// Add the Creature to movingObjects of the GameTimeTaskManager
// The GameTimeTaskManager manage objects movement
GameTimeTaskManager.getInstance().registerMovingObject(this);
// Add the Creature to moving objects of the GameTimeTaskManager.
// The MovementTaskManager manages object movement.
MovementTaskManager.getInstance().registerMovingObject(this);
// Create a task to notify the AI that Creature arrives at a check point of the movement
if ((ticksToMove * GameTimeTaskManager.MILLIS_IN_TICK) > 3000)

View File

@ -41,6 +41,7 @@ import org.l2jmobius.gameserver.network.SystemMessageId;
import org.l2jmobius.gameserver.network.serverpackets.IClientOutgoingPacket;
import org.l2jmobius.gameserver.network.serverpackets.InventoryUpdate;
import org.l2jmobius.gameserver.taskmanager.GameTimeTaskManager;
import org.l2jmobius.gameserver.taskmanager.MovementTaskManager;
import org.l2jmobius.gameserver.util.Util;
/**
@ -165,7 +166,7 @@ public abstract class Vehicle extends Creature
m._moveStartTime = GameTimeTaskManager.getInstance().getGameTicks();
_move = m;
GameTimeTaskManager.getInstance().registerMovingObject(this);
MovementTaskManager.getInstance().registerMovingObject(this);
// Make sure vehicle is not stuck.
if (_monitorTask == null)

View File

@ -42,7 +42,7 @@ public class WarnUserTakeBreakTask implements Runnable
{
if (_player.isOnline())
{
final long hours = TimeUnit.MILLISECONDS.toHours(_player.getUptime());
final long hours = TimeUnit.MILLISECONDS.toHours(_player.getUptime() + 60000);
_player.sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_PLAYED_FOR_S1_H_TAKE_A_BREAK_PLEASE).addLong(hours));
}
else

View File

@ -170,7 +170,6 @@ public enum Stat
ATTACK_COUNT_MAX("atkCountMax"),
PHYSICAL_POLEARM_TARGET_SINGLE("polearmSingleTarget"),
WEAPON_ATTACK_ANGLE_BONUS("weaponAttackAngleBonus"),
HIT_AT_NIGHT("hitAtNight"),
// Run speed, walk & escape speed are calculated proportionally, magic speed is a buff
MOVE_SPEED("moveSpeed"),

View File

@ -22,7 +22,6 @@ import org.l2jmobius.gameserver.model.actor.Creature;
import org.l2jmobius.gameserver.model.item.ItemTemplate;
import org.l2jmobius.gameserver.model.stats.IStatFunction;
import org.l2jmobius.gameserver.model.stats.Stat;
import org.l2jmobius.gameserver.taskmanager.GameTimeTaskManager;
/**
* @author UnAfraid
@ -70,12 +69,6 @@ public class PAccuracyFinalizer implements IStatFunction
baseValue += calcEnchantBodyPart(creature, ItemTemplate.SLOT_GLOVES);
}
// Shadow sense
if (GameTimeTaskManager.getInstance().isNight())
{
baseValue += creature.getStat().getAdd(Stat.HIT_AT_NIGHT, 0);
}
return Stat.defaultValue(creature, stat, baseValue);
}

View File

@ -32,8 +32,8 @@ public class ClientSetTime implements IClientOutgoingPacket
public boolean write(PacketWriter packet)
{
OutgoingPackets.CLIENT_SET_TIME.writeId(packet);
packet.writeD(GameTimeTaskManager.getInstance().getGameTime()); // time in client minutes
packet.writeD(6); // constant to match the server time( this determines the speed of the client clock)
packet.writeD(GameTimeTaskManager.getInstance().getGameTime()); // Time in client minutes.
packet.writeD(GameTimeTaskManager.IG_DAYS_PER_DAY); // Constant to match the server time. This determines the speed of the client clock.
return true;
}
}

View File

@ -17,42 +17,35 @@
package org.l2jmobius.gameserver.taskmanager;
import java.util.Calendar;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.l2jmobius.gameserver.model.actor.Creature;
import org.l2jmobius.gameserver.model.events.EventDispatcher;
import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange;
import org.l2jmobius.gameserver.network.SystemMessageId;
import org.l2jmobius.gameserver.network.serverpackets.SystemMessage;
/**
* Game Time task manager class.
* @author Forsaiken
* GameTime task manager class.
* @author Forsaiken, Mobius
*/
public class GameTimeTaskManager extends Thread
{
private static final Logger LOGGER = Logger.getLogger(GameTimeTaskManager.class.getName());
public static final int TICKS_PER_SECOND = 10; // not able to change this without checking through code
public static final int TICKS_PER_SECOND = 10; // Not able to change this without checking through code.
public static final int MILLIS_IN_TICK = 1000 / TICKS_PER_SECOND;
public static final int IG_DAYS_PER_DAY = 6;
public static final int MILLIS_PER_IG_DAY = (3600000 * 24) / IG_DAYS_PER_DAY;
public static final int SECONDS_PER_IG_DAY = MILLIS_PER_IG_DAY / 1000;
public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND;
private static final int SHADOW_SENSE_ID = 294;
private static final Set<Creature> _movingObjects = ConcurrentHashMap.newKeySet();
private static final Set<Creature> _shadowSenseCharacters = ConcurrentHashMap.newKeySet();
private final long _referenceTime;
private boolean _isNight;
protected GameTimeTaskManager()
{
super("GameTimeTaskManager");
super.setDaemon(true);
super.setPriority(MAX_PRIORITY);
super.setPriority(NORM_PRIORITY);
final Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0);
@ -64,6 +57,41 @@ public class GameTimeTaskManager extends Thread
super.start();
}
@Override
public void run()
{
while (true)
{
if ((getGameHour() < 6) != _isNight)
{
_isNight = !_isNight;
EventDispatcher.getInstance().notifyEventAsync(new OnDayNightChange(_isNight));
}
try
{
Thread.sleep(10000);
}
catch (InterruptedException e)
{
LOGGER.log(Level.WARNING, getClass().getSimpleName(), e);
}
}
}
public boolean isNight()
{
return _isNight;
}
/**
* @return The actual GameTime tick. Directly taken from current time.
*/
public int getGameTicks()
{
return (int) ((System.currentTimeMillis() - _referenceTime) / MILLIS_IN_TICK);
}
public int getGameTime()
{
return (getGameTicks() % TICKS_PER_IG_DAY) / MILLIS_IN_TICK;
@ -79,134 +107,6 @@ public class GameTimeTaskManager extends Thread
return getGameTime() % 60;
}
public boolean isNight()
{
return getGameHour() < 6;
}
/**
* The true GameTime tick. Directly taken from current time. This represents the tick of the time.
* @return
*/
public int getGameTicks()
{
return (int) ((System.currentTimeMillis() - _referenceTime) / MILLIS_IN_TICK);
}
/**
* Add a Creature to movingObjects of GameTimeTaskManager.
* @param creature The Creature to add to movingObjects of GameTimeTaskManager
*/
public void registerMovingObject(Creature creature)
{
if (creature == null)
{
return;
}
_movingObjects.add(creature);
}
/**
* Move all Creatures contained in movingObjects of GameTimeTaskManager.<br>
* <br>
* <b><u>Concept</u>:</b><br>
* <br>
* All Creature in movement are identified in <b>movingObjects</b> of GameTimeTaskManager.<br>
* <br>
* <b><u>Actions</u>:</b><br>
* <ul>
* <li>Update the position of each Creature</li>
* <li>If movement is finished, the Creature is removed from movingObjects</li>
* <li>Create a task to update the _knownObject and _knowPlayers of each Creature that finished its movement and of their already known WorldObject then notify AI with EVT_ARRIVED</li>
* </ul>
*/
private void moveObjects()
{
_movingObjects.removeIf(Creature::updatePosition);
}
public void stopTimer()
{
super.interrupt();
LOGGER.info(getClass().getSimpleName() + ": Stopped.");
}
@Override
public void run()
{
LOGGER.info(getClass().getSimpleName() + ": Started.");
long nextTickTime;
long sleepTime;
boolean isNight = isNight();
EventDispatcher.getInstance().notifyEventAsync(new OnDayNightChange(isNight));
while (true)
{
nextTickTime = ((System.currentTimeMillis() / MILLIS_IN_TICK) * MILLIS_IN_TICK) + 100;
try
{
moveObjects();
}
catch (Throwable e)
{
LOGGER.log(Level.WARNING, getClass().getSimpleName(), e);
}
sleepTime = nextTickTime - System.currentTimeMillis();
if (sleepTime > 0)
{
try
{
Thread.sleep(sleepTime);
}
catch (InterruptedException e)
{
}
}
if (isNight() != isNight)
{
isNight = !isNight;
EventDispatcher.getInstance().notifyEventAsync(new OnDayNightChange(isNight));
notifyShadowSense();
}
}
}
public synchronized void addShadowSenseCharacter(Creature creature)
{
if (!_shadowSenseCharacters.contains(creature))
{
_shadowSenseCharacters.add(creature);
if (isNight())
{
final SystemMessage msg = new SystemMessage(SystemMessageId.IT_IS_NOW_MIDNIGHT_AND_THE_EFFECT_OF_S1_CAN_BE_FELT);
msg.addSkillName(SHADOW_SENSE_ID);
creature.sendPacket(msg);
}
}
}
public void removeShadowSenseCharacter(Creature creature)
{
_shadowSenseCharacters.remove(creature);
}
private void notifyShadowSense()
{
final SystemMessage msg = new SystemMessage(isNight() ? SystemMessageId.IT_IS_NOW_MIDNIGHT_AND_THE_EFFECT_OF_S1_CAN_BE_FELT : SystemMessageId.IT_IS_DAWN_AND_THE_EFFECT_OF_S1_WILL_NOW_DISAPPEAR);
msg.addSkillName(SHADOW_SENSE_ID);
for (Creature creature : _shadowSenseCharacters)
{
creature.getStat().recalculateStats(true);
creature.sendPacket(msg);
}
}
public static final GameTimeTaskManager getInstance()
{
return SingletonHolder.INSTANCE;
@ -216,4 +116,4 @@ public class GameTimeTaskManager extends Thread
{
protected static final GameTimeTaskManager INSTANCE = new GameTimeTaskManager();
}
}
}

View File

@ -0,0 +1,84 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.gameserver.taskmanager;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.l2jmobius.gameserver.model.actor.Creature;
/**
* Movement task manager class.
* @author Forsaiken, Mobius
*/
public class MovementTaskManager extends Thread
{
private static final Logger LOGGER = Logger.getLogger(MovementTaskManager.class.getName());
private static final Set<Creature> MOVING_OBJECTS = ConcurrentHashMap.newKeySet();
protected MovementTaskManager()
{
super("MovementTaskManager");
super.setDaemon(true);
super.setPriority(MAX_PRIORITY);
super.start();
}
/**
* Add a Creature to moving objects of MovementTaskManager.
* @param creature The Creature to add to moving objects of MovementTaskManager.
*/
public void registerMovingObject(Creature creature)
{
if (creature == null)
{
return;
}
MOVING_OBJECTS.add(creature);
}
@Override
public void run()
{
while (true)
{
try
{
MOVING_OBJECTS.removeIf(Creature::updatePosition);
Thread.sleep(100);
}
catch (Throwable e)
{
LOGGER.log(Level.WARNING, getClass().getSimpleName(), e);
}
}
}
public static final MovementTaskManager getInstance()
{
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder
{
protected static final MovementTaskManager INSTANCE = new MovementTaskManager();
}
}