// proper way to send loooong 1D array int BuzzSetMap(buzzvm_t vm){ /* Make sure one parameter has been passed */ buzzvm_lnum_assert(vm, 1); /* Get the parameter */ buzzvm_lload(vm, 1); buzzvm_type_assert(vm, 1, BUZZTYPE_TABLE); // matrix /* Get the table */ buzzobj_t t = buzzvm_stack_at(vm, 1); /* Copy the values into a vector */ std::vector<float> mat; for(int32_t i = 0; i < buzzdict_size(t->t.value); ++i) { /* Duplicate the table */ buzzvm_dup(vm); /* Push the index */ buzzvm_pushi(vm, i); /* Get the value */ buzzvm_tget(vm); /* Store it in the vector (assume all values are float, no mistake...) */ mat.push_back((float)buzzvm_stack_at(vm, 1)->f.value); /* Get rid of the value, now useless */ buzzvm_pop(vm); } /* Get a pointer to the controller */ buzzvm_pushs(vm, buzzvm_string_register(vm, "controller", 1)); buzzvm_gload(vm); /* Copy data into the controller */ reinterpret_cast<CBuzzControllerFootBot*>(buzzvm_stack_at(vm, 1)->u.value)->SetMapParams(mat, Sqrt(buzzdict_size(t->t.value))); /* Done with the function */ return buzzvm_ret0(vm); }
int BuzzSetPath(buzzvm_t vm){ buzzvm_lnum_assert(vm, 1); /* Get the parameter */ buzzvm_lload(vm, 1); buzzvm_type_assert(vm, 1, BUZZTYPE_TABLE); // dictionary buzzobj_t t = buzzvm_stack_at(vm, 1); std::vector<CBuzzControllerFootBot::PathItem> pis; for(int32_t i = 0; i < buzzdict_size(t->t.value); ++i) { buzzvm_dup(vm); buzzvm_pushi(vm, i); buzzvm_tget(vm); int id = 0; int parent = 0; float x = 0.0; float y = 0.0; for(int32_t j = 0; j < buzzdict_size(buzzvm_stack_at(vm, 1)->t.value); ++j) { buzzvm_dup(vm); buzzvm_pushi(vm, j); buzzvm_tget(vm); if(j == 0){ id = (float)buzzvm_stack_at(vm, 1)->i.value; } else if(j == 1){ parent = (float)buzzvm_stack_at(vm, 1)->i.value; } else if (j == 2){ x = (float)buzzvm_stack_at(vm, 1)->f.value; } else if (j == 3){ y = (float)buzzvm_stack_at(vm, 1)->f.value; } //fprintf(stdout, "%d %f \n", j, (float)buzzvm_stack_at(vm, 1)->f.value); buzzvm_pop(vm); } pis.push_back(CBuzzControllerFootBot::PathItem(id, parent, x, y)); buzzvm_pop(vm); } /* Get a pointer to the controller */ buzzvm_pushs(vm, buzzvm_string_register(vm, "controller", 1)); buzzvm_gload(vm); /* Copy data into the controller */ reinterpret_cast<CBuzzControllerFootBot*>(buzzvm_stack_at(vm, 1)->u.value)->Done(pis); /* Done with the function */ return buzzvm_ret0(vm); }
int BuzzDrawObstacles(buzzvm_t vm){ buzzvm_lnum_assert(vm, 1); buzzvm_lload(vm, 1); buzzvm_type_assert(vm, 1, BUZZTYPE_TABLE); // dictionary buzzobj_t t = buzzvm_stack_at(vm, 1); std::vector<CBuzzControllerFootBot::Obstacle> obs; for(int32_t i = 0; i < buzzdict_size(t->t.value); ++i) { buzzvm_dup(vm); buzzvm_pushi(vm, i); buzzvm_tget(vm); float x = 0.0; float y = 0.0; float radius = 0.0; int type = 0; for(int32_t j = 0; j < buzzdict_size(buzzvm_stack_at(vm, 1)->t.value); ++j) { buzzvm_dup(vm); buzzvm_pushi(vm, j); buzzvm_tget(vm); if(j == 0){ x = (float)buzzvm_stack_at(vm, 1)->f.value; } else if(j == 1){ y = (float)buzzvm_stack_at(vm, 1)->f.value; } else if (j == 2){ radius = (float)buzzvm_stack_at(vm, 1)->f.value; } else if (j == 3){ type = (int) buzzvm_stack_at(vm, 1)->i.value; } //fprintf(stdout, "%d %f \n", j, (float)buzzvm_stack_at(vm, 1)->f.value); buzzvm_pop(vm); } obs.push_back(CBuzzControllerFootBot::Obstacle(x, y, radius, type)); buzzvm_pop(vm); } buzzvm_pushs(vm, buzzvm_string_register(vm, "controller", 1)); buzzvm_gload(vm); reinterpret_cast<CBuzzControllerFootBot*>(buzzvm_stack_at(vm, 1)->u.value)->ArgosDrawObstacles(obs); return buzzvm_ret0(vm); }
int buzzvstig_size(buzzvm_t vm) { buzzvm_lnum_assert(vm, 0); /* Get vstig id */ id_get(); /* Look for virtual stigmergy */ const buzzvstig_t* vs = buzzdict_get(vm->vstigs, &id, buzzvstig_t); if(vs) { /* Virtual stigmergy found, return its size */ buzzvm_pushi(vm, buzzdict_size((*vs)->data)); } else { /* Virtual stigmergy not found, return 0 */ buzzvm_pushi(vm, 0); } /* Return */ return buzzvm_ret1(vm); }
int BuzzDebugPrint(buzzvm_t vm) { /* Get pointer to controller user data */ buzzvm_pushs(vm, buzzvm_string_register(vm, "controller", 1)); buzzvm_gload(vm); buzzvm_type_assert(vm, 1, BUZZTYPE_USERDATA); CBuzzController& cContr = *reinterpret_cast<CBuzzController*>(buzzvm_stack_at(vm, 1)->u.value); /* Fill message */ std::ostringstream oss; for(UInt32 i = 1; i < buzzdarray_size(vm->lsyms->syms); ++i) { buzzvm_lload(vm, i); buzzobj_t o = buzzvm_stack_at(vm, 1); buzzvm_pop(vm); switch(o->o.type) { case BUZZTYPE_NIL: oss << "[nil]"; break; case BUZZTYPE_INT: oss << o->i.value; break; case BUZZTYPE_FLOAT: oss << o->f.value; break; case BUZZTYPE_TABLE: oss << "[table with " << (buzzdict_size(o->t.value)) << " elems]"; break; case BUZZTYPE_CLOSURE: if(o->c.value.isnative) oss << "[n-closure @" << o->c.value.ref << "]"; else oss << "[c-closure @" << o->c.value.ref << "]"; break; case BUZZTYPE_STRING: oss << o->s.value.str; break; case BUZZTYPE_USERDATA: oss << "[userdata @" << o->u.value << "]"; break; default: break; } } cContr.GetARGoSDebugInfo().Msg = oss.str(); return buzzvm_ret0(vm); }
void buzzvm_dump(buzzvm_t vm) { int64_t i, j; fprintf(stderr, "============================================================\n"); fprintf(stderr, "state: %d\terror: %d\n", vm->state, vm->error); fprintf(stderr, "code size: %u\tpc: %d\n", vm->bcode_size, vm->pc); fprintf(stderr, "stacks: %" PRId64 "\tcur elem: %" PRId64 " (size %" PRId64 ")\n", buzzdarray_size(vm->stacks), buzzvm_stack_top(vm), buzzvm_stack_top(vm)); for(i = buzzdarray_size(vm->stacks)-1; i >= 0 ; --i) { fprintf(stderr, "===== stack: %" PRId64 " =====\n", i); for(j = buzzdarray_size(buzzdarray_get(vm->stacks, i, buzzdarray_t)) - 1; j >= 0; --j) { fprintf(stderr, "\t%" PRId64 "\t", j); buzzobj_t o = buzzdarray_get(buzzdarray_get(vm->stacks, i, buzzdarray_t), j, buzzobj_t); switch(o->o.type) { case BUZZTYPE_NIL: fprintf(stderr, "[nil]\n"); break; case BUZZTYPE_INT: fprintf(stderr, "[int] %d\n", o->i.value); break; case BUZZTYPE_FLOAT: fprintf(stderr, "[float] %f\n", o->f.value); break; case BUZZTYPE_TABLE: fprintf(stderr, "[table] %d elements\n", buzzdict_size(o->t.value)); break; case BUZZTYPE_CLOSURE: if(o->c.value.isnative) { fprintf(stderr, "[n-closure] %d\n", o->c.value.ref); } else { fprintf(stderr, "[c-closure] %d\n", o->c.value.ref); } break; case BUZZTYPE_STRING: fprintf(stderr, "[string] %d:'%s'\n", o->s.value.sid, o->s.value.str); break; default: fprintf(stderr, "[TODO] type = %d\n", o->o.type); } } } fprintf(stderr, "============================================================\n\n"); }
int buzzvm_vstig_size(buzzvm_t vm) { buzzvm_lnum_assert(vm, 0); /* Get vstig id */ buzzvm_lload(vm, 0); buzzvm_pushs(vm, buzzvm_string_register(vm, "id")); buzzvm_tget(vm); uint16_t id = buzzvm_stack_at(vm, 1)->i.value; /* Look for virtual stigmergy */ buzzvstig_t* vs = buzzdict_get(vm->vstigs, &id, buzzvstig_t); if(vs) { /* Virtual stigmergy found, return its size */ buzzvm_pushi(vm, buzzdict_size((*vs)->data)); } else { /* Virtual stigmergy not found, return 0 */ buzzvm_pushi(vm, 0); } /* Return */ return buzzvm_ret1(vm); }
int BuzzLOG (buzzvm_t vm) { LOG << "BUZZ: "; for(UInt32 i = 1; i < buzzdarray_size(vm->lsyms->syms); ++i) { buzzvm_lload(vm, i); buzzobj_t o = buzzvm_stack_at(vm, 1); buzzvm_pop(vm); switch(o->o.type) { case BUZZTYPE_NIL: LOG << "[nil]"; break; case BUZZTYPE_INT: LOG << o->i.value; break; case BUZZTYPE_FLOAT: LOG << o->f.value; break; case BUZZTYPE_TABLE: LOG << "[table with " << (buzzdict_size(o->t.value)) << " elems]"; break; case BUZZTYPE_CLOSURE: if(o->c.value.isnative) LOG << "[n-closure @" << o->c.value.ref << "]"; else LOG << "[c-closure @" << o->c.value.ref << "]"; break; case BUZZTYPE_STRING: LOG << o->s.value.str; break; case BUZZTYPE_USERDATA: LOG << "[userdata @" << o->u.value << "]"; break; default: break; } } LOG << std::endl; LOG.Flush(); return buzzvm_ret0(vm); }
int print(buzzvm_t vm) { for(int i = 1; i < buzzdarray_size(vm->lsyms->syms); ++i) { buzzvm_lload(vm, i); buzzobj_t o = buzzvm_stack_at(vm, 1); buzzvm_pop(vm); switch(o->o.type) { case BUZZTYPE_NIL: fprintf(stdout, "[nil]"); break; case BUZZTYPE_INT: fprintf(stdout, "%d", o->i.value); break; case BUZZTYPE_FLOAT: fprintf(stdout, "%f", o->f.value); break; case BUZZTYPE_TABLE: fprintf(stdout, "[table with %d elems]", (buzzdict_size(o->t.value))); break; case BUZZTYPE_CLOSURE: if(o->c.value.isnative) fprintf(stdout, "[n-closure @%d]", o->c.value.ref); else fprintf(stdout, "[c-closure @%d]", o->c.value.ref); break; case BUZZTYPE_STRING: fprintf(stdout, "%s", o->s.value.str); break; case BUZZTYPE_USERDATA: fprintf(stdout, "[userdata @%p]", o->u.value); break; default: break; } } fprintf(stdout, "\n"); return buzzvm_ret0(vm); }
void di_print(buzzdict_t di) { fprintf(stdout, "size: %u\n", buzzdict_size(di)); buzzdict_foreach(di, di_print_elem, NULL); fprintf(stdout, "\n"); }
void buzzswarm_members_print(buzzswarm_members_t m, uint16_t id) { fprintf(stdout, "ROBOT %u -> Swarm member table size: %u\n", id, buzzdict_size(m)); buzzdict_foreach(m, print_swarm_elem, &id); }