-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
210 lines (205 loc) · 6.59 KB
/
Main.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
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
201
202
203
204
205
206
207
208
209
210
/* Charlie Gerrie 2018
*
* This class contains an application for visually viewing simulations of the power index game.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
import org.lwjgl.*;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;
public class Main {
// standard graphics variables
public boolean isRunning = false; // used to control the main loop
public long lastFrame = getTime();
public long lastFPS = getTime();
public static long startTime;
public int fps = 0;
// graph being simulated
Graph graph;
List<Integer> hashes;
// graph visualization parameters
double zoom = 50;
int timeSinceLastFrame = 0;
int frameTime = 50;
boolean shouldIterate,
buttonHeld,
isColourful,
powerNeedsRecalculating,
F_held;
int XRES = 600,
YRES = 600;
// main method
public static void main(String[] args) {
Main main = new Main();
main.start();
}
// main loop
public void start() {
startTime = System.nanoTime();
init(XRES,YRES);
isRunning = true;
while(isRunning) {
// refresh screen
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
// call each-frame methods
input();
draw();
move(getDelta());
// update screen
updateFPS();
Display.update();
Display.sync(60);
}
try {
File outfile = new File("data2");
PrintWriter outWriter = new PrintWriter(outfile);
for(int i : hashes) {
outWriter.println(i);
}
outWriter.flush();
outWriter.close();
System.out.println("done writing to file");
} catch(FileNotFoundException e) {
System.err.println("file not found");
}
}
// initialization; gets run once at beginning of start() main loop
public void init(int xres, int yres) {
// create window
try {
Display.setDisplayMode(new DisplayMode(xres,yres));
Display.create();
} catch(LWJGLException e) {
e.printStackTrace();
System.exit(-1);
}
// initialize openGL environment
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,xres,yres);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0,xres,0,yres,1,-1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
// initialize graph
Side[] set = new Side[(int)(xres/zoom)*(int)(yres/zoom)];
for(int i=0;i<set.length;i++)
//set[i] = i%2==0?Side.WEAK:Side.STRONG;
//set[i] = i<set.length/2?(i/((int)(600/zoom)))%4==0||(i/((int)(600/zoom)))%4==1?Side.WEAK:Side.STRONG:Side.STRONG;
set[i] = (i/((int)(xres/zoom)))%4==0||(i/((int)(xres/zoom)))%4==1?Side.WEAK:Side.STRONG;
graph = new Graph(new Graph.GridGraph((int)(xres/zoom),(int)(xres/zoom),true,true), new Graph.SpecificSides(set));
//graph = new Graph(new Graph.GridGraph((int)(xres/zoom),(int)(yres/zoom),true,true), new Graph.RandomSides(0.5));
Game.CalculateWeights(graph);
hashes = new LinkedList<>();
shouldIterate = false;
buttonHeld = false;
isColourful = false;
powerNeedsRecalculating = false;
F_held = false;
}
// this method is for handling input, and is called each frame
public void input() {
// exit
if(Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
isRunning = false;
shouldIterate = Keyboard.isKeyDown(Keyboard.KEY_SPACE);
isColourful = Keyboard.isKeyDown(Keyboard.KEY_C);
if(Mouse.isButtonDown(0)) {
if(!buttonHeld) {
buttonHeld = true;
int x = Mouse.getX(),
y = Mouse.getY();
for(Vertex v : graph.vertices)
if(v.pos.x == x/(int)zoom && v.pos.y == y/(int)zoom)
v.side = Side.flip(v.side);
powerNeedsRecalculating = true;
}
}
else
buttonHeld = false;
// get sizes
if(Keyboard.isKeyDown(Keyboard.KEY_F) && !F_held) {
int[][] plane1 = new int[(int)(XRES/zoom)][(int)(YRES/zoom)],
plane2 = new int[(int)(XRES/zoom)][(int)(YRES/zoom)];
for(int i=0;i<(int)(XRES/zoom);i++) {
for(int j=0;j<(int)(YRES/zoom);j++) {
//System.out.println(graph.vertices.get(i*(int)(600/zoom)+j).side);
plane1[i][j] = graph.vertices.get(i*(int)(XRES/zoom)+j).side==Side.WEAK?0:-1;
plane2[i][j] = graph.vertices.get(i*(int)(XRES/zoom)+j).side==Side.STRONG?0:-1;
}
}
System.out.print("Weaks:");
int prev = 0;
List<Integer> list = Util.floodFill(plane1);
list.sort((x,y) -> y.compareTo(x));
for(Integer i : list)
System.out.printf((i!=prev?"\n":"\t")+"%d",prev=i);
list = Util.floodFill(plane2);
list.sort((x,y) -> y.compareTo(x));
System.out.print("\nStrongs:");
prev = 0;
for(Integer i : list)
System.out.printf((i!=prev?"\n":"\t")+"%d",prev=i);
}
F_held = Keyboard.isKeyDown(Keyboard.KEY_F);
}
// this method is for drawing the visualizaiton, and is called each frame
public void draw() {
// draw the vertices of the graph
for(Vertex v : graph.vertices) {
// TODO coloring based on power, proportion, regional weighting, etc
if(isColourful) {
if(powerNeedsRecalculating) {
Game.CalculateWeights(graph);
powerNeedsRecalculating = false;
}
double[] colour = v.getColor();
GL11.glColor3d(colour[0], colour[1], colour[2]);
}
else {
double colour = v.side.getColor();
GL11.glColor3d(colour, colour, colour);
}
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2d(zoom*v.pos.x, zoom*v.pos.y);
GL11.glVertex2d(zoom*(v.pos.x+1), zoom*v.pos.y);
GL11.glVertex2d(zoom*(v.pos.x+1), zoom*(v.pos.y+1));
GL11.glVertex2d(zoom*v.pos.x, zoom*(v.pos.y+1));
GL11.glEnd();
}
}
// this method is for animating, and is called each frame along with the time in ms since last frame
public void move(int delta) {
// iterate graph if enough time has elapsed
if((timeSinceLastFrame+=delta)>frameTime && shouldIterate) {
timeSinceLastFrame = 0;
hashes.add(graph.hashCode());
Game.IterateGraph(graph);
}
}
// these methods are used for timing
public long getTime() {
return System.nanoTime() / 1000000;
}
public int getDelta() {
long time = getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}
public void updateFPS() {
if(getTime() - lastFPS > 1000) {
System.out.println("FPS: "+ fps);
fps = 0;
lastFPS+=1000;
}
fps++;
}
}