Skip to content

Commit

Permalink
world has now a constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jardimdanificado committed May 9, 2024
1 parent 3704d25 commit f147ada
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 98 deletions.
2 changes: 1 addition & 1 deletion data/config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local config = {};

config.title = "brutopolis";
config.version = "0.0.6";
config.version = "0.0.7";

config.mapSize = {x = 32, y = 32};
config.minRoomSize = {x = 5, y = 5};
Expand Down
113 changes: 19 additions & 94 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,11 @@ local Room = require("types.Room");

local Creature = require("types.Creature");

local World = require("types.World");

local items = require("data.items");
local furnitures = require("data.furnitures");

local function checkPlayer()
if br.player.needs.current.health <= 0 then
print("Game Over");
os.exit();
end
end

local function createMap(sizex,sizey)
local map = {};
for x = 1, sizex do
map[x] = {};
for y = 1, sizey do
map[x][y] = Room(config.minRoomSize, config.maxRoomSize);
map[x][y].position = {x = x, y = y};
end
end
return map;
end

local function printRoom(world,roomx,roomy)
local room = world.map[roomx][roomy];
local temproom = br.utils.table.clone(room.map);
Expand All @@ -63,69 +46,21 @@ local function printRoom(world,roomx,roomy)
end


local world = {};
world.creatures = {};
world.map = createMap(config.mapSize.x, config.mapSize.y);

br.updatefluids = function()
for x = 1, #world.map do
for y = 1, #world.map[1] do
-- map all fluids and verify if there are two of more fluids in the same tile, if some, move one to a adjacent tile that exists and not a wall "#" or 32, the new tile must have less water than the original
for i = 1, #world.map[x][y].items do
if world.map[x][y].items[i].liquid then
for j = 1, #world.map[x][y].items do
if world.map[x][y].items[j].liquid
and i ~= j
and world.map[x][y].items[i] ~= world.map[x][y].items[j]
and world.map[x][y].items[i].position["local"].x == world.map[x][y].items[j].position["local"].x
and world.map[x][y].items[i].position["local"].y == world.map[x][y].items[j].position["local"].y then
local item = world.map[x][y].items[j];
local newx = item.position["local"].x + br.utils.random(-1,1);
local newy = item.position["local"].y + br.utils.random(-1,1);
if newx > 0 and newx < #world.map[x][y].map and newy > 0 and newy < #world.map[x][y].map[1] and world.map[x][y].map[newx][newy] ~= 35 then
item.position["local"].x = newx;
item.position["local"].y = newy;
end
end
end
end
end
end
end
end

br.spawn = function(creature, gx, gy, lx, ly)
local c = Creature(creature);
print(creature .. " spawned at " .. gx .. "," .. gy .. " local " .. lx .. "," .. ly)
c.position.global = {x = gx, y = gy};
c.position["local"] = {x = lx, y = ly};
table.insert(world.creatures, c);
table.insert(world.map[gx][gy].creatures, c);
return c;
end

br.getitem = function(creature, itemid, filledwith)
local item = items[itemid](creature.position);
if filledwith then
for x = 1, item.maxStorage do
table.insert(item.items, items[filledwith](creature.position));
end
end
table.insert(creature.items, item);
return item;
end
local world = World(config, br);
local player = world.spawn("epaminondas", 1, 1, 3, 3);

local player = br.spawn("epaminondas", 1, 1, 3, 3);
printRoom(world,1,1);
br.spawn = world.spawn;
br.getitem = world.getitem;

br.redraw = function()
printRoom(world,player.position.global.x,player.position.global.y);
end
br.redraw();

local function move(direction)
local creature = player;
creature.plan("move", {direction});
world.passTurn();
world.passTurn(br.redraw);
end

br.use = function(itemid)
Expand All @@ -135,17 +70,17 @@ br.use = function(itemid)
end
print(player.name .. " used " .. player.items[itemid].name);
player.plan("consume", {itemid});
world.passTurn();
world.passTurn(br.redraw);
end

br.pee = function(...)
player.plan("pee", {...});
world.passTurn();
world.passTurn(br.redraw);
end

br.poo = function(...)
player.plan("poo", {...});
world.passTurn();
world.passTurn(br.redraw);
end

br.w = function()
Expand All @@ -166,23 +101,13 @@ end

br.world = world;

world.passTurn = function()
for x = 1, #world.creatures do
if #world.creatures[x].planned > 0 then
world.creatures[x].act(world)
end
world.creatures[x]:passTurn();
world.creatures[x]:checkNeeds(world);
end
br.updatefluids();
checkPlayer();
br.redraw();
end

br.player = player;
br.world.player = player;

br.getitem(player, "bottle", "water");
br.getitem(player, "smallbox", "bread")


br.inventory = br.player.items;

br.needs = function()
Expand All @@ -199,33 +124,33 @@ br.drop = function(itemid)
return;
end
player.plan("drop", {itemid});
world.passTurn();
world.passTurn(br.redraw);
end

br.pick = function(direction)
player.plan("pick", {direction});
world.passTurn();
world.passTurn(br.redraw);
end

br.put = function(itemid, containerid, other)
if containerid == "in" or containerid == "inside" then
containerid = other;
end
player.plan("put", {itemid, containerid});
world.passTurn();
world.passTurn(br.redraw);
end

