veloren/server/src/sys/terrain.rs
Christof Petig 15b11d9154 Implement /price_list (work in progress), stub for /buy and /sell
remove outdated economic simulation code

remove old values, document

add natural resources to economy

Remove NaturalResources from Place (now in Economy)

find closest site to each chunk

implement natural resources (the distance scale is wrong)

cargo fmt

working distance calculation

this collection of natural resources seem to make sense, too much Wheat though

use natural resources and controlled area to replenish goods

increase the amount of chunks controlled by one guard to 50

add new professions and goods to the list

implement multiple products per worker

remove the old code and rename the new code to the previous name

correctly determine which goods guards will give you access to

correctly estimate the amount of natural resources controlled

adapt to new server API

instrument tooltips

Now I just need to figure out how to store a (reference to) a closure

closures for tooltip content generation

pass site/cave id to the client

Add economic information to the client structure
(not yet exchanged with the server)

Send SiteId to the client, prepare messages for economy request
Make client::sites a HashMap

Specialize the Crafter into Brewer,Bladesmith and Blacksmith

working server request for economic info from within tooltip

fully operational economic tooltips
I need to fix the id clash between caves and towns though

fix overlapping ids between caves and sites

display stock amount

correctly handle invalid (cave) ids in the request

some initial balancing, turn off most info logging

less intrusive way of implementing the dynamic tool tips in map

further tooltip cleanup

further cleanup, dynamic tooltip not fully working as intended

correctly working tooltip visibility logic

cleanup, display labor value

separate economy info request in a separate translation unit

display values as well

nicer display format for economy

add last_exports and coin to the new economy

do not allocate natural resources to Dungeons (making town so much larger)

balancing attempt

print town size statistics

cargo fmt (dead code)

resource tweaks, csv debugging

output a more interesting town (and then all sites)

fix the labor value logic (now we have meaningful prices)

load professions from ron (WIP)

use assets manager in economy

loading professions works

use professions from ron file

fix Labor debug logic

count chunks per type separately
(preparing for better resource control)

better structured resource data

traders, more professions (WIP)

fix exception when starting the simulation

fix replenish function
TODO:
- use area_ratio for resource usage (chunks should be added to stock, ratio on usage?)
- fix trading

documentation clean up

fix merge artifact

Revise trader mechanic

start Coin with a reasonable default

remove the outdated economy code

preserve documentation from removed old structure

output neighboring sites (preparation)

pass list of neighbors to economy

add trade structures

trading stub

Description of purpose by zesterer on Discord

remember prices (needed for planning)

avoid growing the order vector unboundedly

into_iter doesn't clear the Vec, so clear it manually

use drain to process Vecs, avoid clone

fix the test server

implement a test stub (I need to get it faster than 30 seconds to be more useful)

enable info output in test

debug missing and extra goods

use the same logging extension as water, activate feature

update dependencies

determine good prices, good exchange goods

a working set of revisions

a cozy world which economy tests in 2s

first order planning version

fun with package version

buy according to value/priority, not missing amount

introduce min price constant, fix order priority

in depth debugging

with a correct sign the trading plans start to make sense

move the trade planning to a separate function

rename new function

reorganize code into subroutines (much cleaner)

each trading step now has its own function

cut down the number of debugging output

introduce RoadSecurity and Transportation

transport capacity bookkeeping

only plan to pay with valuable goods, you can no longer stockpile unused options
(which interestingly shows a huge impact, to be investigated)

Coin is now listed as a payment (although not used)

proper transportation estimation (although 0)

remove more left overs uncovered by viewing the code in a merge request

