Module: Manufactured::Entity::HasCargo

Included in:
Loot, Ship, Station
Defined in:
lib/manufactured/entity.rb

Overview

Mixin indicating entity has cargo

Assumes 'id' and 'location' properties are accessible

Cargo Properties collapse

Transfer properties collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cargo_capacityObject

Max cargo capacity of entity

See Also:


78
79
80
# File 'lib/manufactured/entity.rb', line 78

def cargo_capacity
  @cargo_capacity
end

#resourcesObject

List of resources contained in the entity


72
73
74
# File 'lib/manufactured/entity.rb', line 72

def resources
  @resources
end

#transfer_distanceObject

Max distance ship may be away from a target to transfer to it


86
87
88
# File 'lib/manufactured/entity.rb', line 86

def transfer_distance
  @transfer_distance
end

Instance Method Details

#add_resource(resource) ⇒ Object

Add resource locally

Parameters:

  • resource (Resource)

    to add

Raises:

  • (RuntimeError)

    if entity cannot accept resource


114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/manufactured/entity.rb', line 114

def add_resource(resource)
  raise RuntimeError,
        "entity cannot accept resource" unless can_accept?(resource)
  res = @resources.find { |r| r.material_id == resource.material_id }
  if res.nil?
    resource.entity = self
    @resources << resource
  else
    res.quantity += resource.quantity
  end
  nil
end

#can_accept?(resource, quantity = resource.quantity) ⇒ Boolean

Return boolean indicating if entity can accpt the specified quantity of the resource specified by id

Parameters:

  • resource (Resource)

    being transferred

Returns:

  • (Boolean)

182
183
184
# File 'lib/manufactured/entity.rb', line 182

def can_accept?(resource, quantity=resource.quantity)
  self.alive? && self.cargo_quantity + quantity <= @cargo_capacity
end

#can_transfer?(to_entity, resource) ⇒ Boolean

Return boolean if entity can transfer specified quantity of resource specified by material_id to specified destination

Parameters:

  • to_entity (Manufactured::Entity)

    entity which resource is being transfered to

  • resource (Resource)

    being transfered

Returns:

  • (Boolean)

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

def can_transfer?(to_entity, resource)
  res =
    @resources.find { |r|
      r.material_id == resource.material_id &&
      r.quantity >= resource.quantity
    }

  same_entity =
    id == to_entity.id

  same_system =
    (location.parent_id == to_entity.location.parent_id)

  close_enough =
    ((location - to_entity.location) <= @transfer_distance)

  !same_entity && !res.nil? && same_system &&  close_enough
end

#cargo_empty?Boolean

Returns:

  • (Boolean)

90
91
92
# File 'lib/manufactured/entity.rb', line 90

def cargo_empty?
  self.cargo_quantity == 0
end

#cargo_full?Boolean

Returns:

  • (Boolean)

94
95
96
# File 'lib/manufactured/entity.rb', line 94

def cargo_full?
  self.cargo_quantity >= @cargo_capacity
end

#cargo_quantityInteger

Determine the current cargo quantity

Returns:

  • (Integer)

    representing the amount of resource/etc in the entity


150
151
152
# File 'lib/manufactured/entity.rb', line 150

def cargo_quantity
  @resources.inject(0) { |t,r| t+= r.quantity }
end

#cargo_spaceObject


98
99
100
# File 'lib/manufactured/entity.rb', line 98

def cargo_space
  self.cargo_capacity - self.cargo_quantity
end

#remove_resource(resource) ⇒ Object

Remove specified quantity of resource specified by material id from entity

Parameters:

  • resource (Resource)

    to remove

Raises:

  • (RuntimeError)

    if resource cannot be removed


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/manufactured/entity.rb', line 131

def remove_resource(resource)
  res = @resources.find { |r|
    r.material_id == resource.material_id &&
    r.quantity >= resource.quantity
  }
  raise RuntimeError,
        "entity does not contain specified resource" if res.nil?

  if res.quantity == resource.quantity
    @resources.delete(res)
  else
    res.quantity -= resource.quantity
  end
  nil
end

#resources_valid?Boolean

Return bool indicating if resources are valid

Returns:

  • (Boolean)

103
104
105
106
107
108
# File 'lib/manufactured/entity.rb', line 103

def resources_valid?
  @resources.is_a?(Array) &&
  @resources.select { |r|
    !r.is_a?(Cosmos::Resource)
  }.empty? # TODO verify resources are valid in context of entity
end