wrote basic building stuff, need to impliment more in the player.
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
# Nathan Hinton Jan 2026
|
||||
# Game file for the buildings
|
||||
|
||||
|
||||
require 'securerandom'
|
||||
|
||||
# Load the ships into a constant:
|
||||
require 'yaml'
|
||||
BUILDING_DATA = File.open('lib/game/data/buildings.yaml', 'r') do |fi|
|
||||
YAML.safe_load(fi.read())
|
||||
end
|
||||
|
||||
|
||||
class Building
|
||||
attr_reader :name
|
||||
|
||||
# Create a building. Requires a player and a planet to construct on.
|
||||
#
|
||||
# @param player [Player] The player who owns the building.
|
||||
# @param planet [Planet] The planet the building should be built on.
|
||||
# @param name [String] The name of the building to be built.
|
||||
def initialize(player, planet, name)
|
||||
@player = player
|
||||
@planet = planet
|
||||
@name = name
|
||||
@building_exist_time = 0
|
||||
@building_last_production_time = 0
|
||||
# Return error if the building does not exist
|
||||
@building = BUILDING_DATA[name]
|
||||
if @building.nil?
|
||||
raise BuildingError, "Invalid building name `#{name}`"
|
||||
end
|
||||
|
||||
# Check the player owns the planet
|
||||
if planet.owner != player
|
||||
raise BuildingError, 'Player does not own planet!'
|
||||
end
|
||||
|
||||
# Check the player has the resources
|
||||
resources = ['metal', 'crystals', 'fuel', 'credits']
|
||||
resources.each do |resource|
|
||||
if @player.player_resources[resource] < @building['build_cost'][resource]
|
||||
raise BuildingError, "Player does not have enough #{resource}!"
|
||||
end
|
||||
end
|
||||
|
||||
resources.each do |resource|
|
||||
@player.player_resources[resource] -= @building['build_cost'][resource]
|
||||
end
|
||||
|
||||
# Check that the player owns the needed buildings to construct:
|
||||
player_buildings = @player.all_buildings.map{|building| building.name}
|
||||
@building['required_buildings'].each do |required_building|
|
||||
unless player_buildings.include? required_building
|
||||
raise BuildingError, "Requires `#{required_building}` first"
|
||||
end
|
||||
end
|
||||
|
||||
# Check that the player owns the needed tech to construct:
|
||||
player_tech = @player.all_tech.map{|tech| tech.name}
|
||||
@building['required_tech'].each do |required_tech|
|
||||
unless player_tech.include? required_tech
|
||||
raise BuildingError, "Requires the tech of `#{required_tech}` first"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# This is in charge of doing things like adding to the turn build power and
|
||||
# providing resources from buildings.
|
||||
def update(delta_time)
|
||||
@building_exist_time += delta_time
|
||||
# Incriment internal time count:
|
||||
# Do not produce resources if the building is not finished
|
||||
if @building_exist_time < @building['build_cost']['time']
|
||||
return # Exit
|
||||
end
|
||||
|
||||
# Do not produce resources if there are not enough resources for upkeep
|
||||
resources = ['metal', 'crystals', 'fuel', 'credits']
|
||||
resources.each do |resource|
|
||||
if @player.player_resources[resource * delta_time] < @building['upkeep'][resource * delta_time]
|
||||
raise BuildingWarning, "Player does not have enough #{resource} to produce from #{@name}!"
|
||||
end
|
||||
end
|
||||
|
||||
resources.each do |resource|
|
||||
@player.player_resources[resource * delta_time] -= @building['upkeep'][resource * delta_time]
|
||||
end
|
||||
@building_last_production_time += delta_time
|
||||
|
||||
# If the building is a resource production building we need to add the resources:
|
||||
if @building['classification'] == 'mining'
|
||||
if @building_last_production_time >= 1
|
||||
@building_last_production_time -= 1
|
||||
resources.each do |resource|
|
||||
# This resource production should jump
|
||||
@player.player_resources[resource] += @building['produces'][resource]
|
||||
end
|
||||
end
|
||||
elsif @building['classification'] == 'production'
|
||||
@player.ship_build_power += @building['produces']['ship_build_power']
|
||||
@player.research_tech_build_power += @building['produces']['research_tech_build_power']
|
||||
@player.military_tech_build_power += @building['produces']['military_tech_build_power']
|
||||
else
|
||||
raise BuildingError, 'Unknown classification for building type. Trying to add resources etc...'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# Save a player to a file
|
||||
def save_to_file
|
||||
File.open("game_data/#{self.planet_id}.save", 'wb') do |fi|
|
||||
# Load resources for the player:
|
||||
fi.write(Marshal.dump(self))
|
||||
end
|
||||
return self.planet_id
|
||||
end
|
||||
|
||||
# Load a player from a file
|
||||
def self.load_from_save(fname)
|
||||
File.open("game_data/#{fname}_ship_.save", 'rb') do |fi|
|
||||
# Load resources for the player:
|
||||
Marshal.load(fi.read)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Error class for the buildings
|
||||
class BuildingError < StandardError
|
||||
end
|
||||
|
||||
# Warning class for the buildings
|
||||
class BuildingWarning < StandardError
|
||||
end
|
||||
@@ -1,163 +0,0 @@
|
||||
# Nathan Hinton 2 Jan 2025 Buildings for the game
|
||||
#
|
||||
|
||||
# Production buildings:
|
||||
Shipyard Level 1:
|
||||
classification: production
|
||||
faction: good
|
||||
production: # This is what is required to produce the building:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
time:
|
||||
upkeep:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
base_stats: []
|
||||
|
||||
Shipyard Level 2:
|
||||
classification: production
|
||||
faction: good
|
||||
production: # This is what is required to produce the building:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
time:
|
||||
upkeep:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
base_stats: []
|
||||
|
||||
Space Construction Station Level 1:
|
||||
classification: production
|
||||
faction: good
|
||||
production: # This is what is required to produce the building:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
time:
|
||||
upkeep:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
base_stats: []
|
||||
|
||||
Space Construction Station Level 2:
|
||||
classification: production
|
||||
faction: good
|
||||
production: # This is what is required to produce the building:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
time:
|
||||
upkeep:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
base_stats: []
|
||||
|
||||
Research Factory Level 1:
|
||||
classification: production
|
||||
faction: good
|
||||
production: # This is what is required to produce the building:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
time:
|
||||
upkeep:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
base_stats: []
|
||||
|
||||
Research Factory Level 2:
|
||||
classification: production
|
||||
faction: good
|
||||
production: # This is what is required to produce the building:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
time:
|
||||
upkeep:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
base_stats: []
|
||||
|
||||
Shipyard Level 1:
|
||||
classification: production
|
||||
faction: good
|
||||
production: # This is what is required to produce the building:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
time:
|
||||
upkeep:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
base_stats: []
|
||||
|
||||
Shipyard Level 1:
|
||||
classification: production
|
||||
faction: good
|
||||
production: # This is what is required to produce the building:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
time:
|
||||
upkeep:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
base_stats: []
|
||||
|
||||
Shipyard Level 1:
|
||||
classification: production
|
||||
faction: good
|
||||
production: # This is what is required to produce the building:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
time:
|
||||
upkeep:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
base_stats: []
|
||||
|
||||
Shipyard Level 1:
|
||||
classification: production
|
||||
faction: good
|
||||
production: # This is what is required to produce the building:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
time:
|
||||
upkeep:
|
||||
metal:
|
||||
crystals:
|
||||
fuel:
|
||||
credits:
|
||||
base_stats: []
|
||||
@@ -0,0 +1,328 @@
|
||||
# Nathan Hinton 2 Jan 2025 Buildings for the game
|
||||
#
|
||||
|
||||
# Production buildings:
|
||||
Shipyard Level 1:
|
||||
classification: production
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 1,000
|
||||
crystals: 1,000
|
||||
fuel: 1,000
|
||||
credits: 1,000
|
||||
time: 1,000
|
||||
upkeep:
|
||||
metal: 100
|
||||
crystals: 100
|
||||
fuel: 100
|
||||
credits: 100
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
ship_build_power: 100
|
||||
research_tech_build_power: 0
|
||||
military_tech_build_power: 0
|
||||
required_tech: []
|
||||
required_buildings: []
|
||||
|
||||
Shipyard Level 2:
|
||||
classification: production
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 2,000
|
||||
crystals: 2,000
|
||||
fuel: 2,000
|
||||
credits: 2,000
|
||||
time: 2,000
|
||||
upkeep:
|
||||
metal: 200
|
||||
crystals: 200
|
||||
fuel: 200
|
||||
credits: 200
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
ship_build_power: 200
|
||||
research_tech_build_power: 0
|
||||
military_tech_build_power: 0
|
||||
required_tech: []
|
||||
required_buildings: [Shipyard Level 1]
|
||||
|
||||
Space Construction Station Level 1:
|
||||
classification: production
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 2,000
|
||||
crystals: 2,000
|
||||
fuel: 2,000
|
||||
credits: 2,000
|
||||
time: 2,000
|
||||
upkeep:
|
||||
metal: 200
|
||||
crystals: 200
|
||||
fuel: 200
|
||||
credits: 200
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
ship_build_power: 200
|
||||
research_tech_build_power: 0
|
||||
military_tech_build_power: 0
|
||||
required_tech: [Anti Gravity Thrusters 1]
|
||||
required_buildings: [Shipyard Level 1]
|
||||
|
||||
Space Construction Station Level 2:
|
||||
classification: production
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 3,000
|
||||
crystals: 3,000
|
||||
fuel: 3,000
|
||||
credits: 3,000
|
||||
time: 3,000
|
||||
upkeep:
|
||||
metal: 300
|
||||
crystals: 300
|
||||
fuel: 300
|
||||
credits: 300
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
ship_build_power: 300
|
||||
research_tech_build_power: 0
|
||||
military_tech_build_power: 0
|
||||
required_tech: [Anti Gravity Thrusters 2]
|
||||
required_buildings: [Space Construction Station Level 1, Shipyard Level 2]
|
||||
|
||||
Research Factory Level 1:
|
||||
classification: production
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 1,000
|
||||
crystals: 1,000
|
||||
fuel: 1,000
|
||||
credits: 1,000
|
||||
time: 1,000
|
||||
upkeep:
|
||||
metal: 100
|
||||
crystals: 100
|
||||
fuel: 100
|
||||
credits: 100
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
ship_build_power: 0
|
||||
research_tech_build_power: 100
|
||||
military_tech_build_power: 0
|
||||
required_tech: []
|
||||
required_buildings: []
|
||||
|
||||
Research Factory Level 2:
|
||||
classification: production
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 2,000
|
||||
crystals: 2,000
|
||||
fuel: 2,000
|
||||
credits: 2,000
|
||||
time: 2,000
|
||||
upkeep:
|
||||
metal: 200
|
||||
crystals: 200
|
||||
fuel: 200
|
||||
credits: 200
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
ship_build_power: 0
|
||||
research_tech_build_power: 200
|
||||
military_tech_build_power: 0
|
||||
required_tech: []
|
||||
required_buildings: [Research Factory Level 1]
|
||||
|
||||
Military Factory Level 1:
|
||||
classification: production
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 2,000
|
||||
crystals: 2,000
|
||||
fuel: 2,000
|
||||
credits: 2,000
|
||||
time: 2,000
|
||||
upkeep:
|
||||
metal: 200
|
||||
crystals: 200
|
||||
fuel: 200
|
||||
credits: 200
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
ship_build_power: 0
|
||||
research_tech_build_power: 0
|
||||
military_tech_build_power: 100
|
||||
required_tech: []
|
||||
required_buildings: [Research Factory Level 1]
|
||||
|
||||
Military Factory Level 2:
|
||||
classification: production
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 3,000
|
||||
crystals: 3,000
|
||||
fuel: 3,000
|
||||
credits: 3,000
|
||||
time: 3,000
|
||||
upkeep:
|
||||
metal: 300
|
||||
crystals: 300
|
||||
fuel: 300
|
||||
credits: 300
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
ship_build_power: 0
|
||||
research_tech_build_power: 0
|
||||
military_tech_build_power: 200
|
||||
required_tech: []
|
||||
required_buildings: [Military Factory Level 1]
|
||||
|
||||
# Harvesting
|
||||
|
||||
# Each building will produce the primary resource at %60, a secondary resource
|
||||
# at %24, a terciary resource at %10 and a final resource at %6. Here is the
|
||||
# rotation of how they are related:
|
||||
|
||||
# metal -> crystal -> fuel -> credits
|
||||
|
||||
|
||||
Metal Mines 1:
|
||||
classification: mining
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 1,000
|
||||
crystals: 1,000
|
||||
fuel: 1,000
|
||||
credits: 1,000
|
||||
time: 1,000
|
||||
upkeep:
|
||||
metal: 100
|
||||
crystals: 100
|
||||
fuel: 100
|
||||
credits: 100
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
metal: 600
|
||||
crystals: 240
|
||||
fuel: 100
|
||||
credits: 60
|
||||
required_tech: []
|
||||
required_buildings: []
|
||||
|
||||
Crystal Collector 1:
|
||||
classification: mining
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 1,000
|
||||
crystals: 1,000
|
||||
fuel: 1,000
|
||||
credits: 1,000
|
||||
time: 1,000
|
||||
upkeep:
|
||||
metal: 100
|
||||
crystals: 100
|
||||
fuel: 100
|
||||
credits: 100
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
metal: 60
|
||||
crystals: 600
|
||||
fuel: 240
|
||||
credits: 100
|
||||
required_tech: []
|
||||
required_buildings: []
|
||||
|
||||
Fuel Refinery 1:
|
||||
classification: mining
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 1,000
|
||||
crystals: 1,000
|
||||
fuel: 1,000
|
||||
credits: 1,000
|
||||
time: 1,000
|
||||
upkeep:
|
||||
metal: 100
|
||||
crystals: 100
|
||||
fuel: 100
|
||||
credits: 100
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
metal: 100
|
||||
crystals: 60
|
||||
fuel: 600
|
||||
credits: 240
|
||||
required_tech: []
|
||||
required_buildings: []
|
||||
|
||||
Credit Center 1:
|
||||
classification: mining
|
||||
faction: good
|
||||
build_cost: # This is what is required to produce the building:
|
||||
metal: 1,000
|
||||
crystals: 1,000
|
||||
fuel: 1,000
|
||||
credits: 1,000
|
||||
time: 1,000
|
||||
upkeep:
|
||||
metal: 100
|
||||
crystals: 100
|
||||
fuel: 100
|
||||
credits: 100
|
||||
base_stats:
|
||||
hp: 1,000
|
||||
shield: 1,000
|
||||
armor: 30
|
||||
def: 30
|
||||
produces:
|
||||
metal: 240
|
||||
crystals: 100
|
||||
fuel: 60
|
||||
credits: 600
|
||||
required_tech: []
|
||||
required_buildings: []
|
||||
+9
-6
@@ -104,6 +104,7 @@ module Game
|
||||
# @param client [TCPSocket] The client socket
|
||||
# @param command [Hash] JSON parsed command from the client.
|
||||
# @option command [String] :player_id Required, The player ID
|
||||
# @option command [String] :faction Required, The faction the player should join as.
|
||||
#
|
||||
# @return [Hash] status hash that will be converted to JSON before being
|
||||
# sent back to the client
|
||||
@@ -231,10 +232,9 @@ module Game
|
||||
# @param msg [String] The error message
|
||||
# @return [String] JSON-encoded error response
|
||||
#
|
||||
def error_response(msg)
|
||||
{ 'type' => 'error', 'payload' => msg }.to_json
|
||||
def error_response(request_id, msg)
|
||||
{ 'type' => 'error', 'request_id', => id, 'payload' => msg }.to_json
|
||||
end
|
||||
alias error_response error_response
|
||||
|
||||
##
|
||||
# Return a JSON object that tells a client about the current
|
||||
@@ -274,6 +274,9 @@ module Game
|
||||
delta_time * (player_count / GAME_SPEED_FACTOR)
|
||||
@game_time += delta_time
|
||||
|
||||
# Order here is important, We must update the buildings first (To
|
||||
# accrew resources for this delta) Because planets hold the buildings
|
||||
# they must be first.
|
||||
@planets.each do |planet|
|
||||
planet.update(delta_time)
|
||||
end
|
||||
@@ -282,9 +285,9 @@ module Game
|
||||
ship.update(delta_time)
|
||||
end
|
||||
|
||||
#@players.each do |player|
|
||||
# player.update(delta_time)
|
||||
#end
|
||||
@players.each do |player|
|
||||
player.update(delta_time)
|
||||
end
|
||||
|
||||
# Perform game saves (Or not for now...)
|
||||
if (@game_time) > next_save
|
||||
|
||||
@@ -42,7 +42,6 @@ class Planet
|
||||
|
||||
# Function to return info to the client about a planet
|
||||
def info_update
|
||||
raise NotImplementedError, "Need to write tests for this function"
|
||||
return {
|
||||
'id' => @planet_id,
|
||||
'position' => @position
|
||||
|
||||
+2
-1
@@ -5,7 +5,7 @@ require 'securerandom'
|
||||
|
||||
# Load the ships into a constant:
|
||||
require 'yaml'
|
||||
SHIP_DATA = File.open('lib/game/ships.yaml', 'r') do |fi|
|
||||
SHIP_DATA = File.open('lib/game/data/ships.yaml', 'r') do |fi|
|
||||
YAML.safe_load(fi.read())
|
||||
end
|
||||
|
||||
@@ -50,6 +50,7 @@ class Ship
|
||||
raise ShipError, "Player needs the building of '#{building_needed}' to build this ship"
|
||||
end
|
||||
end
|
||||
return ship
|
||||
end
|
||||
|
||||
# Teleport the ship to a given position. Really only should be used for admin
|
||||
|
||||
Reference in New Issue
Block a user