From fd56574a4d60e553c610615660198f0a84bcb7a1 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 1 Mar 2017 15:53:02 +0100 Subject: Initial --- .gitignore | 2 + Makefile | 20 ++++++ main.cpp | 229 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ params.cpp | 33 +++++++++ params.h | 38 ++++++++++ world.cpp | 199 +++++++++++++++++++++++++++++++++++++++++++++++++++++ world.h | 94 +++++++++++++++++++++++++ 7 files changed, 615 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.cpp create mode 100644 params.cpp create mode 100644 params.h create mode 100644 world.cpp create mode 100644 world.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb56aa9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +sim diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..009c5bc --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +CXX = g++ +CXXFLAGS = -Wall -Wextra -std=c++11 -g -fwrapv +TARGET = sim + +.PHONY: all clean remake + +all: $(TARGET) + +clean: + rm -f $(TARGET) *.o + +remake: clean + $(MAKE) all + + +$(TARGET): $(patsubst %.cpp,%.o,$(wildcard *.cpp)) + $(CXX) -o $@ $^ + +%.o: %.cpp $(wildcard *.h) + $(CXX) $(CXXFLAGS) -c -o $@ $< diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..ce93d37 --- /dev/null +++ b/main.cpp @@ -0,0 +1,229 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "params.h" +#include "world.h" + +using namespace std; + + +void trim(string &str){ + while(str.size()>0&&isspace(str[0]))str.erase(0,1); + while(str.size()>0&&isspace(str.back()))str.erase(str.size()-1,1); +} + +void squashSpaces(string &str){ + bool sp=false; + for(size_t i=0;i splittrim(const string &str,char sep){ + if(str.size()==0)return {}; + vector res; + size_t cursor=0; + while(true){ + size_t idx=str.find(sep,cursor); + if(idx==string::npos){ + string s=str.substr(cursor); + trim(s); + res.push_back(s); + return res; + } + string s=str.substr(cursor,idx-cursor); + trim(s); + res.push_back(s); + cursor=idx+1; + } +} + +void lowercase(string &str){ + for(size_t i=0;i &labels,Location ip){ + if(str[0]=='#'||str[0]=='%'){ + bool remote=str[0]=='%'; + if(isdigit(str[1]))return {arg_t::var,remote,atoi(str.data()+1),{}}; + else return {arg_t::name,remote,0,str.substr(1)}; + } else if(str[0]=='@'){ + if(labels.find(str.substr(1))==labels.end()){ + return {arg_t::undeflabel,false,0,str.substr(1)}; + } + Location ref=labels.find(str.substr(1))->second; + assert(ref.bank==ip.bank); + return {arg_t::number,false,ref.bank-ip.bank,{}}; + } else if(str[0]=='$'){ + return {arg_t::constant,false,0,str.substr(1)}; + } else if(isdigit(str[0])){ + return {arg_t::number,false,atoi(str.data()),{}}; + } else { + assert(false); + } +} + +Team assemble(const string &source){ + Team team; + unordered_map labels; + + for(string line : splittrim(source,'\n')){ + if(line.size()==0)continue; + + string word1=line.substr(0,line.find(' ')); + string word2r=line.find(' ')==string::npos?"":line.substr(line.find(' ')+1); + if(word1=="published"){ + string word2=word2r.substr(0,word2r.find(' ')); + string word3r=word2r.find(' ')==string::npos?"":word2r.substr(word2r.find(' ')+1); + if(word2=="name"){ + team.name=word3r; + } + } else if(word1=="bank"){ + team.banks.emplace_back(); + } else if(word1[0]=='@'){ + assert(line.find(' ')==string::npos); + assert(labels.find(word1.substr(1))==labels.end()); + labels[word1.substr(1)]={(int)team.banks.size()-1,(int)team.banks.back().size()}; + } else { + assert(team.banks.size()>0); + vector args=splittrim(word2r,','); + vector pargs; + for(const string &s : args){ + pargs.push_back(parseArgument(s,labels, + {(int)team.banks.size()-1,(int)team.banks.back().size()})); + } + if(word1=="jump"||word1=="turn"||word1=="scan")assert(pargs.size()==1); + else if(word1=="die"||word1=="move")assert(pargs.size()==0); + else if(word1=="create")assert(pargs.size()==3); + else assert(pargs.size()==2); + + Script &s=team.banks.back(); + if(word1=="set")s.push_back(Instruction::make(ins_t::set,{pargs[0],pargs[1]})); + else if(word1=="add")s.push_back(Instruction::make(ins_t::add,{pargs[0],pargs[1]})); + else if(word1=="sub")s.push_back(Instruction::make(ins_t::sub,{pargs[0],pargs[1]})); + else if(word1=="comp")s.push_back(Instruction::make(ins_t::comp,{pargs[0],pargs[1]})); + else if(word1=="trans")s.push_back(Instruction::make(ins_t::trans,{pargs[0],pargs[1]})); + else if(word1=="jump")s.push_back(Instruction::make(ins_t::jump,{pargs[0]})); + else if(word1=="bjump")s.push_back(Instruction::make(ins_t::bjump,{pargs[0],pargs[1]})); + else if(word1=="die")s.push_back(Instruction::make(ins_t::die,{})); + else if(word1=="move")s.push_back(Instruction::make(ins_t::move,{})); + else if(word1=="turn")s.push_back(Instruction::make(ins_t::turn,{pargs[0]})); + else if(word1=="scan")s.push_back(Instruction::make(ins_t::scan,{pargs[0]})); + else if(word1=="create")s.push_back(Instruction::make(ins_t::create,{pargs[0],pargs[1],pargs[2]})); + else assert(false); + } + } + + int bank=0; + for(Script &script : team.banks){ + int pos=0; + for(Instruction &ins : script){ + for(Argument &arg : ins.args){ + if(arg.type==arg_t::undeflabel){ + assert(labels.find(arg.name)!=labels.end()); + assert(labels[arg.name].bank==bank); + arg.type=arg_t::number; + arg.num=labels[arg.name].pos-pos; + } + } + pos++; + } + bank++; + } + + return team; +} + +string preprocess(istream &file){ + unordered_map defines; + + string result; + + string line; + while(getline(file,line)){ + if(line.find(';')!=string::npos){ + line.erase(line.find(';')); + } + trim(line); + if(line.size()==0)continue; + squashSpaces(line); + lowercase(line); + + string word1=line.substr(0,line.find(' ')); + if(word1=="define"){ + string word2r=line.find(' ')==string::npos?"":line.substr(line.find(' ')+1); + string word2=word2r.substr(0,word2r.find(' ')); + string word3r=word2r.find(' ')==string::npos?"":word2r.substr(word2r.find(' ')+1); + assert(defines.find(word2.substr(1))==defines.end()); + assert(word3r[0]=='{'); + string value=word3r.substr(1); + assert(word3r.find('}')==string::npos||word3r.find('}')==word3r.size()-1); + if(word3r.back()!='}'){ + while(true){ + char c=file.get(); + if(c=='}')break; + value+=c; + } + assert(file.get()=='\n'); + } else { + value.erase(value.size()-1); + } + defines[word2.substr(1)]=value; + } else { + while(line.find('&')!=string::npos){ + size_t start=line.find('&')+1; + size_t end=line.find_first_not_of("abcdefghijklmnopqrstuvwxyz0123456789._",start); + if(end==string::npos)end=line.size(); + assert(defines.find(line.substr(start,end-start))!=defines.end()); + line=line.substr(0,start-1)+defines.find(line.substr(start,end-start))->second+line.substr(end); + } + result+=line+'\n'; + } + } + + return result; +} + + +int main(int argc,char **argv){ + struct timeval tv; + gettimeofday(&tv,nullptr); + srand(tv.tv_sec*1000000+tv.tv_usec); + + World world; + vector teams; + for(int i=1;i +#include +#include "world.h" + +using namespace std; + + +Argument::~Argument(){} + + +Instruction Instruction::make(ins_t op,vector args){ + return {op,args}; +} + + +void Robot::load(int idx,const Script &scr){ + banks[idx]=scr; +} + +void Robot::tick(World &world){ + waited++; + Instruction ins=resolve(world); + int dur=calcDuration(ins); + if(duractive; + else arg.num=0; + } + break; + case arg_t::constant: + assert(false); + default: + assert(false); + } + } + return ins; +} + +int Robot::calcDuration(Instruction &ins){ + int d=0; + for(const Argument &arg : ins.args){ + if(arg.remote)d+=C::pen_remote; + } + int base=C::baseDuration[(int)ins.op]; + switch(ins.op){ + case ins_t::set: d+=base; + case ins_t::add: d+=base; + case ins_t::sub: d+=base; + case ins_t::comp: d+=base; + case ins_t::trans: d+=base+C::pen_transinstr*banks.at(ins.args[0].num).size(); + case ins_t::jump: d+=base; + case ins_t::bjump: d+=base; + case ins_t::die: d+=base; + case ins_t::move: d+=base; + case ins_t::turn: d+=base; + case ins_t::scan: d+=base; + case ins_t::create: d+=(base+C::pen_createbank*ins.args[1].num)*C::pen_createmobilemult+C::pen_createmobile*ins.args[2].num+C::pen_createiset1*(ins.args[0].num==1)+C::pen_createiset2*(ins.args[0].num==2); + default: assert(false); + } + return d; +} + + +World::World(){ + memset(&board[0][0],0,SIZE*SIZE*sizeof(Robot*)); +} + +World::~World(){ + for(int y=0;yteam=team; + board[y][x]->banks.resize(nbanks); + board[y][x]->iset=iset; + board[y][x]->mobile=mobile; + board[y][x]->heading=rand()%4; + return *board[y][x]; +} + +void World::tick(){ + for(int y=0;ytick(*this); + } + } +} + +Robot* World::targetbot(const Robot *r){ + for(int y=0;yheading){ + case 0: return board[y][(x+1)%SIZE]; + case 1: return board[(y+SIZE-1)%SIZE][x]; + case 2: return board[y][(x+SIZE-1)%SIZE]; + case 3: return board[(y+1)%SIZE][x]; + } + } + } + } + assert(false); +} + + +ostream& operator<<(ostream &os,const Argument &arg){ + if(arg.remote){ + switch(arg.type){ + case arg_t::number: return os< +#include +#include +#include +#include +#include "params.h" + +using namespace std; + + +struct Location{ + int bank; // 0-based + int pos; // 0-based +}; + + +enum class arg_t{ + number, + var, + name, + constant, + undeflabel, +}; + +struct Argument{ + arg_t type; + bool remote; + int num; + string name; + + ~Argument(); +}; + +struct Instruction{ + ins_t op; + vector args; + + static Instruction make(ins_t op,vector args); +}; + +using Script = vector; + +class Team; +class World; + +class Robot{ + Location ip={0,0}; + int waited=0; + array vars; + uint16_t active; + + Instruction resolve(World &world); + int calcDuration(Instruction &ins); + void execute(Instruction &ins,World &world); + +public: + const Team *team; + vector