summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-03-03 19:52:11 +0100
committertroido <troido@protonmail.com>2020-03-03 19:52:11 +0100
commit42448500c1c2e3dd5d6a0a62c59f1f0367c12b04 (patch)
treed075540851213a90d6d77b582a5216d9f107f8c4 /src
parentf844ed18e29465177a339f244800fb7a2e77daa9 (diff)
moved default encyclopedia to data file
Diffstat (limited to 'src')
-rw-r--r--src/defaultencyclopedia.rs180
-rw-r--r--src/main.rs27
-rw-r--r--src/purgatory.rs6
-rw-r--r--src/world.rs4
-rw-r--r--src/worldloader.rs5
5 files changed, 24 insertions, 198 deletions
diff --git a/src/defaultencyclopedia.rs b/src/defaultencyclopedia.rs
deleted file mode 100644
index 191f0a4..0000000
--- a/src/defaultencyclopedia.rs
+++ /dev/null
@@ -1,180 +0,0 @@
-
-use serde_json::json;
-use crate::Encyclopedia;
-
-pub fn default_encyclopedia() -> Encyclopedia {
- Encyclopedia::from_json(json!({
- "wall": {
- "components": ["Blocking"],
- "sprite": "wall",
- "height": 2
- },
- "rock": {
- "components": ["Blocking"],
- "sprite": "rock",
- "height": 10
- },
- "tree": {
- "components": ["Blocking"],
- "sprite": "tree",
- "height": 3
- },
- "fence": {
- "components": ["Blocking"],
- "sprite": "fence",
- "height": 1
- },
- "grass": {
- "components": [
- ["Visible", {
- "sprite": ["random", [
- ["string", "grass1"],
- ["string", "grass2"],
- ["string", "grass3"],
- ["string", "grass1"],
- ["string", "grass2"],
- ["string", "grass3"],
- ["string", "ground"]
- ]],
- "height": ["float", 0.1],
- "name": ["string", "grass"]
- }],
- "Floor"
- ]
- },
- "greengrass": {
- "components": [
- ["Visible", {
- "sprite": ["random", [
- ["string", "grass1"],
- ["string", "grass2"],
- ["string", "grass3"]
- ]],
- "height": ["float", 0.1],
- "name": ["string", "grass"]
- }],
- "Floor"
- ]
- },
- "ground": {
- "components": ["Floor"],
- "sprite": "ground",
- "height": 0.1
- },
- "floor": {
- "components": ["Floor"],
- "sprite": "floor",
- "height": 0.1
- },
- "bridge": {
- "components": [
- "Floor"
- ],
- "sprite": "bridge",
- "height": 0.1
- },
- "water": {
- "components": [],
- "sprite": "water",
- "height": 0.1
- },
- "pebble": {
- "components": [
- ["Item", {
- "ent": ["template", "pebble"],
- "name": ["string", "pebble"],
- "action": ["action", ["eat", 1]]
- }]
- ],
- "sprite": "pebble",
- "height": 0.3
- },
- "stone": {
- "components": [
- ["Item", {"ent": ["template", "stone"], "name": ["string", "stone"], "action": ["action", ["build", "builtwall"]]}]
- ],
- "sprite": "stone",
- "height": 0.4
- },
- "player": {
- "arguments": [["name", "string", null]],
- "components": [
- ["Visible", {
- "sprite": ["string", "player"],
- "height": ["float", 1.0],
- "name": ["arg", "name"]
- }],
- ["Player", {
- "name": ["arg", "name"]
- }],
- ["Inventory", {"capacity": ["int", 3]}],
- ["Health", {"health": ["int", 9], "maxhealth": ["int", 10]}]
- ]
- },
- "portal": {
- "arguments": [["destination", "string", null], ["dest_pos", "string", ""]],
- "components": [
- ["RoomExit", {"destination": ["arg", "destination"], "dest_pos": ["arg", "dest_pos"]}],
- "Floor"
- ]
- },
- "builtwall": {
- "arguments": [["health", "int", 100]],
- "components": [
- "Blocking",
- ["Health", {"health": ["arg", "health"], "maxhealth": ["int", 100]}],
- "Mortal"
- ],
- "sprite": "wall",
- "height": 2
- },
- "spiketrap": {
- "components": [["Trap", {"damage": ["int", 8]}]],
- "sprite": "spikes",
- "height": 0.8
- },
- "dummy": {
- "arguments": [["health", "int", 20]],
- "sprite": "dummy",
- "height": 1,
- "components": [
- ["Health", {"health": ["arg", "health"], "maxhealth": ["int", 20]}],
- "Mortal"
- ]
- },
- "wound": {
- "sprite": "wound",
- "height": 0.25,
- "components": [["Volatile", {"delay": ["int", 4]}]],
- "save": false
- },
- "rat": {
- "sprite": "rat",
- "height": 1,
- "components": [
- ["MonsterAI", {
- "view_distance": ["int", 3],
- "move_chance": ["float", 0.08],
- "homesickness": ["float", 0.1]
- }],
- ["Health", {"health": ["int", 8], "maxhealth": ["int", 8]}],
- ["Fighter", {"damage": ["int", 2], "cooldown": ["int", 6]}],
- ["Movable", {"cooldown": ["int", 3]}],
- "Mortal",
- ["Faction", {"faction": ["string", "evil"]}]
- ]
- },
- "spawner": {
- "arguments": [["template", "template", null], ["amount", "int", 1], ["delay", "int", 0], ["clan", "string", ""], ["initial_spawn", "bool", true]],
- "components": [
- ["Spawner", {
- "template": ["arg", "template"],
- "amount": ["arg", "amount"],
- "delay": ["arg", "delay"],
- "clan": ["arg", "clan"],
- "initial_spawn": ["arg", "initial_spawn"]
- }]
- ]
- }
- })).unwrap()
-}
diff --git a/src/main.rs b/src/main.rs
index 204926f..4732d77 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,11 +1,10 @@
use std::thread::sleep;
use std::time::Duration;
-use std::path::Path;
-use std::path::PathBuf;
-use std::str::FromStr;
-use std::sync::atomic::{AtomicBool, Ordering};
-use std::sync::Arc;
+use std::path::{Path, PathBuf};
+use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
+use std::fs;
+use serde_json;
mod server;
mod gameserver;
@@ -26,7 +25,6 @@ mod template;
mod roomtemplate;
mod savestate;
mod playerid;
-mod defaultencyclopedia;
mod playerstate;
mod roomid;
mod persistence;
@@ -52,7 +50,6 @@ use self::{
server::unixserver::UnixServer,
server::tcpserver::TcpServer,
server::Server,
- defaultencyclopedia::default_encyclopedia,
persistence::FileStorage,
controls::Action,
worldloader::WorldLoader,
@@ -76,12 +73,22 @@ fn main() -> Result<()>{
let mut gameserver = GameServer::new(servers);
-
- let loader = WorldLoader::new(PathBuf::from_str(&(std::env::var("CARGO_MANIFEST_DIR").unwrap_or(".".to_string()) + "/content/maps/"))?);
+ let content_dir = PathBuf::new().join(std::env::var("CARGO_MANIFEST_DIR").unwrap_or(".".to_string())).join("content/");
+ let loader = WorldLoader::new(content_dir.join("maps"));
let storage = FileStorage::new(FileStorage::savedir().expect("couldn't find any save directory"));
+
+ let encyclopedia = Encyclopedia::from_json(
+ serde_json::from_str(
+ &fs::read_to_string(
+ content_dir
+ .join("encyclopediae")
+ .join("default_encyclopedia.json")
+ )?
+ )?
+ )?;
- let mut world = World::new(default_encyclopedia(), loader, Box::new(storage), RoomId::from_str("room"));
+ let mut world = World::new(encyclopedia, loader, Box::new(storage), RoomId::from_str("room"));
let mut message_cache = MessageCache::default();
diff --git a/src/purgatory.rs b/src/purgatory.rs
index df7f3e9..b96065e 100644
--- a/src/purgatory.rs
+++ b/src/purgatory.rs
@@ -6,7 +6,7 @@ use specs::{
use crate::{
RoomId,
- defaultencyclopedia::default_encyclopedia,
+ Encyclopedia,
room::Room,
roomtemplate::RoomTemplate,
systems::{
@@ -26,7 +26,7 @@ pub fn purgatory_id() -> RoomId {
RoomId{name: String::from("+")}
}
-pub fn create_purgatory<'a, 'b>() -> Room<'a, 'b> {
+pub fn create_purgatory<'a, 'b>(encyclopedia: Encyclopedia) -> Room<'a, 'b> {
let dispatcher = DispatcherBuilder::new()
.with(Volate, "volate", &[])
.with(RegisterNew::default(), "registernew", &[])
@@ -38,7 +38,7 @@ pub fn create_purgatory<'a, 'b>() -> Room<'a, 'b> {
.with(Create, "create", &["view"])
.with(Remove, "remove", &["view", "move"])
.build();
- let mut room = Room::new(purgatory_id(), default_encyclopedia(), dispatcher);
+ let mut room = Room::new(purgatory_id(), encyclopedia, dispatcher);
room.load_from_template(&RoomTemplate::from_json(&json!({
"width": 11,
"height": 11,
diff --git a/src/world.rs b/src/world.rs
index f428cc1..f297d16 100644
--- a/src/world.rs
+++ b/src/world.rs
@@ -35,9 +35,9 @@ impl <'a, 'b>World<'a, 'b> {
template_loader,
persistence,
default_room,
- encyclopedia,
+ encyclopedia: encyclopedia.clone(),
players: HashMap::new(),
- rooms: hashmap!(purgatory::purgatory_id() => purgatory::create_purgatory()),
+ rooms: hashmap!(purgatory::purgatory_id() => purgatory::create_purgatory(encyclopedia)),
time: Timestamp(1000000)
}
}
diff --git a/src/worldloader.rs b/src/worldloader.rs
index d8a7c0f..2d7ae69 100644
--- a/src/worldloader.rs
+++ b/src/worldloader.rs
@@ -1,5 +1,5 @@
-use std::path::PathBuf;
+use std::path::{PathBuf};
use std::fs;
use serde_json;
use serde_json::Value;
@@ -22,9 +22,8 @@ impl WorldLoader {
}
pub fn load_room(&self, id: RoomId) -> Result<RoomTemplate> {
- let mut path = self.directory.clone();
let fname = id.to_string() + ".json";
- path.push(fname);
+ let path = self.directory.join(fname);
let text = fs::read_to_string(path)?;
let json: Value = serde_json::from_str(&text)?;
let template = RoomTemplate::from_json(&json)?;