poniedziałek, 10 sierpnia 2015

#Stencyl: Zerg terrain infection

Part 4: Spreading the infection

Topics:
  • Observation of infection
  • Algorithm of infection spreading
  • Implementation of algorithm
  • Infection over time
  • Extras: counter-infection algorithm
  • Extras: making infected area more interesting

Behaviors used in part IV

Behavior Events:
  • infection_behavior (scene behavior)
    • infect
    • infection_step
    • when <a> pressed
    • when <f> pressed
    • do every <1> seconds

Main variables:
  • infection_behavior
    • infected_list - List; stores as elements infected tiles actors that make edge of infected area
    • temp_list - List; stores the copy of <infected_list>
    • checked_tile - Actor; element of <temp_list>
    • auto_infect - Boolean; true if infection should spread over time

 

Observation of infection spreading


Before jumping to algorithm itself we need to make some observations about the infection process
  • Only edge of infected area is able to infect further tiles. There is only need to track tiles that make edge.
  • After infecting edge tile becomes a tile inside infected area surrounded from four sites by infected tiles (since there are no tiles which can resist the infection).

Edge tile(green) infects tiles around it(blue). Right: after infection tile is surrounded from all sides by infected tiles.


Algorithm for infection spreading


Algorithm for infection (single iteration):
  • Make two lists. First list <temp_list> stores the currently infected edge tiles(pre infection). Second list <infected_list> stores the newly infected edge tiles (post infection).
  • For each tile of current edge try to infect tiles around it. If you encounter uninfected tile infect it and add tile to the list of newly infected edge tiles.
  • Copy elements from newly infected edge tiles to currently infected edge tiles.

Implementation of algorithm
       -> infection_behavior -> infection_step
       -> infection_behavior -> infect
       -> infection_behavior -> animate
       -> infection_behavior -> when <a> pressed

Trigger event <infection_step> does a single iteration of infection. It is called when you press <a>.

At first we set pre-infection edge tile list <temp_list> and post-infection edge tile list <infected_list>. <infected_list> is empty at the beginning.

Then we go through each tile actor of pre-infection edge tile list. Set that actor's animation to animation of being surrounded by 4 infected tiles. Send information to <infect> trigger event that you would like to infect tiles around you. We need to set <checked_tile> before every <infect> trigger event because <checked_tile> is changed upon execution of <infect> trigger event.

<infect> trigger will determine if tile is infected or uninfected. If it's uninfected it will infect it - set proper value in matrix <ground_list> (matrix responsible for keeping status of tile; Part 3) and add the newly infected tile to <infected_list>.

After infection process is done we need to update animation for post-infection edge tiles based on their surroundings. It is done by <animate>.

We add event <when <a> pressed> so that we can call manually single iteration of infection.

Infection over time
       -> infection_behavior -> when created
       -> infection_behavior -> do every <1> seconds
       -> infection_behavior -> when <f> pressed

Infection over time is nothing more than calling <infection_step> once every <some time>. We add <do every <1> seconds> to achieve it. Since we don't want infection to spread until we tell it to do so we add <auto_infect> condition. In when created we set value of <auto_infect> to false. Value of <auto_infect > can be changed to <true> upon pressing <f>.

Events allowing infection over time

Extras: counter-infection algorithm

Sometimes we want for ground to regain lost ground. For example if the infecting race lost supply that allowed the infection. The biomass on the infected tile is dying so neutral ground regains it. The algorithm for this is very similar to that of infection.

Algorithm for counter-infection (single iteration):
  • Make two lists. First list <temp_list> stores the currently infected edge tiles(pre counter-infection). Second list <infected_list> stores the newly infected edge tiles (post counter-infection infection).
  • For each tile of current edge. See if around it there is a tile that has four sides infected. If yes and it's not in <infected_list> add it.
  • After you are done with creating <infected_list> for each tile of currently infected edge set it to neutral.
  • Copy elements from newly infected edge tiles to currently infected edge tiles.
  • Animate the edge tiles based on their surroundings.

Extras: making infected area more interesting

We have infected area but once it gets big we will end up with big boring purple plane. If infected area is big enough we can build some objects inside infected area. The objects themselves can be build up from tiles.

This will require checking once in a while values in <ground_list> to answer the question if we have enough infected tiles to build. Since it's only graphical effect with no influence on gameplay we don't need to do it often.

Infected area with added pools of green goo(sized 3x3) and grouped infected tiles(2x2).

To choose other part follow the link:


Brak komentarzy:

Prześlij komentarz