public void ThreadRun()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            try
            {
                _cfgFile = new FLDataFile("flopenserver.cfg", false);
                FLPath   = _cfgFile.GetSetting("server", "fl_path").Str(0);
                AcctPath = _cfgFile.GetSetting("server", "acct_path").Str(0);

                if (AcctPath == "")
                {
                    AcctPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
                               @"\My Games\Freelancer\Accts\MultiPlayer";
                }

                if (_cfgFile.SettingExists("server", "server_id"))
                {
                    ServerID = _cfgFile.GetSetting("server", "server_id").Str(0);
                }

                if (_cfgFile.SettingExists("server", "version"))
                {
                    server_version = _cfgFile.GetSetting("server", "version").Str(0);
                }

                if (server_version == "")
                {
                    FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(FLPath + @"\EXE\Freelancer.exe");
                    var             ver = new int[4];
                    ver[0] = myFileVersionInfo.FileMajorPart;
                    ver[1] = myFileVersionInfo.FileMinorPart;
                    ver[2] = myFileVersionInfo.FileBuildPart;
                    ver[3] = myFileVersionInfo.FilePrivatePart;

                    var sb = new StringBuilder();
                    foreach (var num in ver)
                    {
                        sb.Append(".");
                        if (num < 10)
                        {
                            sb.Append("0");
                        }
                        sb.Append(num);
                    }

                    server_version = sb.ToString().Substring(1);
                }


                ServerDescription = _cfgFile.GetSetting("server", "description").Str(0);
                server_news       = _cfgFile.GetSetting("server", "news").Str(0);

                foreach (FLDataFile.Setting set in _cfgFile.GetSettings("intro_msg"))
                {
                    IntroMsg += set.Line;
                }

                foreach (FLDataFile.Setting set in _cfgFile.GetSettings("banner_msg"))
                {
                    BannerMsg += set.Line;
                }

                foreach (FLDataFile.Setting set in _cfgFile.GetSettings("user_help_msg"))
                {
                    UserHelpMsg += set.Line;
                }

                foreach (FLDataFile.Setting set in _cfgFile.GetSettings("admin_help_msg"))
                {
                    AdminHelpMsg += set.Line;
                }
            }
            catch (Exception e)
            {
                _log.AddLog(LogType.ERROR, "error: flopenserver.cfg not found or missing parameter " + e.Message);
                return;
            }

            string name        = _cfgFile.GetSetting("server", "name").Str(0);
            uint   port        = _cfgFile.GetSetting("server", "port").UInt(0);
            var    max_players = (int)_cfgFile.GetSetting("server", "max_players").UInt(0);

            if (ServerID == null)
            {
                ReadFLDefaults();
            }

            _log.AddLog(LogType.GENERAL, "cfg_file = " + _cfgFile.FilePath);
            _log.AddLog(LogType.GENERAL, "fl_path = " + FLPath);
            _log.AddLog(LogType.GENERAL, "acct_path = " + AcctPath);
            _log.AddLog(LogType.GENERAL, "server_id = " + ServerID);
            _log.AddLog(LogType.GENERAL, "version = " + server_version);
            _log.AddLog(LogType.GENERAL, "name = " + name);
            _log.AddLog(LogType.GENERAL, "port = " + port);
            _log.AddLog(LogType.GENERAL, "max_players = " + max_players);


            ArchetypeDB.Load(FLPath, _log);
            UniverseDB.Load(FLPath, _log);
            News.Load(FLPath, _log);
            _log.AddLog(LogType.GENERAL, "Ready to start");

            try
            {
                string[] arg     = server_version.Split('.');
                uint     major   = uint.Parse(arg[0]) & 0xFF;
                uint     minor   = uint.Parse(arg[1]) & 0xFF;
                uint     patch   = uint.Parse(arg[2]) & 0xFF;
                uint     build   = uint.Parse(arg[3]) & 0xFF;
                uint     version = (major << 24) | (minor << 16) | (patch << 8) | build;
                server_version = version.ToString(CultureInfo.InvariantCulture);
            }
            catch
            {
                _log.AddLog(LogType.ERROR, "invalid server version string");
                server_version = "0";
            }

            // Try to start the direct play server. Complain if it fails but don't pass the exception on.
            try
            {
                _dplay = new DirectPlayServer(this, _log, (int)port)
                {
                    server_name        = name + "\0",
                    server_id          = ServerID,
                    server_description = ServerDescription + "\0",
                    server_version     = server_version,
                    max_players        = (uint)max_players
                };
                _log.AddLog(LogType.GENERAL, "Server started");
            }
            catch (Exception e)
            {
                _log.AddLog(LogType.ERROR, "Cannot open socket reason={0}", e.Message);
            }

            _dplay.GotMessage      += _dplay_GotMessage;
            _dplay.PlayerConnected += _dplay_PlayerConnected;
            _dplay.PlayerDestroyed += _dplay_PlayerDestroyed;

            // Kick off the game runner threads. We have one thread per system. New
            // players who have not selected a character are assigned to a random
            // runner thread.
            uint baseObjid = 1;

            foreach (var starSystem in UniverseDB.Systems.Values)
            {
                _runners[starSystem] = new DPGameRunner(this, _log, baseObjid, starSystem);
                baseObjid           += 10000;
            }
        }
