/* Apply a random mutation to the specified triangle/circle. */ void mutatetriangle(struct triangle *t, int width, int height) { int choice = random() % 6; if (choice == 0) { setRandomVertexes(t,width,height); normalize(t,width,height); } else if (choice == 1) { moveVertexes(t,20); normalize(t,width,height); } else if (choice == 2) { moveVertexes(t,5); normalize(t,width,height); } else if (choice == 3) { t->r = random()%256; t->g = random()%256; t->b = random()%256; } else if (choice == 4) { int r,g,b; r = t->r + randbetween(-5,5); g = t->g + randbetween(-5,5); b = t->b + randbetween(-5,5); if (r < 0) r = 0; else if (r > 255) r = 255; if (g < 0) g = 0; else if (g > 255) g = 255; if (b < 0) b = 0; else if (b > 255) b = 255; t->r = r; t->g = g; t->b = b; } else if (choice == 5) { t->alpha = randbetween(MINALPHA,MAXALPHA); } }
/* Create a random triangle or circle. */ void randomtriangle(struct triangle *r, int width, int height) { int triangle; if (opt_use_circles && opt_use_triangles) { triangle = random()&1; } else if (opt_use_circles) { triangle = 0; } else { triangle = 1; } if (triangle) { r->type = TYPE_TRIANGLE; r->u.t.x1 = random()%width; r->u.t.y1 = random()%height; r->u.t.x2 = random()%width; r->u.t.y2 = random()%height; r->u.t.x3 = random()%width; r->u.t.y3 = random()%height; } else { r->type = TYPE_CIRCLE; r->u.c.x1 = random()%width; r->u.c.y1 = random()%height; r->u.c.radius = random()%width; } r->r = random()%256; r->g = random()%256; r->b = random()%256; r->alpha = randbetween(MINALPHA,MAXALPHA); normalize(r,width,height); }
/* Translate vertexes at random, from -delta to delta. */ void moveVertexes(struct triangle *t, int delta) { if (t->type == TYPE_TRIANGLE) { t->u.t.x1 += randbetween(-delta,delta); t->u.t.y1 += randbetween(-delta,delta); t->u.t.x2 += randbetween(-delta,delta); t->u.t.y2 += randbetween(-delta,delta); t->u.t.x3 += randbetween(-delta,delta); t->u.t.y3 += randbetween(-delta,delta); } else { t->u.c.x1 += randbetween(-delta,delta); t->u.c.y1 += randbetween(-delta,delta); t->u.c.radius += randbetween(-delta,delta); } }
/* Like randomtriangle() but vertex/radius can't be more than 'delta' pixel * away from initial random coordinates. TODO: refactor me into a single * function. */ void randomsmalltriangle(struct triangle *r, int width, int height, int delta) { int x = random()%width; int y = random()%height; int triangle; if (opt_use_circles && opt_use_triangles) { triangle = random()&1; } else if (opt_use_circles) { triangle = 0; } else { triangle = 1; } if (triangle) { r->type = TYPE_TRIANGLE; r->u.t.x1 = x + randbetween(-delta,delta); r->u.t.y1 = y + randbetween(-delta,delta); r->u.t.x2 = x + randbetween(-delta,delta); r->u.t.y2 = y + randbetween(-delta,delta); r->u.t.x3 = x + randbetween(-delta,delta); r->u.t.y3 = y + randbetween(-delta,delta); } else { r->type = TYPE_CIRCLE; r->u.c.x1 = x; r->u.c.y1 = y; r->u.c.radius = delta; } r->r = random()%256; r->g = random()%256; r->b = random()%256; r->alpha = randbetween(MINALPHA,MAXALPHA); normalize(r,width,height); }
void TestFKIKFK() { int arm = RIGHT; // Create random legitimate joint values std::vector<RobotKin::Joint*> joints = kinematics->linkage("RightArm").joints(); for (int i = 0; i < joints.size(); i++) { double jointVal = randbetween(joints[i]->min(), joints[i]->max()); kinematics->joint(joints[i]->name()).value(jointVal); } // Do FK to get ee pose Eigen::Isometry3d initialFrame; initialFrame = kinematics->linkage("RightArm").tool().respectToRobot(); // Do IK to get joints DrcHuboKin::ArmVector q = DrcHuboKin::ArmVector::Zero(); RobotKin::rk_result_t result = kinematics->armIK(arm, q, initialFrame); std::cerr << "Solver result: " << result << std::endl; CPPUNIT_ASSERT(RobotKin::RK_SOLVED == result); // Do FK and verify that it's pretty much the same int baseJoint = 0; if (LEFT == arm) { baseJoint = LSP; } else if (RIGHT == arm) { baseJoint = RSP; } for (int i = baseJoint; i < baseJoint+7; i++) { //kinematics->joint(DRCHUBO_URDF_JOINT_NAMES[i]).value(q[DRCHUBO_JOINT_INDEX_TO_LIMB_POSITION[i]]); } Eigen::Isometry3d finalFrame; finalFrame = kinematics->linkage("RightArm").tool().respectToRobot(); //CPPUNIT_ASSERT((initialFrame.matrix() - finalFrame.matrix()).norm() < 0.001); std::cerr << "Inital pose: \n" << initialFrame.matrix() << std::endl; std::cerr << "Final pose: \n" << finalFrame.matrix() << std::endl; }
/* Like randomtriangle() but vertex/radius can't be more than 'delta' pixel * away from initial random coordinates. */ void randomsmalltriangle(struct triangle *r, int width, int height, int delta) { int x = random()%width; int y = random()%height; r->type = selectShapeType(); if (r->type == TYPE_TRIANGLE) { r->u.t.x1 = x + randbetween(-delta,delta); r->u.t.y1 = y + randbetween(-delta,delta); r->u.t.x2 = x + randbetween(-delta,delta); r->u.t.y2 = y + randbetween(-delta,delta); r->u.t.x3 = x + randbetween(-delta,delta); r->u.t.y3 = y + randbetween(-delta,delta); } else { r->u.c.x1 = x; r->u.c.y1 = y; r->u.c.radius = randbetween(1,delta); } setRandomColor(r); normalize(r,width,height); }
static unsigned long randomData(long seed) { unsigned long datalen; /* We use the key number as seed of the PRNG, so we'll be able to check if * a given key contains the right data later, without the use of additional * memory. */ if (config.check) { rc4rand_seed(seed); datalen = rc4rand_between(config.datasize_min,config.datasize_max); rc4rand_set(config.databuf,datalen); } else { datalen = randbetween(config.datasize_min,config.datasize_max); if (config.rand) { rc4rand_seed(seed); rc4rand_set(config.databuf,datalen); } else { memset(config.databuf,'x',datalen); } } return datalen; }
/* Set random RGB and alpha. */ void setRandomColor(struct triangle *r) { r->r = random()%256; r->g = random()%256; r->b = random()%256; r->alpha = randbetween(MINALPHA,MAXALPHA); }
int creature_init(struct t_creature* o_entity, struct t_creature_generate_rules* genrules) { memset(o_entity,0,sizeof(struct t_creature)); if ((genrules == NULL) || (genrules->gender = EG_RANDOM) || !vrange(&genrules->age)) { random_gender_and_age(&o_entity->age, &o_entity->gender_id); } else { if ((genrules) || vrange(&genrules->age)) { o_entity->age = randrange(&genrules->age); } else o_entity->age = 18 + randval(40); enum entity_gender g_gender = genrules ? genrules->gender : EG_RANDOM; switch (g_gender) { case EG_MALE: o_entity->gender_id = EG_MALE; break; case EG_FEMALE: o_entity->gender_id = EG_FEMALE; break; case EG_WMPATRIARCH: o_entity->gender_id = EG_WMPATRIARCH; break; //will be replaced with male after the name is generated. case EG_NEUTRAL: o_entity->gender_id = EG_NEUTRAL; o_entity->gender_bio = randbetween(EG_MALE,EG_FEMALE); break; case EG_MBIAS: o_entity->gender_id = (randval(100) < 75) ? EG_MALE : EG_FEMALE; break; case EG_FBIAS: o_entity->gender_id = (randval(100) < 75) ? EG_MALE : EG_FEMALE; break; case EG_RANDOM: default: o_entity->gender_id = (randval(100) < 52) ? EG_FEMALE : EG_MALE; break; //52% females according to U.S. Census data } } if ((randval(100) < 1) && (o_entity->gender_bio == EG_RANDOM)) o_entity->gender_bio = randbetween(EG_NEUTRAL,EG_FEMALE); else o_entity->gender_bio = o_entity->gender_id; //will result in mismatches for 0.67% of cases. o_entity->birthday_month = randval(12) + 1; switch(o_entity->birthday_month) { case 4: case 6: case 9: case 11: o_entity->birthday_day=randval(30)+1; break; case 2: o_entity->birthday_day=randval(28)+1; break; default: o_entity->birthday_day=randval(31)+1; break; } o_entity->id=curcreatureid++; for(int w=0;w<EB_COUNT;w++)o_entity->wound[w]=0; for(int a=0;a<EA_COUNT;a++)o_entity->attributes[a]=1; int attnum=32; if (genrules) { o_entity->type = genrules->type; for (int i=0; i < RANDATTRS; i++) if (vrange(&genrules->attrlim[i])) o_entity->attributes[genrules->attrs[i]] = randrange(&genrules->attrlim[i]); for (int i=0; i < RANDSKILLS; i++) if (vrange(&genrules->skilllim[i])) o_entity->skills[genrules->skill[i]] = randrange(&genrules->skilllim[i]); enum Alignment align = genrules->align; if (genrules->random_align) align += randbetween(-2,2); if (align < ALIGN_ARCHCONSERVATIVE) align = ALIGN_ARCHCONSERVATIVE; if (align > ALIGN_ELITELIBERAL) align = ALIGN_ELITELIBERAL; o_entity->align = align; o_entity->orig_align = align; if (vrange(&genrules->juice)) o_entity->juice = randrange(&genrules->juice); if (vrange(&genrules->money)) o_entity->money = randrange(&genrules->money); for (int i=0; i < RANDATTRS; i++) if (vrange(&genrules->attrlim[i])) o_entity->attributes[genrules->attrs[i]] = randrange(&genrules->attrlim[i]); for (int i=0; i < RANDSKILLS; i++) if (vrange(&genrules->skilllim[i])) o_entity->skills[genrules->skill[i]] = randrange(&genrules->skilllim[i]); if (vrange(&genrules->attrpts)) attnum = randrange(&genrules->attrpts); } while(attnum>0) { int a=randval(EA_COUNT); if(o_entity->attributes[a]<10) { o_entity->attributes[a]++; attnum--; } } //at this point, both the entity's alignment and gender are fixed. //let's generate a name. random_first_name(o_entity->firstname,o_entity->gender_id); random_last_name(o_entity->lastname,o_entity->align == ALIGN_ARCHCONSERVATIVE,o_entity->gender_id); int wi=0; int wn=0; while ((wi < RANDWEAPONS) && (genrules->weapons[wi])) { wn++; wi++; } if (wn) { wi = randval(wn); struct t_item new_weapon = {.type = IT_WEAPON, .itemtypeid = genrules->weapons[wi], .ammo = 200}; struct t_item clips = {.type = IT_CLIP, .itemtypeid = weapontypes[genrules->weapons[wi]].attacks[0].ammotype, .ammo = cliptypes[weapontypes[genrules->weapons[wi]].attacks[0].ammotype].ammo }; struct t_item* added_weapon = inv_add(o_entity->inventory, &new_weapon); o_entity->weapon = added_weapon; } o_entity->special[ESW_TEETH]=TOOTHNUM; o_entity->special[ESW_RIGHTEYE]=1; o_entity->special[ESW_LEFTEYE]=1; o_entity->special[ESW_NOSE]=1; o_entity->special[ESW_TONGUE]=1; o_entity->special[ESW_RIGHTLUNG]=1; o_entity->special[ESW_LEFTLUNG]=1; o_entity->special[ESW_HEART]=1; o_entity->special[ESW_LIVER]=1; o_entity->special[ESW_STOMACH]=1; o_entity->special[ESW_RIGHTKIDNEY]=1; o_entity->special[ESW_LEFTKIDNEY]=1; o_entity->special[ESW_SPLEEN]=1; o_entity->special[ESW_RIBS]=RIBNUM; o_entity->special[ESW_NECK]=1; o_entity->special[ESW_UPPERSPINE]=1; o_entity->special[ESW_LOWERSPINE]=1; o_entity->blood = 100; o_entity->alive = true; o_entity->exists = true; return 0; } int entity_name(struct t_creature* who) { random_first_name(who->firstname,who->gender_id); random_last_name(who->lastname,who->align == ALIGN_ARCHCONSERVATIVE,who->gender_id); return 0; } char* entityattrstr[EA_COUNT] = { "STR", "INT", "WIS", "AGI", "CON", "CHA", "HRT" }; const char* safe_name(const char* nameptr) { if ((nameptr == NULL) || (strlen(nameptr) == 0)) return "???"; return nameptr; } int describe_entity(struct t_creature* me, char* const restrict o_name, size_t strsize) { if (me != NULL) { if (strlen(me->nickname) != 0) { strncpy(o_name,me->nickname,strsize); return 0;} if (((strlen(me->firstname) != 0) || (strlen(me->firstname) != 0)) && (me->name_known)) { char fullname[66]; strncpy(fullname,safe_name(me->firstname),32); strncat(fullname," ",1); strncat(fullname,safe_name(me->lastname),32); strncpy(o_name,fullname,strsize); return 0; } const char* td = type_description(me); if (td) { strncpy(o_name,type_description(me),strsize); return 0; } } strncpy(o_name, "*",32); return 0; } char temp_name[16][128]; int temp_name_i = 0; const char* describe_entity_static(struct t_creature* me) { int r = describe_entity(me,temp_name[temp_name_i],128); const char* ret = (r == 0 ? temp_name[temp_name_i] : NULL); temp_name_i = (temp_name_i+1) % 16; return ret; }
/* Apply a random mutation to the specified triangle/circle. */ void mutatetriangle(struct triangle *t, int width, int height) { if (rand() % MUTATION_RANDMOVE_PROB == 0) { if (t->type == TYPE_TRIANGLE) { t->u.t.x1 = random()%width; t->u.t.y1 = random()%height; t->u.t.x2 = random()%width; t->u.t.y2 = random()%height; t->u.t.x3 = random()%width; t->u.t.y3 = random()%height; } else { t->u.c.x1 = random()%width; t->u.c.y1 = random()%height; t->u.c.radius = random()%width; } normalize(t,width,height); } if (rand() % MUTATION_MEDMOVE_PROB == 0) { if (t->type == TYPE_TRIANGLE) { t->u.t.x1 += randbetween(-20,20); t->u.t.y1 += randbetween(-20,20); t->u.t.x2 += randbetween(-20,20); t->u.t.y2 += randbetween(-20,20); t->u.t.x3 += randbetween(-20,20); t->u.t.y3 += randbetween(-20,20); } else { t->u.c.x1 += randbetween(-20,20); t->u.c.y1 += randbetween(-20,20); t->u.c.radius += randbetween(-20,20); } normalize(t,width,height); } if (rand() % MUTATION_SMALLMOVE_PROB == 0) { if (t->type == TYPE_TRIANGLE) { t->u.t.x1 += randbetween(-5,5); t->u.t.y1 += randbetween(-5,5); t->u.t.x2 += randbetween(-5,5); t->u.t.y2 += randbetween(-5,5); t->u.t.x3 += randbetween(-5,5); t->u.t.y3 += randbetween(-5,5); } else { t->u.c.x1 += randbetween(-5,5); t->u.c.y1 += randbetween(-5,5); t->u.c.radius += randbetween(-5,5); } normalize(t,width,height); } if (rand() % MUTATION_RANDCOLOR_PROB == 0) { t->r = random()%256; t->g = random()%256; t->b = random()%256; } if (rand() % MUTATION_COLORMOVE_PROB == 0) { int r,g,b; r = t->r + randbetween(-5,5); g = t->g + randbetween(-5,5); b = t->b + randbetween(-5,5); if (r < 0) r = 0; else if (r > 255) r = 255; if (g < 0) g = 0; else if (g > 255) g = 255; if (b < 0) b = 0; else if (b > 255) b = 255; t->r = r; t->g = g; t->b = b; } if (rand() % MUTATION_RANDALPHA_PROB == 0) { t->alpha = randbetween(MINALPHA,MAXALPHA); } }