Class: Manufactured::Station

Inherits:
Object
  • Object
show all
Includes:
Entity::HasCargo, Entity::InSystem
Defined in:
lib/manufactured/station.rb

Overview

A player owned entity residing in a Cosmos::Entities::SolarSystem. They can move inbetween systems on their own without requiring a Cosmos::Entities::JumpGate. May construct other manufactured entities depending on the station type.

Constant Summary collapse

TYPES =

General station classification, used to determine a station's capabilities

[:defense, :offense, :mining, :exploration, :science,
:technology, :manufacturing, :commerce]
SIZES =

Mapping of station types to default sizes

{:defense => 35, :offense => 35, :mining => 27,
:exploration => 20, :science => 20,
:technology => 20, :manufacturing => 40,
:commerce => 30}

Instance Attribute Summary collapse

Attributes included from Entity::HasCargo

#cargo_capacity, #resources, #transfer_distance

Attributes included from Entity::InSystem

#location, #solar_system, #system_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Entity::HasCargo

#add_resource, #can_accept?, #can_transfer?, #cargo_empty?, #cargo_full?, #cargo_quantity, #cargo_space, #remove_resource, #resources_valid?

Methods included from Entity::InSystem

#movement_strategy, #movement_strategy=

Constructor Details

#initialize(args = {}) ⇒ Station

Station initializer

Parameters:

  • args (Hash) (defaults to: {})

    hash of options to initialize attack command with

Options Hash (args):

  • :id,'id' (String)

    id to assign to the station

  • :user_id,'user_id' (String)

    id of user that owns the station

  • :type,'type' (TYPE)

    type to assign to station, if not set a random type will be assigned

  • Hash] (Hash<Symbol,Array<String>] :errors, 'errors' operation errors to set on station)

    :errors,'errors' operation errors to set on station

  • :resources,'resources' (Hash<String,Int>)

    hash of resource ids to quantities contained in the station

  • :solar_system,'solar_system' (Cosmos::Entities::SolarSystem)

    solar system which the station is in

  • :location,'location' (Motel::Location)

    location of the station in the solar system


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/manufactured/station.rb', line 110

def initialize(args = {})
  args[:location] =
    Motel::Location.new :coordinates => [0,0,1],
                        :orientation => [1,0,0]  unless args.has_key?(:location) ||
                                                        args.has_key?('location')


  attr_from_args args, :id                   => nil,
                       :user_id              => nil,
                       :type                 => nil,
                       :callbacks            =>  [],
                       :resources            =>  [],
                       :location             => nil,
                       :system_id            => nil,
                       :solar_system         => nil,
                       :docking_distance     => 200,
                       :transfer_distance    => 200,
                       :construction_distance=> 300,
                       :cargo_capacity       => 10000

  @location.orientation = [0,0,1] if @location.orientation == [nil, nil, nil]

end

Instance Attribute Details

#callbacksObject

Array of callbacks to invoke on certain events relating to ship


45
46
47
# File 'lib/manufactured/station.rb', line 45

def callbacks
  @callbacks
end

#construction_distanceObject

Distance away from the station which new entities are constructed


68
69
70
# File 'lib/manufactured/station.rb', line 68

def construction_distance
  @construction_distance
end

#docking_distanceObject

Max distance a ship can be from station to dock with it


62
63
64
# File 'lib/manufactured/station.rb', line 62

def docking_distance
  @docking_distance
end

#idObject

Unique string id of the station


21
22
23
# File 'lib/manufactured/station.rb', line 21

def id
  @id
end

#sizeObject

Size of the station


27
28
29
# File 'lib/manufactured/station.rb', line 27

def size
  @size
end

#typeObject

TYPE

General category / classification of station


30
31
32
# File 'lib/manufactured/station.rb', line 30

def type
  @type
end

#user_idObject

ID of user which station belongs to


24
25
26
# File 'lib/manufactured/station.rb', line 24

def user_id
  @user_id
end

Class Method Details

.construction_cost(type) ⇒ Integer

Return the cost to construct a station of the specified type

TODO right now just return a fixed cost for every station, eventually make more variable

Parameters:

  • type (TYPE)

    type of station which to return construction cost

Returns:

  • (Integer)

    quantity of resources required to construct station


87
88
89
# File 'lib/manufactured/station.rb', line 87

def self.construction_cost(type)
  100
end

.construction_time(type) ⇒ Float

Return the time (in seconds) to construct a station of the specified type

TODO right now just return a fixed time for every station, eventually make more variable

Parameters:

  • type (TYPE)

    type of station which to return construction time

Returns:

  • (Float)

    seconds which it takes to construct the station


97
98
99
# File 'lib/manufactured/station.rb', line 97

def self.construction_time(type)
  10
end

.json_create(o) ⇒ Object

Create new station from json representation


281
282
283
284
# File 'lib/manufactured/station.rb', line 281

def self.json_create(o)
  station = new(o['data'])
  return station
end

Instance Method Details

#alive?Boolean

Just for compatability for now, always return true

Returns:

  • (Boolean)

178
179
180
# File 'lib/manufactured/station.rb', line 178

def alive?
  true
end

#can_construct?(args = {}) ⇒ true, false

Return true / false indiciating if station can construct entity specified by args.

