]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/terrain/terrain.factor
Fix truncation with unparse-string
[factor.git] / extra / terrain / terrain.factor
index 4062dca108472a2323a5e59e380db532bc2ef7d6..934e809c25b18748750cbab81f0d5a3422f445fd 100644 (file)
@@ -1,17 +1,15 @@
-! (c)2009 Joe Groff, Doug Coleman. bsd license
-USING: accessors arrays combinators game.input game.loop
-game.input.scancodes grouping kernel literals locals
-math math.constants math.functions math.order
-math.vectors opengl opengl.capabilities opengl.gl
-opengl.shaders opengl.textures opengl.textures.private
-sequences sequences.product specialized-arrays
-terrain.generation terrain.shaders typed ui ui.gadgets
-ui.gadgets.worlds ui.pixel-formats game.worlds method-chains
-math.matrices.simd noise ui.gestures combinators.short-circuit
-destructors grid-meshes math.vectors.simd ;
+! Copyright (C) 2009 Joe Groff, Doug Coleman.
+! See https://factorcode.org/license.txt for BSD license.
+USING: accessors arrays combinators combinators.short-circuit
+destructors game.input game.input.scancodes game.loop
+game.worlds grid-meshes grouping kernel literals math
+math.functions math.matrices.simd math.order
+math.vectors math.vectors.simd noise opengl
+opengl.capabilities opengl.gl opengl.shaders opengl.textures
+sequences specialized-arrays terrain.generation terrain.shaders
+typed ui ui.gadgets.worlds ui.gestures ui.pixel-formats ;
 QUALIFIED-WITH: alien.c-types c
 SPECIALIZED-ARRAY: c:float
-SIMD: c:float
 IN: terrain
 
 CONSTANT: FOV $[ 2.0 sqrt 1 + ]
@@ -20,15 +18,16 @@ CONSTANT: FAR-PLANE 2.0
 CONSTANT: PLAYER-START-LOCATION float-4{ 0.5 0.51 0.5 1.0 }
 CONSTANT: VELOCITY-MODIFIER-NORMAL float-4{ 1.0 1.0 1.0 0.0 }
 CONSTANT: VELOCITY-MODIFIER-FAST float-4{ 2.0 1.0 2.0 0.0 }
+CONSTANT: BOUNCE float-4{ 1.0 -0.2 1.0 1.0 }
 CONSTANT: PLAYER-HEIGHT 1/256.
-CONSTANT: GRAVITY float-4{ 0.0 -1/4096. 0.0 0.0 }
-CONSTANT: JUMP 1/1024.
-CONSTANT: MOUSE-SCALE 1/10.
-CONSTANT: MOVEMENT-SPEED 1/16384.
-CONSTANT: FRICTION float-4{ 0.95 0.99 0.95 1.0 }
+CONSTANT: GRAVITY float-4{ 0.0 -1/8192. 0.0 0.0 }
+CONSTANT: JUMP 1/2048.
+CONSTANT: MOUSE-SCALE 1/20.
+CONSTANT: MOVEMENT-SPEED 1/32768.
+CONSTANT: FRICTION float-4{ 0.97 0.995 0.97 1.0 }
 CONSTANT: COMPONENT-SCALE float-4{ 0.5 0.01 0.0005 0.0 }
-CONSTANT: SKY-PERIOD 1200
-CONSTANT: SKY-SPEED 0.0005
+CONSTANT: SKY-PERIOD 2400
+CONSTANT: SKY-SPEED 0.00025
 
 CONSTANT: terrain-vertex-size { 512 512 }
 
@@ -55,9 +54,6 @@ TUPLE: terrain-world < game-world
         float-4{ 0.0 0.0 0.0 1.0 } >>velocity
         VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
 
-M: terrain-world tick-length
-    drop 1000 30 /i ;
-
 : frustum ( dim -- -x x -y y near far )
     dup first2 min v/n
     NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
