Browse Source

energy consumption and production working

master
arnaucube 5 years ago
parent
commit
fc44d3cbec
6 changed files with 30 additions and 22 deletions
  1. +6
    -1
      README.md
  2. +1
    -1
      constants/constants.go
  3. +1
    -1
      go.mod
  4. +3
    -6
      models/calc.go
  5. +3
    -3
      models/calc_test.go
  6. +16
    -10
      models/planet.go

+ 6
- 1
README.md

@ -26,8 +26,13 @@ The frontend is in https://github.com/arnaucube/gogame-frontend
- [x] building costs
- [x] building time
- [x] research time
- [ ] energy costs
- [x] energy costs
- [x] energy production
- [ ] production factor (based on positive/negative energy)
- [ ] ships
- [ ] ships costs
- [ ] ships building time
- [ ] ships travel time
- [ ] battle engine
- [ ] lab technologies
- [ ] how tech affects to production of material & times

+ 1
- 1
constants/constants.go

@ -5,7 +5,7 @@ package constants
const GALAXYSIZE = 50
const SOLARSYSTEMSIZE = 15
const UniverseAcceleration = 5
const UniverseAcceleration = 1
const MineVelocity = 1
// extra

+ 1
- 1
go.mod

@ -17,4 +17,4 @@ require (
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce
)
replace githoub.com/arnaucube/gogame => ./
replace github.com/arnaucube/gogame => ./

+ 3
- 6
models/calc.go

@ -39,14 +39,11 @@ func DeuteriumGrowth(ilvl int64, idelta int64) int64 {
r := (perHour / 60) * delta * constants.MineVelocity
return int64(r)
}
func SolarGrowth(ilvl int64, idelta int64) int64 {
func SolarGrowth(ilvl int64) int64 {
lvl := float64(ilvl)
delta := float64(idelta)
// 20 * L * 1.1^L
perHour := constants.UniverseAcceleration * 20 * lvl * math.Pow(1.1, lvl)
r := (perHour / 60) * delta * constants.MineVelocity
return int64(r)
production := 20 * lvl * math.Pow(1.1, lvl)
return int64(production)
}
func FusionGrowth(ilvl int64, ilvlTech int64, idelta int64) int64 {
lvl := float64(ilvl)

+ 3
- 3
models/calc_test.go

@ -24,9 +24,9 @@ func TestGrowth(t *testing.T) {
assert.Equal(t, int64(19), DeuteriumGrowth(2, 60)/constants.MineVelocity)
// solar
assert.Equal(t, int64(22), SolarGrowth(1, 60)/constants.MineVelocity)
assert.Equal(t, int64(44), SolarGrowth(1, 120)/constants.MineVelocity)
assert.Equal(t, int64(48), SolarGrowth(2, 60)/constants.MineVelocity)
assert.Equal(t, int64(22), SolarGrowth(1))
assert.Equal(t, int64(48), SolarGrowth(2))
assert.Equal(t, int64(79), SolarGrowth(3))
// fusion
assert.Equal(t, int64(31), FusionGrowth(1, 1, 60)/constants.MineVelocity)

+ 16
- 10
models/planet.go

@ -2,7 +2,6 @@ package models
import (
"errors"
"fmt"
"strconv"
"time"
@ -116,17 +115,24 @@ func (p *Planet) GetResources() (*Resources, error) {
// get Resource-Plant level in each planet
// and calculate growth = ResourcePlant.Level for each planet
var metalGrowth, crystalGrowth, deuteriumGrowth, energyGrowth int64
var metalGrowth, crystalGrowth, deuteriumGrowth int64
metalGrowth = metalGrowth + MetalGrowth(p.Buildings["metalmine"], int64(delta))
crystalGrowth = crystalGrowth + MetalGrowth(p.Buildings["crystalmine"], int64(delta))
deuteriumGrowth = deuteriumGrowth + MetalGrowth(p.Buildings["deuteriummine"], int64(delta))
energyGrowth = energyGrowth + MetalGrowth(p.Buildings["energymine"], int64(delta))
crystalGrowth = crystalGrowth + CrystalGrowth(p.Buildings["crystalmine"], int64(delta))
deuteriumGrowth = deuteriumGrowth + DeuteriumGrowth(p.Buildings["deuteriummine"], int64(delta))
// get energy generated and used
energyGenerated := int64(500)
energyGenerated = energyGenerated + SolarGrowth(p.Buildings["energymine"])
var energyUsed int64
energyUsed = energyUsed + MetalMineEnergyConsumption(p.Buildings["metalmine"])
energyUsed = energyUsed + CrystalMineEnergyConsumption(p.Buildings["crystalmine"])
energyUsed = energyUsed + DeuteriumMineEnergyConsumption(p.Buildings["deuteriummine"])
// calculate newAmount = oldAmount + growth
p.Resources.Metal = p.Resources.Metal + metalGrowth
p.Resources.Crystal = p.Resources.Crystal + crystalGrowth
p.Resources.Deuterium = p.Resources.Deuterium + deuteriumGrowth
p.Resources.Energy = p.Resources.Energy + energyGrowth
p.Resources.Energy = energyGenerated - energyUsed
// store new amount to user db
err = p.StoreInDb()
@ -152,9 +158,9 @@ func (p *Planet) SpendResources(r Resources) error {
if p.Resources.Deuterium < r.Deuterium {
return errors.New("not enough deuterium resources")
}
if p.Resources.Energy < r.Energy {
return errors.New("not enough energy resources")
}
// if p.Resources.Energy < r.Energy {
// return errors.New("not enough energy resources")
// }
p.Resources.Metal = p.Resources.Metal - r.Metal
p.Resources.Crystal = p.Resources.Crystal - r.Crystal
@ -271,6 +277,7 @@ func (p *Planet) UpgradeBuilding(building string) error {
if err != nil {
return err
}
resourcesNeeded.Energy = 0
// get time cost of the build
timei64 := ConstructionTime(resourcesNeeded, p.Buildings["roboticsfactory"])
endsTime := time.Now().Add(time.Second * time.Duration(timei64))
@ -298,6 +305,5 @@ func ResourcesToPoints(r Resources) int64 {
p = p + r.Crystal
p = p + r.Deuterium
p = p + r.Energy
fmt.Println("p", p)
return p
}

Loading…
Cancel
Save