1 /*
2  *  Copyright (C) 2011-2016  OpenDungeons Team
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "game/Skill.h"
19 
20 #include "game/Player.h"
21 #include "game/SkillType.h"
22 #include "network/ODPacket.h"
23 #include "utils/Helper.h"
24 
25 
Skill(SkillType type,int32_t neededSkillPoints,const std::vector<const Skill * > & skillDepends)26 Skill::Skill(SkillType type, int32_t neededSkillPoints, const std::vector<const Skill*>& skillDepends):
27     mType(type),
28     mNeededSkillPoints(neededSkillPoints),
29     mSkillDepends(skillDepends)
30 {
31 }
32 
canBeSkilled(const std::vector<SkillType> & skillsDone) const33 bool Skill::canBeSkilled(const std::vector<SkillType>& skillsDone) const
34 {
35     for(const Skill* skill : mSkillDepends)
36     {
37         if(std::find(skillsDone.begin(), skillsDone.end(), skill->getType()) != skillsDone.end())
38             continue;
39 
40         return false;
41     }
42     return true;
43 }
44 
buildDependencies(const std::vector<SkillType> & skillsDone,std::vector<SkillType> & dependencies) const45 void Skill::buildDependencies(const std::vector<SkillType>& skillsDone, std::vector<SkillType>& dependencies) const
46 {
47     // If the current skill is already in the dependencies list, no need to process it
48     if(std::find(dependencies.begin(), dependencies.end(), getType()) != dependencies.end())
49         return;
50 
51     for(const Skill* skill : mSkillDepends)
52     {
53         SkillType resType = skill->getType();
54         if(std::find(skillsDone.begin(), skillsDone.end(), resType) != skillsDone.end())
55             continue;
56 
57         if(std::find(dependencies.begin(), dependencies.end(), resType) != dependencies.end())
58             continue;
59 
60         skill->buildDependencies(skillsDone, dependencies);
61     }
62 
63     if(std::find(dependencies.begin(), dependencies.end(), getType()) == dependencies.end())
64         dependencies.push_back(getType());
65 }
66 
dependsOn(const std::vector<SkillType> & skills) const67 bool Skill::dependsOn(const std::vector<SkillType>& skills) const
68 {
69     for(SkillType resType : skills)
70     {
71         if(dependsOn(resType))
72             return true;
73     }
74     return false;
75 }
76 
dependsOn(SkillType type) const77 bool Skill::dependsOn(SkillType type) const
78 {
79     if(getType() == type)
80         return true;
81 
82     for(const Skill* skill : mSkillDepends)
83     {
84         if(skill->getType() == type)
85             return true;
86 
87         if(skill->dependsOn(type))
88             return true;
89     }
90 
91     return false;
92 }
93