Skip to content

Commit

Permalink
Bug-fixes-0.3.2
Browse files Browse the repository at this point in the history
Fixed saving and loading for all compoudns.
Fixed a multititude of crashes.
Eating a toxin vacuole has a 30% chance of being assimilated.
Only 50 mp in editor.
  • Loading branch information
TheCreator-- authored Aug 9, 2016
1 parent 0546dd6 commit affa536
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 82 deletions.
2 changes: 1 addition & 1 deletion scripts/microbe_editor/microbe_editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function MicrobeEditor:activate()
Engine:playerData():setBool("edited_microbe", true)
Engine:playerData():setActiveCreature(self.nextMicrobeEntity.id, GameState.MICROBE_EDITOR)
end
self.mutationPoints = 100
self.mutationPoints = 50
self.actionHistory = {} -- where all user actions will be registered
self.actionIndex = 0 -- marks the last action that has been done (not undone, but possibly redone), is 0 if there is none
for _, cytoplasm in pairs(self.occupiedHexes) do
Expand Down
24 changes: 24 additions & 0 deletions scripts/microbe_stage/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ agents = {
},
}


function toxinEffect(entityId)
print ("toxin called")
if math.random(1,4) > 1 then return true end
if Engine:playerData():lockedMap():isLocked("Toxin") then
showMessage("Toxin Unlocked!")
Engine:playerData():lockedMap():unlock("Toxin")
local guiSoundEntity = Entity("gui_sounds")
guiSoundEntity:getComponent(SoundSourceComponent.TYPE_ID):playSound("microbe-pickup-organelle")
end
return true
end

function chloroplastEffect(entityId)
if math.random(1,3) > 1 then return true end
if Engine:playerData():lockedMap():isLocked("chloroplast") then
showMessage("Chloroplast Unlocked!")
Engine:playerData():lockedMap():unlock("chloroplast")
local guiSoundEntity = Entity("gui_sounds")
guiSoundEntity:getComponent(SoundSourceComponent.TYPE_ID):playSound("microbe-pickup-organelle")
end
return true
end

