aboutsummaryrefslogtreecommitdiff
path: root/world.h
blob: fde3907107def3388080736fba06d769293d5ccd (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
#pragma once

#include <iostream>
#include <string>
#include <array>
#include <vector>

#include "params.h"
#include "screenbuffer.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<Argument> args;

	static Instruction make(ins_t op,vector<Argument> args);
};

using Script = vector<Instruction>;

class Team;
class World;

enum class WorldAction{
	none,
	die,
	move,
};

class Robot{
	Location ip={0,0};
	int waited=0;
	array<uint16_t,20> vars;

	uint16_t* resolveVar(const Argument &arg,World &world);
	Instruction resolve(World &world);
	int calcDuration(Instruction &ins);
	WorldAction execute(Instruction &ins,World &world);
	void advanceIP();
	bool ipInRange() const;

public:
	const Team *team;
	vector<Script> banks;
	int iset;
	bool mobile;

	uint16_t active=0;

	int heading;

	void load(int idx,const Script &scr);

	WorldAction tick(World &world);
};

class Team{
public:
	string name;
	vector<Script> banks;
};

class World{
	Robot** botptr(const Robot *r);
	Robot** targetbotptr(const Robot *r);

public:
	Robot *board[SIZE][SIZE];
	
	World();
	~World();

	Robot& create(const Team *team,int iset,int nbanks,bool mobile);
	Robot& create(const Team *team,int iset,int nbanks,bool mobile,int x,int y,int heading);
	Robot& createInFront(const Robot *caller,const Team *team,int iset,int nbanks,bool mobile);

	void removeRobot(const Robot *r);
	void removeTeam(const Team *team);

	void tick();
	Robot* targetbot(const Robot *r);

	void print(ScreenBuffer &sb) const;
};


ostream& operator<<(ostream &os,const Argument &arg);
ostream& operator<<(ostream &os,const ins_t &type);
ostream& operator<<(ostream &os,const Instruction &ins);
ostream& operator<<(ostream &os,const Team &team);