Pac-Man Evolution
Loading...
Searching...
No Matches
BrainsFactory.cpp
1/*----------------------------------------------------------------------
2Pac-Man Evolution - Roberto Prieto
3Copyright (C) 2018-2024 MegaStorm Systems
4contact@megastormsystems.com - http://www.megastormsystems.com
5
6This software is provided 'as-is', without any express or implied
7warranty. In no event will the authors be held liable for any damages
8arising from the use of this software.
9
10Permission is granted to anyone to use this software for any purpose,
11including commercial applications, and to alter it and redistribute it
12freely, subject to the following restrictions:
13
141. The origin of this software must not be misrepresented; you must not
15claim that you wrote the original software. If you use this software
16in a product, an acknowledgment in the product documentation would be
17appreciated but is not required.
182. Altered source versions must be plainly marked as such, and must not be
19misrepresented as being the original software.
203. This notice may not be removed or altered from any source distribution.
21
22------------------------------------------------------------------------
23
24Brains Factory class
25
26------------------------------------------------------------------------ */
27
28#include "BrainsFactory.h"
29#include "Brains.h"
30#include "GameField.h"
31#include "EVNTrainer.h"
32
33// Singleton stuff
34BrainsFactory* BrainsFactory::mInstance = nullptr;
35
36// Create a single instance
37BrainsFactory& BrainsFactory::Instance()
38{
39 if(!mInstance) mInstance = new(std::nothrow) BrainsFactory;
40 return *mInstance;
41}
42
43// Explicit destructor.
44void BrainsFactory::Terminate()
45{
46 if(mInstance) delete mInstance;
47 mInstance = nullptr;
48}
49
50// Constructor
51BrainsFactory::BrainsFactory()
52{
53 Brain* pNewBrain;
54 Sint32 i;
55
56 // Create and register the brains. With error control
57 mapBrains.clear();
58 pNewBrain = new(std::nothrow) Brain();
59 if(mapBrains.insert(std::make_pair(PME_BRAIN_TYPE_RANDOM, pNewBrain)).second == false) delete pNewBrain;
60
61 pNewBrain = new(std::nothrow) BrainPacManHuman();
62 if(mapBrains.insert(std::make_pair(PME_BRAIN_TYPE_HUMAN | PME_OBJECT_PACMAN, pNewBrain)).second == false) delete pNewBrain;
63
64 pNewBrain = new(std::nothrow) BrainPacMan();
65 if(mapBrains.insert(std::make_pair(PME_BRAIN_TYPE_FIXED | PME_OBJECT_PACMAN, pNewBrain)).second == false) delete pNewBrain;
66
67 pNewBrain = new(std::nothrow) BrainRedGhost();
68 if(mapBrains.insert(std::make_pair(PME_BRAIN_TYPE_FIXED | PME_OBJECT_GHOST_RED, pNewBrain)).second == false) delete pNewBrain;
69 pNewBrain = new(std::nothrow) BrainEvolvedRedGhost(-1);
70 if(mapBrains.insert(std::make_pair(PME_BRAIN_TYPE_EVOLVED | PME_OBJECT_GHOST_RED, pNewBrain)).second == false) delete pNewBrain;
71 for(i = 0; i < PME_GA_POPULATION; ++i)
72 {
73 pNewBrain = new(std::nothrow) BrainEvolvedRedGhost(i);
74 if(mapBrains.insert(std::make_pair((PME_BRAIN_TYPE_TRAINING0 << i) | PME_OBJECT_GHOST_RED, pNewBrain)).second == false) delete pNewBrain;
75 }
76
77 pNewBrain = new(std::nothrow) BrainPinkGhost();
78 if(mapBrains.insert(std::make_pair(PME_BRAIN_TYPE_FIXED | PME_OBJECT_GHOST_PINK, pNewBrain)).second == false) delete pNewBrain;
79 pNewBrain = new(std::nothrow) BrainEvolvedPinkGhost(-1);
80 if(mapBrains.insert(std::make_pair(PME_BRAIN_TYPE_EVOLVED | PME_OBJECT_GHOST_PINK, pNewBrain)).second == false) delete pNewBrain;
81 for(i = 0; i < PME_GA_POPULATION; ++i)
82 {
83 pNewBrain = new(std::nothrow) BrainEvolvedPinkGhost(i);
84 if(mapBrains.insert(std::make_pair((PME_BRAIN_TYPE_TRAINING0 << i) | PME_OBJECT_GHOST_PINK, pNewBrain)).second == false) delete pNewBrain;
85 }
86
87 pNewBrain = new(std::nothrow) BrainBlueGhost();
88 if(mapBrains.insert(std::make_pair(PME_BRAIN_TYPE_FIXED | PME_OBJECT_GHOST_BLUE, pNewBrain)).second == false) delete pNewBrain;
89 pNewBrain = new(std::nothrow) BrainEvolvedBlueGhost(-1);
90 if(mapBrains.insert(std::make_pair(PME_BRAIN_TYPE_EVOLVED | PME_OBJECT_GHOST_BLUE, pNewBrain)).second == false) delete pNewBrain;
91 for(i = 0; i < PME_GA_POPULATION; ++i)
92 {
93 pNewBrain = new(std::nothrow) BrainEvolvedBlueGhost(i);
94 if(mapBrains.insert(std::make_pair((PME_BRAIN_TYPE_TRAINING0 << i) | PME_OBJECT_GHOST_BLUE, pNewBrain)).second == false) delete pNewBrain;
95 }
96
97 pNewBrain = new(std::nothrow) BrainOrangeGhost();
98 if(mapBrains.insert(std::make_pair(PME_BRAIN_TYPE_FIXED | PME_OBJECT_GHOST_ORANGE, pNewBrain)).second == false) delete pNewBrain;
99 pNewBrain = new(std::nothrow) BrainEvolvedOrangeGhost(-1);
100 if(mapBrains.insert(std::make_pair(PME_BRAIN_TYPE_EVOLVED | PME_OBJECT_GHOST_ORANGE, pNewBrain)).second == false) delete pNewBrain;
101 for(i = 0; i < PME_GA_POPULATION; ++i)
102 {
103 pNewBrain = new(std::nothrow) BrainEvolvedOrangeGhost(i);
104 if(mapBrains.insert(std::make_pair((PME_BRAIN_TYPE_TRAINING0 << i) | PME_OBJECT_GHOST_ORANGE, pNewBrain)).second == false) delete pNewBrain;
105 }
106
107 #ifdef DEBUG_INTERNAL
108 Main::Instance().ILogMgr().get()->msg(LML_NORMAL, " [BrainFactory] Info: registered '%d' brains.\n", mapBrains.size());
109 #endif
110}
111
112// Destructor
113BrainsFactory::~BrainsFactory()
114{
115 // Destroy all brains
116 for(auto itBrain = mapBrains.begin(); itBrain != mapBrains.end(); ++itBrain)
117 {
118 delete itBrain->second;
119 }
120 mapBrains.clear();
121}
122
123// Generic think method
124Sint32 BrainsFactory::think(Actor* pActor, Sint32 iBrainID, Sint32& iTX, Sint32& iTY, GameField* pGF)
125{
126 auto itBrain = mapBrains.find(iBrainID);
127 if(itBrain != mapBrains.end())
128 {
129 itBrain->second->think(pActor, iTX, iTY, pGF);
130 return 0;
131 }
132 return -1;
133}
134
135// Validate that a given id corresponds to a valid brain
136bool BrainsFactory::validate(Sint32 iBrainID)
137{
138 auto itBrain = mapBrains.find(iBrainID);
139 if(itBrain != mapBrains.end()) return true;
140 return false;
141}
142
143// Get name of a given brain id
144Sint32 BrainsFactory::getName(Sint32 iBrainID, string& sN)
145{
146 auto itBrain = mapBrains.find(iBrainID);
147 if(itBrain != mapBrains.end())
148 {
149 itBrain->second->getName(sN);
150 return 0;
151 }
152 return -1;
153}
154
155// Get target type
156Sint32 BrainsFactory::getTargetType(Sint32 iBrainID)
157{
158 auto itBrain = mapBrains.find(iBrainID);
159 if(itBrain != mapBrains.end())
160 {
161 return itBrain->second->getTargetType();
162 }
163 return -1;
164}
165
166// Get a pointer to the brain
167Brain* BrainsFactory::getBrain(Sint32 iBrainID)
168{
169 auto itBrain = mapBrains.find(iBrainID);
170 if(itBrain != mapBrains.end())
171 {
172 return itBrain->second;
173 }
174 return nullptr;
175}