-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.java
151 lines (130 loc) · 5.93 KB
/
World.java
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
package model;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.*;
public class World {
public Player me;
public List<Player> opponents;
public IdentityHashMap<Point, Integer> opponentsTerritory;
public int width, height, speed;
public int tick;
public int remainingTicks;
public IdentityHashMap<Point, Bonus> bonuses;
public double[][] influenceMap;
public Map<Point, Direction> sawDanger;
private int cellWidth;
public World() {
}
public void init(JSONObject params) {
cellWidth = params.getInt("width");
width = params.getInt("x_cells_count");
height = params.getInt("y_cells_count");
Point.init(width, height, cellWidth);
}
public void initTick(JSONObject params) {
tick = params.getInt("tick_num");
remainingTicks = Math.max(1, (Constants.LAST_TICK_NUM - tick) / 6);
if (remainingTicks < 0) {
remainingTicks = 100500;
}
opponents = new ArrayList<>();
opponentsTerritory = new IdentityHashMap<>();
for (Object id : params.getJSONObject("players").names()) {
JSONObject p = params.getJSONObject("players").getJSONObject((String) id);
Direction direction = null;
if (!p.get("direction").toString().equals("null")) {
direction = Direction.valueOf(p.getString("direction").toUpperCase());
}
JSONArray pos = p.getJSONArray("position");
int x, y;
double addTime = 0;
if (direction == Direction.UP) {
x = (pos.getInt(0) - cellWidth / 2) / cellWidth;
y = (int) Math.ceil((pos.getInt(1) - cellWidth / 2.0) / cellWidth);
addTime = ((cellWidth - (pos.getInt(1) - cellWidth / 2) % cellWidth) * 1.0 / cellWidth) % 1;
} else if (direction == Direction.DOWN) {
x = (pos.getInt(0) - cellWidth / 2) / cellWidth;
y = (int) Math.floor((pos.getInt(1) - cellWidth / 2.0) / cellWidth);
addTime = (((pos.getInt(1) - cellWidth / 2) % cellWidth) * 1.0 / cellWidth) % 1;
} else if (direction == Direction.LEFT) {
x = (int) Math.floor((pos.getInt(0) - cellWidth / 2.) / cellWidth);
y = (pos.getInt(1) - cellWidth / 2) / cellWidth;
addTime = (((pos.getInt(0) - cellWidth / 2) % cellWidth) * 1.0 / cellWidth) % 1;
} else if (direction == Direction.RIGHT) {
x = (int) Math.ceil((pos.getInt(0) - cellWidth / 2.) / cellWidth);
y = (pos.getInt(1) - cellWidth / 2) / cellWidth;
addTime = ((cellWidth - (pos.getInt(0) - cellWidth / 2) % cellWidth) * 1.0 / cellWidth) % 1;
} else {
x = (pos.getInt(0) - cellWidth / 2) / cellWidth;
y = (pos.getInt(1) - cellWidth / 2) / cellWidth;
}
Player player = new Player(Point.get(x, y));
player.addTime = addTime;
player.score = p.getInt("score");
JSONArray lines = p.getJSONArray("lines");
for (Object c : lines) {
JSONArray line = (JSONArray) c;
player.putLine(Point.convert(line.getInt(0), line.getInt(1)));
}
JSONArray terr = p.getJSONArray("territory");
for (Object c : terr) {
JSONArray t = (JSONArray) c;
player.putTerritory(Point.convert(t.getInt(0), t.getInt(1)));
}
JSONArray bonuses = p.getJSONArray("bonuses");
for (Object c : bonuses) {
JSONObject b = (JSONObject) c;
Bonus bonus = new Bonus();
bonus.time = b.getInt("ticks");
bonus.type = Bonus.Type.getTypeByCode(b.getString("type"));
player.bonuses.add(bonus);
if (bonus.type == Bonus.Type.SLOW || bonus.type == Bonus.Type.NITRO) {
player.speedBonus = bonus;
}
}
if (!p.get("direction").toString().equals("null")) {
player.direction = Direction.valueOf(p.getString("direction").toUpperCase());
}
if (id.equals("i")) {
me = player;
} else {
opponents.add(player);
opponentsTerritory.putAll(player.territory);
}
}
bonuses = new IdentityHashMap<>();
for (Object o : params.getJSONArray("bonuses")) {
JSONObject jsonObject = (JSONObject) o;
Bonus bonus = new Bonus();
bonus.position = Point.convert(jsonObject.getJSONArray("position").getInt(0), jsonObject.getJSONArray("position").getInt(1));
bonus.type = Bonus.Type.getTypeByCode(jsonObject.getString("type"));
bonuses.put(bonus.position, bonus);
}
initInfluence();
sawDanger = new HashMap<>();
}
private void initInfluence() {
influenceMap = new double[height][width];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for (int dx = 0; dx < width; dx++) {
for (int dy = 0; dy < height; dy++) {
Point point = Point.get(dx, dy);
int len = Math.abs(x - dx) + Math.abs(y + dy);
if (len == 0 || point == null || len <= 3 || me.territory.containsKey(point)) {
continue;
}
if (opponentsTerritory.containsKey(point)) {
influenceMap[y][x] += 2.5 / len;
} else {
influenceMap[y][x] += 0.5 / len;
}
}
}
for (Player enemy : opponents) {
influenceMap[y][x] -= 31 / Math.max(1, enemy.position.distanceTo(Point.get(x, y)));
}
}
}
}
}