]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Model/src/union.cpp
* Se mejora la deteccion de conexiones en el constructor
[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                                 who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &actual_flow);
40                                 break;
41                         }
42                         updated = true;
43                         if (m_data == 0) {
44                                 in_on_zero++;
45                                 tmp = 0.0f;
46                                 who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
47                                 if (in_on_zero == 1) {
48                                         if (in_ready == 0)
49                                                 tmp = max_flow;
50                                         else
51                                                 tmp = actual_flow/2.0f;
52                                 }
53                                 send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, &tmp);
54                         } else {
55                                 switch (in_on_zero) {
56                                         case 0:
57                                                 actual_flow = (m_data<actual_flow)?m_data:actual_flow;
58                                                 actual_flow = (actual_flow<max_flow)?actual_flow:max_flow;
59                                         break;
60                                         case 1:
61                                                 if (in_ready == 1)
62                                                         actual_flow = actual_flow/2.0f;
63                                                 else
64                                                         actual_flow = max_flow/2.0f;
65                                         break;
66                                         case 2:
67                                                 actual_flow = 0;
68                                 }
69                                 send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, &actual_flow);
70                                 tmp = (in_on_zero==0)?actual_flow/2.0f:actual_flow;
71                                 who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
72                         }
73                         updated = true;
74                         if (pos == IN) in_ready++;
75                 }
76                 break;
77                 case MSG_RESPONSE_MAX_FLOW: {
78                         float max = *((float *)data);
79                         if (pos == OUT) {
80                                 if (max < actual_flow)  actual_flow = max;
81                                 if (in_on_zero == 2) actual_flow = 0;
82                         } else {
83                                         if (((2*max) < actual_flow) && (max != 0)) {
84                                                 actual_flow = 2*max;
85                                                 if (in_on_zero == 1) actual_flow /= 2.0;
86                                         }
87                         }
88                 }
89                 break;
90                 default:
91                         Transport::recieve_msg(msg, who, data);
92         }
93 }
94
95 void Union::update(int dir)
96 {
97         // Si ya me actualice, no lo tengo que hacer de nuevo
98         RGB c_in1, c_in2;
99         unsigned r=0,g=0,b=0;
100         float c_f1, c_f2; // para ver los flujos de cada uno
101
102         if (updated) {
103                 std::list<IConector *>::iterator i = in_list.begin();
104                 if (i != in_list.end()) {
105                         PlantItem *o = (PlantItem *)(*i);
106                         c_in1 = o->get_color();
107                         c_f1 = o->get_actual_flow();
108                         i++;
109                         o = (PlantItem *)(*i);
110                         c_in2 = o->get_color();
111                         c_f2 = o->get_actual_flow();
112                 }
113
114                 set_color( RGB(r,g,b) );
115                 if ((c_f1 != 0) && (c_f2 != 0)) {
116                         // Si ambas entradas traen flujo, sumo sus colores
117                         int total = c_f1+c_f2;
118                         r = ((int)((c_in1.r()*c_f1/total+c_in2.r()*c_f2/total)))%256;
119                         g = ((int)((c_in1.g()*c_f1/total+c_in2.g()*c_f2/total)))%256;
120                         b = ((int)((c_in1.b()*c_f1/total+c_in2.b()*c_f2/total)))%256;
121                         set_color(RGB(r,g,b));
122                 } else {
123                         // Veo que entrada trae color
124                         if (c_f1 != 0) {
125                                 set_color( c_in1 );
126                         }
127                         if (c_f2 != 0) {
128                                 set_color( c_in2 );
129                         }
130                 }
131                 return;
132         }
133         // Seteo mi actualizar en true para evitar entrar de nuevo
134         // FIXME : 99999 == INFINITO!!
135         actual_flow = 99999;
136         updated = true;
137         switch (dir) {
138                 case IN:
139                         send_msg(IN, MSG_QUERY_MAX_FLOW_IN, (void *)&max_flow);
140                 break;
141                 case OUT:
142                         send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, (void *)&max_flow);
143         }
144 }
145
146 void Union::simulate()
147 {
148         if (!updated) return;
149
150 #ifdef DEBUG
151         std::cout << name << "::Flujo actual = " << actual_flow << std::endl;
152 #endif
153         updated = false;
154         in_on_zero = 0;
155         in_ready = 0;
156 }
157