-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.java
529 lines (456 loc) · 17.1 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import javax.sound.sampled.*;
import java.awt.*;
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.List;
// Test
public class Main {
private final int width = 20;
private final int height = 20;
private int wall_damage;
public static boolean game_over = false;
public static List<Safe> safes = new ArrayList<>();
Room[][] rooms = new Room[width+1][height+1];
private Player player = new Player();
private void ini()
{
Random random = new Random();
boolean bRunning = true;
///////////////////////////////////////////// story making
int storyRoomCount = 4; // NUMBER OF STORIES
int[][] srooms = new int[storyRoomCount][2];
while (bRunning)
{
// Create
for (int i = 0; i < storyRoomCount - 1; ++i) // YES -1 IS IMPORTANT
{
srooms[i][0] = random.nextInt(width);
srooms[i][1] = random.nextInt(height);
if (srooms[i][0] == 0 && srooms[i][1] == 0)
{
--i;
}
}
// Check
bRunning = false;
BEXIT:
for (int topPos = 0; topPos < srooms.length; ++topPos)
for (int botPos = 0; botPos < srooms.length; ++botPos)
{
if (topPos != botPos)
if (Math.abs(srooms[topPos][0] - srooms[botPos][0]) +
Math.abs(srooms[topPos][1] - srooms[botPos][1]) <
(storyRoomCount * 4) / (width * (height / width)))
{
bRunning = true;
break BEXIT;
}
}
}
rooms[0][0] = new Room(5, player);
for (int[] storyRoom : srooms)
{
rooms[ storyRoom[0] ][ storyRoom[1] ] = new Room(5, player);
}
///////////////////////////////////////////// secret making
// FOR DIFFERENT OCCURRENCY CHANGE THE LAST NUMBER
int secretRoomCount = (width * height) / 100;
int keyRoomCount = storyRoomCount;
/*
0 == SECRECT ROOM GEN
1 == KEY ROOM GEN
*/
for (int rco = 0; rco < 2; ++rco)
{
int[][] crooms;
int thisRoomCount;
if (rco == 0)
{
crooms = new int[secretRoomCount][2];
thisRoomCount = secretRoomCount;
}
else // if (rco == 1)
{
crooms = new int[keyRoomCount][2];
thisRoomCount = storyRoomCount;
}
bRunning = true;
while (bRunning)
{
for (int i = 0; i < thisRoomCount; ++i)
{
crooms[i][0] = random.nextInt(width);
crooms[i][1] = random.nextInt(height);
if (rooms[crooms[i][0]][crooms[i][1]] != null)
{
--i;
}
}
bRunning = false;
BEXIT:
for (int topPos = 0; topPos < crooms.length; ++topPos)
for (int botPos = 0; botPos < crooms.length; ++botPos)
{
if (topPos != botPos)
if (Math.abs(crooms[topPos][0] - crooms[botPos][0]) +
Math.abs(crooms[topPos][1] - crooms[botPos][1]) <
(thisRoomCount * 3) / (width * (height / width)))
{
bRunning = true;
break BEXIT;
}
}
}
if (rco == 0)
{
for (int[] secretRoom : crooms)
{
rooms[secretRoom[0]][secretRoom[1]] = new Room(7, player);
}
}
else if (rco == 1)
{
for (int i = 0; i < crooms.length; ++i)
{
rooms[ crooms[i][0] ][ crooms[i][1] ] = new Room(8, player);
rooms[ crooms[i][0] ][ crooms[i][1] ].KEYBYTE = (byte) Math.pow(2, i);
}
}
}
///////////////////////////////////////////// Rest
for (int pos = 0; pos < rooms.length * rooms[0].length; ++pos)
{
if (rooms[pos % rooms.length][pos / rooms.length] == null)
{
rooms[pos % rooms.length][pos / rooms.length] = new Room(0, player);
}
}
/**
0 = wall
1 = safe
2 = fight
3 = passive
4 = treasure
5 = story
6 = shop
7 = secret
8 = KeyRoom
*/
// possibilitiesSum HAS TO BE THE SUM OF ALL FIRST VALUES FROM possibilities
int possibilitiesSum = 1000;
int[][] possibilities =
{
{300, 0}, // WALL
{100, 1}, // SAFE
{150, 2}, // FIGHT
{350, 3}, // PASSIVE
{60, 4}, // TREASURE
{40, 6}, // SHOP
};
bRunning = true;
while (bRunning)
{
int rand = 0;
for (Room[] roomArray : rooms) for (Room room : roomArray)
{
if (room.getType() != 7 &&
room.getType() != 5 &&
room.getType() != 8)
{
rand = Math.abs(random.nextInt(possibilitiesSum));
int probability = 0;
for (int[] posb : possibilities)
{
if (rand <= (probability += posb[0]))
{
room.setType(posb[1]);
break;
}
}
}
if (room.isResearved())
{
room.setResearved(false);
}
}
bRunning = recrusiveWayCheck(0, 0, 0, (byte) 0) != storyRoomCount && checkWithoutKeys(0, 0) != storyRoomCount;
}
}
private int recrusiveWayCheck(int x, int y, int MAXBEFORE, byte TKEYBYTE){
if (x < 0 || y < 0 || x >= width || y >= height ||
!rooms[x][y].getName().equals("Tür"))
{
return 0;
}
if (rooms[x][y].isResearved() &&
rooms[x][y].getMAXKEYSHERE() >= MAXBEFORE)
{
return 0;
}
rooms[x][y].setResearved(true);
if (rooms[x][y].getType() == 8 &&
(rooms[x][y].KEYBYTE & TKEYBYTE) == 0)
{
++MAXBEFORE;
TKEYBYTE |= rooms[x][y].KEYBYTE;
}
rooms[x][y].setMAXKEYSHERE(MAXBEFORE);
int found = 0;
if (x != 0 && y != 0 &&
rooms[x][y].getType() == 5)
{
if (MAXBEFORE > 0)
{
--MAXBEFORE;
++found;
}
else
{
return 0;
}
}
found += recrusiveWayCheck(x + 1, y, MAXBEFORE, TKEYBYTE);
found += recrusiveWayCheck(x - 1, y, MAXBEFORE, TKEYBYTE);
found += recrusiveWayCheck(x, y + 1, MAXBEFORE, TKEYBYTE);
found += recrusiveWayCheck(x, y - 1, MAXBEFORE, TKEYBYTE);
return found;
}
private int checkWithoutKeys(int x, int y){
if (x < 0 || y < 0 || x >= width || y >= height)
{
return 0;
}
if (rooms[x][y].isResearved() ||
!rooms[x][y].getName().equals("Tür") || rooms[x][y].isLocked())
{
return 0;
}
rooms[x][y].setResearved(true);
int found = 0;
found += checkWithoutKeys(x + 1, y);
found += checkWithoutKeys(x - 1, y);
found += checkWithoutKeys(x, y + 1);
found += checkWithoutKeys(x, y - 1);
if (rooms[x][y].getType() == 8)
{
++found;
}
return found;
}
private void printMap(Player p){
boolean shown = false;
System.out.println("O = Position / X = Ziel / M = Mauer / $ = Shop");
System.out.println(" -----------------------------------------");
for(int j = width; j >= 0; j--){
System.out.print("| ");
for(int i = 0; i <= height; i++) {
if (i == p.getPos_x() && j == p.getPos_y()) {
System.out.print("O");
} else if (rooms[i][j].getType() == 5 && !shown) {
System.out.print("X");
shown = true;
} else if (rooms[i][j].getType() == 0 ||
rooms[i][j].getType() == 7) {
System.out.print("M");
} else if(rooms[i][j].getType() == 6){
System.out.print("$");
} else {
System.out.print(" ");
}
System.out.print(" ");
}
System.out.println("|");
}
System.out.println(" -----------------------------------------");
}
private void printRooms(Player p){
int x = p.getPos_x();
int y = p.getPos_y();
System.out.println("Vor dir befindet sich eine " + (y < height ? rooms[x][y+1].getName() : "Wand") +
", Links eine " + (x > 0 ? rooms[x-1][y].getName() : "Wand") +
", Rechts eine " + (x < width ? rooms[x+1][y].getName() : "Wand") +
" und hinter dir eine " + (y > 0 ? rooms[x][y-1].getName() : "Wand") + ".");
}
private void compass(Player p){
int storyX = 0, storyY = 0;
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
if(rooms[i][j].getType() == 5){
storyX = i;
storyY = j;
}
}
}
System.out.print("Kompass zeigt nach ");
if(player.getPos_y() < storyY){
System.out.print("NORDEN");
} else if(player.getPos_x() < storyX){
System.out.print("OSTEN");
} else if(player.getPos_y() > storyY){
System.out.print("SÜDEN");
} else if(player.getPos_x() < storyX){
System.out.print("WESTEN");
}
System.out.println();
}
private boolean check(int x, int y) {
if(x >= 0 && x <= width && y >= 0 && y <= height && rooms[x][y].getType() == 5 && player.getKeys() <= 0 && rooms[x][y].isLocked()){
Event.printText("Diese Tür ist verschlossen. Finde zunächst einen passenden Schlüssel um diese zu öffnen.");
return false;
} else if(x >= 0 && x <= width && y >= 0 && y <= height && rooms[x][y].getType() == 5 && player.getKeys() > 0 && rooms[x][y].isLocked()){
rooms[x][y].setLocked(false);
Event.printText("Du konntest die Tür mit dem Schlüssel öffnen, jedoch ist dieser leider abgebrochen und somit nicht mehr verwendbar.");
}
return x >= 0 && x <= width && y >= 0 && y <= height && rooms[x][y].getType() != 0;
}
private void movementError(){
Random r = new Random();
String s = null;
switch (r.nextInt(5)){
case 0:
s = "Nein! Das geht leider nicht..";
break;
case 1:
s = "Hier geht es leider nicht lang..";
break;
case 2:
s = "Schade! Ich dachte schon hier kann man lang gehen.";
break;
case 3:
s = "Hmm.. Hier scheint nichts zu sein.";
break;
case 4:
s = "Better luck next time!";
break;
}
Event.printText(s);
player.setHealth( player.getHealth() - wall_damage );
}
private Main(){
Scanner s = new Scanner(System.in);
ini();
System.out.println("Willkommen bei 2 2 0 5. Einem auf Text basiertem Open-World-Game.\n" +
"Wähle einen Schwierigkeitsgrad:\n" +
"[1] EINFACH: Du startest mit viel Sauerstoff, Monster können gemütlich besiegt werden und du hast ein GPS-Gerät um dich zu orientieren.\n" +
"[2] NORMAL: Du startest mit genug Sauerstoff, Monster können relativ einfach besiegt werden und du hast einen Kompass um dich zu orientieren.\n" +
"[3] SCHWER: Du startest mit einem knappen Vorrat an Sauerstoff und musst dich anstrengen um Monster zu besiegen.\n");
Event.printText("Wie möchtest du spielen? [1|2|3] ", 30, false);
String difficulty = s.nextLine();
boolean printMap = false;
boolean displayCompass = false;
switch (difficulty){
case "1":
player.setOxygen(1500);
Event.maxReactionTime = 2000;
wall_damage = 0;
printMap = true;
break;
case "2":
player.setOxygen(1000);
Event.maxReactionTime = 1500;
wall_damage = 5;
displayCompass = true;
break;
case "3":
player.setOxygen(750);
Event.maxReactionTime = 1000;
wall_damage = 40;
break;
default:
player.setOxygen(1000);
Event.maxReactionTime = 1500;
wall_damage = 5;
displayCompass = true;
break;
}
Event.cls(false);
rooms[0][0].getEvent().execute();
Event.printText("Du kannst dich ab sofort frei auf der Map bewegen. Und Übrigens: Wände sind nicht immer Wände ;)");
while (!game_over)
{
if(player.getHealth() <= 0 || player.getOxygen() <= 0){
game_over = true;
break;
}
int x, y;
String input;
Event.cls(false);
if(printMap) printMap(player);
if(displayCompass) compass(player);
System.out.println("Aktueller Sauerstoffgehalt: " + player.getOxygen());
printRooms(player);
do {
Event.printText("Wohin möchtest du gehen? ", 30, false);
input = s.nextLine().toLowerCase(Locale.GERMAN);
x = player.getPos_x();
y = player.getPos_y();
if (input.matches(Debug.DEBUG_PREFIX + ".*"))
{
String err = Debug.resolve_debug(input.substring(4), player, rooms);
if (err != null)
System.out.println("DEBUG&" + err + ";");
}
} while (input.matches(Debug.DEBUG_PREFIX + ".*"));
if(input.contains("links")){
if(check(x-1, y)) {
player.setPos_x(x - 1);
} else {
movementError();
}
} else if(input.contains("rechts")){
if(check(x+1,y)) {
player.setPos_x(x + 1);
} else {
movementError();
}
} else if(input.contains("vor") || input.contains("geradeaus")){
if(check(x, y+1)) {
player.setPos_y(y + 1);
} else {
movementError();
}
} else if(input.contains("hinter") || input.matches("\\s*zur.ck\\s*")){
if(check(x, y-1)) {
player.setPos_y(y - 1);
} else {
movementError();
}
} else {
System.out.println("Das habe ich nicht verstanden..");
continue;
}
player.setOxygen(player.getOxygen() - 10);
rooms[player.getPos_x()][player.getPos_y()].getEvent().execute();
}
}
public static void playSound(String url) {
try {
Clip clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP)
clip.close();
});
clip.open(AudioSystem.getAudioInputStream(Main.class.getResource(url)));
clip.start();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
if(System.getProperty("os.name").startsWith("Windows")) {
Console console = System.console();
if (console == null && !GraphicsEnvironment.isHeadless()) {
String filename = Main.class.getProtectionDomain().getCodeSource().getLocation().toString().substring(6);
try {
Runtime.getRuntime().exec(new String[]{"cmd", "/c", "start", "cmd", "/k", "java -jar \"" + filename + "\""});
} catch (IOException e) {
e.printStackTrace();
}
}
}
new Main();
}
}