Parameters:

  • args (Hash) (defaults to: {})

    args which will be passed to #construct to construct entity

Returns:

  • (true, false)

    indicating if station can construct entity


197
198
199
200
201
202
203
204
205
# File 'lib/manufactured/station.rb', line 197

def can_construct?(args = {})
  @type == :manufacturing &&

  ['Ship', 'Station'].include?(args[:entity_type]) &&

  cargo_quantity >=
    Manufactured.const_get(args[:entity_type]).
                 construction_cost(args)
end

#construct(args = {}) ⇒ Object

Use this station to construct new manufactured entities.

Sets up the entity in the correct context, including the right location properties and verifies its validitiy before deducting resources necessary to construct and instanting new entity.

Parameters:

  • args (Hash) (defaults to: {})

    hash of options to pass to new entity being initialized

Returns:

  • new entity created, nil otherwise


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/manufactured/station.rb', line 215

def construct(args = {})
  # return if we can't construct
  return nil unless can_construct?(args)

  # grab handle to entity class & generate construction cost
  eclass = Manufactured.const_get(args[:entity_type])
  ecost  = eclass.construction_cost(args)

  # remove resources from the station
  # TODO map entities to specific construction requirements
  remaining = ecost
  @resources.each { |r|
    if r.quantity > remaining
      r.quantity -= remaining
      break
    else
      remaining -= r.quantity
      @resources.delete(r)
    end
  }

  # instantiate the new entity
  entity = eclass.new args
  entity.location.parent = self.location.parent

  # setup location
  entity.parent = self.parent

  # allow user to specify coordinates unless too far away
  # in which case, construct at closest location to specified
  # location withing construction distance
  distance = entity.location - self.location
  if distance > @construction_distance
    dx = (entity.location.x - self.location.x) / distance
    dy = (entity.location.y - self.location.y) / distance
    dz = (entity.location.z - self.location.z) / distance
    entity.location.x = self.location.x + dx * @construction_distance
    entity.location.y = self.location.y + dy * @construction_distance
    entity.location.z = self.location.z + dz * @construction_distance
  end
  # TODO introduce optional random element that can be added to entity location ?

  entity
end

#dockable?(ship) ⇒ true, false

Return true / false indicating station permits specified ship to dock

Parameters:

Returns:

  • (true, false)

    indicating if ship is allowed to dock at station


186
187
188
189
190
191
# File 'lib/manufactured/station.rb', line 186

def dockable?(ship)
  # TODO incorporate # of ports
  (ship.location.parent_id == @location.parent_id) &&
  (ship.location - @location) <= @docking_distance &&
  !ship.docked?
end

#remove_callbacks(args = {}) ⇒ Object

Remove callbacks matching the specified args


54
55
56
57
58
59
# File 'lib/manufactured/station.rb', line 54

def remove_callbacks(args={})
  @callbacks.reject! { |cb|
    (!args.has_key?(:event_type)  || cb.event_type  == args[:event_type]) &&
    (!args.has_key?(:endpoint_id) || cb.endpoint_id == args[:endpoint_id])
  }
end

#run_callbacks(type, *args) ⇒ Object

Run callbacks


48
49
50
51
# File 'lib/manufactured/station.rb', line 48

def run_callbacks(type, *args)
  @callbacks.select { |c| c.event_type == type }.
             each   { |c| c.invoke self, *args  }
end

#to_json(*a) ⇒ Object

Convert station to json representation and return it


266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/manufactured/station.rb', line 266

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       =>
      {:id => id, :user_id => user_id,
       :type => type, :size => size,
       :cargo_capacity => @cargo_capacity,
       :docking_distance => @docking_distance,
       :location => @location,
       :system_id => (@solar_system.nil? ? @system_id : @solar_system.id),
       :resources => @resources}
  }.to_json(*a)
end

#to_sObject

Convert station to human readable string and return it


261
262
263
# File 'lib/manufactured/station.rb', line 261

def to_s
  "station-#{@id}"
end

#update(station) ⇒ Object

Update this station's attributes from other station

Parameters:


137
138
139
# File 'lib/manufactured/station.rb', line 137

def update(station)
  update_from(station, :location, :resources, :solar_system)
end

#valid?Boolean

Return boolean indicating if this station is valid

Tests the various attributes of the Station, returning true if everything is consistent, else false.

Current tests

  • id is set to a valid (non-empty) string

  • location is set to a Motel::Location

  • user id is set to a string

  • type is one of valid TYPES

  • size corresponds to the correct value for type

  • solar system is set to Cosmos::Entities::SolarSystem

  • resources is a list of resources

At a minimum the following should be set on the default station to be valid:

  • id

  • user_id

  • solar_system

  • type

Returns:

  • (Boolean)

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/manufactured/station.rb', line 161

def valid?
  !@id.nil? && @id.is_a?(String) && @id != "" &&
  !@user_id.nil? && @user_id.is_a?(String) &&

  !@location.nil? && @location.is_a?(Motel::Location) &&
  !@system_id.nil? &&
  (@solar_system.nil? || @solar_system.is_a?(Cosmos::Entities::SolarSystem)) &&

  !@type.nil? && TYPES.include?(@type) &&
  !@size.nil? && @size == SIZES[@type] &&

  self.resources_valid?

  # TODO validate cargo properties when they become variable
end