Skip to content

Instantly share code, notes, and snippets.

View redblobgames's full-sized avatar
🥰

Amit Patel redblobgames

🥰
View GitHub Profile
@redblobgames
redblobgames / LLM-OUTPUT.md
Created May 19, 2025 22:45
LLM-generated MMO

Okay, creating a full tank-based combat MMO is a very big project! But I can help you get started with a simplified version in JavaScript, showing you the basic parts for both the server (which runs the game's logic) and the client (what players see and interact with in their web browser).

We'll use Node.js with the express and ws libraries for the server, and HTML, CSS, and JavaScript (with the Phaser 3 game framework) for the client. Phaser will make it easier to draw things on the screen and handle game actions.

Think of it like this:

  • The Server: It's like the main brain of the game. It knows where every player is, handles the rules (like when a tank gets hit), and tells all the players what's happening.
  • The Client: This is what runs in each player's web browser. It shows the game world, lets the player control their tank, and sends their actions to the server. It also receives updates from the server to show what other players are doing.

This will be a basic example to show how the

@redblobgames
redblobgames / my-project-color.el
Last active April 25, 2025 10:45
Emacs config to assign a different color per project
;;; my-project-color.el ---
;;; Commentary: assign a color per project, for use in the mode line and other places
;;
;;; Code:
(require 'project)
(require 'color)
@redblobgames
redblobgames / utils.js
Created March 21, 2025 15:22
My one line utility functions that I copy into various projects
function clamp(x, lo, hi) { return x < lo ? lo : x > hi ? hi : x; }
function lerp(a, b, t) { return a * (1-t) + b * t; }
function unlerp(a, b, t) { return (t - a) / (b - a); }
function rescale(v, from_lo, from_hi, to_lo, to_hi) { return lerp(to_lo, to_hi, unlerp(from_lo, from_hi, v)); }
function mod(a, b) { return (a % b + b) % b; }
function radiansToDegrees(radians) { return 180 / Math.PI * radians; }
function linearstep(a, b, t) { return clamp(unlerp(a, b, t), 0, 1); }
function smoothstep(a, b, t) { let x = linearstep(a, b, t); return 3 * x**2 - 2 * x**3; }
function randRange(lo, hi) { return Math.floor(Math.random() * (hi-lo)) + lo; }
function randInt(lo, hi) { return randRange(lo, hi+1); }
@redblobgames
redblobgames / client.js
Created March 1, 2025 17:56
Hello World of websockets, javascript client, python server
/**
* This is based on the example code from
* https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications
*/
const exampleSocket = new WebSocket("ws://localhost:9999");
function send(msg) {
document.querySelector("#send").textContent += msg + "\n";
exampleSocket.send(msg);
@redblobgames
redblobgames / README.md
Created April 29, 2023 23:19
Three-Rhombus storage of a hex map

Reddit user /u/GavrielBA is using three rhombuses to store a hexagon-shaped map

@redblobgames
redblobgames / Test.cs
Last active October 21, 2022 23:58
C# distance test, are Abs, Pow, or Sqrt slow? Yes - Pow(v,2) is much slower than v*v
using System;
using System.Numerics;
static bool InDistance1(Vector2 pos1, Vector2 pos2, float maxDistance) {
var distance = Math.Sqrt(Math.Pow(Math.Abs(pos2.X - pos1.X), 2) + Math.Pow(Math.Abs(pos2.Y - pos1.Y), 2));
return distance <= maxDistance;
}
static bool InDistance2(Vector2 pos1, Vector2 pos2, float maxDistance) {
var distanceSquared = (pos2.X - pos1.X) * (pos2.X - pos1.X) + (pos2.Y - pos1.Y) * (pos2.Y - pos1.Y);
@redblobgames
redblobgames / .block
Last active September 30, 2022 16:42 — forked from HarryStevens/.block
Voronoi Jiggle
license: gpl-3.0
@redblobgames
redblobgames / treedemo.as
Created June 2, 2022 04:37
Tree generator I wrote in 2011 - but it's for Flash
// Draw simple trees
// Author: [email protected]
// License: MIT
// TODO: read http://procworld.blogspot.com/2011/04/tree-bits.html
// TODO: read http://procworld.blogspot.com/2011/05/mango-sequoia-baobab.html
package {
import flash.display.*;
import flash.geom.Point;
@redblobgames
redblobgames / sfc32.ts
Created May 10, 2022 16:21
sfc32 random number generator, typescript
// SFC32 random number generator, public domain code adapted
// from https://github.com/bryc/code/blob/master/jshash/PRNGs.md
function PRNG(seed: number): {
nextInt(): number;
nextFloat(): number;
getState(): number[];
setState(state: number[]): void
} {
let a = 0, b = seed, c = 0, d = 1;
function sfc32() {
@redblobgames
redblobgames / for.js
Created March 22, 2022 02:16
for loop comparison - probably poor benchmarking on my part
(function() {
let array = [];
for (let i = 0; i < 10000000; ++i) {
array.push({
a: i,
b: i / 2,
r: 0,
});
}