summaryrefslogtreecommitdiff
path: root/animalfix.py
blob: 020ac7120f0afee2198b7e03817a017921f2c17f (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from pymclevel import mclevel
import wx
import random

def mkcoord(name, x,y,z):
	coord = mclevel.TAG_List()
	cx = mclevel.TAG_Double(name="", value=x)
	cy = mclevel.TAG_Double(name="", value=y)
	cz = mclevel.TAG_Double(name="", value=z)
	coord.insert(0, cx)
	coord.insert(1, cy)
	coord.insert(2, cz)
	coord.name = name
	return coord

def getsheepcol():
	r = random.randrange(0,10000)
	if r < 8184: # 81.84 % chance
		return 0 # White Wool
	elif r < 8684: # 5 % chance
		return 8 # Light Gray Wool
	elif r < 9184: # 5 % chance
		return 7 # Gray Wool
	elif r < 9684: # 5 % chance
		return 15 # Black Wool
	elif r < 9984: # 3 % chance
		return 12 # Brown Wool
	else: # 0.16 chance
		return 6 # Pink Wool

def mkanimal(name, x,y,z):
	if name == u"Chicken":
		animal = mclevel.Entity.Create(u"Chicken")
		animal["Health"] = mclevel.TAG_Short(name="Health", value=4)
	elif name == u"Pig":
		animal = mclevel.Entity.Create(u"Pig")
		animal["Health"] = mclevel.TAG_Short(name="Health", value=10)
		animal["Saddle"] = mclevel.TAG_Byte(name="Saddle", value=0)
	elif name == u"Cow":
		animal = mclevel.Entity.Create(u"Cow")
		animal["Health"] = mclevel.TAG_Short(name="Health", value=10)
	elif name == u"Sheep":
		animal = mclevel.Entity.Create(u"Sheep")
		animal["Health"] = mclevel.TAG_Short(name="Health", value=10)
		animal["Color"] = mclevel.TAG_Byte(name="Color", value=getsheepcol())
		animal["Sheared"] = mclevel.TAG_Byte(name="Sheared", value=0)
	elif name == u"Wolf":
		animal = mclevel.Entity.Create(u"Wolf")
		animal["Health"] = mclevel.TAG_Short(name="Health", value=8)
		animal["Owner"] = mclevel.TAG_String(name="Owner", value=u"")
		animal["Angry"] = mclevel.TAG_Byte(name="Angry", value=0)
		animal["Sitting"] = mclevel.TAG_Byte(name="Angry", value=0)
	else:
		raise ValueError("Unknown animlal type '{}'.".format(name))
	
	animal["Pos"] = mkcoord("Pos", x,y,z)
	animal["Motion"] = mkcoord("Motion", 0,0,0)
	animal["Rotation"] = mclevel.TAG_List(name="Rotation")
	animal["Rotation"].insert(0, mclevel.TAG_Float(name="", value=0))
	animal["Rotation"].insert(1, mclevel.TAG_Float(name="", value=0))
	animal["Fire"] = mclevel.TAG_Short(name="Fire", value=-1)
	animal["AttackTime"] = mclevel.TAG_Short(name="AttackTime", value=0)
	animal["HurtTime"] = mclevel.TAG_Short(name="HurtTime", value=0)
	animal["DeathTime"] = mclevel.TAG_Short(name="DeathTime", value=0)
	animal["Air"] = mclevel.TAG_Short(name="Air", value=300)
	animal["FallDistance"] = mclevel.TAG_Float(name="FallDistance", value=0)
	animal["OnGround"] = mclevel.TAG_Byte(name="OnGround", value=0)
	return animal

class AnimalFixFrame(wx.Frame):
	def __init__(self):
		wx.Frame.__init__(self, None, title="AnimalFix", size=(400, 600))
		
		self.mainpanel = wx.Panel(self, -1)
		vbox = wx.BoxSizer(wx.VERTICAL)
		
		vbox.Add(wx.StaticText(self.mainpanel,
label="""This tool will place animals into your Minecraft map, if you do not have any.
This sometimes happens to an old map after updating to >Beta 1.8.
PLEASE! Make a backup of your map before apply this tool on your map!""", style=wx.ALIGN_CENTER), 0, wx.EXPAND | wx.ALL, 5)
		
		vbox.AddStretchSpacer(1)
		
		hbox_mapin = wx.BoxSizer(wx.HORIZONTAL)
		hbox_mapin.Add(wx.StaticText(self.mainpanel, label="The directory of your old map:"), 0, wx.EXPAND | wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)
		self.map_input = wx.DirPickerCtrl(self.mainpanel)
		hbox_mapin.Add(self.map_input, 1, wx.EXPAND, 0)
		vbox.Add(hbox_mapin, 0, wx.EXPAND | wx.ALL, 5)
		
		vbox.AddStretchSpacer(1)
		
		self.process_txt   = wx.StaticText(self.mainpanel, label="")
		vbox.Add(self.process_txt, 0, wx.EXPAND | wx.ALL, 5)
		self.process_gauge = wx.Gauge(self.mainpanel, range=1000, style=wx.GA_HORIZONTAL | wx.GA_SMOOTH)
		vbox.Add(self.process_gauge, 0, wx.EXPAND, wx.ALL, 5)
		
		vbox.AddStretchSpacer(1)
		
		self.letsgo = wx.Button(self.mainpanel, label="Lets do it!")
		vbox.Add(self.letsgo, 0, wx.EXPAND | wx.ALL, 5)
		
		self.mainpanel.SetSizer(vbox)
		vbox.Fit(self)
		self.SetMinSize(vbox.GetMinSize())
		
		# Events
		self.Bind(wx.EVT_BUTTON, self.on_letsgo, id=self.letsgo.GetId())
	
	def on_letsgo(self, evt):
		try:
			world = mclevel.fromFile(self.map_input.GetPath())
		except ValueError:
			dialog = wx.MessageDialog(self, message="This is not a valid minecraft level!", caption="Could not load level", style=wx.OK | wx.ICON_ERROR)
			dialog.ShowModal()
			return True
		except IOError:
			dialog = wx.MessageDialog(self, message="Could not open directory!", caption="Could not load level", style=wx.OK | wx.ICON_ERROR)
			dialog.ShowModal()
			return True
		
		self.letsgo.Enable(False)
		
		overworld = world.getDimension(0)
		
		spawnon = [
			world.materials.Dirt.ID,
			world.materials.Grass.ID,
			world.materials.Gravel.ID,
			world.materials.Farmland.ID,
			world.materials.Snow.ID,
			world.materials.Ice.ID,
			world.materials.SnowLayer
		]
		
		treatasair = [
			world.materials.Air.ID,
			world.materials.TallGrass.ID,
			world.materials.Shrub.ID,
			world.materials.Flower.ID,
			world.materials.Rose.ID,
			world.materials.RedMushroom.ID,
			world.materials.BrownMushroom.ID,
		]
		
		animals = [u"Pig", u"Sheep", u"Chicken", u"Cow"]
		allanimals = animals + [u"Wolf"]
		
		# Count Chunks
		n_chunks = 0
		for _ in overworld.allChunks:
			n_chunks += 1
		
		i = 0
		nextanimalblock = random.randrange(1,11)
		for cx, cz in overworld.allChunks:
			i += 1
			chunk = overworld.getChunk(cx, cz)
			
			self.process_txt.SetLabel("{} / {}".format(i, n_chunks))
			self.process_gauge.SetValue(int((float(i) / float(n_chunks)) * 1000))
			self.Update()
			
			hasanimals = False
			for entity in chunk.Entities:
				if entity["id"].value in allanimals:
					hasanimals = True
					break
			if hasanimals:
				continue
			
			nextanimalblock -= 1
			if nextanimalblock > 0:
				continue
			
			nextanimalblock = random.randrange(1,11)
			
			countleaves = 0
			possible_spawnpoints = []
			for x in xrange(16):
				for y in xrange(16):
					noleaves = True
					for z in xrange(127, 0, -1):
						blkmat = chunk.Blocks[x,y,z]
						if blkmat not in treatasair:
							if (blkmat == world.materials.Leaves.ID) and noleaves:
								countleaves += 1
								noleaves = False
							else:
								if blkmat in spawnon:
									if world.materials.Leaves.ID not in chunk.Blocks[x,y,z+1:z+3]:
										# otherwise there would not be enough space...
										possible_spawnpoints.append((x,y,z+1))
								break
			
			isforest = countleaves >= (16*16*0.45) # We assume the chunk belongs to a forest, if there are 45% leaves seen from above.
			spawn_n = random.randrange(1,5)
			if spawn_n > len(possible_spawnpoints):
				spawn_n = len(possible_spawnpoints)
			if spawn_n == 0:
				continue
			else:
				random.shuffle(possible_spawnpoints)
				current_animals = allanimals if isforest else animals
				for x,z,y in possible_spawnpoints[0:spawn_n]:
					x = cx * 16 + x
					z = cz * 16 + z
					chunk.Entities.append(mkanimal(random.choice(current_animals), x,y,z))
				chunk.chunkChanged()
		
		self.process_txt.SetLabel("Saving data. This can take \"some\" time. Please be patient...")
		self.Update()
		
		world.generateLights();
		world.saveInPlace();
		
		dialog = wx.MessageDialog(self, message="Finished", caption="New animals were placed successfully.", style=wx.OK | wx.ICON_INFORMATION)
		dialog.ShowModal()
		
		self.process_gauge.SetValue(0)
		self.process_txt.SetLabel("")
		
		self.letsgo.Enable(True)
		return True

class AnimalFixApplication(wx.App):
	def OnInit(self):
		my_frame = AnimalFixFrame()
		my_frame.Show()
		self.SetTopWindow(my_frame)
		return True

if __name__ == '__main__':
	application = AnimalFixApplication()
	application.MainLoop()