FContainer* FLogistics::addContainer(VRTransform* t) { FContainer* c = new FContainer(); t = (VRTransform*)t->duplicate(true); t->addAttachment("dynamicaly_generated", 0); c->setTransformation(t); objects[c->getID()] = c; return c; }
FContainer* FLogistics::addContainer(VRTransformPtr t) { if (t == 0) return 0; FContainer* c = new FContainer(); t = static_pointer_cast<VRTransform>(t->duplicate(true)); t->setVisible(true); t->setPersistency(0); c->setTransformation(t); objects[c->getID()] = c; return c; }
void FTransporter::update(float dt) { vector<FNode*> nodes = path->get(); vector<FNode*>::reverse_iterator itr; FNode *n1, *n2; FObject *o1, *o2; FProduct *p1, *p2; FContainer *c1, *c2; FNode::State s1, s2; FObject::Type t1, t2; n1=n2=0; o1=o2=0; p1=p2=0; c1=c2=0; float dx = speed*dt; //cout << "\n Transport id " << getID() << flush; int test_id = -1; // state machine for (itr = nodes.rbegin(); itr != nodes.rend(); itr++) { n2 = n1; n1 = *itr; s2 = s1; s1 = n1->getState(); if (n2 == 0) continue; // ignore last node of path, this is where everything gets transported to o1 = n1->get(); o2 = n2->get(); o1 ? t1 = o1->getType() : t1 = FObject::NONE; o2 ? t2 = o2->getType() : t2 = FObject::NONE; (s1 == FNode::PRODUCT) ? p1 = (FProduct*)o1 : p1 = 0; (s2 == FNode::PRODUCT) ? p2 = (FProduct*)o2 : p2 = 0; (s1 == FNode::CONTAINER) ? c1 = (FContainer*)o1 : c1 = 0; (s2 == FNode::CONTAINER) ? c2 = (FContainer*)o2 : c2 = 0; if (o1 == 0) continue; /* nothing here to do */ if (getID() == test_id) cout << "\n Node content " << o1->getID() << " ,reserved?" << flush; if (s2 == FNode::RESERVED) continue; /* next node reserved*/ if (getID() == test_id) cout << "\n Product there? " << flush; if (p2) continue; /* no place at next node */ if (getID() == test_id) cout << "\n Container there? " << flush; if (c2) if (c2->isFull()) continue; /* no place in container*/ if (getID() == test_id) cout << "\n Next node has place for a product " << flush; switch(transport_type) { case PRODUCT: if (getID() == test_id) cout << "\n Transport product, container? " << flush; if (t1 == FObject::CONTAINER) { if (getID() == test_id) cout << "\n yes, is container empty? " << flush; if (c1->isEmpty()) continue; /* nothing to do, empty container */ if (getID() == test_id) cout << "\n no, get object from container " << flush; o1 = c1->pop(); c1 = 0; p1 = (FProduct*)o1; t1 = o1->getType(); } if (t1 == FObject::PRODUCT) { if (getID() == test_id) cout << "\n a product, move it! " << flush; if (n1->get() == o1) n1->set(0); cargo[n2] = o1; if(c2 == 0) n2->setState(FNode::RESERVED); continue; } continue; case CONTAINER_FULL: if (getID() == test_id) cout << "\n Transport full container, container?" << flush; if (c1 == 0) continue; if (getID() == test_id) cout << "\n found container, full? " << flush; if (!c1->isFull()) continue; /* wait until container is full*/ if (getID() == test_id) cout << "\n yes, move it! " << flush; n1->set(0); n2->setState(FNode::RESERVED); cargo[n2] = c1; continue; case CONTAINER_EMPTY: if (getID() == test_id) cout << "\n Transport empty container, container?" << flush; if (c1 == 0) continue; if (getID() == test_id) cout << "\n found container, empty? " << flush; if (!c1->isEmpty()) continue; /* wait until container is empty*/ if (getID() == test_id) cout << "\n yes, move it! " << flush; n1->set(0); n2->setState(FNode::RESERVED); cargo[n2] = c1; continue; } } // objects in cargo are moved map<FNode*, FObject*>::iterator c_itr; vector<FNode*> toErase; for (c_itr = cargo.begin(); c_itr != cargo.end(); c_itr++) { FNode* n = c_itr->first; FObject* o = c_itr->second; if (n == 0) continue; if (o->move(n, dx)) { toErase.push_back(n); FObject* no = n->get(); if (no == 0) { n->set(o); continue; } // no is not a container, just place the object there if (no->getType() == FObject::CONTAINER and o->getType() == FObject::PRODUCT) { FContainer* c = (FContainer*)no; FProduct* p = (FProduct*)o; c->add(p); } } } // delete cargo for (unsigned int i=0; i<toErase.size(); i++) cargo.erase(toErase[i]); }