public static void CreateEventFromDiplomacy(DiplomaticEntity sender, DiplomaticEntity target, Relation newRelation, Relation oldRelation)
        {
            int RelationImproved   = newRelation > oldRelation ? 1 : 0;
            GalacticEventType type = DetermineDiplomaticEventType(sender.diplomaticType == 2, target.diplomaticType == 2, newRelation);

            GalacticEvents.AddNewEvent(type, int1: sender.id, int2: target.id, int3: (int?)newRelation, int4: (int?)oldRelation, int5: sender.diplomaticType, int6: target.diplomaticType);
        }
        public static void AddNewEvent(GalacticEventType eventType
                                       , int?int1       = null
                                       , int?int2       = null
                                       , int?int3       = null
                                       , int?int4       = null
                                       , int?int5       = null
                                       , int?int6       = null
                                       , string string1 = null
                                       , string string2 = null
                                       , string string3 = null
                                       , string string4 = null
                                       , string string5 = null
                                       , string string6 = null
                                       , string string7 = null
                                       , string string8 = null)
        {
            int eventId = (int)Core.Instance.identities.galacticEvents.getNext();

            // Datetime send to JS have to be in universal, and they should not include to many milliseconds
            DateTime EventTime = DateTime.Now;

            EventTime = EventTime.ToUniversalTime();
            EventTime = DateTime.Parse(EventTime.ToString("s"));

            GalacticEvents newEvent = new GalacticEvents(eventId, (int)eventType, EventTime,
                                                         int1, int2, int3, int4, int5, int6,
                                                         string1, string2, string3, string4, string5, string6, string7, string8);

            Core.Instance.galactivEvents.TryAdd(newEvent.Id, newEvent);
            Core.Instance.dataConnection.insertGalacticEvent(newEvent);
            //Core.Instance.dataConnection.insertGalacticEvent(new { x : newEvent });

            if (Core.Instance.SendEvent != null)
            {
                Core.Instance.SendEvent(newEvent);
            }
        }
