-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMasterState.cpp
More file actions
224 lines (178 loc) · 6.27 KB
/
Copy pathMasterState.cpp
File metadata and controls
224 lines (178 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "MasterState.h"
#include "CommandList.h"
#include "Company.h"
#include "Language.h"
#include "Market.h"
#include "NameContainer.h"
#include "Person.h"
#include "Platform.h"
#include "Trait.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
using std::string;
MasterState::MasterState() {
this->_playerCompany = new Company("Testing Corp", 100000);
this->_allCompanies = new list<Company *>();
this->_allCompanies->push_back(this->_playerCompany);
this->_allWorkers = new list<Person *>();
this->_allConsumers = new list<Person *>();
this->_time = 0;
this->_commandList = new CommandList(this);
this->_messages = new vector<char *>();
this->_names = new NameContainer();
this->_allMarkets = new vector<Market *>();
Market *oldPeopleMarket = new Market("Old People");
oldPeopleMarket->addRequirement("stability", 10);
this->_allMarkets->push_back(oldPeopleMarket);
this->_setupPlatforms();
this->_setupLanguages();
this->_setupConsumers();
}
vector<Market *> *MasterState::getMarkets() {
return this->_allMarkets;
}
vector <char *> *MasterState::getMessages() {
return this->_messages;
}
void MasterState::clearMessages() {
for (vector<char *>::iterator it = this->_messages->begin(); it != this->_messages->end(); ++it) {
char *tmp = *it;
free(tmp);
}
this->_messages->clear();
}
void MasterState::addMessage(const char *message) {
char *tmp = (char *)malloc(strlen(message) + 1);
strcpy(tmp, message);
this->_messages->push_back(tmp);
}
int MasterState::getTime() {
return this->_time;
}
vector<Platform *> *MasterState::getPlatforms() {
return this->_allPlatforms;
}
vector<Language *> *MasterState::getLanguages() {
return this->_allLanguages;
}
Company *MasterState::getPlayerCompany() {
return this->_playerCompany;
}
list <string> *MasterState::advanceTime(int amount) {
for (int i = 0; i < amount; i++) {
this->_time++;
// One in ten chance of creating a new person.
if (rand() % (this->_allWorkers->size() + 1) == 0) {
this->_createWorkers(1);
}
for (list<Company *>::iterator it = this->_allCompanies->begin(); it != this->_allCompanies->end(); ++it) {
Company *c = *it;
c->doPayments(this->_time);
for (list<Person *>::iterator it = this->_allWorkers->begin(); it != this->_allWorkers->end(); ++it) {
int found = 0;
vector<Person *> *knownPeople = c->getKnownPeople();
for (vector<Person *>::iterator it2 = knownPeople->begin(); it2 != knownPeople->end(); ++it2) {
if (*it == *it2) {
found = 1;
break;
}
}
if (found == 0) {
// Every person that's not already known has a 1 in 10
// chance of becoming discovered by the company. This number
// needs to be tweaked as it causes way too many employees
// too early.
if (rand() % 10) {
c->addKnownPerson(*it);
}
}
}
c->advanceTime(this->_time);
}
}
}
void MasterState::_setupLanguages() {
this->_allLanguages = new vector<Language *>();
this->_allLanguages->push_back(new Language("C"));
this->_allLanguages->push_back(new Language("C++"));
this->_allLanguages->push_back(new Language("JavaScript"));
this->_allLanguages->push_back(new Language("Lua"));
this->_allLanguages->push_back(new Language("Perl"));
this->_allLanguages->push_back(new Language("PHP"));
this->_allLanguages->push_back(new Language("Python"));
this->_allLanguages->push_back(new Language("Ruby"));
}
void MasterState::_setupPlatforms() {
this->_allPlatforms = new vector<Platform *>();
Platform *platform = new Platform("Browser");
platform->addProvides("mobility", 10);
this->_allPlatforms->push_back(platform);
this->_allPlatforms->push_back(new Platform("Console"));
this->_allPlatforms->push_back(new Platform("Desktop"));
this->_allPlatforms->push_back(new Platform("Mobile"));
this->_allPlatforms->push_back(new Platform("Server"));
}
void MasterState::_setupConsumers() {
for (int i = 0; i < 500; i++) {
string firstName = this->_names->getFirstName();
string lastName = this->_names->getLastName();
int money = rand() % 100000;
Person *p = new Person(firstName, lastName, money);
for (vector<Platform *>::iterator it = this->_allPlatforms->begin(); it != this->_allPlatforms->end(); ++it) {
p->setPlatformSkill(*it, rand() % 100);
}
for (vector<Language *>::iterator it2 = this->_allLanguages->begin(); it2 != this->_allLanguages->end(); ++it2) {
p->setLanguageSkill(*it2, rand() % 100);
}
this->_allConsumers->push_back(p);
}
}
void MasterState::_createWorkers(int count) {
for (int i = 0; i < count; i++) {
string firstName = this->_names->getFirstName();
string lastName = this->_names->getLastName();
int money = rand() % 100000;
char message[100];
sprintf(message, "%s %s applied for a job.", firstName.c_str(), lastName.c_str());
this->addMessage(message);
Person *p = new Person(firstName, lastName, money);
for (vector<Platform *>::iterator it = this->_allPlatforms->begin(); it != this->_allPlatforms->end(); ++it) {
p->setPlatformSkill(*it, rand() % 100);
}
for (vector<Language *>::iterator it2 = this->_allLanguages->begin(); it2 != this->_allLanguages->end(); ++it2) {
p->setLanguageSkill(*it2, rand() % 100);
}
this->_allWorkers->push_back(p);
}
}
void MasterState::executeCommand(const char *command) {
this->_commandList->executeCommand(command);
}
MasterState::~MasterState() {
delete this->_playerCompany;
this->_playerCompany = NULL;
delete this->_allCompanies;
this->_allCompanies = NULL;
for (list<Person *>::iterator allWorkersIterator = this->_allWorkers->begin(); allWorkersIterator != this->_allWorkers->end(); ++allWorkersIterator) {
Person *p = *allWorkersIterator;
delete p;
}
delete this->_allWorkers;
this->_allWorkers = NULL;
for (list<Person *>::iterator allConsumersIterator = this->_allConsumers->begin(); allConsumersIterator != this->_allConsumers->end(); ++allConsumersIterator) {
Person *p = *allConsumersIterator;
delete p;
}
delete this->_allConsumers;
this->_allConsumers = NULL;
for (vector<Platform *>::iterator platformIterator = this->_allPlatforms->begin(); platformIterator != this->_allPlatforms->end(); ++platformIterator) {
Platform *platform = *platformIterator;
delete platform;
}
delete this->_allPlatforms;
this->_allPlatforms = NULL;
delete this->_playerCompany;
this->_playerCompany = NULL;
}