Example #1
0
        private int checkCharacterListDeath(Dictionary <int, FullCombatCharacter> characters, bool justGetCount, bool isNPC = false)
        {
            int livingCount = characters.Count;

            foreach (int key in characters.Keys)
            {
                if (!characters[key].defeated && characters[key].hp <= 0)
                {
                    characters[key].hp             = 0;
                    characters[key].turnOrder      = 0;
                    characters[key].nextAttackTime = int.MaxValue;
                    if (!justGetCount)
                    {
                        characters[key].defeated = true;
                        currentEffects.Add(new Effect(EffectTypes.DestroyCharacter, characters[key].combatUniq, string.Empty, 0));
                        if (isNPC)
                        {
                            //Calculate xp and cp
                            MapDataClasses.MapDataClasses.Enemy enemy = MapDataClasses.MapDataManager.getEnemy(map, characters[key].className, false);
                            getXPAndCP(enemy.xp, enemy.cp);
                        }
                    }
                    livingCount--;
                }
                else if (characters[key].defeated)
                {
                    livingCount--;
                }
            }

            return(livingCount);
        }
Example #2
0
        public Combat(PlayerModels.PlayerModel playerModel, string map, int encounterSelection, Func <float> initiativeCalculator, Action onGameOver, Action onUpdate, Action <CombatEndType> onCombatComplete)
        {
            int currentUniq = 1;

            this.playerModel = playerModel;
            uniqBridge       = new Dictionary <int, int>();
            pcs                   = new Dictionary <int, FullCombatCharacter>();
            npcs                  = new Dictionary <int, FullCombatCharacter>();
            currentEffects        = new List <IEffect>();
            currentCharacter      = new FullCombatCharacter();
            this.onGameOver       = onGameOver;
            this.onUpdate         = onUpdate;
            this.onCombatComplete = onCombatComplete;
            this.map              = map;
            bool canFlee = true;

            List <PlayerModels.Models.PartyCharacterModel> partyCharacterModels = PlayerModels.PlayerDataManager.getCurrentPartyPartyStats(playerModel);
            List <int> characterUniqs = new List <int>();

            foreach (PlayerModels.Models.PartyCharacterModel pcm in partyCharacterModels)
            {
                characterUniqs.Add(pcm.characterUniq);
            }
            List <PlayerModels.Models.CharacterModel> characterModels = PlayerModels.PlayerDataManager.getCurrentParty(playerModel, characterUniqs);
            bool hasPreviousCombatModels = false;

            if (playerModel.currentCombat == null)
            {
                combatCharacterModels                 = new List <PlayerModels.CombatDataModels.CombatCharacterModel>();
                combatNPCModels                       = new List <CombatCharacterModel>();
                playerModel.currentCombat             = new CombatModel();
                playerModel.currentCombat.currentTime = 0;
                playerModel.currentCombat.pcs         = combatCharacterModels;
                playerModel.currentCombat.npcs        = combatNPCModels;
            }
            else
            {
                combatCharacterModels   = playerModel.currentCombat.pcs;
                combatNPCModels         = playerModel.currentCombat.npcs;
                hasPreviousCombatModels = true;
            }

            foreach (int characterUniq in characterUniqs)
            {
                string name           = string.Empty;
                int    hp             = 0;
                int    maxHP          = 0;
                int    mp             = 0;
                int    maxMP          = 0;
                int    turnOrder      = 0;
                string classType      = string.Empty;
                int    level          = 0;
                int    strength       = 0;
                int    vitality       = 0;
                int    agility        = 0;
                int    intellect      = 0;
                int    wisdom         = 0;
                int    nextAttackTime = 0;
                int    classLevel     = 0;
                int    combatUniq     = currentUniq;
                List <CombatModificationsModel> mods = new List <CombatModificationsModel>();
                List <string> usedAbilities          = new List <string>();
                uniqBridge.Add(currentUniq, characterUniq);

                foreach (PlayerModels.Models.CharacterModel cm in characterModels)
                {
                    if (cm.uniq == characterUniq)
                    {
                        name      = cm.name;
                        maxHP     = cm.stats.maxHP;
                        maxMP     = cm.stats.maxMP;
                        classType = cm.currentClass;
                        level     = cm.lvl;
                        strength  = cm.stats.strength;
                        vitality  = cm.stats.vitality;
                        agility   = cm.stats.agility;
                        intellect = cm.stats.intellect;
                        wisdom    = cm.stats.wisdom;

                        foreach (PlayerModels.Models.CharacterClassModel ccm in cm.characterClasses)
                        {
                            if (ccm.className == classType)
                            {
                                classLevel = ccm.lvl;
                            }
                        }
                    }
                }

                foreach (PlayerModels.Models.PartyCharacterModel pcm in partyCharacterModels)
                {
                    if (pcm.characterUniq == characterUniq)
                    {
                        hp            = pcm.hp;
                        mp            = pcm.mp;
                        usedAbilities = (pcm.usedAbilities == null ? new List <string>() : pcm.usedAbilities);
                    }
                }

                nextAttackTime = GeneralProcessor.calculateNextAttackTime(0, initiativeCalculator(), agility);

                if (hasPreviousCombatModels)
                {
                    foreach (CombatCharacterModel ccm in combatCharacterModels)
                    {
                        if (ccm.characterUniq == characterUniq)
                        {
                            nextAttackTime = ccm.nextAttackTime;
                            hp             = ccm.stats.hp;
                            mp             = ccm.stats.mp;
                            combatUniq     = ccm.combatUniq;
                        }
                    }
                }
                else
                {
                    combatCharacterModels.Add(new PlayerModels.CombatDataModels.CombatCharacterModel()
                    {
                        characterUniq = characterUniq,
                        stats         = new PlayerModels.CombatDataModels.TemporaryCombatStatsModel()
                        {
                            hp = hp, mp = mp
                        },
                        nextAttackTime = nextAttackTime,
                        combatUniq     = combatUniq
                    });
                }

                pcs.Add(characterUniq, new FullCombatCharacter()
                {
                    name           = name,
                    maxHP          = maxHP,
                    hp             = hp,
                    maxMP          = maxMP,
                    mp             = mp,
                    characterUniq  = characterUniq,
                    combatUniq     = currentUniq,
                    className      = classType,
                    classLevel     = classLevel,
                    level          = level,
                    strength       = strength,
                    vitality       = vitality,
                    intellect      = intellect,
                    agility        = agility,
                    wisdom         = wisdom,
                    nextAttackTime = nextAttackTime,
                    mods           = new List <CombatModificationsModel>(),
                    usedAbilities  = usedAbilities
                });

                combatCharacterModels[combatCharacterModels.Count - 1].mods = pcs[characterUniq].mods;

                currentUniq++;
            }

            if (hasPreviousCombatModels) //Combat was already initialized previously, just load the previous combatants
            {
                foreach (CombatCharacterModel ccm in combatNPCModels)
                {
                    int    hp             = ccm.stats.hp;
                    int    mp             = ccm.stats.mp;
                    string name           = ccm.name;
                    int    nextAttackTime = ccm.nextAttackTime;
                    List <CombatModificationsModel> mods = ccm.mods;
                    List <string> usedAbilities          = new List <string>();

                    MapDataClasses.MapDataClasses.Enemy enemy = MapDataClasses.MapDataManager.getEnemy(map, ccm.classType);

                    if (!this.npcs.ContainsKey(currentUniq))
                    {
                        this.npcs.Add(currentUniq, new FullCombatCharacter()
                        {
                            name           = name,
                            hp             = hp,
                            maxHP          = enemy.maxHP,
                            mp             = mp,
                            maxMP          = enemy.maxMP,
                            characterUniq  = 0,
                            combatUniq     = currentUniq,
                            className      = enemy.type,
                            level          = enemy.level,
                            strength       = enemy.strength,
                            vitality       = enemy.vitality,
                            agility        = enemy.agility,
                            intellect      = enemy.intellect,
                            wisdom         = enemy.wisdom,
                            nextAttackTime = nextAttackTime,
                            mods           = mods,
                            usedAbilities  = usedAbilities
                        });
                    }

                    currentUniq++;
                }
            }
            else //Generate a new encounter based on the map type
            {
                Encounter encounter = MapDataClasses.MapDataManager.getEncounter(this.playerModel.getActiveParty().location, playerModel.rootX, playerModel.rootY, encounterSelection);
                currentEffects.Add(new Effect(EffectTypes.Message, 0, encounter.message, 0));
                combatNPCModels = new List <PlayerModels.CombatDataModels.CombatCharacterModel>();
                canFlee         = encounter.canFlee;
                foreach (MapDataClasses.MapDataClasses.Enemy enemy in encounter.enemies)
                {
                    int nextAttackTime = GeneralProcessor.calculateNextAttackTime(0, initiativeCalculator(), enemy.agility);
                    int hp             = enemy.maxHP;
                    int mp             = enemy.maxMP;
                    int combatUniq     = currentUniq;
                    List <CombatModificationsModel> mods = new List <CombatModificationsModel>();
                    List <string> usedAbilities          = new List <string>();

                    this.npcs.Add(currentUniq, new FullCombatCharacter()
                    {
                        name           = enemy.name,
                        hp             = hp,
                        maxHP          = enemy.maxHP,
                        mp             = mp,
                        maxMP          = enemy.maxMP,
                        characterUniq  = 0,
                        combatUniq     = currentUniq,
                        className      = enemy.type,
                        level          = enemy.level,
                        strength       = enemy.strength,
                        vitality       = enemy.vitality,
                        agility        = enemy.agility,
                        intellect      = enemy.intellect,
                        wisdom         = enemy.wisdom,
                        nextAttackTime = nextAttackTime,
                        mods           = mods,
                        usedAbilities  = usedAbilities
                    });


                    this.combatNPCModels.Add(new PlayerModels.CombatDataModels.CombatCharacterModel()
                    {
                        name  = enemy.name,
                        stats = new PlayerModels.CombatDataModels.TemporaryCombatStatsModel()
                        {
                            hp = hp,
                            mp = mp
                        },
                        nextAttackTime = nextAttackTime,
                        combatUniq     = currentUniq,
                        characterUniq  = 0,
                        classType      = enemy.type,
                        mods           = this.npcs[currentUniq].mods
                    });

                    currentUniq++;
                }
            }

            playerModel.currentCombat.npcs = combatNPCModels;
            if (combatNPCModels.Count == 0)
            {
                throw new Exception("No NPCs were loaded :(");
            }

            calculateTurnOrder();
            this.combatData = new CombatData(playerModel.currentCombat, canFlee);

            if (!this.combatData.combatInitalized)
            {
                foreach (int key in pcs.Keys) //Check for initial code
                {
                    FullCombatCharacter fcc        = pcs[key];
                    List <IEffect>      newEffects = GeneralProcessor.initialExecute(fcc)(getAllPcsAsList(), getAllNpcsAsList(), this.combatData);
                    currentEffects.AddRange(newEffects);
                }

                this.combatData.combatInitalized = true;
            }
            calculateTurn(false);
        }