@@ -72,12 +68,9 @@ M: terrain-world tick-length
     [ yaw>> 0.0 1.0 0.0 glRotatef ]
     [ location>> vneg first3 glTranslatef ] tri ;
 
-: degrees ( deg -- rad )
-    pi 180.0 / * ; inline
-
 TYPED: eye-rotate ( yaw: float pitch: float v: float-4 -- v': float-4 )
-    [ float-4{  0.0 -1.0 0.0 0.0 } swap degrees rotation-matrix4 ]
-    [ float-4{ -1.0  0.0 0.0 0.0 } swap degrees rotation-matrix4 m4. ]
+    [ float-4{  0.0 -1.0 0.0 0.0 } swap deg>rad rotation-matrix4 ]
+    [ float-4{ -1.0  0.0 0.0 0.0 } swap deg>rad rotation-matrix4 m4. ]
     [ m4.v ] tri* float-4{ t t t f } vand ;
 
 : forward-vector ( player -- v )
@@ -123,7 +116,7 @@ terrain-world H{
     read-keyboard keys>> :> keys
 
     key-left-shift keys nth
-    VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player (>>velocity-modifier)
+    VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player velocity-modifier<<
 
     {
         [ key-1 keys nth 1  f ? ]
@@ -131,19 +124,19 @@ terrain-world H{
         [ key-3 keys nth 3  f ? ]
         [ key-4 keys nth 4  f ? ]
         [ key-5 keys nth 10000 f ? ]
-    } 0|| player (>>reverse-time)
-
-    key-w keys nth [ player walk-forward ] when 
-    key-s keys nth [ player walk-backward ] when 
-    key-a keys nth [ player walk-leftward ] when 
-    key-d keys nth [ player walk-rightward ] when 
-    key-q keys nth [ player -1 look-horizontally ] when 
-    key-e keys nth [ player 1 look-horizontally ] when 
-    key-left-arrow keys nth [ player -1 look-horizontally ] when 
-    key-right-arrow keys nth [ player 1 look-horizontally ] when 
-    key-down-arrow keys nth [ player 1 look-vertically ] when 
-    key-up-arrow keys nth [ player -1 look-vertically ] when 
-    key-space keys nth [ player jump ] when 
+    } 0|| player reverse-time<<
+
+    key-w keys nth [ player walk-forward ] when
+    key-s keys nth [ player walk-backward ] when
+    key-a keys nth [ player walk-leftward ] when
+    key-d keys nth [ player walk-rightward ] when
+    key-q keys nth [ player -1 look-horizontally ] when
+    key-e keys nth [ player 1 look-horizontally ] when
+    key-left-arrow keys nth [ player -1 look-horizontally ] when
+    key-right-arrow keys nth [ player 1 look-horizontally ] when
+    key-down-arrow keys nth [ player 1 look-vertically ] when
+    key-up-arrow keys nth [ player -1 look-vertically ] when
+    key-space keys nth [ player jump ] when
     key-escape keys nth [ world close-window ] when
     player read-mouse rotate-with-mouse
     reset-mouse ;
@@ -155,10 +148,10 @@ terrain-world H{
     GRAVITY v+ ;
 
 : clamp-coords ( coords dim -- coords' )
-    [ { 0 0 } vmax ] dip { 2 2 } v- vmin ;
+    { 0 0 } swap { 2 2 } v- vclamp ;
 
 :: pixel-indices ( coords dim -- indices )
-    coords vfloor [ >integer ] map dim clamp-coords :> floor-coords
+    coords vfloor v>integer dim clamp-coords :> floor-coords
     floor-coords first2 dim first * + :> base-index
     base-index dim first + :> next-row-index
 
@@ -173,14 +166,27 @@ terrain-world H{
     pixel dup vfloor v- :> pixel-mantissa
     segment bitmap>> 4 <groups> :> pixels
     pixel dim pixel-indices :> indices
-    
-    indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
+
+    indices [ pixels nth COMPONENT-SCALE vdot 255.0 / ] map
     first4 pixel-mantissa bilerp ;
 
-: collide ( segment location -- location' )
-    [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
-    [ [ 1 ] 2dip [ max ] with change-nth ]
-    [ ] tri ;
+: (collide) ( segment location -- location' )
+    [
+        { 0 2 3 3 } vshuffle terrain-height-at PLAYER-HEIGHT +
+        -1/0. swap -1/0. -1/0. float-4-boa
+    ] keep vmax ; inline
+
+TYPED:: collide ( world: terrain-world player: player -- )
+    world terrain-segment>> :> segment
+    player location>> :> location
+    segment location (collide) :> location'
+
+    location location' = not [
+        player
+            location' >>location
+            [ BOUNCE v* ] change-velocity
+            drop
+    ] when ;
 
 : scaled-velocity ( player -- velocity )
     [ velocity>> ] [ velocity-modifier>> ] bi v* ;
@@ -193,21 +199,21 @@ terrain-world H{
     world history>> :> history
     history length 0 > [
         history length reverse-time 1 - - 1 max history set-length
-        history pop world (>>player)
+        history pop world player<<
     ] when ;
 
 : tick-player-forward ( world player -- )
     2dup save-history
     [ apply-friction apply-gravity ] change-velocity
-    dup scaled-velocity [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
-    drop ;
+    dup scaled-velocity [ v+ ] curry change-location
+    collide ;
 
 : tick-player ( world player -- )
     dup reverse-time>>
     [ tick-player-reverse ]
     [ tick-player-forward ] if ;
 
-M: terrain-world tick*
+M: terrain-world tick-game-world
     [ dup focused?>> [ handle-input ] [ drop ] if ]
     [ dup player>> tick-player ] bi ;
 
@@ -219,11 +225,11 @@ M: terrain-world tick*
     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
 
 : sky-gradient ( world -- t )
-    game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
+    game-loop>> tick#>> SKY-PERIOD mod SKY-PERIOD /f ;
 : sky-theta ( world -- theta )
-    game-loop>> tick-number>> SKY-SPEED * ;
+    game-loop>> tick#>> SKY-SPEED * ;
 
-BEFORE: terrain-world begin-world
+M: terrain-world begin-game-world
     "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
     require-gl-version-or-extensions
     GL_DEPTH_TEST glEnable
@@ -244,7 +250,7 @@ BEFORE: terrain-world begin-world
     terrain-vertex-size <grid-mesh> >>terrain-mesh
     drop ;
 
-AFTER: terrain-world end-world
+M: terrain-world end-game-world
     {
         [ terrain-mesh>> dispose ]
         [ terrain-program>> delete-gl-program ]
@@ -256,7 +262,7 @@ AFTER: terrain-world end-world
 M: terrain-world resize-world
     GL_PROJECTION glMatrixMode
     glLoadIdentity
-    dim>> [ [ 0 0 ] dip first2 glViewport ]
+    dim>> [ [ { 0 0 } ] dip gl-viewport ]
     [ frustum glFrustum ] bi ;
 
 M: terrain-world draw-world*
@@ -277,20 +283,16 @@ M: terrain-world draw-world*
         ] with-gl-program ]
     } cleave gl-error ;
 
-M: terrain-world pref-dim* drop { 1024 768 } ;
-
-: terrain-window ( -- )
-    [
-        f T{ world-attributes
-            { world-class terrain-world }
-            { title "Terrain" }
-            { pixel-format-attributes {
-                windowed
-                double-buffered
-                T{ depth-bits { value 24 } }
-            } }
-            { grab-input? t }
-        } open-window
-    ] with-ui ;
-
-MAIN: terrain-window
+GAME: terrain-game {
+        { world-class terrain-world }
+        { title "Terrain" }
+        { pixel-format-attributes {
+            windowed
+            double-buffered
+            T{ depth-bits { value 24 } }
+        } }
+        { use-game-input? t }
+        { grab-input? t }
+        { pref-dim { 1024 768 } }
+        { tick-interval-nanos $[ 60 fps ] }
+    } ;