use the old default values, handle non-pileable stocks directly before increasing it
(as economy is based on last year's products)

don't order the missing good multiple times
also it uses coin to buy things!

fix warnings and use the transportation from stock again

cargo fmt

prepare evaluation of trade

don't count transportation multiple times

fix merge artifact

operational trade planning
trade itself is still misleading

make clippy happy

clean up

correct labor ratio of merchants (no need to multiply with amount produced)

incomplete merchant labor_value computation

correct last commit

make economy of scale more explicit

make clippy happy (and code cleaner)

more merchant tweaks (more pop=better)

beginning of real trading code

revert the update of dependencies

remove stale comments/unused code

trading implementation complete (but untested)

something is still strange ...

fix sign in trading

another sign fix

some bugfixes and plenty of debugging code

another bug fixed, more to go

fix another invariant (rounding will lead to very small negative value)

introduce Terrain and Territory

fix merge mistakes
2021-03-14 03:18:32 +00:00

262 lines
10 KiB
Rust

use crate::{
chunk_generator::ChunkGenerator, client::Client, presence::Presence, rtsim::RtSim, Tick,
};
use common::{
comp::{self, bird_medium, inventory::loadout_builder::LoadoutConfig, Alignment, Pos},
event::{EventBus, ServerEvent},
generation::get_npc_name,
npc::NPC_NAMES,
terrain::TerrainGrid,
LoadoutBuilder, SkillSetBuilder,
};
use common_ecs::{Job, Origin, Phase, System};
use common_net::msg::ServerGeneral;
use common_sys::state::TerrainChanges;
use specs::{Join, Read, ReadStorage, Write, WriteExpect};
use std::sync::Arc;
use vek::*;
/// This system will handle loading generated chunks and unloading
/// unneeded chunks.
/// 1. Inserts newly generated chunks into the TerrainGrid
/// 2. Sends new chunks to nearby clients
/// 3. Handles the chunk's supplement (e.g. npcs)
/// 4. Removes chunks outside the range of players
#[derive(Default)]
pub struct Sys;
impl<'a> System<'a> for Sys {
#[allow(clippy::type_complexity)] // TODO: Pending review in #587
type SystemData = (
Read<'a, EventBus<ServerEvent>>,
Read<'a, Tick>,
WriteExpect<'a, ChunkGenerator>,
WriteExpect<'a, TerrainGrid>,
Write<'a, TerrainChanges>,
WriteExpect<'a, RtSim>,
ReadStorage<'a, Pos>,
ReadStorage<'a, Presence>,
ReadStorage<'a, Client>,
);
const NAME: &'static str = "terrain";
const ORIGIN: Origin = Origin::Server;
const PHASE: Phase = Phase::Create;
fn run(
_job: &mut Job<Self>,
(
server_event_bus,
tick,
mut chunk_generator,
mut terrain,
mut terrain_changes,
mut rtsim,
positions,
presences,
clients,
): Self::SystemData,
) {
let mut server_emitter = server_event_bus.emitter();
// Fetch any generated `TerrainChunk`s and insert them into the terrain.
// Also, send the chunk data to anybody that is close by.
'insert_terrain_chunks: while let Some((key, res)) = chunk_generator.recv_new_chunk() {
let (chunk, supplement) = match res {
Ok((chunk, supplement)) => (chunk, supplement),
Err(Some(entity)) => {
if let Some(client) = clients.get(entity) {
client.send_fallible(ServerGeneral::TerrainChunkUpdate {
key,
chunk: Err(()),
});
}
continue 'insert_terrain_chunks;
},
Err(None) => {
continue 'insert_terrain_chunks;
},
};
// Send the chunk to all nearby players.
for (presence, pos, client) in (&presences, &positions, &clients).join() {
let chunk_pos = terrain.pos_key(pos.0.map(|e| e as i32));
// Subtract 2 from the offset before computing squared magnitude
// 1 since chunks need neighbors to be meshed
// 1 to act as a buffer if the player moves in that direction
let adjusted_dist_sqr = (chunk_pos - key)
.map(|e: i32| (e.abs() as u32).saturating_sub(2))
.magnitude_squared();
if adjusted_dist_sqr <= presence.view_distance.pow(2) {
client.send_fallible(ServerGeneral::TerrainChunkUpdate {
key,
chunk: Ok(Box::new(chunk.clone())),
});
}
}
// TODO: code duplication for chunk insertion between here and state.rs
// Insert the chunk into terrain changes
if terrain.insert(key, Arc::new(chunk)).is_some() {
terrain_changes.modified_chunks.insert(key);
} else {
terrain_changes.new_chunks.insert(key);
rtsim.hook_load_chunk(key);
}
// Handle chunk supplement
for entity in supplement.entities {
// Check this because it's a common source of weird bugs
assert!(
terrain
.pos_key(entity.pos.map(|e| e.floor() as i32))
.map2(key, |e, tgt| (e - tgt).abs() <= 1)
.reduce_and(),
"Chunk spawned entity that wasn't nearby",
);
if entity.is_waypoint {
server_emitter.emit(ServerEvent::CreateWaypoint(entity.pos));
continue;
}
let mut body = entity.body;
let name = entity.name.unwrap_or_else(|| "Unnamed".to_string());
let alignment = entity.alignment;
let main_tool = entity.main_tool;
let mut stats = comp::Stats::new(name);
let mut scale = entity.scale;
// Replace stuff if it's a boss
if entity.is_giant {
if rand::random::<f32>() < 0.65 && entity.alignment != Alignment::Enemy {
let body_new = comp::humanoid::Body::random();
let npc_names = NPC_NAMES.read();
body = comp::Body::Humanoid(body_new);
stats = comp::Stats::new(format!(
"Gentle Giant {}",
get_npc_name(&npc_names.humanoid, body_new.species)
));
}
scale = 2.0 + rand::random::<f32>();
}
let loadout_config = entity.loadout_config;
let economy = entity.trading_information.as_ref();
let skillset_config = entity.skillset_config;
stats.skill_set =
SkillSetBuilder::build_skillset(&main_tool, skillset_config).build();
let loadout =
LoadoutBuilder::build_loadout(body, main_tool, loadout_config, economy).build();
let health = comp::Health::new(body, entity.level.unwrap_or(0));
let poise = comp::Poise::new(body);
let can_speak = match body {
comp::Body::Humanoid(_) => alignment == comp::Alignment::Npc,
comp::Body::BirdMedium(bird_medium) => match bird_medium.species {
// Parrots like to have a word in this, too...
bird_medium::Species::Parrot => alignment == comp::Alignment::Npc,
_ => false,
},
_ => false,
};
let trade_for_site = if matches!(loadout_config, Some(LoadoutConfig::Merchant)) {
economy.map(|e| e.id)
} else {
None
};
// TODO: This code sets an appropriate base_damage for the enemy. This doesn't
// work because the damage is now saved in an ability
/*
if let Some(item::ItemKind::Tool(item::ToolData { base_damage, .. })) =
&mut loadout.active_item.map(|i| i.item.kind)
{
*base_damage = stats.level.level() as u32 * 3;
}
*/
server_emitter.emit(ServerEvent::CreateNpc {
pos: Pos(entity.pos),
stats,
health,
poise,
loadout,
agent: if entity.has_agency {
Some(comp::Agent::new(
Some(entity.pos),
can_speak,
trade_for_site,
&body,
matches!(
loadout_config,
Some(comp::inventory::loadout_builder::LoadoutConfig::Guard)
),
))
} else {
None
},
body,
alignment,
scale: comp::Scale(scale),
home_chunk: Some(comp::HomeChunk(key)),
drop_item: entity.loot_drop,
rtsim_entity: None,
})
}
}
// Remove chunks that are too far from players.
let mut chunks_to_remove = Vec::new();
terrain
.iter()
.map(|(k, _)| k)
// Don't check every chunk every tick (spread over 16 ticks)
.filter(|k| k.x.abs() as u64 % 4 + (k.y.abs() as u64 % 4) * 4 == tick.0 % 16)
// There shouldn't be to many pending chunks so we will just check them all
.chain(chunk_generator.pending_chunks())
.for_each(|chunk_key| {
let mut should_drop = true;
// For each player with a position, calculate the distance.
for (presence, pos) in (&presences, &positions).join() {
if chunk_in_vd(pos.0, chunk_key, &terrain, presence.view_distance) {
should_drop = false;
break;
}
}
if should_drop {
chunks_to_remove.push(chunk_key);
}
});
for key in chunks_to_remove {
// TODO: code duplication for chunk insertion between here and state.rs
if terrain.remove(key).is_some() {
terrain_changes.removed_chunks.insert(key);
rtsim.hook_unload_chunk(key);
}
chunk_generator.cancel_if_pending(key);
}
}
}
pub fn chunk_in_vd(
player_pos: Vec3<f32>,
chunk_pos: Vec2<i32>,
terrain: &TerrainGrid,
vd: u32,
) -> bool {
let player_chunk_pos = terrain.pos_key(player_pos.map(|e| e as i32));
let adjusted_dist_sqr = (player_chunk_pos - chunk_pos)
.map(|e: i32| (e.abs() as u32).saturating_sub(2))
.magnitude_squared();
adjusted_dist_sqr <= vd.pow(2)
}