2019-06-18 22:55:32 -04:00
/ *
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Copyright ( C ) 2015 - 2019 Project Meteor Dev Team
This file is part of Project Meteor Server .
Project Meteor Server is free software : you can redistribute it and / or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
Project Meteor Server 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 Affero General Public License for more details .
You should have received a copy of the GNU Affero General Public License
along with Project Meteor Server . If not , see < https : www . gnu . org / licenses / > .
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
* /
2019-06-19 00:05:18 -04:00
using Meteor.Common ;
2019-06-19 01:10:15 -04:00
using Meteor.Map.Actors ;
using Meteor.Map.lua ;
using Meteor.Map.packets.send.actor ;
2015-10-05 19:36:15 -04:00
using System ;
using System.Collections.Generic ;
2019-06-19 01:10:15 -04:00
using Meteor.Map.actors.director ;
2015-10-05 19:36:15 -04:00
2019-06-19 01:10:15 -04:00
namespace Meteor.Map.actors.area
2015-10-05 19:36:15 -04:00
{
2016-02-07 13:05:54 -05:00
class Zone : Area
2016-05-29 15:14:09 -04:00
{
2016-02-07 13:05:54 -05:00
Dictionary < string , Dictionary < uint , PrivateArea > > privateAreas = new Dictionary < string , Dictionary < uint , PrivateArea > > ( ) ;
2017-04-15 16:33:56 -04:00
Dictionary < string , List < PrivateAreaContent > > contentAreas = new Dictionary < string , List < PrivateAreaContent > > ( ) ;
2017-04-29 20:30:54 -04:00
Object contentAreasLock = new Object ( ) ;
2016-01-17 23:36:34 -05:00
2017-05-27 02:17:25 +01:00
public SharpNav . TiledNavMesh tiledNavMesh ;
public SharpNav . NavMeshQuery navMeshQuery ;
2017-06-09 04:17:08 +01:00
2017-05-27 02:17:25 +01:00
public Int64 pathCalls ;
2017-12-10 09:20:42 -06:00
public Int64 prevPathCalls = 0 ;
2017-05-27 02:17:25 +01:00
public Int64 pathCallTime ;
2017-06-09 04:17:08 +01:00
public Zone ( uint id , string zoneName , ushort regionId , string classPath , ushort bgmDay , ushort bgmNight , ushort bgmBattle , bool isIsolated , bool isInn , bool canRideChocobo , bool canStealth , bool isInstanceRaid , bool loadNavMesh = false )
2017-04-29 20:30:54 -04:00
: base ( id , zoneName , regionId , classPath , bgmDay , bgmNight , bgmBattle , isIsolated , isInn , canRideChocobo , canStealth , isInstanceRaid )
2015-10-13 19:15:44 -04:00
{
2017-06-11 02:29:08 +01:00
if ( loadNavMesh )
2017-05-27 02:17:25 +01:00
{
try
{
2017-06-11 02:29:08 +01:00
tiledNavMesh = utils . NavmeshUtils . LoadNavmesh ( tiledNavMesh , zoneName + ".snb" ) ;
2017-05-27 02:17:25 +01:00
navMeshQuery = new SharpNav . NavMeshQuery ( tiledNavMesh , 100 ) ;
2017-06-09 04:17:08 +01:00
2017-06-11 02:29:08 +01:00
if ( tiledNavMesh ! = null & & tiledNavMesh . Tiles [ 0 ] . PolyCount > 0 )
2017-06-09 04:17:08 +01:00
Program . Log . Info ( $"Loaded navmesh for {zoneName}" ) ;
2017-05-27 02:17:25 +01:00
}
2017-06-09 04:17:08 +01:00
catch ( Exception e )
2017-05-27 02:17:25 +01:00
{
Program . Log . Error ( e . Message ) ;
}
}
2016-01-24 17:11:35 -05:00
}
2016-06-14 21:29:10 +01:00
public void AddPrivateArea ( PrivateArea pa )
2016-03-20 19:29:38 -04:00
{
2016-06-14 21:29:10 +01:00
if ( privateAreas . ContainsKey ( pa . GetPrivateAreaName ( ) ) )
2017-03-07 00:09:37 -05:00
privateAreas [ pa . GetPrivateAreaName ( ) ] [ pa . GetPrivateAreaType ( ) ] = pa ;
2016-03-20 19:29:38 -04:00
else
{
2016-06-14 21:29:10 +01:00
privateAreas [ pa . GetPrivateAreaName ( ) ] = new Dictionary < uint , PrivateArea > ( ) ;
2017-03-07 00:09:37 -05:00
privateAreas [ pa . GetPrivateAreaName ( ) ] [ pa . GetPrivateAreaType ( ) ] = pa ;
2016-03-20 19:29:38 -04:00
}
}
2016-06-14 21:29:10 +01:00
public PrivateArea GetPrivateArea ( string type , uint number )
2016-01-17 23:36:34 -05:00
{
2016-02-07 13:05:54 -05:00
if ( privateAreas . ContainsKey ( type ) )
2016-01-17 23:36:34 -05:00
{
2016-02-07 13:05:54 -05:00
Dictionary < uint , PrivateArea > instances = privateAreas [ type ] ;
if ( instances . ContainsKey ( number ) )
return instances [ number ] ;
else
return null ;
2016-01-17 23:36:34 -05:00
}
2016-02-07 13:05:54 -05:00
else
2016-01-17 23:36:34 -05:00
return null ;
}
2017-06-27 16:55:14 -04:00
public override SubPacket CreateScriptBindPacket ( )
2016-01-24 03:10:17 -05:00
{
2016-02-07 13:05:54 -05:00
bool isEntranceDesion = false ;
2016-01-24 03:10:17 -05:00
2016-02-07 13:05:54 -05:00
List < LuaParam > lParams ;
2017-04-29 20:30:54 -04:00
lParams = LuaUtils . CreateLuaParamList ( classPath , false , true , zoneName , "" , - 1 , canRideChocobo ? ( byte ) 1 : ( byte ) 0 , canStealth , isInn , false , false , false , true , isInstanceRaid , isEntranceDesion ) ;
2017-06-27 16:55:14 -04:00
return ActorInstantiatePacket . BuildPacket ( actorId , actorName , className , lParams ) ;
2016-02-02 00:02:06 -05:00
}
2016-06-14 22:54:02 +01:00
public void AddSpawnLocation ( SpawnLocation spawn )
2016-05-29 15:14:09 -04:00
{
//Is it in a private area?
if ( ! spawn . privAreaName . Equals ( "" ) )
{
if ( privateAreas . ContainsKey ( spawn . privAreaName ) )
{
Dictionary < uint , PrivateArea > levels = privateAreas [ spawn . privAreaName ] ;
if ( levels . ContainsKey ( spawn . privAreaLevel ) )
2016-06-14 22:54:02 +01:00
levels [ spawn . privAreaLevel ] . AddSpawnLocation ( spawn ) ;
2016-05-29 15:14:09 -04:00
else
2016-06-14 22:54:02 +01:00
Program . Log . Error ( "Tried to add a spawn location to non-existing private area level \"{0}\" in area {1} in zone {2}" , spawn . privAreaName , spawn . privAreaLevel , zoneName ) ;
2016-05-29 15:14:09 -04:00
}
else
2016-06-14 22:54:02 +01:00
Program . Log . Error ( "Tried to add a spawn location to non-existing private area \"{0}\" in zone {1}" , spawn . privAreaName , zoneName ) ;
2016-05-29 15:14:09 -04:00
}
else
mSpawnLocations . Add ( spawn ) ;
}
2016-06-14 22:54:02 +01:00
public void SpawnAllActors ( bool doPrivAreas )
2016-05-29 15:14:09 -04:00
{
foreach ( SpawnLocation spawn in mSpawnLocations )
2016-06-14 22:54:02 +01:00
SpawnActor ( spawn ) ;
2016-05-29 15:14:09 -04:00
if ( doPrivAreas )
{
foreach ( Dictionary < uint , PrivateArea > areas in privateAreas . Values )
{
foreach ( PrivateArea pa in areas . Values )
2016-06-14 22:54:02 +01:00
pa . SpawnAllActors ( ) ;
2016-05-29 15:14:09 -04:00
}
}
}
2017-04-15 16:33:56 -04:00
public Actor FindActorInZone ( uint id )
{
2017-08-23 03:08:43 +01:00
lock ( mActorList )
2017-04-15 16:33:56 -04:00
{
2017-08-23 03:08:43 +01:00
if ( ! mActorList . ContainsKey ( id ) )
2017-04-15 16:33:56 -04:00
{
2017-08-23 03:08:43 +01:00
foreach ( Dictionary < uint , PrivateArea > paList in privateAreas . Values )
2017-04-15 16:33:56 -04:00
{
2017-08-23 03:08:43 +01:00
foreach ( PrivateArea pa in paList . Values )
{
Actor actor = pa . FindActorInArea ( id ) ;
if ( actor ! = null )
return actor ;
}
2017-04-15 16:33:56 -04:00
}
2017-12-08 00:58:39 -06:00
foreach ( List < PrivateAreaContent > paList in contentAreas . Values )
{
foreach ( PrivateArea pa in paList )
{
Actor actor = pa . FindActorInArea ( id ) ;
if ( actor ! = null )
return actor ;
}
}
2017-08-23 03:08:43 +01:00
return null ;
2017-04-15 16:33:56 -04:00
}
2017-08-23 03:08:43 +01:00
else
return mActorList [ id ] ;
2017-04-15 16:33:56 -04:00
}
}
2017-07-09 18:38:01 -04:00
public PrivateAreaContent CreateContentArea ( Player starterPlayer , string areaClassPath , string contentScript , string areaName , string directorName , params object [ ] args )
2017-04-15 16:33:56 -04:00
{
2017-04-29 20:30:54 -04:00
lock ( contentAreasLock )
{
2017-07-09 18:38:01 -04:00
Director director = CreateDirector ( directorName , true , args ) ;
2017-04-29 20:30:54 -04:00
if ( director = = null )
return null ;
if ( ! contentAreas . ContainsKey ( areaName ) )
contentAreas . Add ( areaName , new List < PrivateAreaContent > ( ) ) ;
PrivateAreaContent contentArea = new PrivateAreaContent ( this , classPath , areaName , 1 , director , starterPlayer ) ;
contentAreas [ areaName ] . Add ( contentArea ) ;
2017-10-10 13:32:47 -05:00
2017-04-29 20:30:54 -04:00
return contentArea ;
}
}
2017-04-15 16:33:56 -04:00
2017-04-29 20:30:54 -04:00
public void DeleteContentArea ( PrivateAreaContent area )
{
if ( contentAreas . ContainsKey ( area . GetPrivateAreaName ( ) ) )
{
contentAreas [ area . GetPrivateAreaName ( ) ] . Remove ( area ) ;
}
2017-04-15 16:33:56 -04:00
}
2017-04-29 20:30:54 -04:00
2017-06-18 22:01:55 +01:00
public override void Update ( DateTime tick )
2017-06-09 04:17:08 +01:00
{
2017-06-12 22:07:50 +01:00
base . Update ( tick ) ;
2017-10-11 14:46:24 +01:00
2017-10-10 13:32:47 -05:00
foreach ( var a in privateAreas . Values )
foreach ( var b in a . Values )
b . Update ( tick ) ;
foreach ( var a in contentAreas . Values )
foreach ( var b in a )
b . Update ( tick ) ;
2017-06-09 04:17:08 +01:00
2017-09-05 05:05:25 +01:00
// todo: again, this is retarded but debug stuff
2017-07-18 04:51:35 +01:00
var diffTime = tick - lastUpdate ;
2017-06-09 04:17:08 +01:00
2017-07-27 03:58:42 +01:00
if ( diffTime . TotalSeconds > = 10 )
2017-06-09 04:17:08 +01:00
{
if ( this . pathCalls > 0 )
{
2017-12-10 09:20:42 -06:00
Program . Log . Debug ( "Number of pathfinding calls {0} average time {1}ms. {2} this tick" , pathCalls , ( float ) ( pathCallTime / pathCalls ) , pathCalls - prevPathCalls ) ;
prevPathCalls = pathCalls ;
2017-06-09 04:17:08 +01:00
}
2017-07-27 03:58:42 +01:00
lastUpdate = tick ;
2017-06-09 04:17:08 +01:00
}
}
2015-10-05 19:36:15 -04:00
}
}