forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
growcrops.rb
109 lines (90 loc) · 3.35 KB
/
growcrops.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Instantly grow crops in farm plots
=begin
growcrops
=========
Instantly grow seeds inside farming plots.
With no argument, this command list the various seed types
currently in use in your farming plots. With a seed type,
the script will grow specified seeds, ready to be harvested.
Arguments:
- ``list`` or ``help``
List the various seed types currently in use in your farming plots
- ``<crop name>``
Grow specific planted seed type
- ``all``
Grow all planted seeds
Example:
- Grow plump helmet spawn:
``growcrops plump``
=end
# cache information from the raws
def cacheCropRaws()
if @raws_crop_name.empty?
# store ID and grow duration for each crop
df.world.raws.plants.all.each_with_index { |p, idx|
@raws_crop_name[idx] = p.id # Crop ID
@raws_crop_growdur[idx] = p.growdur # Grow Duration
}
end
end
# create a list of available crops to grow, from seeds
def buildSeedList()
df.world.items.other[:SEEDS].each { |seed|
next if not seed.flags.in_building # skip if seed in building
next if not seed.general_refs.find { |ref| ref._rtti_classname == :general_ref_building_holderst } # skip if seed in depot
next if seed.grow_counter >= @raws_crop_growdur[seed.mat_index]
# add to list of potential crops to grow
@inventory[seed.mat_index] += 1
}
end
# Display a list of planted crops
def listPlantedCrops()
@inventory.sort_by { |mat, seedCount| seedCount }.each { |mat, seedCount|
cropName = df.world.raws.plants.all[mat].id
puts " #{cropName} #{seedCount}"
}
end
# grow specific crop
def growCrop(material)
# find the matching crop
mat = df.match_rawname(material, @inventory.keys.map { |k| @raws_crop_name[k] })
unless wantmat = @raws_crop_name.index(mat)
raise "invalid plant material #{material}" # no crop with that name
end
# grow each seed for specified crop
count = 0
df.world.items.other[:SEEDS].each { |seed|
next if seed.mat_index != wantmat # skip if not desired seed
next if not seed.flags.in_building # skip if seed is in building
next if not seed.general_refs.find { |ref| ref._rtti_classname == :general_ref_building_holderst } # skip if seed in depot
next if seed.grow_counter >= @raws_crop_growdur[seed.mat_index] # skip if already grew desired amount
seed.grow_counter = @raws_crop_growdur[seed.mat_index]
count += 1
}
puts "Grown #{count} #{mat}"
end
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
material = $script_args[0] # Grow which crop
@inventory = Hash.new(0) # a list of available crops to grow, from seeds
@raws_crop_name ||= {}
@raws_crop_growdur ||= {}
# cache information from the raws
cacheCropRaws()
# create a list of available crops to grow, from seeds
buildSeedList()
if !material or material == 'help' or material == 'list'
# show a list of available crop types
listPlantedCrops()
else
if material == 'all'
# loop through all planted crops
@inventory.sort_by { |mat, seedCount| seedCount }.each { |mat, seedCount|
# grow that plant
growCrop(df.world.raws.plants.all[mat].id)
}
else
# grow a single crop type
growCrop(material)
end
end