]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Model/src/union.cpp
5f22ef59d7c726b9f508de17329d4e0d25c7d170
[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 }
16
17 Union::~Union()
18 {
19 }
20
21 void Union::recieve_msg(int msg, IConector *who, void *data)
22 {
23         int pos = OUT;
24
25         // Verifico si esta conectado a mi entrada o a mi salida
26         std::list<IConector *>::iterator i;
27         for(i=in_list.begin(); i!=in_list.end(); i++) {
28                 if ((*i) == who) pos = IN;
29         }
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 (m_data == 0) {
39                                 in_on_zero++;
40                                 tmp = 0.0f;
41                                 who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
42                                 if (in_on_zero == 1) {
43                                         if (in_ready == 0)
44                                                 tmp = max_flow;
45                                         else
46                                                 tmp = max_flow/2.0f;
47                                 }
48                                 send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, &tmp);
49                         } else {
50                                 switch (in_on_zero) {
51                                         case 0:
52                                                 actual_flow = (m_data<actual_flow)?m_data:actual_flow;
53                                                 actual_flow = (actual_flow<max_flow)?actual_flow:max_flow;
54                                         break;
55                                         case 1:
56                                                 if (in_ready == 1)
57                                                         actual_flow = actual_flow/2.0f;
58                                                 else
59                                                         actual_flow = max_flow/2.0f;
60                                         break;
61                                         case 2:
62                                                 actual_flow = 0;
63                                 }
64                                 send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, &actual_flow);
65                                 std::cout << in_on_zero << " " << actual_flow << std::endl;
66                                 tmp = (in_on_zero==0)?actual_flow/2.0f:actual_flow;
67                                 who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
68                         }
69                         updated = true;
70                         if (pos == IN) in_ready++;
71                 }
72                 break;
73                 case MSG_RESPONSE_MAX_FLOW: {
74                         float max = *((float *)data);
75                         if (pos == OUT) {
76                                 if (max < actual_flow)  actual_flow = max;
77                                 if (in_on_zero == 2) actual_flow = 0;
78                         } else {
79                                         if (((2*max) < actual_flow) && (max != 0))
80                                                 actual_flow = 2*max;
81                         }
82                 }
83                 break;
84                 default:
85                         Transport::recieve_msg(msg, who, data);
86         }
87 }
88
89 void Union::update(int dir)
90 {
91         // Si ya me actualice, no lo tengo que hacer de nuevo
92         if (updated) return;
93         // Seteo mi actualizar en true para evitar entrar de nuevo
94         // FIXME : 99999 == INFINITO!!
95         actual_flow = 99999;
96         updated = true;
97         switch (dir) {
98                 case IN:
99                         send_msg(IN, MSG_QUERY_MAX_FLOW_IN, (void *)&max_flow);
100                 break;
101                 case OUT:
102                         send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, (void *)&max_flow);
103         }
104 }
105
106 void Union::simulate()
107 {
108         RGB c_in1, c_in2;
109         unsigned r,g,b;
110
111         if (!updated) {
112                 return;
113         }
114         std::list<IConector *>::iterator i = in_list.begin();
115         if (i != in_list.end()) {
116                 PlantItem *o = (PlantItem *)(*i);
117                 c_in1 = o->get_color();
118                 i++;
119                 o = (PlantItem *)(*i);
120                 c_in2 = o->get_color();
121         }
122
123         r = ((c_in1.r()+c_in2.r())/2)%256;
124         g = ((c_in1.g()+c_in2.g())/2)%256;
125         b = ((c_in1.b()+c_in2.b())/2)%256;
126         set_color(RGB(r,g,b));
127         std::cout << name << "::Flujo actual = " << actual_flow << std::endl;
128         updated = false;
129         in_on_zero = 0;
130         in_ready = 0;
131 }
132