LuaSTG Pattern Making Tool-Parametrization

From LuaSTG Wiki
Jump to navigation Jump to search

Before going into a detailed description of the pattern generation algorithm, we need to be more patient and talk about a few other concepts that are the building blocks of the algorithm. The first concept here is parametrization.


What is Parametrization?[edit | edit source]

Parametrization is the process of introducing named variables to replace the literal values in a piece of code. In the context of a function, a full parametrization of the function replaces every value in the function to a named parameter of that function, and the values can be modified for each call to that function. Exceptions to values to be replaced are those constants that never changes in the execution, for example literal constants like 0, 1, or PI are rarely if ever modified. (If you are still confused about my explanation, look up "parameterization programming" on google, also google for some reasons doesn't seem to like the spelling of 'parametrization'.)


Often a piece of code will be executed many times with only some number values in it changed, so parametrizing such a piece of code to allowing changing those values for each different run and avoids just duplicating the code many times just to change those values.


In the previous tutorial we've seen an example of a nested for loop. In some sense that example code is already parametrized because the values 24, 6, 0.1 are given names nBullet, nRing, incSpeed. On the other hand, it is hard to directly re-use the example code. First because it is not put into a function so it can be called in other places. Second observe it only increments a in the outer loop, and v in the inner loop. Sometimes we want to increment both of them in the same for loop, or even increment other variables at the same time.

Generalizing the Increments[edit | edit source]

Assume you want to write a single loop to spawn bullets and increments a and v together. This will spawn a spiral shaped bullet pattern. Further assume that you want to reuse the code that we've written in the previous tutorial by put it into a function.


Because this can be done with a single for loop, we consider modifying the first example in the previous tutorial. A solution is to add other parameters alongside the increment of a. Rewriting the example code and we get:

A Parametrized For Loop


We can parametrize the nested for loop similarly, the results are shown below.

Parametrizing a Nested For Loop


Parameter List[edit | edit source]

The parameter list in the above image is quite long, so how can we organize these parameters to understand them better? We can define a few categories for these parameters by how they are used in the for loop. Starting with the incrementable variables.


Incrementable Variables[edit | edit source]

For a given set of spawn parameters (we have image, x, y, a, v), the incrementable variables are variables in the spawn parameters that will be incremented during the repetition of the for loop. For example, all of x, y, a, v are variables that are incremented in the above for loop.


It is up to us to determine whether a numerical variable in the spawn parameters should be incremented during the execution of a loop, because in most cases not all the numerical parameters are incremented E.g. if you have a number that specifies the rotational velocity of the sprite. However, in most cases incrementable variables need to be numerical variables in order to be incremented, that is, supporting the '+' operation. For this reason, they are usually integers or floating point numbers. In Lua, they can only be of type "number".


In the parameter list, we need to include an initial value for each of the incrementable variables.

Increments[edit | edit source]

Increments are specific to a for loop. For a given set of spawn parameters and a given for loop, they specify the values that each incrementable variable in the spawn parameters need to be incremented in that particular for loop. This implies that we need to have a different set of increments for each for loop we have.


Each increment variable must have an associated incrementable variable that it increments E.g. incSpeed increments the value of variable v.

Non-Incrementable Variables[edit | edit source]

For a given set of spawn parameters, non-incrementable variables are spawn parameters that are not incrementable variables. They may be either a numerical value or a non-numerical value. For example, the string variable image is an non-incrementable variable.

Number of Times a Loop Repeats[edit | edit source]

We also have a special category for variables that specify the number of times the loop repeats. This include n, n2, one for each for loop that we write.


Summary[edit | edit source]

When we look at the parameter lists of the functions that we've written, we can summarize the parameters into the following categories:

  1. the number of repeats; one such variable for each for loop E.g. n, n2
  2. the image of bullets image
  3. four incrementable variables x, y, a, v
  4. and four increment variables for each for loop there originally is E.g. incX, incY, incAngle, incSpeed, incX2, ...


A simple conclusion would be that each for loop adds a total repeat variable and the same number of increment variables as there are incrementable variables.