Beispiel #3
0
        public void fight(Ship Attacker, Ship Defender, Field BattleField)
        {
            Core core = Core.Instance;

            //init values
            this.attackerId              = Attacker.id;
            this.defenderId              = Defender.id;
            this.attackerUserId          = Attacker.userid;
            this.defenderUserId          = Defender.userid;
            this.starId                  = BattleField.starId ?? 0;
            this.spaceX                  = BattleField.x;
            this.spaceY                  = BattleField.y;
            this.systemX                 = Defender.systemX;
            this.systemY                 = Defender.systemY;
            this.attackerDamageDealt     = 0;
            this.defenderDamageDealt     = 0;
            this.attackerHitPointsRemain = Attacker.hitpoints;
            this.defenderHitPointsRemain = Defender.hitpoints;
            this.AttackerName            = Attacker.NAME;
            this.DefenderName            = Defender.NAME;
            this.DefenderHasRead         = false;
            this.MessageDT               = DateTime.Now;

            this.DefenderShield = Defender.damagereduction;
            this.AttackerShield = Attacker.damagereduction;

            this.AttackerExperience      = Attacker.Experience;
            this.DefenderExperience      = Defender.Experience;
            this.AttackerShipHullId      = Attacker.hullid;
            this.DefenderShipHullId      = Defender.hullid;
            this.AttackerShipHullImageId = Attacker.shipHullsImage;
            this.DefenderShipHullImageId = Defender.shipHullsImage;

            this.AttackerEvasion         = Attacker.FullDefense();
            this.AttackerMaxHitPoints    = Attacker.CombatMaxHitpoint;
            this.attackerStartHitpoint   = Attacker.hitpoints;
            Attacker.CombatStartHitpoint = Attacker.hitpoints;

            this.DefenderEvasion         = Defender.FullDefense();
            this.DefenderMaxHitPoints    = Defender.CombatMaxHitpoint;
            this.DefenderStartHitpoint   = Defender.hitpoints;
            Defender.CombatStartHitpoint = Defender.hitpoints;



            //mock the Tanscendence Defense
            if (Defender.isTranscension())
            {
                Defender.shipModules.Clear();
                Defender.shipModules.Add(new ShipModule(Defender.id, 1105, 1, 1));
                Defender.shipModules.Add(new ShipModule(Defender.id, 1105, 2, 2));
            }


            //get target Field modifiers:
            Tuple <byte, byte> systemXY = null;

            if (this.starId != 0 && this.systemX != 0 && this.systemY != 0)
            {
                systemXY = new Tuple <byte, byte>((byte)(this.systemX), (byte)(this.systemY));
            }

            this.CombatField = new SpacegameServer.Core.CombatField(BattleField, systemXY);

            /*
             * ObjectDescription targetFieldObjDesc = Core.Instance.TargetFieldObject(BattleField, systemXY);
             * objectOnMap = null;
             * Dictionary<short,ObjectWeaponModificator> ObjectWeaponModificator = null;
             * if (targetFieldObjDesc != null)
             * {
             *  objectOnMap = core.ObjectsOnMap.ContainsKey(targetFieldObjDesc.id) ? core.ObjectsOnMap[targetFieldObjDesc.id] : null;
             *  ObjectWeaponModificator = core.ObjectWeaponModificators.ContainsKey(targetFieldObjDesc.id) ? core.ObjectWeaponModificators[targetFieldObjDesc.id] : null;
             * }
             */
            int defenseBonus = this.CombatField.objectOnMap != null ? this.CombatField.objectOnMap.Defensebonus : 0;


            //fight:
            int  battleCounter   = 0;
            bool defenderFights  = true;
            var  AttackerEvasion = Attacker.FullDefense();

            double HitValue = Lockable.rnd.Next(0, AttackerEvasion + Defender.FullDefense());

            if (HitValue < AttackerEvasion)
            {
                defenderFights = false;
            }

            while (battleCounter < 100 && Attacker.hitpoints > 0 && Defender.hitpoints > 0)
            {
                if (defenderFights)
                {
                    if (Defender.crew >= 0 && Defender.energy >= 0)
                    {
                        this.createBattleRound(Defender, Attacker, 0, battleCounter, defenderFights);
                    }
                }
                else
                {
                    this.createBattleRound(Attacker, Defender, defenseBonus, battleCounter, defenderFights);
                }
                defenderFights = !defenderFights;
                battleCounter++;
            }
            Attacker.CombatStartHitpoint = Attacker.hitpoints;
            Defender.CombatStartHitpoint = Defender.hitpoints;

            var Systemname = this.starId != 0 ? Core.Instance.stars[this.starId].systemname : "";


            if (Attacker.hitpoints <= 0)
            {
                GalacticEvents.AddNewEvent(GalacticEventType.CombatDefenderWins, int1: Attacker.id, int2: Defender.id, int3: Attacker.hullid, int4: Defender.hullid, int5: this.spaceX, int6: this.spaceY, string1: Attacker.NAME, string2: Defender.NAME, string3: Systemname, string4: Attacker.userid.ToString(), string5: Defender.userid.ToString());
                Defender.Experience += Attacker.ExperienceBase();
                Attacker.destroy();
            }

            if (Defender.hitpoints <= 0)
            {
                GalacticEvents.AddNewEvent(GalacticEventType.CombatAttackerWins, int1: Attacker.id, int2: Defender.id, int3: Attacker.hullid, int4: Defender.hullid, int5: this.spaceX, int6: this.spaceY, string1: Attacker.NAME, string2: Defender.NAME, string3: Systemname, string4: Attacker.userid.ToString(), string5: Defender.userid.ToString());
                Attacker.Experience += Defender.ExperienceBase();
                Defender.destroy();
            }

            if (Core.Instance.users[Attacker.userid].showCombatPopup)
            {
                int x = Attacker.userid;
                core.SendCombat(this, x);
            }
        }
Beispiel #4
0
 public void insertGalacticEvent(SpacegameServer.Core.GalacticEvents galacticEvent)
 {
 }
