]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Model/src/union.cpp
despues de estar como un pelotudo buscando el error, lo encontre, era un == ( yo...
[z.facultad/75.42/plaqui.git] / Model / src / union.cpp
1
2 #include "union.h"
3 #include <iostream>
4
5 using namespace PlaQui::Model;
6
7 Union::Union(const std::string &_name):Transport(_name)
8 {
9         in_slots = 2;
10         out_slots = 1;
11         max_flow = 0.0f;
12         in_on_zero = 0;
13         in_ready = 0;
14         actual_flow = 999999;
15         updated = false;
16 }
17
18 Union::~Union()
19 {
20 }
21
22 void Union::recieve_msg(int msg, IConector *who, void *data)
23 {
24         int pos = OUT;
25
26         // Verifico si esta conectado a mi entrada o a mi salida
27         std::list<IConector *>::iterator i;
28         for(i=in_list.begin(); i!=in_list.end(); i++) {
29                 if ((*i) == who) pos = IN;
30         }
31         switch (msg) {
32                 case MSG_QUERY_MAX_FLOW_OUT: {
33                         // Me preguntan por el flujo máximo.
34                         // Primero me actualizo, y luego respondo
35                         float m_data =  *((float *)data)*2;
36                         float tmp;
37
38                         if (updated) {
39                                 float tmp = actual_flow/2.0f;
40                                 who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
41                                 break;
42                         }
43                         updated = true;
44                         if (m_data == 0) {
45                                 in_on_zero++;
46                                 tmp = 0.0f;
47                                 who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
48                                 if (in_on_zero == 1) {
49                                         if (in_ready == 0)
50                                                 tmp = max_flow;
51                                         else
52                                                 tmp = actual_flow/2.0f;
53                                 }
54                                 send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, &tmp);
55                         } else {
56                                 switch (in_on_zero) {
57                                         case 0:
58                                                 actual_flow = (m_data<actual_flow)?m_data:actual_flow;
59                                                 actual_flow = (actual_flow<max_flow)?actual_flow:max_flow;
60                                         break;
61                                         case 1:
62                                                 if (in_ready == 1)
63                                                         actual_flow = actual_flow/2.0f;
64                                                 else
65                                                         actual_flow = max_flow/2.0f;
66                                         break;
67                                         case 2:
68                                                 actual_flow = 0;
69                                 }
70                                 send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, &actual_flow);
71                                 tmp = (in_on_zero==0)?actual_flow/2.0f:actual_flow;
72                                 who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
73                         }
74                         updated = true;
75                         if (pos == IN) in_ready++;
76                 }
77                 break;
78                 case MSG_RESPONSE_MAX_FLOW: {
79                         float max = *((float *)data);
80                         if (pos == OUT) {
81                                 if (max < actual_flow)  actual_flow = max;
82                                 if (in_on_zero == 2) actual_flow = 0;
83                         } else {
84                                         if (((2*max) < actual_flow) && (max != 0)) {
85                                                 actual_flow = 2*max;
86                                                 if (in_on_zero == 1) actual_flow /= 2.0;
87                                         }
88                         }
89                 }
90                 break;
91                 case MSG_RESPONSE_COLOR:
92                         if (in_colors == 0) {
93                                 color1 = *((RGB *)data);
94                                 entrada1 = static_cast<PlantItem *>(who);
95                                 flow1 = entrada1->get_actual_flow();
96                                 in_colors++;
97                         } else if ((in_colors == 1) && (entrada1 != who)) {
98                                 color2  = *((RGB *)data);
99                                 flow2 = static_cast<PlantItem *>(who)->get_actual_flow();
100                                 in_colors++;
101                                 if (color1 == color2) {
102                                                 std::cout << "TODO MAL" << std::endl << std::endl;
103                                 }
104
105                                 /* Ya tengo mis 2 colores, los sumo */
106                                 int r,g,b;
107                                 int n=2;
108                                 if (flow1 == 0) n--;
109                                 if (flow2 == 0) n--;
110                                 float suma = flow1+flow2;
111                                 if (n != 0) {
112                                         r = (int)(color1.r()*flow1/suma + color2.r()*flow2/suma);
113                                         g = (int)(color1.g()*flow1/suma + color2.g()*flow2/suma);
114                                         b = (int)(color1.b()*flow1/suma + color2.b()*flow2/suma);
115                                         set_color(RGB(r,g,b));
116                                 } else {
117                                         set_color(RGB(0,0,0));
118                                 }
119                                 color_updated = true;
120                         }
121                 break;
122                 default:
123                         Transport::recieve_msg(msg, who, data);
124         }
125 }
126
127 void Union::update(int dir)
128 {
129         // Si ya me actualice, no lo tengo que hacer de nuevo
130         if (updated) return;
131         
132         // Seteo mi actualizar en true para evitar entrar de nuevo
133         // FIXME : 99999 == INFINITO!!
134         actual_flow = 99999;
135         updated = true;
136         switch (dir) {
137                 case IN:
138                         send_msg(IN, MSG_QUERY_MAX_FLOW_IN, (void *)&max_flow);
139                 break;
140                 case OUT:
141                         send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, (void *)&max_flow);
142         }
143 }
144
145 void Union::simulate()
146 {
147         if (!updated) return;
148
149 #ifdef DEBUG
150         std::cout << name << "::Flujo actual = " << actual_flow << std::endl;
151 #endif
152         updated = false;
153         color_updated = false;
154         in_on_zero = 0;
155         in_ready = 0;
156         color_updated = false;
157         in_colors = 0;
158 }
159