aboutsummaryrefslogtreecommitdiff
path: root/world.cpp
blob: 40c49ea1ffd9c7c5f337b4b93b7f83e4bb4925b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <cstdlib>
#include <cassert>
#include <cstring>
#include "world.h"

using namespace std;


Argument::~Argument(){}


Instruction Instruction::make(ins_t op,vector<Argument> 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(dur<waited)return;
	waited=0;
	execute(ins,world);
}

Instruction Robot::resolve(World &world){
	Instruction ins=banks.at(ip.bank).at(ip.pos);
	for(Argument &arg : ins.args){
		switch(arg.type){
			case arg_t::number:
				break;
			case arg_t::var:
				assert(!arg.remote);
				arg.type=arg_t::number;
				arg.num=vars.at(arg.num-1);
				break;
			case arg_t::name:
				assert(arg.name=="active");
				arg.type=arg_t::number;
				if(!arg.remote){
					arg.num=active;
				} else {
					Robot *target=world.targetbot(this);
					if(target)arg.num=target->active;
					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;y<SIZE;y++){
		for(int x=0;x<SIZE;x++){
			if(board[y][x])delete board[y][x];
		}
	}
}

Robot& World::create(const Team *team,int iset,int nbanks,bool mobile){
	int x,y;
	while(true){
		x=rand()%SIZE;
		y=rand()%SIZE;
		if(!board[y][x])break;
	}
	board[y][x]=new Robot();
	board[y][x]->team=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;y<SIZE;y++){
		for(int x=0;x<SIZE;x++){
			if(board[y][x])board[y][x]->tick(*this);
		}
	}
}

Robot* World::targetbot(const Robot *r){
	for(int y=0;y<SIZE;y++){
		for(int x=0;x<SIZE;x++){
			if(board[y][x]==r){
				switch(r->heading){
					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<<arg.num;
			case arg_t::var: assert(false);
			case arg_t::name: return os<<'%'<<arg.name;
			case arg_t::constant: assert(false);
			case arg_t::undeflabel: return os<<"\x1B[31;43mUNDEFLABEL\x1B[0m";
		}
	} else {
		switch(arg.type){
			case arg_t::number: return os<<arg.num;
			case arg_t::var: return os<<'#'<<arg.num;
			case arg_t::name: return os<<'#'<<arg.name;
			case arg_t::constant: return os<<'$'<<arg.name;
			case arg_t::undeflabel: return os<<"\x1B[31;43mUNDEFLABEL\x1B[0m";
		}
	}
}

ostream& operator<<(ostream &os,const ins_t &type){
	switch(type){
		case ins_t::set: return os<<"set";
		case ins_t::add: return os<<"add";
		case ins_t::sub: return os<<"sub";
		case ins_t::comp: return os<<"comp";
		case ins_t::trans: return os<<"trans";
		case ins_t::jump: return os<<"jump";
		case ins_t::bjump: return os<<"bjump";
		case ins_t::die: return os<<"die";
		case ins_t::move: return os<<"move";
		case ins_t::turn: return os<<"turn";
		case ins_t::scan: return os<<"scan";
		case ins_t::create: return os<<"create";
		default: assert(false);
	}
}

ostream& operator<<(ostream &os,const Instruction &ins){
	os<<ins.op;
	bool first=true;
	for(const Argument &arg : ins.args){
		if(first)os<<' ';
		else os<<", ";
		first=false;
		os<<arg;
	}
	return os;
}

ostream& operator<<(ostream &os,const Team &team){
	os<<"TEAM: "<<team.name<<endl;
	int bank=0;
	for(const Script &scr : team.banks){
		os<<"Bank "<<bank<<endl;
		for(const Instruction &ins : scr){
			os<<'\t'<<ins<<endl;
		}
		bank++;
	}
	return os;
}