%{ #include "world.h" #include #include #include #define MAX_PARAM 8 %} %x JOINTCOUNT %x JOINT %x SPRING %x MUSCLE %x GRAVITY %x SPRINGK %x MASS %% float params[MAX_PARAM]; int param = 0; int lines = 1; gravity { BEGIN(GRAVITY); param = 0; } [0-9]+"."?[0-9]* { params[param++] = atof(yytext); } \n { lines++; if (param == 1) World::Gravity(params[0]); else { cout << "Invalid gravity definition at line " << lines << "\n"; exit(1); } BEGIN(INITIAL); } "#".* ;/* eat up comments */ \t+ ; " "+ ;/* eat up whitespace */ \r ; springk { BEGIN(SPRINGK); param = 0; } [0-9]+"."?[0-9]* { params[param++] = atof(yytext); } \n { lines++; if (param == 1) World::SpringK(params[0]); else { cout << "Invalid springk definition at line " << lines << "\n"; exit(1); } BEGIN(INITIAL); } "#".* ;/* eat up comments */ \t+ ; " "+ ;/* eat up whitespace */ \r ; mass { BEGIN(MASS); param = 0; } [0-9]+"."?[0-9]* { params[param++] = atof(yytext); } \n { lines++; if (param == 1) World::Mass(params[0]); else { cout << "Invalid mass definition at line " << lines << "\n"; exit(1); } BEGIN(INITIAL); } "#".* ;/* eat up comments */ \t+ ; " "+ ;/* eat up whitespace */ \r ; jointcount { BEGIN(JOINTCOUNT); param = 0; } [0-9]+ { params[param++] = atof(yytext); } \n { lines++; if (param == 1) World::InitJoints((int) params[0]); else { cout << "Invalid jointcount definition at line " << lines << "\n"; exit(1); } BEGIN(INITIAL); } "#".* ;/* eat up comments */ \t+ ; " "+ ;/* eat up whitespace */ \r ; joint { BEGIN(JOINT); param = 0; } [0-9]+"."?[0-9]* { params[param++] = atof(yytext); } \n { lines++; if (param==3) World::AddJoint(params[0],params[1],(int) params[2]); else { cout << "Inavlid joint definition at line " << lines << "\n"; exit(1); } BEGIN(INITIAL); } "#".* ;/* eat up comments */ \t+ ; " "+ ;/* eat up whitespace */ \r ; spring { BEGIN(SPRING); param = 0; } [0-9]+"."?[0-9]* { params[param++] = atof(yytext); } \n { lines++; if (param == 2) World::AddSpring((int) params[0], (int) params[1]); else { cout << "Invalid spring definition at line " << lines << "\n"; exit(1); } BEGIN(INITIAL); } "#".* ;/* eat up comments */ \t+ ; " "+ ;/* eat up whitespace */ \r ; muscle { BEGIN(MUSCLE); param = 0; } [0-9]+"."?[0-9]* { params[param++] = atof(yytext); } \n { lines++; if (param == 5) World::AddMuscle((int) params[0], (int) params[1], params[2], params[3], params[4]); else { cout << "Invalid muscle definition at line " << lines <<"\n"; exit(1); } BEGIN(INITIAL); } "#".* ;/* eat up comments */ \t+ ; " "+ ;/* eat up whitespace */ \r ; "#".* ;/* eat up comments */ \t+ ; " "+ ;/* eat up whitespace */ \n lines++; \r ; . { cout << "Unrecognized character: " << yytext << " at line "<< lines << "\n"; exit(1); } %% float World::gravity = GRAVITY; float World::elasticity = ELASTICITY; float World::friction = FRICTION; float World::width = SIZE_WORLD_X; float World::height = SIZE_WORLD_Y; float World::spring_k = SPRING_K; float World::mass = JOINT_MASS; Joint **World::structure = 0; int World::joints = 0; int World::maxjoints = 0; int World::joint_offset = 0; Joint **World::copy = 0; int World::copy_joints = 0; int yywrap() {return 1;} void World::InitJoints(int count) { if (!maxjoints) { maxjoints = count; structure = new Joint*[count]; for(int x=0;xSprings();j++) { Spring *S = J->GetSpring(j); S->ConnectedJoint(J->SpringEnd(j),i-1); } } joints--; } } void World::AddSpring(int from, int to) { from+=joint_offset; to+=joint_offset; if (structure[from] && structure[to] && !structure[from]->ConnectedTo(structure[to])) { Spring *s = structure[from]->ConnectJoint(*structure[to]); s->ConnectedJoint(0,from); s->ConnectedJoint(1,to); } } void World::AddMuscle(int from, int to, float freq, float amp, float ofs) { from += joint_offset; to += joint_offset; if (structure[from] && structure[to] && !structure[from]->ConnectedTo(structure[to])) { Spring *s = structure[from]->ConnectJoint(*structure[to]); s->AddMuscle(freq,amp,ofs); s->ConnectedJoint(0,from); s->ConnectedJoint(1,to); } } void World::Load(const char* fn) { FlexLexer* lexer = NULL; if (fn[0] == '-' && fn[1] == 0) { lexer = new yyFlexLexer(&cin,NULL); lexer->yylex(); } else { ifstream fin(fn); lexer = new yyFlexLexer(&fin,NULL); lexer->yylex(); fin.close(); } } void World::Include(const char* fn) { joint_offset = joints; World::Load(fn); joint_offset = 0; } void World::Save(const char* fn) { Joint *J; Joint *K; Spring *S; std::ofstream ofs; std::ostream fout(0); if (fn[0]=='-' && fn[1]==0) { fout.rdbuf(cout.rdbuf()); } else { ofs.open(fn); fout.rdbuf(ofs.rdbuf()); } fout.setf(ios::fixed); fout <<"jointcount "<X()<<" "<Y()<<" "<IsFixed()<<"\n"; fout <<"joint "<X()<<" "<Y()<<" 0\n"; } for (int j=0;jSprings();k++) if (!J->SpringEnd(k)) { S = J->GetSpring(k); if (S->MuscleAmplitude()) fout << "muscle " << S->ConnectedJoint(0) << " " << S->ConnectedJoint(1) << " " << S->MuscleFrequency() << " " << S->MuscleAmplitude() << " " << S->MuscleOffset() << "\n"; else fout << "spring " << S->ConnectedJoint(0) << " " << S->ConnectedJoint(1) << "\n"; } } ofs.close(); }