Exemple #2
0
        /// <summary>
        ///     Start and run the simulation for this system.
        /// </summary>
        public void GameThreadRun()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            CurrentTime = Utilities.GetTime();

            AddTimer(new UpdateMonitoredObjs(this));
            AddTimer(new SpawnBackgroundNPCs(this));

            bool running = true;

            while (running)
            {
                // Calculate the delta time.
                double delta = Utilities.GetTime() - CurrentTime;
                CurrentTime += delta;

                // Call the reactor to return the next event the process
                // and run any timer functions.
                ReactorEvent next_event = Run(CurrentTime, delta);
                if (next_event is DPGameRunnerRxMsgEvent)
                {
                    var revent = next_event as DPGameRunnerRxMsgEvent;

                    // Process a message from this player. If the message isn't for this runner because
                    // the runner ownership changes, requeue in the right place.
                    if (revent.player.Runner != this)
                    {
                        Log.AddLog(LogType.FL_MSG, "Warning: requeued rx msg onto changed runner");
                        revent.player.Runner.AddEvent(revent);
                    }
                    else
                    {
                        revent.player.RxMsgFromClient(revent.msg);
                    }
                }
                else if (next_event is DPGameRunnerPlayerUpdateEvent)
                {
                    var revent = next_event as DPGameRunnerPlayerUpdateEvent;

                    // If the player is assigned to this runner, make sure we've got it in our
                    // owned player list
                    Player.Player player = revent.player;
                    if (revent.runner == this)
                    {
                        if (!Players.ContainsKey(player.FLPlayerID))
                        {
                            Log.AddLog(LogType.GENERAL, "Player control gained runner={0} flplayerid={1}",
                                       system.Nickname, player.FLPlayerID);
                            Players.Add(player.FLPlayerID, player);
                            if (player.Ship.Objid != 0)
                            {
                                AffObjects[player.Ship.Objid] = player.Ship;
                            }
                            Objects[player.Ship.Objid] = player.Ship;
                            player.Runner      = this;
                            player.Ship.Runner = this;
                            AddTimer(player.Ship);
                        }
                    }
                    // If the player is not assigned to this runner, make sure it's not in our
                    // owned player list.
                    else
                    {
                        if (Players.ContainsKey(player.FLPlayerID))
                        {
                            Log.AddLog(LogType.GENERAL, "Player control lost runner={0} flplayerid={1}", system.Nickname,
                                       player.FLPlayerID);
                            if (player.Ship.Objid != 0)
                            {
                                AffObjects.Remove(player.Ship.Objid);
                            }
                            Objects.Remove(player.Ship.Objid);
                            DelTimer(player.Ship);
                            Players.Remove(player.FLPlayerID);
                        }
                    }

                    PlayerListItem update;
                    update = !Playerlist.ContainsKey(revent.flplayerid) ? new PlayerListItem() : Playerlist[revent.flplayerid];

                    update.Player                 = revent.player;
                    update.FlPlayerID             = revent.flplayerid;
                    update.Name                   = revent.name;
                    update.Rank                   = revent.rank;
                    update.System                 = revent.system;
                    update.Group                  = revent.group;
                    update.GroupInvited           = revent.groupInvited;
                    Playerlist[revent.flplayerid] = update;

                    // Notify all owned players of the player list update
                    foreach (Player.Player p in Players.Values)
                    {
                        p.SendPlayerListUpdate(update);
                    }
                }
                else if (next_event is DPGameRunnerPlayerDeletedEvent)
                {
                    var revent = next_event as DPGameRunnerPlayerDeletedEvent;

                    // fixme: might crash if the player leaves
                    if (!Players.ContainsKey(revent.FlPlayerID))
                    {
                        continue;
                    }

                    var player = Players[revent.FlPlayerID];

                    Players.Remove(revent.FlPlayerID);
                    if (player.Ship.Objid != 0)
                    {
                        DelSimObject(player.Ship);
                    }

                    if (Playerlist.ContainsKey(revent.FlPlayerID))
                    {
                        Playerlist.Remove(revent.FlPlayerID);
                    }

                    foreach (PlayerListItem item in Playerlist.Values)
                    {
                        item.Player.SendPlayerListDepart(player);
                    }
                }
                else if (next_event is ReactorShutdownEvent)
                {
                    running = false;
                }
                else if (next_event is DPGRAddCash)
                {
                    var revent = next_event as DPGRAddCash;
                    if (revent.player.Runner != this)
                    {
                        Log.AddLog(LogType.FL_MSG, "Warning: requeued rx msg onto changed runner");
                        revent.player.Runner.AddEvent(revent);
                    }
                    else
                    {
                        revent.player.Money += revent.cash;
                        revent.player.SendSetMoney();
                    }
                }
                else if (next_event is DPGRSetCash)
                {
                    var revent = next_event as DPGRSetCash;
                    if (revent.player.Runner != this)
                    {
                        Log.AddLog(LogType.FL_MSG, "Warning: requeued rx msg onto changed runner");
                        revent.player.Runner.AddEvent(revent);
                    }
                    else
                    {
                        revent.player.Money = revent.cash;
                        revent.player.SendSetMoney();
                    }
                }
                else if (next_event is DPGRBeam)
                {
                    var revent = next_event as DPGRBeam;
                    if (revent.Player.Runner != this)
                    {
                        Log.AddLog(LogType.FL_MSG, "Warning: requeued rx msg onto changed runner");
                        revent.Player.Runner.AddEvent(revent);
                    }
                    else
                    {
                        revent.Player.MonitoredObjs.Clear();
                        revent.Player.Ship.Basedata        = revent.TargetBase;
                        revent.Player.Ship.RespawnBasedata = revent.TargetBase;
                        revent.Player.Ship.System          = UniverseDB.FindSystem(revent.TargetBase.SystemID);
                        revent.Player.Ship.IsDestroyed     = false;
                        revent.Player.SendServerLand(revent.Player.Ship, 0, revent.Player.Ship.Basedata.BaseID);
                        revent.Player.Runner.Server.AddEvent(new DPGameRunnerPlayerUpdateEvent(revent.Player));
                    }
                }
            }
        }
