1 package com.codingame.game;
3 import java.util.Random;
4 import java.util.ArrayList;
5 import java.util.Comparator;
7 import com.codingame.gameengine.core.GameManager;
8 import com.codingame.gameengine.core.MultiplayerGameManager;
9 import com.codingame.gameengine.module.entities.GraphicEntityModule;
10 import com.codingame.gameengine.module.entities.Rectangle;
11 import com.codingame.gameengine.module.entities.Sprite;
12 import com.codingame.gameengine.module.entities.SpriteAnimation;
13 import com.codingame.gameengine.module.entities.Text;
14 import com.codingame.gameengine.module.entities.TextBasedEntity;
15 import com.codingame.gameengine.module.entities.Group;
16 import com.codingame.gameengine.module.entities.Curve;
17 import com.codingame.gameengine.module.toggle.ToggleModule;
18 import com.google.inject.Inject;
21 @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
22 @Inject private GraphicEntityModule graphicEntityModule;
23 @Inject ToggleModule toggleModule;
26 LEFT("walks left.", 0),
27 STILL("stands still.", 1),
28 RIGHT("walks right.", 2);
32 Dir(String mvt, int i) { movement = mvt; index = i; }
51 void init(com.codingame.game.Player p) {
53 colorToken = p.getColorToken();
54 nicknameToken = p.getNicknameToken();
55 avatarToken = p.getAvatarToken();
57 boolean p0 = model.index == 0;
58 int x = p0 ? 280 : 1920 - 280;
61 Sprite frame = graphicEntityModule.createSprite()
62 .setImage("frame.png")
64 .setRotation(frameRot)
68 Sprite frameBg = graphicEntityModule.createSprite()
69 .setImage("frame_bg.png")
71 .setRotation(frameRot)
74 Sprite avatarSprite = graphicEntityModule.createSprite()
76 .setImage(avatarToken)
81 avatar = graphicEntityModule
82 .createGroup(frame, frameBg, avatarSprite)
85 Text text = graphicEntityModule.createText(nicknameToken)
90 .setFillColor(0x7f3f00)
93 stoneCounter = graphicEntityModule.createText()
98 .setFillColor(0x7f3f00)
100 updateStoneCounter();
102 message = graphicEntityModule.createText()
103 .setX(p0 ? 15 : 1920-15)
107 .setFillColor(0xffbf7f)
108 .setAnchorX(p0 ? 0 : 1)
111 castle = graphicEntityModule.createSprite()
112 .setImage("castle.png")
114 .setX(p0 ? 160 : 1920-160)
115 .setY(p0 ? 890 : 880)
119 .setScaleX(p0 ? 1 : -1);
121 stone = graphicEntityModule.createText()
124 .setFillColor(0x12322a)
128 stoneReminder = graphicEntityModule.createText()
129 .setX(p0 ? x + 100 : x - 100)
133 .setFontFamily("monospace")
134 .setStrokeColor(0xff0080)
135 .setFillColor(0xff0080)
136 .setAnchorX(p0 ? 0 : 1)
138 toggleModule.displayOnToggleState(stoneReminder, "debug", true);
141 void updateStoneCounter() {
142 int stones = model.getStones();
144 stoneCounter.setText("Out of stones!");
145 stoneCounter.setFillColor(0xff7777);
147 else if (stones == 1) {
148 stoneCounter.setText("1 stone");
149 stoneCounter.setFillColor(0xffbb77);
152 stoneCounter.setText(stones + " stones");
156 void animateStones(int stones) {
157 String stonesString = Integer.valueOf(stones).toString();
158 stone.setX(castle.getX());
159 stone.setY(castle.getY() - 100);
160 stone.setText(stonesString);
162 graphicEntityModule.commitEntityState(0, stone);
164 int peakX = (castle.getX() + troll.getX()) / 2;
167 stone.setY(peakY, Curve.EASE_OUT);
168 graphicEntityModule.commitEntityState(0.25,
172 stone.setX(troll.getX());
173 stone.setY(troll.getY() - 50, Curve.EASE_IN);
174 stone.setAlpha(0, Curve.EASE_IN);
175 graphicEntityModule.commitEntityState(0.5, stone);
177 stoneReminder.setText(stonesString);
178 graphicEntityModule.commitEntityState(0, stoneReminder);
181 void displayMessage(String msg) {
182 message.setText(msg);
183 graphicEntityModule.commitEntityState(0, message);
187 graphicEntityModule.commitEntityState(0.5, avatar);
188 int dir = random.nextInt(2) == 1 ? 1 : -1;
189 avatar.setRotation(dir * 170 * Math.PI / 180, Curve.ELASTIC);
193 gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + nicknameToken + "."));
194 graphicEntityModule.commitEntityState(0.5, castle);
195 castle.setX(castle.getX(), Curve.ELASTIC);
196 castle.setScaleY(-0.2, Curve.EASE_IN);
200 graphicEntityModule.commitEntityState(0, stoneReminder);
204 gameManager.addToGameSummary(GameManager.formatSuccessMessage(nicknameToken + " wins."));
205 graphicEntityModule.commitEntityState(0.5, avatar);
206 avatar.setScaleX(1.5, Curve.EASE_OUT);
207 avatar.setScaleY(1.5, Curve.EASE_OUT);
208 avatar.setRotation((random.nextDouble() - 0.5) * Math.PI / 18,
212 void throwStones(int stones) {
213 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", nicknameToken, stones, stones == 1 ? "" : "s"));
216 void threwMoreStonesThanHad() {
217 gameManager.addToGameSummary(GameManager.formatErrorMessage(nicknameToken + " tried to throw more stones than they had. I'll let it slide for this time. (But not let them throw that much!)"));
220 void failedToThrowStonesAndShouldHave() {
221 gameManager.addToGameSummary(GameManager.formatErrorMessage(nicknameToken + " tried not throwing any stones. Fixing that for them because I'm in a good mood today."));
225 animateLoss(avatar.getX(), avatar.getY(), 100, "SLOW\nPOKE");
229 animateLoss(avatar.getX(), avatar.getY(), 100, "STUPID");
233 animateLoss(avatar.getX(), avatar.getY(), 100, "CHEATER");
238 Random random = new Random();
241 Text trollPositionGauge;
242 Player p0 = new Player(), p1 = new Player();
243 Text turnCounter; int _turns = 0;
250 * Random π/2-grained rotation of the avatar frames. Avoid
251 * having them π/2 apart, though, as one of them is likely
252 * going to end upside-down and the trick would be revealed.
253 * And I'd have to "draw" a new frame. Ewww.
255 p0.frameRot = random.nextInt(4) * Math.PI / 2;
256 p0.init(gameManager.getPlayer(0));
257 p1.frameRot = p1.frameRot +
258 (random.nextInt(2) == 1 ? 1 : -1) * Math.PI / 2;
259 p1.init(gameManager.getPlayer(1));
270 trollMessage.setX(troll.getX());
272 animateTurnCounter();
277 Pos(int _x, int _y) { x = _x; y = _y; }
280 private void drawBackground() {
281 graphicEntityModule.createSprite()
282 .setImage("background.png")
285 int numMountains = random.nextInt(5);
286 while (numMountains --> 0) {
287 final int pngWidth = 366;
288 double scale = 0.5 * (1 + random.nextDouble());
289 int x = random.nextInt(1920 + (int) (scale*pngWidth))
290 - (int) (scale*pngWidth/2);
291 int baseTint = 64 + random.nextInt(128);
292 Sprite mountain = graphicEntityModule.createSprite()
293 .setImage("mountain.png")
297 .setAnchorY(283.0 / 321.0)
298 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
299 .setScaleX(random.nextInt(2) == 0 ? scale : -scale)
300 .setScaleY(scale * (1 + (random.nextDouble() - 0.5) / 2))
301 .setSkewX((random.nextDouble() - 0.5) / 4)
302 .setSkewY((random.nextDouble() - 0.5) / 8)
303 .setTint((baseTint + random.nextInt(16) - 8) * 0x010000
304 + (baseTint + random.nextInt(16) - 8) * 0x0100
305 + (baseTint + random.nextInt(16) - 8) * 0x01);
306 graphicEntityModule.createSprite().setImage("mountaintop.png")
307 .setX(mountain.getX())
308 .setY(mountain.getY())
309 .setAnchorX(mountain.getAnchorX())
310 .setAnchorY(mountain.getAnchorY())
311 .setRotation(mountain.getRotation())
312 .setScaleX(mountain.getScaleX())
313 .setScaleY(mountain.getScaleY())
314 .setSkewX(mountain.getSkewX())
315 .setSkewY(mountain.getSkewY());
318 int numTrees = random.nextInt(21);
319 ArrayList<Pos> poss = new ArrayList<Pos>(numTrees);
320 while (numTrees --> 0) {
323 x = random.nextInt(1920);
324 // yes, this biases randomness wrt perspective! :-(
325 y = 700 + random.nextInt(175);
326 } while (y > 880 && (x < 200 || x > 1720));
327 poss.add(new Pos(x, y));
329 poss.sort(new Comparator<Pos>() {
330 public int compare(Pos a, Pos b) { return a.y < b.y ? -1 : 1; }
334 double scale = ( 90.0 / 433.0 // base height from PNG
335 * (p.y - 680) / (875 - 680) ); // perspective
336 graphicEntityModule.createSprite()
337 .setImage(random.nextInt(2) == 0 ? "Alshockv1.png"
343 .setScaleX(scale * (random.nextInt(2) == 0 ? -1 : 1)
344 * (1 + (random.nextDouble() - 0.5) / 6))
345 .setScaleY(scale * (1 + (random.nextDouble() -0.5) / 6))
346 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
347 .setSkewX((random.nextDouble() - 0.5) /4)
348 .setSkewY((random.nextDouble() - 0.5) /8);
352 private void drawTroll() {
353 troll = graphicEntityModule.createSprite()
354 .setImage("troll.png")
360 trollPositionGauge = graphicEntityModule.createText()
366 .setFillColor(0xffffff);
369 trollMessage = graphicEntityModule.createText()
374 .setTextAlign(TextBasedEntity.TextAlign.CENTER)
375 .setStrokeColor(0xFFFF00)
376 .setFillColor(0xFFFF00)
380 private void moveTroll() {
381 graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
382 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
383 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
384 troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
386 troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
389 trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
390 int distLeft = model.trollPosition;
391 int distRight = model.roadLength - model.trollPosition;
393 trollPositionGauge.setText("← " + distRight);
395 else if (distRight <= 0) {
396 trollPositionGauge.setText(distLeft + " →");
399 trollPositionGauge.setText(distLeft + " ↔ " + distRight);
401 graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
402 trollPositionGauge.setX(troll.getX());
405 void moveTroll(Dir d) {
407 gameManager.addToGameSummary("Troll " + d.movement);
409 trollMessage.setText(selectTrollMessage(d)).setAlpha(1, Curve.NONE);
410 graphicEntityModule.commitEntityState(0.5, trollMessage);
411 trollMessage.setAlpha(0, Curve.EASE_IN);
412 graphicEntityModule.commitEntityState(1, trollMessage);
415 void animateTurnCounter() {
416 for (int i = 0; i < 10; i++) {
417 turnCounter.setText("T" + _turns + "." + i);
418 // The following line is likely not a bug.
419 graphicEntityModule.commitEntityState((double) i/9, turnCounter);
425 String[] debugModePngs = graphicEntityModule.createSpriteSheetSplitter()
426 .setSourceImage("debug.png")
435 SpriteAnimation debugMode = graphicEntityModule.createSpriteAnimation()
436 .setImages(debugModePngs)
441 toggleModule.displayOnToggleState(debugMode, "debug", true);
443 turnCounter = graphicEntityModule.createText()
448 .setStrokeColor(0xff0080)
449 .setFillColor(0xff0080)
450 .setFontFamily("monospace")
451 .setFontWeight(Text.FontWeight.BOLD)
453 toggleModule.displayOnToggleState(turnCounter, "debug", true);
454 animateTurnCounter();
457 void animateLoss(int x, int y, int size, String message) {
459 if (x < 1920/2) { startX = 1920; }
460 else if (x > 1920/2) { startX = 1920; }
461 else { startX = 1920 * random.nextInt(2); }
463 Text msg = graphicEntityModule.createText(message)
468 .setScaleX(3*random.nextDouble() - 1)
469 .setScaleY(3*random.nextDouble() - 1)
470 .setSkewX(2*random.nextDouble() - 1)
471 .setSkewY(2*random.nextDouble() - 1)
472 .setRotation(4*Math.PI * (1 + random.nextDouble())
473 * (random.nextInt(2) == 0 ? 1 : -1))
475 .setStrokeColor(0xff7f7f)
476 .setFillColor(0xff7f7f)
477 .setFontWeight(Text.FontWeight.BOLD)
478 .setTextAlign(TextBasedEntity.TextAlign.CENTER);
479 graphicEntityModule.commitEntityState(0.25, msg);
480 Curve curve = Curve.ELASTIC;
481 msg.setX(x, Curve.EASE_OUT)
482 .setY(y, Curve.ELASTIC)
487 .setRotation(2*Math.PI * (random.nextDouble() - 0.5), Curve.LINEAR)
488 .setFontSize(size, curve);
491 void doubleDefeat() {
492 gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
493 animateLoss(1920/2, 680, 150, "L0SERZ!");
497 gameManager.addToGameSummary("Draw.");
498 animateLoss(1920/2, 680, 200, "DRAW");
501 String selectTrollMessage(Dir d) {
502 if (random.nextInt(10000) == 0) {
503 return specials[random.nextInt(specials.length)];
507 int i = random.nextInt(directed.length + isotropic.length / 3);
508 if (i < directed.length) {
509 return directed[i][d.index];
512 return isotropic[random.nextInt(isotropic.length)];
516 String specials[] = {
517 "Never gonna give you up\nNever gonna let you down\nNever gonna run around and desert you\nNever gonna make you cry\nNever gonna say goodbye\nNever gonna tell a lie and hurt you",
518 "CG know what they're doing."
521 String directed[][] = {
523 { "Han shot first", "I am your father", "Greedo shot first" },
526 { "tea > coffee", "just drink\nkool-aid", "coffee > tea" },
527 { "Marvel > DC", "Disney > 50 shades", "DC > Marvel" },
528 { "cats > dogs", "humans make\ngood pets", "dogs > cats" },
531 { "PC > console", "pong is still\nunequaled", "console > PC" },
532 { "Windows > Linux", "it's all Android anyway", "Linux > Windows" },
533 { "pad > stick", "mouse gaming is lame", "stick > pad" },
534 { "RTS > FPS", "solitaire best game", "FPS > RTS" },
535 { "YT gaming > twitch", "i watch other ppl play", "twitch > YT gaming" },
538 { "vi < emacs", "i code with Notepad", "emacs > vi" },
539 { "tabs < spaces", "gofmt FTW", "spaces < tabs" },
540 { "LISP is the most\npowerful language", "HTML is a\nprogramming language", "Forth is the most\npowerful language" },
541 { "static linking best", "symlinking best", "dynamic linking best" },
542 { "NPE > SIGSEGV", "kernel panic", "SIGSEGV > NPE" },
543 { "objects > functions", "it's closures\nall the way down", "functions > objects" },
544 { "GOTO FTW", "COME FROM FTW", "don't use GOTO" },
545 { "Agile > Waterfall", "SCRUM isn't Agile", "Waterfall > Agile" },
548 { "gmail > github", "copy-paste FTW", "github > gmail" },
549 { "MSIE > Safari", "Opera did it first", "Safari > MSIE" },
550 { "bing > yahoo", "duckduckgo best SE", "yahoo > bing" },
551 { "jira > trello", "bugzilla FTW", "trello > jira" },
552 { "IRC > slack", "chat is work", "discord < IRC" },
553 { "trolls > SJW", "i'm not trolling\njust misunderstood", "SJW > trolls" },
556 { "searcho no chokudai", "GAimax is True AI", "Smitsimax FTW" },
557 { "Automaton2000 > NN", "bots > humans", "AutomatonNN > 2000" },
558 { "light theme best", "ascii > graphics", "dark theme best" },
559 { "simulation beats heuristics", "true AI is just ifs", "heuristics beat simulation" },
560 { "bruteforce always prevails", "you'll timeout anyway", "algorithms always prevail" }
563 String isotropic[] = {
564 "Electron apps are the fastest",
566 "Thanos did nothing wrong",
567 "developers developers developers",
568 "the cloud is just\nother ppl's computers",
573 "ASCII stupid question\nget a stupid ANSI",