Skip to content

Instantly share code, notes, and snippets.

import random, math
const
objectCount = 1000000
avoidCount = 20
maxSpriteCount = 1100000
type
SpriteData = object
posX, posY: float
import math, random
proc gauss(mu = 0.0, sigma = 1.0): float =
var
s = 0.0
u = 0.0
v = 0.0
while s > 1 or s == 0:
u = 2.0 * rand(1.0) - 1.0
v = 2.0 * rand(1.0) - 1.0
import random, math
template gaussImpl(randCall) =
const K = sqrt(2 / E)
var
a = 0.0
b = 0.0
while true:
a = randCall
b = (2.0 * randCall - 1.0) * K
const componentRegistry = CacheSeq"anymap.componentRegistry"
proc makeField(n: NimNode): NimNode =
let s = n.strVal
result = ident(toLowerAscii(s[0]) & substr(s, 1))
macro component*(s: untyped): untyped =
expectKind s, nnkTypeDef
result = copyNimTree(s)
componentRegistry.add result[0].basename
##[
Fixed point arithmetic
A fixed point number is an alternative representation for a real number.
IEEE floats, `f32` and `f64`, being the standard format in processors with
Floating Point Units (FPU). You should consider using fixed numbers on
systems where there's no FPU and performance is critical as fixed point
arithmetic is faster than software emulated IEEE float arithmetic. Do note
that fixed point numbers tend to be more prone to overflows as they operate
in ranges much smaller than floats.
type
CStringArray* = object
len*: int
impl*: cstringArray
proc `=destroy`*(cstrs: var CStringArray) =
if cstrs.impl != nil:
for i in 0 ..< cstrs.len:
deallocShared(cstrs.impl[i])
deallocShared(cstrs.impl)
import game_types, registry, storage
# proc `>`(game: var Game; x, y: Entity): bool =
# template hierX: untyped = game.hierarchy[x]
# template hierY: untyped = game.hierarchy[y]
#
# hierY.parent == x or hierX.next == y or
# (hierX.parent != y and hierY.next != x) and
# (hierX.parent < hierY.parent or (hierX.parent == hierY.parent and hierX < hierY)))
import tables
type
DirKind = enum # must be ordered alphabetically!
dkNone, dkAttention, dkAuthor, dkAuthors, dkCaution, dkCode, dkCodeBlock, dkContainer, dkContents, dkDanger,
dkError, dkFigure, dkHint, dkImage, dkImportant, dkInclude, dkIndex, dkNote, dkRaw, dkTip, dkTitle, dkWarning
const
DirIds: array[DirKind, string] = [
dkNone: "",
@planetis-m
planetis-m / csp.nim
Created January 19, 2021 00:47
Classic Computer Science Problems in Python
import tables, options, sugar
type
Constraint*[V, D] = ref object
# Base class for all constraints
variables*: seq[V] # The variables that the constraint is between
satisfied*: proc (assignment: Table[V, D]): bool
Csp*[V, D] = object
# A constraint satisfaction problem consists of variables of type V
import eminim, std/[parsejson, streams]
type
Fruit = enum
Banana, Apple
Bar = object
shared: int
kind: Fruit
bad: float