Exemple #3
0
        public void ThreadRun()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            try
            {
                _cfgFile = new FLDataFile("flopenserver.cfg", false);
                FLPath   = _cfgFile.GetSetting("server", "fl_path").Str(0);
                AcctPath = _cfgFile.GetSetting("server", "acct_path").Str(0);

                if (AcctPath == "")
                {
                    AcctPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
                               @"\My Games\Freelancer\Accts\MultiPlayer";
                }

                if (_cfgFile.SettingExists("server", "server_id"))
                {
                    ServerID = _cfgFile.GetSetting("server", "server_id").Str(0);
                }

                if (_cfgFile.SettingExists("server", "version"))
                {
                    server_version = _cfgFile.GetSetting("server", "version").Str(0);
                }

                if (server_version == "")
                {
                    FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(FLPath + @"\EXE\Freelancer.exe");
                    var             ver = new int[4];
                    ver[0] = myFileVersionInfo.FileMajorPart;
                    ver[1] = myFileVersionInfo.FileMinorPart;
                    ver[2] = myFileVersionInfo.FileBuildPart;
                    ver[3] = myFileVersionInfo.FilePrivatePart;

                    var sb = new StringBuilder();
                    foreach (var num in ver)
                    {
                        sb.Append(".");
                        if (num < 10)
                        {
                            sb.Append("0");
                        }
                        sb.Append(num);
                    }

                    server_version = sb.ToString().Substring(1);
                }


                ServerDescription = _cfgFile.GetSetting("server", "description").Str(0);
                server_news       = _cfgFile.GetSetting("server", "news").Str(0);

                foreach (FLDataFile.Setting set in _cfgFile.GetSettings("intro_msg"))
                {
                    IntroMsg += set.Line;
                }

                foreach (FLDataFile.Setting set in _cfgFile.GetSettings("banner_msg"))
                {
                    BannerMsg += set.Line;
                }

                foreach (FLDataFile.Setting set in _cfgFile.GetSettings("user_help_msg"))
                {
                    UserHelpMsg += set.Line;
                }

                foreach (FLDataFile.Setting set in _cfgFile.GetSettings("admin_help_msg"))
                {
                    AdminHelpMsg += set.Line;
                }
            }
            catch (Exception e)
            {
                _log.AddLog(LogType.ERROR, "error: flopenserver.cfg not found or missing parameter " + e.Message);
                return;
            }

            string name        = _cfgFile.GetSetting("server", "name").Str(0);
            uint   port        = _cfgFile.GetSetting("server", "port").UInt(0);
            var    max_players = (int)_cfgFile.GetSetting("server", "max_players").UInt(0);

            if (ServerID == null)
            {
                ReadFLDefaults();
            }

            _log.AddLog(LogType.GENERAL, "cfg_file = " + _cfgFile.FilePath);
            _log.AddLog(LogType.GENERAL, "fl_path = " + FLPath);
            _log.AddLog(LogType.GENERAL, "acct_path = " + AcctPath);
            _log.AddLog(LogType.GENERAL, "server_id = " + ServerID);
            _log.AddLog(LogType.GENERAL, "version = " + server_version);
            _log.AddLog(LogType.GENERAL, "name = " + name);
            _log.AddLog(LogType.GENERAL, "port = " + port);
            _log.AddLog(LogType.GENERAL, "max_players = " + max_players);


            ArchetypeDB.Load(FLPath, _log);
            UniverseDB.Load(FLPath, _log);
            News.Load(FLPath, _log);
            _log.AddLog(LogType.GENERAL, "Ready to start");

            try
            {
                string[] arg     = server_version.Split('.');
                uint     major   = uint.Parse(arg[0]) & 0xFF;
                uint     minor   = uint.Parse(arg[1]) & 0xFF;
                uint     patch   = uint.Parse(arg[2]) & 0xFF;
                uint     build   = uint.Parse(arg[3]) & 0xFF;
                uint     version = (major << 24) | (minor << 16) | (patch << 8) | build;
                server_version = version.ToString(CultureInfo.InvariantCulture);
            }
            catch
            {
                _log.AddLog(LogType.ERROR, "invalid server version string");
                server_version = "0";
            }

            // Try to start the direct play server. Complain if it fails but don't pass the exception on.
            try
            {
                _dplay = new DirectPlayServer(this, _log, (int)port)
                {
                    server_name        = name + "\0",
                    server_id          = ServerID,
                    server_description = ServerDescription + "\0",
                    server_version     = server_version,
                    max_players        = (uint)max_players
                };
                _log.AddLog(LogType.GENERAL, "Server started");
            }
            catch (Exception e)
            {
                _log.AddLog(LogType.ERROR, "Cannot open socket reason={0}", e.Message);
            }

            // Kick off the game runner threads. We have one thread per system. New
            // players who have not selected a character are assigned to a random
            // runner thread.
            uint baseObjid = 1;

            foreach (var starSystem in UniverseDB.Systems.Values)
            {
                _runners[starSystem] = new DPGameRunner(this, _log, baseObjid, starSystem);
                baseObjid           += 10000;
            }

            // Run the event/timer processing loop for this thread.
            var currentTime = Utilities.GetTime();
            var running     = true;

            while (running)
            {
                // Calculate the delta time.
                var delta = Utilities.GetTime() - currentTime;
                currentTime += delta;

                // Call the reactor to return the next event the process
                // and run any timer functions.
                var nextEvent = Run(currentTime, delta);

                if (nextEvent is DPSessionConnectedEvent)
                {
                    // On receipt of a new connection, create a new player assigning it to a
                    // random game thread. Notify all game threads of the new player.
                    var serverEvent = nextEvent as DPSessionConnectedEvent;

                    var defaultRunner = _runners.ElementAt(_rand.Next(_runners.Count - 1)).Value;

                    var player = new Player.Player(serverEvent.dplayid, _log, NewPlayerID(), defaultRunner);
                    Players[serverEvent.dplayid] = player;

                    foreach (var runner in _runners.Values)
                    {
                        runner.AddEvent(new DPGameRunnerPlayerUpdateEvent(player));
                    }
                }
                else if (nextEvent is DPSessionTerminatedEvent)
                {
                    // The session has died. Tell all game threads that it is dead and the player
                    // is gone.
                    // TODO: save player?
                    var serverEvent = nextEvent as DPSessionTerminatedEvent;
                    if (Players.ContainsKey(serverEvent.dplayid))
                    {
                        var player = Players[serverEvent.dplayid];
                        Players.Remove(serverEvent.dplayid);

                        foreach (var runner in _runners.Values)
                        {
                            runner.AddEvent(new DPGameRunnerPlayerDeletedEvent(player.FLPlayerID));
                        }
                    }

                    // Kill the dplay connection if it is not already dead
                    try
                    {
                        /* fixme: dplay.DestroyClient(server_event.dplayid, null); */
                    }
                    catch
                    {
                    }
                }
                else if (nextEvent is DPGameRunnerPlayerUpdateEvent)
                {
                    // A game thread has sent a notification to say that the player has changed - either
                    // name, rank or system. We might change the assigned thread as a result.
                    // In any case, we notify all game threads of the current name, rank and system.
                    var serverEvent = nextEvent as DPGameRunnerPlayerUpdateEvent;

                    // Find runner for this system and if it's changed tell the old and new runners
                    // of the change in ownership.
                    var currRunner = serverEvent.runner;
                    var newRunner  = _runners[serverEvent.system];
                    if (newRunner != currRunner)
                    {
                        serverEvent.runner = newRunner;
                    }

                    // Notify all game threads of the current player info so they can update their
                    // player lists.
                    foreach (var runner in _runners.Values)
                    {
                        runner.AddEvent(serverEvent);
                    }
                }
                else if (nextEvent is DPSessionRxMessageFromClient)
                {
                    var server_event = nextEvent as DPSessionRxMessageFromClient;
                    if (Players.ContainsKey(server_event.dplayid))
                    {
                        Player.Player player = Players[server_event.dplayid];
                        player.Runner.AddEvent(new DPGameRunnerRxMsgEvent(player, server_event.msg));
                    }
                }
                else if (nextEvent is ReactorShutdownEvent)
                {
                    foreach (DPGameRunner runner in _runners.Values)
                    {
                        runner.AddEvent(new ReactorShutdownEvent());
                    }
                    running = false;
                }
            }
        }
