]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Model/src/union.cpp
* Se agrega un nuevo mensaje para intercambio de colores que anda muy bien
[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                                 flow1 = static_cast<PlantItem *>(who)->get_actual_flow();
95                                 in_colors++;
96                         } else {
97                                 color2  = *((RGB *)data);
98                                 flow2 = static_cast<PlantItem *>(who)->get_actual_flow();
99
100                                 /* Ya tengo mis 2 colores, los sumo */
101                                 int r,g,b;
102                                 int n=2;
103                                 if (flow1 == 0) n--;
104                                 if (flow2 == 0) n--;
105                                 float suma = flow1+flow2;
106                                 if (n != 0) {
107                                         r = (int)(color1.r()*flow1/suma + color2.r()*flow2/suma)/n;
108                                         g = (int)(color1.g()*flow1/suma + color2.g()*flow2/suma)/n;
109                                         b = (int)(color1.b()*flow1/suma + color2.b()*flow2/suma)/n;
110                                         r %= 256;
111                                         g %= 256;
112                                         b %= 256;
113                                         set_color(RGB(r,g,b));
114                                 } else {
115                                         set_color(RGB(0,0,0));
116                                 }
117                                 color_updated = true;
118                         }
119                 break;
120                 default:
121                         Transport::recieve_msg(msg, who, data);
122         }
123 }
124
125 void Union::update(int dir)
126 {
127         // Si ya me actualice, no lo tengo que hacer de nuevo
128         if (updated) return;
129         
130         // Seteo mi actualizar en true para evitar entrar de nuevo
131         // FIXME : 99999 == INFINITO!!
132         actual_flow = 99999;
133         updated = true;
134         switch (dir) {
135                 case IN:
136                         send_msg(IN, MSG_QUERY_MAX_FLOW_IN, (void *)&max_flow);
137                 break;
138                 case OUT:
139                         send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, (void *)&max_flow);
140         }
141 }
142
143 void Union::simulate()
144 {
145         if (!updated) return;
146
147 #ifdef DEBUG
148         std::cout << name << "::Flujo actual = " << actual_flow << std::endl;
149 #endif
150         updated = false;
151         color_updated = false;
152         in_on_zero = 0;
153         in_ready = 0;
154         color_updated = false;
155         in_colors = 0;
156 }
157