Beispiel #5
0
        public bool Abandon(int previousOwnerId, ref List <Ship> ships)
        {
            Core Core = Core.Instance;

            if (!Core.users.Any(e => e.Value.AiId == 3))
            {
                return(false);
            }
            User Separatist = Core.users.First(e => e.Value.AiId == 3).Value;

            User oldUser = Core.users[this.userId];

            List <Lockable> elementsToLock = new List <Lockable>(1);

            elementsToLock.Add(this);
            List <Ship> shipsChangingOwnership = new List <Ship>();

            foreach (var ship in this.field.ships.Where(e => e.userid == oldUser.id && e.systemX == this.SystemX && e.systemY == this.SystemY))
            {
                shipsChangingOwnership.Add(ship);
                elementsToLock.Add(ship);
            }

            if (!LockingManager.lockAllOrSleep(elementsToLock))
            {
                return(false);
            }
            try
            {
                if (this.userId != previousOwnerId)
                {
                    return(false);
                }
                this.userId = Separatist.id;
                foreach (var colBuilding in this.colonyBuildings)
                {
                    colBuilding.userId = this.userId;
                }

                foreach (var ship in shipsChangingOwnership)
                {
                    ship.userid = Separatist.id;
                    ships.Add(ship);
                }

                //save colony and buildings
                Core.dataConnection.saveColonyFull(this.planet, this, false);

                //save Ships
                Core.dataConnection.saveShips(shipsChangingOwnership);

                GalacticEvents.AddNewEvent(GalacticEventType.ColonyAbandoned, int1: previousOwnerId, int2: this.id, string1: this.name);
            }
            catch (Exception ex)
            {
                Core.writeExceptionToLog(ex);
            }
            finally
            {
                LockingManager.unlockAll(elementsToLock);
            }


            return(true);
        }
        /// <summary>
        /// set the relation between two entities
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="target"></param>
        /// <param name="relation"></param>
        /// <param name="relations">Is needed to write result to DB (and generate XML?) </param>
        /// <remarks>called with input data is already checked</remarks>
        protected void SetRelation(DiplomaticEntity sender, DiplomaticEntity target, Relation relation, List <DiplomaticRelation> relations)
        {
            Relation currentRelation = Relation.Neutral;

            if (DiplomaticEntityState.ContainsKey(sender) &&
                DiplomaticEntityState[sender].ContainsKey(target) &&
                DiplomaticEntityState.ContainsKey(target) &&
                DiplomaticEntityState[target].ContainsKey(sender))
            {
                currentRelation = Min(DiplomaticEntityState[sender][target], DiplomaticEntityState[target][sender]);
            }

            //worsening of the relation
            if (relation < currentRelation)
            {
                //Todo: lock all users, ships and alliances. Use a priority lock (to be implemented), since a lot of objects can be part of the transaction
                worsenRelation(sender, target, relation, relations);

                //always worth a Galactic Event
                GalacticEvents.CreateEventFromDiplomacy(sender, target, relation, currentRelation);

                return;
            }

            //relationion is not worsened:
            //either a proposal is done or
            // a proposal is accepted (and perhaps an additional proposal is done)

            //if targetRelationToSender is the currentRelation
            Relation targetRelationToSender = Relation.Neutral;

            if (DiplomaticEntityState.ContainsKey(target) &&
                DiplomaticEntityState[target].ContainsKey(sender))
            {
                targetRelationToSender = (Relation)DiplomaticEntityState[target][sender];
            }



            Relation newCurrentRelation = (Relation)Math.Min((int)targetRelationToSender, (int)relation);

            if (newCurrentRelation != currentRelation)
            {
                //change of relation: always worth a Galactic Event
                GalacticEvents.CreateEventFromDiplomacy(sender, target, newCurrentRelation, currentRelation);
            }

            //Some Relations can't be changed
            if (target is User)
            {
                if (((User)target).AiId > 0 && ((User)target).AiRelation < (int)Relation.Neutral)
                {
                    newCurrentRelation = UserRelations.Min(newCurrentRelation, ((Relation)((User)target).AiRelation));
                }
            }


            /*
             * //all senderusers should be set to newCurrentRelation
             * //create list of sender-Users
             * List<DiplomaticEntity> senders = sender.getMembers();
             *
             * //create list of target-Users
             * List<DiplomaticEntity> targets = target.getMembers();
             *
             * //update all users that are mebers and have a lower relationship than the new one:
             * foreach(var senderUser in senders)
             * {
             *  foreach(var targetUser in targets)
             *  {
             *      if (UserRelations.IsLower( getRelation(senderUser, targetUser) , newCurrentRelation))
             *      {
             *          setDiplomaticEntityState(senderUser, targetUser, newCurrentRelation);
             *          relations.Add(new DiplomaticRelation(senderUser.GetHashCode(), targetUser.GetHashCode(), (int)newCurrentRelation));
             *      }
             *      if (UserRelations.IsLower(getRelation(targetUser, senderUser) , newCurrentRelation))
             *      {
             *          setDiplomaticEntityState(targetUser, senderUser, newCurrentRelation);
             *          relations.Add(new DiplomaticRelation(targetUser.GetHashCode(), senderUser.GetHashCode(), (int)newCurrentRelation));
             *      }
             *  }
             * }
             *
             *
             * //and senderDiplomaticEntity proposes "his" relation
             * setDiplomaticEntityState(sender, target, relation);
             */

            foreach (var senderUser in sender.GetAllEntities())
            {
                foreach (var targetUser in target.GetAllEntities())
                {
                    setDiplomaticEntityState(senderUser, targetUser, relation);
                    relations.Add(new DiplomaticRelation(senderUser.GetHashCode(), targetUser.GetHashCode(), (int)relation));

                    /*
                     * if (UserRelations.IsLower(getRelation(senderUser, targetUser), newCurrentRelation))
                     * {
                     *  setDiplomaticEntityState(senderUser, targetUser, newCurrentRelation);
                     *  relations.Add(new DiplomaticRelation(senderUser.GetHashCode(), targetUser.GetHashCode(), (int)newCurrentRelation));
                     * }
                     * if (UserRelations.IsLower(getRelation(targetUser, senderUser), newCurrentRelation))
                     * {
                     *  setDiplomaticEntityState(targetUser, senderUser, newCurrentRelation);
                     *  relations.Add(new DiplomaticRelation(targetUser.GetHashCode(), senderUser.GetHashCode(), (int)newCurrentRelation));
                     * }
                     */
                }
            }
        }