- def Plant(World world):
- int NUM_LEVELS = 5; # 5 levels displayed plant state.
- int NUM_UNITS = 72; # 1 level is 72 units water
- int FRACTION_MULTIPLY = 1024; # 1 unit water is 1024/1024 water
- int INITIAL_LEVEL = (NUM_LEVELS * NUM_UNITS - 1) * FRACTION_MULTIPLY;
- int WARN_LEVEL = 1; # Level to reach before the user is warned.
- XY position = nullptr; # Position of the plant in the world (if any).
- int water; # Fractions of water for all levels.
- bool warned; # Whether the user was warned about low level.
- # Return the level of water available for the plant. Used for drawing the plant.
- function int GetLevel():
- return water // (NUM_UNITS * FRACTION_MULTIPLY);
- end
- state initial:
- do water = INITIAL_LEVEL;
- warned = false;
- SetAnimation(GetLevel());
- goto wait_drop;
- state wait_drop:
- recv drop_down(XY newpos)
- do position = newpos;
- goto alive;
- recv time_tick; # Ignore time while in inventory.
- state alive:
- recv time_tick
- do if refreshing:
- water = water + REFRESH_AMOUNT;
- if water >= INITIAL_LEVEL:
- water = INITIAL_LEVEL;
- refreshing = false;
- warned = false;
- end
- else:
- water = water - position.GetTemperature();
- if water < 0: water = 0; end;
- end;
- SetAnimation(GetLevel());
- if not warned and GetLevel() <= WARN_LEVEL:
- ui.WarnUser("Plant at %s has low water level", position);
- end;
- recv refresh_water:
- refreshing = true;
- recv pick_up:
- do position = nullptr;
- goto wait_drop;
- end