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