]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Model/src/union.cpp
subo el workplace.h y decime si asi esta bien puesto el comentario
[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 = INFINITO;
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                                 if (m_data == 0) {
40                                         tmp = 0;
41                                         actual_flow /= 2.0f;
42                                 } else {
43                                         tmp = actual_flow/2.0f;
44                                 }
45                                 who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
46                                 break;
47                         }*/
48                         updated = true;
49                         if (m_data == 0) {
50                                 in_on_zero++;
51                                 tmp = 0.0f;
52                                 who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
53                                 switch (in_on_zero) {
54                                         case 1:
55                                                 if (in_ready == 0)
56                                                         tmp = max_flow;
57                                                 else
58                                                         tmp = actual_flow/2.0f;
59                                         break;
60                                         case 0:
61                                                 if (in_ready == 1) {
62                                                         tmp = actual_flow/2.0f;
63                                                 }
64                                         break;
65                                         case 2:
66                                                 tmp = 0.0f;
67                                 }
68                                 send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, &tmp);
69                         } else {
70                                 switch (in_on_zero) {
71                                         case 0:
72                                                 actual_flow = (m_data<actual_flow)?m_data:actual_flow;
73                                                 actual_flow = (actual_flow<max_flow)?actual_flow:max_flow;
74                                         break;
75                                         case 1:
76                                                 if (in_ready == 1)
77                                                         actual_flow = actual_flow/2.0f;
78                                                 else
79                                                         actual_flow = max_flow/2.0f;
80                                         break;
81                                         case 2:
82                                                 actual_flow = 0;
83                                 }
84                                 send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, &actual_flow);
85                                 tmp = (in_on_zero==0)?actual_flow/2.0f:actual_flow;
86                                 who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
87                         }
88                         updated = true;
89                         if (pos == IN) in_ready++;
90                 }
91                 break;
92                 case MSG_RESPONSE_MAX_FLOW: {
93                         float max = *((float *)data);
94                         if (pos == OUT) {
95                                 if (max < actual_flow)  actual_flow = max;
96                                 if (in_on_zero == 2) actual_flow = 0;
97                         } else {
98                                         if (((2*max) < actual_flow) && (max != 0)) {
99                                                 actual_flow = 2*max;
100                                                 if (in_on_zero == 1) actual_flow /= 2.0;
101                                         }
102                         }
103                 }
104                 break;
105                 case MSG_RESPONSE_COLOR:
106                         if (in_colors == 0) {
107                                 color1 = *((RGB *)data);
108                                 entrada1 = static_cast<PlantItem *>(who);
109                                 flow1 = entrada1->get_actual_flow();
110                                 in_colors++;
111                         } else if ((in_colors == 1) && (entrada1 != who)) {
112                                 color2  = *((RGB *)data);
113                                 flow2 = static_cast<PlantItem *>(who)->get_actual_flow();
114                                 in_colors++;
115                                 if (color1 == color2) {
116                                                 std::cout << "TODO MAL" << std::endl << std::endl;
117                                 }
118
119                                 /* Ya tengo mis 2 colores, los sumo */
120                                 int r,g,b;
121                                 int n=2;
122                                 if (flow1 == 0) n--;
123                                 if (flow2 == 0) n--;
124                                 float suma = flow1+flow2;
125                                 if (n != 0) {
126                                         r = (int)(color1.r()*flow1/suma + color2.r()*flow2/suma);
127                                         g = (int)(color1.g()*flow1/suma + color2.g()*flow2/suma);
128                                         b = (int)(color1.b()*flow1/suma + color2.b()*flow2/suma);
129                                         set_color(RGB(r,g,b));
130                                 } else {
131                                         set_color(RGB(0,0,0));
132                                 }
133                                 color_updated = true;
134                         }
135                 break;
136                 default:
137                         Transport::recieve_msg(msg, who, data);
138         }
139 }
140
141 void Union::update(int dir)
142 {
143         // Si ya me actualice, no lo tengo que hacer de nuevo
144         if (updated) return;
145         
146         // Seteo mi actualizar en true para evitar entrar de nuevo
147         actual_flow = INFINITO;
148         updated = true;
149         switch (dir) {
150                 case IN:
151                         send_msg(IN, MSG_QUERY_MAX_FLOW_IN, (void *)&max_flow);
152                 break;
153                 case OUT:
154                         send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, (void *)&max_flow);
155         }
156 }
157
158 void Union::simulate()
159 {
160         if (!updated) return;
161
162 #ifdef DEBUG
163         std::cout << name << "::Flujo actual = " << actual_flow << std::endl;
164 #endif
165         updated = false;
166         color_updated = false;
167         in_on_zero = 0;
168         in_ready = 0;
169         color_updated = false;
170         in_colors = 0;
171 }
172