br.remove = function(itemid, containerid, other)
if containerid == "from" then
containerid = other;
end
player.plan("remove", {itemid, containerid});
world.passTurn();
world.passTurn(br.redraw);
end

br.sleep = function()
player.plan("sleep",{});
world.passTurn();
world.passTurn(br.redraw);
end

br.items = function()
Expand Down
10 changes: 7 additions & 3 deletions types/Creature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ local creatureConsume = function(creature, world, itemid)

print(creature.name .. "'s " .. item.name .. " has " .. #item.items*100 .. "ml left");
end
elseif item.liquid then
print("liquids can only be consumed from liquid containers");
else
sumEffect(creature, item);
end
Expand Down Expand Up @@ -384,8 +386,10 @@ local creatureDrop = function(creature, world, itemid)
local item;
if creature.items[itemid] then
item = table.remove(creature.items, itemid);
item.position["global"] = br.utils.table.clone(creature.position["global"]);
item.position["local"] = br.utils.table.clone(creature.position["local"]);

item.position = br.utils.table.clone(creature.position);

--br.help(creature)
table.insert(world.map[creature.position["global"].x][creature.position["global"].y].items, item);
print(creature.name .. " dropped " .. item.name);
end
Expand All @@ -407,7 +411,7 @@ local creaturePick = function(creature, world, direction)
end

for k,v in pairs(room.items) do
if v.position["local"].x == creature.position["local"].x + direction.x and v.position["local"].y == creature.position["local"].y + direction.y then
if not v.liquid and v.position["local"].x == creature.position["local"].x + direction.x and v.position["local"].y == creature.position["local"].y + direction.y then
local item = table.remove(room.items, k);
item.position = creature.position;
table.insert(creature.items, v);
Expand Down
12 changes: 12 additions & 0 deletions types/Event.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local Event = function(actor, victim, action, duration, effect, callback)
local self = {};
self.actor = actor;
self.victim = victim;
self.action = action;
self.duration = duration;
self.callback = callback or function() end;
self.effect = effect;
return self;
end

return Event;
103 changes: 103 additions & 0 deletions types/World.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
local Room = require("types.Room");
local Creature = require("types.Creature");

local items = require("data.items");

local function createMap(config)
local map = {};
for x = 1, config.mapSize.x do
map[x] = {};
for y = 1, config.mapSize.y do
map[x][y] = Room(config.minRoomSize, config.maxRoomSize);
map[x][y].position = {x = x, y = y};
end
end
return map;
end

local function checkPlayer(player)
if player.needs.current.health <= 0 then
print("Game Over");
os.exit();
end
end


local function World(config, br)
br = br or require("bruter.bruter");
local world = {};
world.creatures = {};
world.map = createMap(config);

world.updatefluids = function()
for x = 1, #world.map do
for y = 1, #world.map[1] do
-- map all fluids and verify if there are two of more fluids in the same tile, if some, move one to a adjacent tile that exists and not a wall "#" or 32, the new tile must have less water than the original
for i = 1, #world.map[x][y].items do
if world.map[x][y].items[i].liquid then
for j = 1, #world.map[x][y].items do
if world.map[x][y].items[j].liquid
and i ~= j
and world.map[x][y].items[i] ~= world.map[x][y].items[j]
and world.map[x][y].items[i].position["local"].x == world.map[x][y].items[j].position["local"].x
and world.map[x][y].items[i].position["local"].y == world.map[x][y].items[j].position["local"].y then
local item = world.map[x][y].items[j];
local newx = item.position["local"].x + br.utils.random(-1,1);
local newy = item.position["local"].y + br.utils.random(-1,1);
if newx > 0 and newx < #world.map[x][y].map and newy > 0 and newy < #world.map[x][y].map[1] and world.map[x][y].map[newx][newy] ~= 35 then
item.position["local"].x = newx;
item.position["local"].y = newy;
end
end
end
end
end
end
end
end

world.passTurn = function(_callback)
for x = 1, #world.creatures do
if #world.creatures[x].planned > 0 then
world.creatures[x].act(world)
end

for y = 1, #world.creatures[x].items do
if world.creatures[x].items[y].liquid then
world.creatures[x].actions.drop(world.creatures[x], world, y);
end
end

world.creatures[x]:passTurn();
world.creatures[x]:checkNeeds(world);
end
world.updatefluids();
checkPlayer(world.player);
_callback();
end

world.spawn = function(creature, gx, gy, lx, ly)
local c = Creature(creature);
print(creature .. " spawned at " .. gx .. "," .. gy .. " local " .. lx .. "," .. ly)
c.position.global = {x = gx, y = gy};
c.position["local"] = {x = lx, y = ly};
table.insert(world.creatures, c);
table.insert(world.map[gx][gy].creatures, c);
return c;
end

world.getitem = function(creature, itemid, filledwith)
local item = items[itemid](creature.position);
if filledwith then
for x = 1, item.maxStorage do
table.insert(item.items, items[filledwith](creature.position));
end
end
table.insert(creature.items, item);
return item;
end

return world;
end

return World;

0 comments on commit f147ada

Please sign in to comment.