]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blobdiff - Model/src/union.cpp
* Se agrega carga de lineas entre compuertas.
[z.facultad/75.42/plaqui.git] / Model / src / union.cpp
index 331e9536245f099f0ce5f54dc2d9572783db5ead..a9397ddf10202b10cdc17b7b0c3eec9ea6700162 100644 (file)
@@ -8,7 +8,11 @@ Union::Union(const std::string &_name):Transport(_name)
 {
        in_slots = 2;
        out_slots = 1;
-       max_flow = actual_flow = 0.0f;
+       max_flow = 0.0f;
+       in_on_zero = 0;
+       in_ready = 0;
+       actual_flow = 999999;
+       updated = false;
 }
 
 Union::~Union()
@@ -19,60 +23,151 @@ void Union::recieve_msg(int msg, IConector *who, void *data)
 {
        int pos = OUT;
 
-       // Verifico si esta conectado a mi entrada
+       // Verifico si esta conectado a mi entrada o a mi salida
        std::list<IConector *>::iterator i;
        for(i=in_list.begin(); i!=in_list.end(); i++) {
                if ((*i) == who) pos = IN;
        }
-       
        switch (msg) {
-               case MSG_QUERY_MAX_FLOW: {
+               case MSG_QUERY_MAX_FLOW_OUT: {
                        // Me preguntan por el flujo máximo.
                        // Primero me actualizo, y luego respondo
-                       update();
+                       float m_data =  *((float *)data)*2;
                        float tmp;
 
-                       tmp = (actual_flow<max_flow)?actual_flow:max_flow;
-                       if (pos == IN) {
-                                       // Si esta conectado a mi entrada, le puedo aceptar
-                                       // solo la mitad del flujo maximo
-                                       tmp /= 2;
+/*                     if (updated) {
+                               if (m_data == 0) {
+                                       tmp = 0;
+                                       actual_flow /= 2.0f;
+                               } else {
+                                       tmp = actual_flow/2.0f;
+                               }
+                               who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
+                               break;
+                       }*/
+                       updated = true;
+                       if (m_data == 0) {
+                               in_on_zero++;
+                               tmp = 0.0f;
+                               who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
+                               switch (in_on_zero) {
+                                       case 1:
+                                               if (in_ready == 0)
+                                                       tmp = max_flow;
+                                               else
+                                                       tmp = actual_flow/2.0f;
+                                       break;
+                                       case 0:
+                                               if (in_ready == 1) {
+                                                       tmp = actual_flow/2.0f;
+                                               }
+                                       break;
+                                       case 2:
+                                               tmp = 0.0f;
+                               }
+                               send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, &tmp);
+                       } else {
+                               switch (in_on_zero) {
+                                       case 0:
+                                               actual_flow = (m_data<actual_flow)?m_data:actual_flow;
+                                               actual_flow = (actual_flow<max_flow)?actual_flow:max_flow;
+                                       break;
+                                       case 1:
+                                               if (in_ready == 1)
+                                                       actual_flow = actual_flow/2.0f;
+                                               else
+                                                       actual_flow = max_flow/2.0f;
+                                       break;
+                                       case 2:
+                                               actual_flow = 0;
+                               }
+                               send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, &actual_flow);
+                               tmp = (in_on_zero==0)?actual_flow/2.0f:actual_flow;
+                               who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
                        }
-                       who->recieve_msg(MSG_RESPONSE_MAX_FLOW, this, &tmp);
+                       updated = true;
+                       if (pos == IN) in_ready++;
                }
                break;
                case MSG_RESPONSE_MAX_FLOW: {
                        float max = *((float *)data);
                        if (pos == OUT) {
                                if (max < actual_flow)  actual_flow = max;
+                               if (in_on_zero == 2) actual_flow = 0;
                        } else {
-                               if ((2*max) < actual_flow) actual_flow = 2*max;
+                                       if (((2*max) < actual_flow) && (max != 0)) {
+                                               actual_flow = 2*max;
+                                               if (in_on_zero == 1) actual_flow /= 2.0;
+                                       }
                        }
                }
                break;
+               case MSG_RESPONSE_COLOR:
+                       if (in_colors == 0) {
+                               color1 = *((RGB *)data);
+                               entrada1 = static_cast<PlantItem *>(who);
+                               flow1 = entrada1->get_actual_flow();
+                               in_colors++;
+                       } else if ((in_colors == 1) && (entrada1 != who)) {
+                               color2  = *((RGB *)data);
+                               flow2 = static_cast<PlantItem *>(who)->get_actual_flow();
+                               in_colors++;
+                               if (color1 == color2) {
+                                               std::cout << "TODO MAL" << std::endl << std::endl;
+                               }
+
+                               /* Ya tengo mis 2 colores, los sumo */
+                               int r,g,b;
+                               int n=2;
+                               if (flow1 == 0) n--;
+                               if (flow2 == 0) n--;
+                               float suma = flow1+flow2;
+                               if (n != 0) {
+                                       r = (int)(color1.r()*flow1/suma + color2.r()*flow2/suma);
+                                       g = (int)(color1.g()*flow1/suma + color2.g()*flow2/suma);
+                                       b = (int)(color1.b()*flow1/suma + color2.b()*flow2/suma);
+                                       set_color(RGB(r,g,b));
+                               } else {
+                                       set_color(RGB(0,0,0));
+                               }
+                               color_updated = true;
+                       }
+               break;
                default:
                        Transport::recieve_msg(msg, who, data);
        }
 }
 
-void Union::update()
+void Union::update(int dir)
 {
        // Si ya me actualice, no lo tengo que hacer de nuevo
        if (updated) return;
+       
        // Seteo mi actualizar en true para evitar entrar de nuevo
+       // FIXME : 99999 == INFINITO!!
        actual_flow = 99999;
        updated = true;
-       send_msg(OUT, MSG_QUERY_MAX_FLOW);
-       send_msg(IN, MSG_QUERY_MAX_FLOW);
+       switch (dir) {
+               case IN:
+                       send_msg(IN, MSG_QUERY_MAX_FLOW_IN, (void *)&max_flow);
+               break;
+               case OUT:
+                       send_msg(OUT, MSG_QUERY_MAX_FLOW_OUT, (void *)&max_flow);
+       }
 }
 
 void Union::simulate()
 {
-       if (!updated) {
-               return;
-       }
+       if (!updated) return;
 
+#ifdef DEBUG
        std::cout << name << "::Flujo actual = " << actual_flow << std::endl;
+#endif
        updated = false;
+       color_updated = false;
+       in_on_zero = 0;
+       in_ready = 0;
+       color_updated = false;
+       in_colors = 0;
 }