Exemple #4
0
            public override void HandleTimerEvent(double deltaSeconds)
            {
                if (runner.system == UniverseDB.FindSystem("li01"))
                {
                    //TODO: AI debug here
                    for (int i = 0; i < 1; i++)
                    {
                        var npc = new Ship.Ship(runner);
                        npc.AI   = new AI.DebugAI(npc);
                        npc.Arch = ArchetypeDB.Find(FLUtility.CreateID("dsy_csv"));
                        if (npc.Arch == null)
                        {
                            return;
                        }

                        npc.Position = new Vector(-30000 + i * 300, i * 100, -25000);
                        //npc.orientation = ;
                        npc.Rank    = 20;
                        npc.System  = runner.system;
                        npc.Health  = 1.0f;
                        npc.faction = UniverseDB.FindFaction("fc_wild");
                        Loadout loadout = UniverseDB.FindLoadout("fc_j_ge_csv_loadout01");
                        if (loadout != null)
                        {
                            uint hpid = 34;
                            foreach (ShipItem item in loadout.Items)
                            {
                                var new_item = new ShipItem();
                                new_item.arch    = item.arch;
                                new_item.count   = item.count;
                                new_item.health  = 1.0f;
                                new_item.hpid    = hpid++;
                                new_item.hpname  = item.hpname;
                                new_item.mounted = item.mounted;
                                npc.Items.Add(new_item.hpid, new_item);
                            }
                        }
                        npc.InitialiseEquipmentSimulation();
                        runner.CreateSimObject(npc);
                    }
                }
                //    int total = 0;
                //    if (runner.players.Count > 0)
                //    {
                //        if (delta_seconds > 1.5)
                //            runner.log.AddLog(LogType.FL_MSG, "bad delta " + delta_seconds);

                //        // wow, this'll really suck if there are lots of NPCs
                //        foreach (Zone z in runner.system.zones)
                //        {
                //            if (z.shape != null && z.density > 0)
                //            {
                //                while (z.interference < z.density) // borrow this
                //                {
                //                    Ship npc = new Ship(runner);
                //                    npc.position = z.shape.position;
                //                    npc.orientation = z.shape.orientation;
                //                    npc.rank = 20;
                //                    npc.arch = ArchetypeDB.Find(FLUtility.CreateID("dsy_csv"));
                //                    npc.system = runner.system;
                //                    npc.health = 1.0f;
                //                    runner.CreateSimObject(npc);

                //                    z.interference++;
                //                    total++;
                //                }
                //            }
                //        }

                //        int working_npcs = 0;
                //        foreach (SimObject o in runner.objects.Values)
                //        {
                //            if (o.health > 0)
                //            {
                //                working_npcs++;

                //                foreach (Player player in runner.players.Values)
                //                {
                //                    if (player.ship != o)
                //                    {
                //                        Vector position = player.ship.position;
                //                        position.x += rand.Next(100);
                //                        position.z += rand.Next(100);
                //                        o.SetUpdateObject(position, player.ship.orientation, 1.0f, 0);
                //                    }
                //                }
                //            }
                //        }

                //        runner.log.AddLog(LogType.GENERAL, "system={0} npcs={1} objects={2} running={3}",
                //            runner.system.nickname, total, runner.objects.Count, working_npcs));

                //    }

                //    ExpireAfter(1);
            }