processes = {
Respiration = {
speedFactor = 0.1,
Expand Down
4 changes: 2 additions & 2 deletions scripts/microbe_stage/microbe.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ function Microbe:__init(entity, in_editor)
if not self.microbe.initialized then
self:_initialize()
if in_editor == nil then
self.compoundBag:setProcessor(Entity(self.microbe.speciesName):getComponent(ProcessorComponent.TYPE_ID))
self.compoundBag:setProcessor(Entity(self.microbe.speciesName):getComponent(ProcessorComponent.TYPE_ID), self.microbe.speciesName)
SpeciesSystem.template(self, self:getSpeciesComponent())
end
end
Expand Down Expand Up @@ -540,7 +540,7 @@ function Microbe:storeCompound(compoundId, amount, bandwidthLimited)
storedAmount = self.microbe:getBandwidth(amount, compoundId)
end
storedAmount = math.min(storedAmount , self.microbe.capacity - self.microbe.stored)
self.entity:getComponent(CompoundBagComponent.TYPE_ID):giveCompound(compoundId, storedAmount)
self.entity:getComponent(CompoundBagComponent.TYPE_ID):giveCompound(tonumber(compoundId), storedAmount)
self.microbe.stored = self.microbe.stored + storedAmount
return amount - storedAmount
end
Expand Down
6 changes: 3 additions & 3 deletions scripts/microbe_stage/microbe_replacement.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ function MicrobeReplacementSystem:activate()
workingMicrobe.entity:destroy()

species.avgCompoundAmounts = {}
species.avgCompoundAmounts[CompoundRegistry.getCompoundId("atp")] = 10
species.avgCompoundAmounts[CompoundRegistry.getCompoundId("glucose")] = 20
species.avgCompoundAmounts[CompoundRegistry.getCompoundId("oxygen")] = 30
species.avgCompoundAmounts["" .. CompoundRegistry.getCompoundId("atp")] = 10
species.avgCompoundAmounts["" .. CompoundRegistry.getCompoundId("glucose")] = 20
species.avgCompoundAmounts["" .. CompoundRegistry.getCompoundId("oxygen")] = 30

SpeciesSystem.initProcessorComponent(speciesEntity, species)

Expand Down
1 change: 1 addition & 0 deletions scripts/microbe_stage/microbe_stage_hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function HudSystem:update(renderTime)
local playerSpecies = playerMicrobe:getSpeciesComponent()
--TODO display population in home patch here
for compoundID in CompoundRegistry.getCompoundList() do

local compoundsString = string.format("%s - %d", CompoundRegistry.getCompoundDisplayName(compoundID), playerMicrobe:getCompoundAmount(compoundID))
if self.compoundListItems[compoundID] == nil then
-- TODO: fix this colour
Expand Down
52 changes: 27 additions & 25 deletions scripts/microbe_stage/setup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ local function setupCamera()
end

local function setupCompounds()
CompoundRegistry.loadFromLua(compounds, agents)

local ordered_keys = {}

for k in pairs(compounds) do
table.insert(ordered_keys, k)
end

table.sort(ordered_keys)
for i = 1, #ordered_keys do
local k, v = ordered_keys[i], compounds[ ordered_keys[i] ]
CompoundRegistry.registerCompoundType(k, v["name"], v["mesh"], v["size"], v["weight"])
end
CompoundRegistry.loadFromLua({}, agents)
--CompoundRegistry.loadFromXML("../scripts/definitions/compounds.xml")
end

Expand Down Expand Up @@ -108,7 +120,14 @@ function setupSpecies()
speciesEntity = Entity(name)
speciesComponent = SpeciesComponent(name)
speciesEntity:addComponent(speciesComponent)
speciesComponent.organelles = data.organelles -- note, shallow assignment
for i, organelle in pairs(data.organelles) do
local org = {}
org.name = organelle.name
org.q = organelle.q
org.r = organelle.r
org.rotation = organelle.rotation
speciesComponent.organelles[i] = org
end
processorComponent = ProcessorComponent()
speciesEntity:addComponent(processorComponent)
speciesComponent.colour = Vector3(data.colour.r, data.colour.g, data.colour.b)
Expand All @@ -123,7 +142,7 @@ function setupSpecies()
if compoundData ~= nil then
amount = compoundData.amount
-- priority = compoundData.priority
speciesComponent.avgCompoundAmounts[compoundID] = amount
speciesComponent.avgCompoundAmounts["" .. compoundID] = amount
-- speciesComponent.compoundPriorities[compoundID] = priority
end
end
Expand Down Expand Up @@ -275,7 +294,7 @@ end
local function createSpawnSystem()
local spawnSystem = SpawnSystem()

local toxinOrganelleSpawnFunction = function(pos)
local toxinOrganelleSpawnFunction = function(pos)
powerupEntity = Entity()
setSpawnablePhysics(powerupEntity, pos, "AgentVacuole.mesh", 0.9, SphereShape(HEX_SIZE))

Expand All @@ -284,7 +303,8 @@ local function createSpawnSystem()
powerupEntity:addComponent(reactionHandler)

local powerupComponent = PowerupComponent()
powerupComponent:setEffect(unlockToxin)
-- Function name must be in configs.lua
powerupComponent:setEffect("toxinEffect")
powerupEntity:addComponent(powerupComponent)
return powerupEntity
end
Expand All @@ -297,7 +317,8 @@ local function createSpawnSystem()
powerupEntity:addComponent(reactionHandler)

local powerupComponent = PowerupComponent()
powerupComponent:setEffect(unlockChloroplast)
-- Function name must be in configs.lua
powerupComponent:setEffect("chloroplastEffect")
powerupEntity:addComponent(powerupComponent)
return powerupEntity
end
Expand Down Expand Up @@ -372,25 +393,6 @@ local function setupEmitter()
-- addEmitter2Entity(entity, "glucose")
end

function unlockToxin(entityId)
if Engine:playerData():lockedMap():isLocked("Toxin") then
showMessage("Toxin Unlocked!")
Engine:playerData():lockedMap():unlock("Toxin")
local guiSoundEntity = Entity("gui_sounds")
guiSoundEntity:getComponent(SoundSourceComponent.TYPE_ID):playSound("microbe-pickup-organelle")
end
return true
end
function unlockChloroplast(entityId)
if Engine:playerData():lockedMap():isLocked("chloroplast") then
showMessage("Chloroplast Unlocked!")
Engine:playerData():lockedMap():unlock("chloroplast")
local guiSoundEntity = Entity("gui_sounds")
guiSoundEntity:getComponent(SoundSourceComponent.TYPE_ID):playSound("microbe-pickup-organelle")
end
return true
end

local function setupPlayer()
local microbe = microbeSpawnFunctionGeneric(nil, "Default", false, PLAYER_NAME)
microbe.collisionHandler:addCollisionGroup("powerupable")
Expand Down
2 changes: 1 addition & 1 deletion scripts/microbe_stage/species_system.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function SpeciesSystem.template(microbe, species)
microbe.microbe.speciesName = species.name
microbe:setMembraneColour(species.colour)
-- give it organelles
for i, orgdata in pairs(species.organelles) do
for _, orgdata in pairs(species.organelles) do
organelle = OrganelleFactory.makeOrganelle(orgdata)
microbe:addOrganelle(orgdata.q, orgdata.r, orgdata.rotation, organelle)
end
Expand Down
18 changes: 9 additions & 9 deletions src/engine/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <vector>

static_assert(
CHAR_BIT == 8,
CHAR_BIT == 8,
"char must be 8 bit long for properly portable serialization."
);

Expand Down Expand Up @@ -140,8 +140,8 @@ class StorageContainer {
* The value to return when the key is not present (or the value has the
* wrong type)
*
* @return
* The value associated with \a key or \a defaultValue if the key could
* @return
* The value associated with \a key or \a defaultValue if the key could
* not be found or has a value associated with it that is not \a T.
*/
template<typename T>
Expand All @@ -164,7 +164,7 @@ class StorageContainer {
* @param key
* @param defaultValue
*
* @return
* @return
*/
luabind::object
luaGet(
Expand All @@ -191,13 +191,13 @@ class StorageContainer {
T value
);

friend std::ostream&
friend std::ostream&
operator << (
std::ostream& stream,
const StorageContainer& storage
);

friend std::istream&
friend std::istream&
operator >> (
std::istream& stream,
StorageContainer& storage
Expand All @@ -215,7 +215,7 @@ class StorageContainer {
* @param stream
* @param storage
*
* @return
* @return
*/
std::ostream&
operator << (
Expand All @@ -229,7 +229,7 @@ operator << (
* @param stream
* @param storage
*
* @return
* @return
*/
std::istream&
operator >> (
Expand All @@ -252,7 +252,7 @@ class StorageList : public std::vector<StorageContainer> {
* - StorageList::get
* - StorageList::size
*
* @return
* @return
*/
static luabind::scope
luaBindings();
Expand Down
61 changes: 51 additions & 10 deletions src/general/powerup_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
#include "bullet/collision_filter.h"
#include "engine/component_factory.h"
#include "engine/engine.h"
#include "engine/game_state.h"
#include "game.h"
#include "engine/entity.h"
#include "engine/entity_filter.h"
#include "engine/game_state.h"
#include "engine/serialization.h"
#include "scripting/luabind.h"

#include <iostream>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>


using namespace thrive;

Expand All @@ -24,44 +32,77 @@ PowerupComponent::luaBindings() {
]
.def(constructor<>())
.def("setEffect",
static_cast<void (PowerupComponent::*)(const luabind::object&)>(&PowerupComponent::setEffect)
static_cast<void (PowerupComponent::*)(const std::string&)>(&PowerupComponent::setEffect)
)
;
}

void
PowerupComponent::setEffect(
std::function<bool(EntityId)>* effect
const std::string& funcName
){
m_effect = effect;
this->effectName = funcName;
this->setEffect(new std::function<bool(EntityId)>(
[funcName](EntityId entityId) -> bool
{
lua_State* L = Game::instance().engine().luaState();
luaL_openlibs(L);
luaL_loadfile(L, "config.lua");

lua_getglobal(L, funcName.c_str());
lua_pushnumber(L, entityId);
if (lua_pcall(L, 1, 1, 0) != 0)
{
std::cerr << "error: cannot call the function" << std::endl;
return false;
}

if (!lua_isboolean(L, -1))
{
std::cerr << "function '" + funcName + "' must return a boolean" << std::endl;
return false;
}
bool result = lua_toboolean(L, -1);
lua_pop(L, 1);

return result;
}
));
}

void
PowerupComponent::setEffect(
const luabind::object& effect
std::function<bool(EntityId)>* effect
){
this->setEffect(new std::function<bool(EntityId)>(
[effect](EntityId entityId) -> bool
{
return luabind::call_function<bool>(effect, entityId);
}
));
m_effect = effect;
}



void
PowerupComponent::load(
const StorageContainer& storage
) {
Component::load(storage);

this->effectName = storage.get<std::string>("effect");
setEffect(this->effectName);
}


StorageContainer
PowerupComponent::storage() const {
StorageContainer storage = Component::storage();

storage.set("effect", effectName);

return storage;
}





REGISTER_COMPONENT(PowerupComponent)

////////////////////////////////////////////////////////////////////////////////
Expand Down
Loading

0 comments on commit affa536

Please sign in to comment.