1 //////////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 1993 - 2001 California Institute of Technology //
3 // //
4 // Read the COPYING and README files, or contact 'avida@alife.org', //
5 // before continuing. SOME RESTRICTIONS MAY APPLY TO USE OF THIS FILE. //
6 //////////////////////////////////////////////////////////////////////////////
7
8 #include "cEnvironmentScreen.h"
9
10 #include "cEnvironment.h"
11 #include "cPopulation.h"
12 #include "cReaction.h"
13 #include "cReactionProcess.h"
14 #include "cResource.h"
15 #include "cStats.h"
16 #include "cPopulationCell.h"
17 #include "cOrganism.h"
18
19 using namespace std;
20
Draw(cAvidaContext & ctx)21 void cEnvironmentScreen::Draw(cAvidaContext& ctx)
22 {
23 SetBoldColor(COLOR_WHITE);
24 Print(1, 54, "[ ] [ ]");
25 if (info.GetPauseLevel()) {
26 Print(3, 54, "Un-[ ]ause");
27 Print(4, 54, "[ ]ext Update");
28 } else {
29 Print(3, 54, "[ ]ause ");
30 Print(4, 54, " ");
31 }
32
33 SetBoldColor(COLOR_CYAN);
34 Print(1, 55, "<");
35 Print(1, 73, ">");
36 Print(5, 54, "[ ]");
37 if (info.GetPauseLevel()) {
38 Print(3, 58, "P");
39 Print(4, 55, "N");
40 } else {
41 Print(3, 55, "P");
42 }
43 Print(5, 55, "UP, DOWN");
44
45 if(mode==ENVIRONMENT_MODE_RESOURCE)
46 DrawResource();
47 else
48 DrawReaction();
49 Update(ctx);
50 }
51
DrawResource()52 void cEnvironmentScreen::DrawResource()
53 {
54 const cResourceLib & res_lib = m_world->GetEnvironment().GetResourceLib();
55
56 SetBoldColor(COLOR_WHITE);
57
58 Print(0, 0, " --Name-- ");
59 Print(0, 12, "--Inflow--");
60 Print(0, 24, "--Outflow--");
61 Print(0, 37, "--Quantity--");
62
63 Print(res_lib.GetSize()+3, 2, "Reactions associated with Resource ");
64 Print(res_lib.GetSize()+4, 2, "--Name-- --Num Rxns (last update)-- --");
65
66 Print(6, 54, "Next Resource");
67
68 SetBoldColor(COLOR_CYAN);
69 for(int i=0; i<res_lib.GetSize(); i++)
70 {
71 Print(i+1, 1, res_lib.GetResource(i)->GetName());
72 Print(i+1, 12, "%7.2f", res_lib.GetResource(i)->GetInflow());
73 Print(i+1, 24, "%7.2f", res_lib.GetResource(i)->GetOutflow());
74 }
75 if (info.GetPauseLevel()) {
76 Print(3, 58, "P");
77 Print(4, 55, "N");
78 } else {
79 Print(3, 55, "P");
80 }
81 Print(5, 55, "UP, DOWN");
82
83
84 SetColor(COLOR_WHITE);
85 Box(res_lib.GetSize()+2, 0, Height()-res_lib.GetSize()-2, Width(), true);
86
87 SetBoldColor(COLOR_YELLOW);
88 Print(1, 58, "Resource View");
89 }
90
DrawReaction()91 void cEnvironmentScreen::DrawReaction()
92 {
93 const cReactionLib & rxn_lib = m_world->GetEnvironment().GetReactionLib();
94
95 SetBoldColor(COLOR_WHITE);
96
97 Print(0, 0, " --Name-- ");
98 //Print(0, 12, "--Inflow--");
99 //Print(0, 24, "--Outflow--");
100 //Print(0, 37, "--Quantity--");
101
102 Print(rxn_lib.GetSize()+3, 2, "Resources associated with Reaction ");
103 Print(rxn_lib.GetSize()+4, 2, "--Name-- --Inflow-- --Outflow-- --Quantity--");
104
105 Print(6, 54, "Next Reaction");
106
107 SetBoldColor(COLOR_CYAN);
108 for(int i=0; i<rxn_lib.GetSize(); i++)
109 {
110 Print(i+1, 1, rxn_lib.GetReaction(i)->GetName());
111 //Print(i+1, 12, "%7.2f", res_lib.GetResource(i)->GetInflow());
112 //Print(i+1, 24, "%7.2f", res_lib.GetResource(i)->GetOutflow());
113 }
114
115 SetColor(COLOR_WHITE);
116 Box(rxn_lib.GetSize()+2, 0, Height()-rxn_lib.GetSize()-2, Width(), true);
117
118 SetBoldColor(COLOR_YELLOW);
119 Print(1, 58, "Reaction View");
120 }
121
Update(cAvidaContext & ctx)122 void cEnvironmentScreen::Update(cAvidaContext& ctx)
123 {
124 if(mode==ENVIRONMENT_MODE_RESOURCE)
125 UpdateResource(ctx);
126 else
127 UpdateReaction(ctx);
128 Refresh();
129 }
130
UpdateResource(cAvidaContext & ctx)131 void cEnvironmentScreen::UpdateResource(cAvidaContext& ctx)
132 {
133 const cResourceLib & res_lib = m_world->GetEnvironment().GetResourceLib();
134 const cReactionLib & rxn_lib = m_world->GetEnvironment().GetReactionLib();
135 const int num_resources = m_world->GetPopulation().GetResources(ctx).GetSize();
136
137 // If there are no resources, then we have nothing to update.
138 if (num_resources == 0) return;
139
140 // Update the quantity of each resource.
141 SetBoldColor(COLOR_CYAN);
142 for(int i = 0; i < num_resources; i++)
143 {
144 Print(i+1, 40, "%7.2f", m_world->GetPopulation().GetResources(ctx)[i]);
145 }
146
147 // Highlight the current resource in blue.
148 SetBoldColor(COLOR_BLUE);
149 Print(res_selection+1, 1, res_lib.GetResource(res_selection)->GetName());
150 Print(res_selection+1, 12, "%7.2f", res_lib.GetResource(res_selection)->GetInflow());
151 Print(res_selection+1, 24, "%7.2f", res_lib.GetResource(res_selection)->GetOutflow());
152 Print(res_selection+1, 40, "%7.2f", m_world->GetPopulation().GetResources(ctx)[res_selection]);
153
154 // Print all of the information about the reaction(s) associated with
155 // current resource.
156
157 int offset=0;
158 SetBoldColor(COLOR_CYAN);
159 for(int i=0; i<rxn_lib.GetSize(); i++) {
160 for(int j=0; j<rxn_lib.GetReaction(i)->GetProcesses().GetSize(); j++) {
161 if(rxn_lib.GetReaction(i)->GetProcesses().GetPos(j)->GetResource() != NULL)
162 {
163 cout << "BDB part 1 " << rxn_lib.GetReaction(i)->GetProcesses().GetPos(j)->GetResource()->GetName() << endl;
164 cout << "BDB part 2 " << res_lib.GetResource(res_selection)->GetName() << endl;
165 if(rxn_lib.GetReaction(i)->GetProcesses().GetPos(j)->GetResource()->GetName() ==
166 res_lib.GetResource(res_selection)->GetName()) {
167 int reactions = 0;
168 for(int k=0; k<m_world->GetPopulation().GetSize(); ++k) {
169 cPopulationCell& cell = m_world->GetPopulation().GetCell(k);
170 if(cell.IsOccupied()) {
171 const tArray<int>& org_rx = cell.GetOrganism()->GetPhenotype().GetLastReactionCount();
172 reactions += org_rx[i];
173 }
174 }
175
176 Print(num_resources + 5 + offset, 2, "%-10s %7d",
177 static_cast<const char*>(rxn_lib.GetReaction(i)->GetName()),
178 reactions);
179 offset++;
180 }
181 }
182 }
183 }
184
185 // Print the name of the current resource at the bottom of the screen.
186 SetBoldColor(COLOR_WHITE);
187 Print(res_lib.GetSize()+3, 37, "%s", static_cast<const char*>(res_lib.GetResource(res_selection)->GetName()));
188 Print(res_lib.GetSize()+3, res_lib.GetResource(res_selection)->GetName().GetSize()+37, ":");
189 Print(res_lib.GetSize()+3, res_lib.GetResource(res_selection)->GetName().GetSize()+38, " ");
190
191 }
192
UpdateReaction(cAvidaContext & ctx)193 void cEnvironmentScreen::UpdateReaction(cAvidaContext& ctx)
194 {
195 const cReactionLib & rxn_lib = m_world->GetEnvironment().GetReactionLib();
196 const cResourceLib & res_lib = m_world->GetEnvironment().GetResourceLib();
197 const int num_reactions = m_world->GetStats().GetReactions().GetSize();
198
199 // If we have no reactions, stop right here.
200 if (num_reactions == 0) return;
201
202 // Find the sum of the reactions
203
204 tArray<int> reactions(num_reactions);
205 reactions.SetAll(0);
206
207 for(int i=0; i<m_world->GetPopulation().GetSize(); ++i) {
208 cPopulationCell& cell = m_world->GetPopulation().GetCell(i);
209 if(cell.IsOccupied()) {
210 const tArray<int>& org_rx = cell.GetOrganism()->GetPhenotype().GetLastReactionCount();
211 for(int j=0; j<num_reactions; ++j) {
212 reactions[j] += org_rx[j];
213 }
214 }
215 }
216
217 // For each reaction, print how often it was performed.
218 SetBoldColor(COLOR_CYAN);
219 for(int i = 0; i < num_reactions; i++) {
220 Print(i+1, 40, "%7d", reactions[i]);
221 }
222
223 // Highlight the selected reaction.
224 SetBoldColor(COLOR_BLUE);
225 Print(rxn_selection+1, 1, rxn_lib.GetReaction(rxn_selection)->GetName());
226 Print(rxn_selection+1, 40, "%7d", reactions[rxn_selection]);
227
228
229 // Update header on reaction section.
230 SetBoldColor(COLOR_WHITE);
231 Print(rxn_lib.GetSize()+3, 37, "%-s", static_cast<const char*>(rxn_lib.GetReaction(rxn_selection)->GetName()));
232 Print(rxn_lib.GetSize()+3, rxn_lib.GetReaction(rxn_selection)->GetName().GetSize()+37, ":");
233 Print(rxn_lib.GetSize()+3, rxn_lib.GetReaction(rxn_selection)->GetName().GetSize()+38, " ");
234
235 // Print information about each resource associated with active reaction.
236 int offset=0;
237 SetBoldColor(COLOR_CYAN);
238 const int num_processes = rxn_lib.GetReaction(rxn_selection)->GetProcesses().GetSize();
239 for(int i = 0; i < num_processes; i++) {
240 cResource * cur_resource =
241 rxn_lib.GetReaction(rxn_selection)->GetProcesses().GetPos(i)->GetResource();
242
243 // Ignore all processes that are not associated with resources.
244 if (cur_resource == NULL) continue;
245
246 // Print info about this resource.
247 Print(m_world->GetStats().GetReactions().GetSize()+5+offset, 2,
248 "%-10s", static_cast<const char*>(cur_resource->GetName()));
249 Print(m_world->GetStats().GetReactions().GetSize()+5+offset, 13, "%7.2f",
250 cur_resource->GetInflow());
251 Print(m_world->GetStats().GetReactions().GetSize()+5+offset, 25, "%7.2f",
252 cur_resource->GetOutflow());
253 for(int j=0; j < res_lib.GetSize(); j++) {
254 if (res_lib.GetResource(j)->GetName() == cur_resource->GetName()) {
255 Print(m_world->GetStats().GetReactions().GetSize()+5+offset, 40, "%7.2f",
256 m_world->GetPopulation().GetResources(ctx)[j]);
257 }
258 }
259 offset++;
260 }
261 }
262
DoInput(cAvidaContext & ctx,int in_char)263 void cEnvironmentScreen::DoInput(cAvidaContext& ctx, int in_char)
264 {
265 int last_selection;
266 const cResourceLib & res_lib = m_world->GetEnvironment().GetResourceLib();
267 const cReactionLib & rxn_lib = m_world->GetEnvironment().GetReactionLib();
268 SetBoldColor(COLOR_CYAN);
269
270 switch (in_char) {
271 case KEY_DOWN:
272 if(mode==ENVIRONMENT_MODE_RESOURCE ) {
273 const int num_resources = m_world->GetPopulation().GetResources(ctx).GetSize();
274 if (num_resources > 0) {
275 last_selection=res_selection;
276 res_selection++;
277 res_selection %= num_resources;
278
279 Print(last_selection+1, 1, res_lib.GetResource(last_selection)->GetName());
280 Print(last_selection+1, 12, "%7.2f", res_lib.GetResource(last_selection)->GetInflow());
281 Print(last_selection+1, 24, "%7.2f", res_lib.GetResource(last_selection)->GetOutflow());
282 }
283 }
284 else { // ENVIRONMENT_MODE_REACTION
285 const int num_reactions = m_world->GetStats().GetReactions().GetSize();
286 if (num_reactions > 0) {
287 last_selection = rxn_selection;
288 rxn_selection++;
289 rxn_selection %= num_reactions;
290
291 Print(last_selection+1, 1, rxn_lib.GetReaction(last_selection)->GetName());
292 //Print(last_selection+1, 12, "%7.2f", rxn_lib.GetReaction(last_selection)->GetInflow());
293 //Print(last_selection+1, 24, "%7.2f", rxn_lib.GetReaction(last_selection)->GetOutflow());
294 }
295 }
296
297 Update(ctx);
298 break;
299 case KEY_UP:
300 if(mode == ENVIRONMENT_MODE_RESOURCE) {
301 const int num_resources = m_world->GetPopulation().GetResources(ctx).GetSize();
302 if (num_resources > 0) {
303 last_selection = res_selection;
304 res_selection--;
305 if(res_selection < 0) res_selection = num_resources - 1;
306
307 Print(last_selection+1, 1, res_lib.GetResource(last_selection)->GetName());
308 Print(last_selection+1, 12, "%7.2f", res_lib.GetResource(last_selection)->GetInflow());
309 Print(last_selection+1, 24, "%7.2f", res_lib.GetResource(last_selection)->GetOutflow());
310 }
311 }
312 else { // ENVIRONMENT_MODE_REACTIONS
313 const int num_reactions = m_world->GetStats().GetReactions().GetSize();
314 if (num_reactions > 0) {
315 last_selection=rxn_selection;
316 rxn_selection--;
317 if (rxn_selection < 0) rxn_selection = num_reactions - 1;
318
319 Print(last_selection+1, 1, rxn_lib.GetReaction(last_selection)->GetName());
320 //Print(last_selection+1, 12, "%7.2f", rxn_lib.GetReaction(last_selection)->GetInflow());
321 //Print(last_selection+1, 24, "%7.2f", rxn_lib.GetReaction(last_selection)->GetOutflow());
322 }
323 }
324
325 Update(ctx);
326 break;
327 case '>':
328 case '.':
329 case '<':
330 case ',':
331 if(mode==ENVIRONMENT_MODE_RESOURCE)
332 mode=ENVIRONMENT_MODE_REACTION;
333 else
334 mode=ENVIRONMENT_MODE_RESOURCE;
335 Clear();
336 Draw(ctx);
337 break;
338 }
339 }
340
341