Difference between revisions of "Byte Battle"
m (→Palettes) |
m (→Palettes) |
||
Line 108: | Line 108: | ||
|- | |- | ||
| <code>poke(16320+j,255/(1+2^(5-j%3-j/5)))</code> || 35 || [[File:Bright blue palette.png]] || <code>2*j%3</code> for a green variant | | <code>poke(16320+j,255/(1+2^(5-j%3-j/5)))</code> || 35 || [[File:Bright blue palette.png]] || <code>2*j%3</code> for a green variant | ||
+ | |- | ||
+ | | <code>poke(16320+j,s(j/15+s(j%3*3))^2*255)</code>|| 37 || [[File:Green-purple palette.png]] || Cyclic, based on [[https://iquilezles.org/www/articles/palettes/palettes.htm]] | ||
|} | |} | ||
− | Code for testing palettes: | + | The last one is an entire family of palettes. You can replace <code>s(j%3*3)</code> with any function that depends on <code>j%3</code>; this ensures the palette remains cyclic. Code for testing palettes: |
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
Line 121: | Line 123: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Any of the palettes can be quickly inverted by adding a <code> | + | Any of the palettes can be quickly inverted by adding a <code>-1-</code> in the expression. It's also possible to just "tune" the default Sweetie 16 palette, for example by setting all the blue channels to zero: <code>poke(16322+j*3,0)</code>. Here <code>j</code> is between 0 and 15. |
== Motion blur == | == Motion blur == |
Revision as of 12:36, 18 February 2022
Contents
Bytebattles
Bytebattles are a form of live coding, similar to Shader Showdowns, where two contestants compete in writing a visual effect in 25 minutes. The coding environment is the TIC-80 fantasy console. However, unlike Shader Showdowns, there is an additional limit: the final code should be 256 characters or less. This requires the contestants to use efficient code (e.g. single letter variables) and to minimize the code (e.g. remove the whitespace), all within the time limit. Unlike in normal TIC-80 sizecoding, there is no compression, so every character counts.
General notation in this article
Symbol | Meaning |
---|---|
i |
Pixel index |
s |
Alias for math.sin |
x |
Pixel x-coordinate |
y |
Pixel y-coordinate |
Basic optimizations
- Functions, that are called three or more times should be aliased. For example,
e=elli
withe()e()e()
is 3 characters shorter thanelli()elli()elli()
. Functions with 5 character names may already benefit from aliasing with two calls:r=rectb
withr()r()
is 1 character shorter thanrectb()rectb()
. -
t=0
witht=t+.1
is 3 characters shorter thant=time()/399
. -
for i=0,32639 do x=i%240y=i/240 end
is 2-3 characters shorter thanfor y=0,135 do for x=0,239 do end end
. -
(x*x+y*y)^.5
is 6 characters shorter thanmath.sqrt(x*x+y*y)
. -
s(w-11)
ands(w+8)
both approximatemath.cos(w)
, so onlymath.sin
needs to be aliased.s(w-11)
is far more accurate, with the cost of one more character.
One-lining
Most whitespace can be removed from LUA code. For example: x=0y=0
is valid. All new lines can be removed or replaced with space, making the whole code a single line:
function TIC()for i=0,32639 do poke4(i,i)end end
Warning: Letters a-f
and A-F
after a number cause problems. a=0b=0
is not valid code. It is advisable to only used one letter variables in the ranges g-z
and G-Z
from the start; this will make eventual one-lining easier.
Load-function
Function load
takes a string of code and returns a function with no named arguments, with the code as its body. It's particularly useful for shortening the TIC function after one-lining:
TIC=load'for i=0,32639 do poke4(i,i)end'
As a rule of thumb, one-lining and using the load trick can bring a ~ 275 character code down to 256.
load
can be even used to minimize a function with parameters: ...
returns the parameters. For example, the following example saves 3 bytes:
SCN=load'r=...poke(16320,r)'
Warning: The backslash causes problems when using the load trick. In particular, if you have a string with escaped characters in the original code e.g. print("foo\nbar")
, then this needs to be double-escaped: load'print("foo\\nbar")'
Dithering
If you have a floating point color value, TIC-80 pix
and poke4
functions round it (toward zero). To add dithering, add a small value, between 0 and 1, to the color. The best technique depends whether you have x
and y
available or only i
and how many bytes you can spare:
A quick example demonstrating the 2x2 block dithering:
function TIC()
cls()
for i=0,2399 do
x=i%240
y=i//240
poke4(i,x/30+(x*2-y%2)%4/4)
end
end
Palettes
The following palettes assume that j
goes from 0 to 47. Usually there's no need to make a new loop for this: just reuse another loop with j=i%48
.
Expression | Length | Result | Notes |
---|---|---|---|
poke(16320+j,j*5) |
17 | ||
poke(16320+j,j%3*j/.4) |
22 | Use (j+1)%3 , (j+2)%3 or 2*j%3 for different colors
| |
poke(16320+j,s(j)^2*255) |
24 | Change the phase of the palette with s(j+p) | |
poke(16320+j,s(j)^2*j*6) |
24 | Change the phase of the palette with s(j+p) | |
poke(16320+j,s(j/15)*255) |
25 | s(j/15)^2 is less bright
| |
poke(16320+j,s(j+2*j%3)^2*255) |
30 | j%3*2 for a more blue/beige variant, -j%3*4 for beige/blue variant
| |
poke(16320+j,255/(1+2^(4+j%3-j/5))) |
35 | 2*j%3 for a pink variant
| |
poke(16320+j,255/(1+2^(5-j%3-j/5))) |
35 | 2*j%3 for a green variant
| |
poke(16320+j,s(j/15+s(j%3*3))^2*255) |
37 | Cyclic, based on [[1]] |
The last one is an entire family of palettes. You can replace s(j%3*3)
with any function that depends on j%3
; this ensures the palette remains cyclic. Code for testing palettes:
function TIC()
cls()
for j=0,47 do poke(16320+j,s(j/15)*255)end
for c=0,15 do rect(c*5,0,5,5,c)end
end
s=math.sin
Any of the palettes can be quickly inverted by adding a -1-
in the expression. It's also possible to just "tune" the default Sweetie 16 palette, for example by setting all the blue channels to zero: poke(16322+j*3,0)
. Here j
is between 0 and 15.
Motion blur
In TIC-80 API, the pix
and poke4
functions round numbers towards zero. This can be abused for a motion blur: poke4(i,peek4(i)-.9)
maps colors 1 to 15 into one lower value, but value 0 stays at it is. Like so:
function TIC()
t=time()/9
circ(t%240,t%136,9,15)
for i=0,32639 do poke4(i,peek4(i)-.9)end
end
Updating only some pixels
Pixel-based effects, especially raycasting and raymarching, can become excessively slow. A simple trick to update only ~ half of the pixels, giving a dithered/motion blur look and making the update smoother:
function TIC()
t=time()/499
for i=t%2,32639,1.9 do poke4(i,i/4e3+t)end
end
Basic raymarcher
The basic structure for a raymarcher that has not been crunched to keep it readable. The map is a bunch of repeated spheres here:
function TIC()
for i=0,32639 do
-- ray (u,v,w), not normalized!
u=i%240/120-1
v=i/32639-.5
w=1
-- camera origo (x,y,z)
x=3
y=0
z=time()/999 -- camera moves with time
j=0
repeat
X=x%6-3 -- domain repetition
Y=y%6-3
Z=z%6-3
-- ray not normalized=>reduce scale
m=(X*X+Y*Y+Z*Z)^.5/2-1
x=x+m*u
y=y+m*v
z=z+m*w
j=j+1
until j>15 or m<.1
poke4(i,j)
end
end
Additional Resources
- Code from past bytebattles https://livecode.demozoo.org/