User Tools

Site Tools


4rpl:commands:signalgenerator

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
4rpl:commands:signalgenerator [2022/07/25 18:53] – Fix doc Karsten754rpl:commands:signalgenerator [2025/02/14 14:57] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +<=[[4rpl:start| Index]] \\
 +<=[[4rpl:start#math_utility| Math Utility]]
  
-<=[[4rpl:start| Index]] 
  
 ====== SignalGenerator ====== ====== SignalGenerator ======
Line 13: Line 14:
  
 Arguments and type in order: Arguments and type in order:
-  * time:  (floating point numberX coordinate in the waveform or how long one repetition of the effect lasts  +  * time: (floatCurrent step in the waveform. For a wave duration of 10.0, a step of 2.5 is a quarter of the way into a pattern. 
-  * frequency: (floating point number) The number of values to generate over the Interval (stepsizeof the waveform +  * frequency: (float) The frequency dictates the duration of each repeating pattern. A duration of 10.0 must set the frequency to 0.1 (1/10.0
-  * phaseShift: (floating point numberAn offset from the origin value of the waveform (See Notes) +  * phaseShift: (floatA starting offset, between 0 and 1, where 0 will start the pattern at the beginning, and 1 will start it at the end. For sine wave, 0.25 will begin the wave at the peak, and 0.75 will begin at the valley. 
-  * invert:  (true/false) inverts the waveform+  * invert: (0/1) inverts the waveform
   * signalType: (0 to 6)   * signalType: (0 to 6)
      * 0 = NONE      * 0 = NONE
Line 22: Line 23:
      * 2 = SQUARE      * 2 = SQUARE
      * 3 = TRIANGLE      * 3 = TRIANGLE
-     * 4 = SAW - TOOTH+     * 4 = SAW-TOOTH
      * 5 = RANDOM      * 5 = RANDOM
      * 6 = CONSTANT      * 6 = CONSTANT
  
-===== Notes ===== +The resulting variable sigValue contains float value between -1 and 1 which represents the current state of the wave at the provided step (timegiven its provided frequency and other factors.
-==== For all waveforms ==== +
-=== Phase Shift === +
-The phase difference, or phase shift as it is also called, of Sinusoidal Waveform is the angle Φ(Greek letter Phi), in degrees or radians that the waveform has shifted from a certain reference point along the horizontal zero axis. In other words phase shift is the lateral difference between two or more waveforms along a common axis. Sinusoidal waveforms of the same frequency can have a phase difference.+
  
-The phase difference, Φ of an alternating waveform can vary from between 0 to its maximum time period, T of the waveform during one complete cycle and this can be anywhere along the horizontal axis betweenΦ = 0 to (radians) or Φ = 0 to 360o depending upon the angular units used.+===== Example ===== 
 +To create a sine wave that should start at its peak and run for 80 frames until it repeatswe should set the frequency to (1.0/80.0), phase shift to 0.25, start the time value at 0 and add 1 to it every frame.
  
-==== For sine wave function ==== +Play the following example in the editor console to spawn a continuous stream of creeper using the sine wave at the bottom of the map. 
-===  Time and Frequency ===+<code 4RPL> 
 +$time:0 
 +$duration:80.0 
 +$phaseShift:0.25 
 +$invert:0 
 +$signalType:
 + 
 +SignalGenerator(<-time <-frequency <-phaseShift <-invert <-signalType) ->sigValue 
 +<-time 1 add ->time 
 + 
 +#Let's use the result to spawn some creeper in a range of cells: 
 +AddCreeper(<-time <-mapX mod, <-sigValue 20.0 mul 20 add, 1) 
 + 
 +:Once 
 +   1.0 <-duration div ->frequency 
 +   GetMapSize ->mapZ ->mapX 
 +</code> 
 + 
 +===== Patterns ===== 
 +[{{cw4_signalgenerator_1.png|1 - SINE, Pattern repeated every 60 cells}}] \\ 
 +[{{cw4_signalgenerator_2.png|2 - SQUARE, Pattern repeated every 60 cells}}]  
 +[{{cw4_signalgenerator_3.png|3 - TRIANGLE, Pattern repeated every 60 cells}}] \\ 
 +[{{cw4_signalgenerator_4.png|4 - SAW-TOOTH, Pattern repeated every 60 cells}}]  
 +[{{cw4_signalgenerator_5.png|5 - RANDOM, Pattern repeated every 5 cells}}] \\ 
 + 
 +===== Extra Notes ===== 
 +This is how each wave is produced in the game's source code: 
 +==== Sine wave function ====
 <code C#> <code C#>
 float t = frequency * time + phase; float t = frequency * time + phase;
 value = (float)Mathf.Sin(2f * Mathf.PI * t); value = (float)Mathf.Sin(2f * Mathf.PI * t);
 </code> </code>
-==== For square wave function ==== +==== Square wave function ====
-===  Time and Frequency ===+
 <code c#> <code c#>
-Square: value = Mathf.Sign(Mathf.Sin(2f * Mathf.PI * t));  +value = Mathf.Sign(Mathf.Sin(2f * Mathf.PI * t)); 
 </code> </code>
 ==== For triangle wave function ==== ==== For triangle wave function ====
-===  Time and Frequency === 
 <code c#> <code c#>
-Triangle value = 1f - 4f * (float)Mathf.Abs(Mathf.Round(t - 0.25f) - (t - 0.25f));+value = 1f - 4f * (float)Mathf.Abs(Mathf.Round(t - 0.25f) - (t - 0.25f));
 </code> </code>
-==== For sawtooth wave function ==== +==== Saw-tooth wave function ====
-===  Time and Frequency ===+
 <code c#> <code c#>
-Sawtooth: value = 2f * (t - (float)Mathf.Floor(t + 0.5f));+value = 2f * (t - (float)Mathf.Floor(t + 0.5f));
 </code> </code>
-==== For random wave function ==== +==== Random wave function ==== 
-===  Time and Frequency ===+The function will output the same number for a given interval.  Interval is 1/frequency.  It only changes to a new value on the next interval.  Hence a random number per interval, or a random number at a given frequency.  Otherwise, just use the rand functions to get a new value on each call. 
 <code c#> <code c#>
-Random: float interval = 1 / frequency; +float interval = 1 / frequency; 
-                int slot = (int)(time / interval) ; +int slot = (int)(time / interval) ; 
-                slot = (slot * 1431655781) + (slot * 1183186591) + (slot * 622729787) + (slot * 338294347); +slot = (slot * 1431655781) + (slot * 1183186591) + (slot * 622729787) + (slot * 338294347); 
-                if (slot < 0) slot = -slot; +if (slot < 0) slot = -slot; 
-                value = (float)GameSpace.instance.RandDoubleInput(randSeed + slot)*2-1;+value = (float)GameSpace.instance.RandDoubleInput(randSeed + slot)*2-1;
 </code> </code>
  
-===== Examples =====+===== Extra examples =====
 ==== Sine Wave Creeper on Terrain ==== ==== Sine Wave Creeper on Terrain ====
 <code 4rpl> <code 4rpl>
Line 90: Line 113:
  
 {{sine_creeper.png?450}} {{sine_creeper.png?450}}
- 
- 
  
 ==== Make a Unit Move or "Float" Above Terrain ==== ==== Make a Unit Move or "Float" Above Terrain ====
Line 98: Line 119:
 # Oscillate # Oscillate
 $UID:1 $UID:1
- 
-SignalGenerator(GetGameUpdateCount , <-frequency , <-phaseShift , <-invert , <-signaltype) ->sigValue 
-<-sigvalue 1 + <-scale * ->sigvalue 
-TraceAllSp (GetGameUpdateCount 180 mod <-sigvalue) 
-GetUnitPosition(<-UID) ->pos 
  
 GetUnitMoveCell(<-UID) ->cellZ ->cellX GetUnitMoveCell(<-UID) ->cellZ ->cellX
 if (<-cellz  -1 EQ) if (<-cellz  -1 EQ)
 + SignalGenerator(<-time , <-frequency , <-phaseShift , <-invert , <-signaltype) ->sigValue
 + <-time 1 + ->time
 + <-sigvalue 1 + <-scale * ->sigvalue
 + GetUnitPosition(<-UID) ->pos
  GetExactTerrain(<-pos.X <-pos.Z false) ->exactTerrainHeight  GetExactTerrain(<-pos.X <-pos.Z false) ->exactTerrainHeight
  SetUnitPosition(<-UID V3(<-pos.x , <-exactTerrainHeight <-sigvalue + , <-pos.z)) # Buil-in unit   SetUnitPosition(<-UID V3(<-pos.x , <-exactTerrainHeight <-sigvalue + , <-pos.z)) # Buil-in unit
  # SetObjPosition(2  "" V3(0 <-sigValue 0) false)          # for custom unit  # SetObjPosition(2  "" V3(0 <-sigValue 0) false)          # for custom unit
 +else
 + 0 ->time
 endIf endIf
 +
 :Once :Once
-# Parameters for SignalGenerator+ # Parameters for SignalGenerator
  1.0  180 / ->frequency  # we cycle over 180 frames  1.0  180 / ->frequency  # we cycle over 180 frames
  0.0 ->phaseShift # offset from zero-time  0.0 ->phaseShift # offset from zero-time
Line 117: Line 140:
  1 ->signalType # type of signal generated  1 ->signalType # type of signal generated
  0.5 ->scale # control the maximum size of movement  0.5 ->scale # control the maximum size of movement
-  
 </code> </code>
  
 <=[[4rpl:start| Index]] <=[[4rpl:start| Index]]
4rpl/commands/signalgenerator.1658775223.txt.gz · Last modified: 2025/02/14 14:56 (external edit)