· 7 years ago · Dec 09, 2018, 07:18 PM
1-- Set toggles
2megas = false
3gyms = false
4autodeal = false
5autofill = false
6loaded = false
7
8-- Additional GUIDs to know
9megaGUID = "6e6e6e"
10gymGUID = "6fa1c7"
11
12-- Define table of decks/positions to use
13decks = {
14pink = {GUID = "7e7be0",
15 snaps = {{-19.46, 2.35, -6.06},
16 {-21.74, 2.35, -3.11},
17 {-20.82, 2.35, 3.02},
18 {-15.46, 2.35, 6.69},
19 {-22.51, 2.35, 6.69},
20 {-22.78, 2.35, 10.11},
21 {-19.09, 2.35, 12.53},
22 {-16.92, 2.35, 16.13},
23 {-10.91, 2.35, 16.16},
24 {-10.85, 2.35, 9.67}}
25 },
26green = {GUID = "fbebd0",
27 snaps = {{-6.24, 2.35, 9.93},
28 {-3.49, 2.35, 16.27},
29 {0.08, 2.35, 15.99},
30 {0.25, 2.35, 9.38},
31 {-0.10, 2.35, 4.10},
32 {6.98, 2.35, 15.94},
33 {13.05, 2.35, 15.24},
34 {15.31, 2.35, 9.82},
35 {21.95, 2.35, 11.13},
36 {11.42, 2.35, 8.30}}
37 },
38blue = {GUID = "0d878e",
39 snaps = {{6.98, 2.35, 2.24},
40 {13.97, 2.35, 3.18},
41 {19.98, 2.35, 3.14},
42 {20.51, 2.35, -1.94},
43 {11.53, 2.35, -4.84},
44 {19.28, 2.35, -5.60},
45 {15.61, 2.35, -7.64},
46 {13.79, 2.35, -11.32},
47 {7.08, 2.35, -8.44},
48 {6.71, 2.35, -13.61}}
49 },
50red = {GUID = "88b8ab",
51 snaps = {{2.50, 2.35, -15.63},
52 {-3.26, 2.35, -13.92},
53 {-6.11, 2.35, -12.00},
54 {-12.35, 2.35, -5.58},
55 {-12.43, 2.35, -0.80},
56 {-12.34, 2.35, 2.72},
57 {-11.78, 2.35, 6.33}}
58 },
59yellow = {GUID = "0dfadf",
60 snaps = {{18.07, 2.35, -10.96},
61 {22.71, 2.35, -10.95},
62 {22.64, 2.35, -14.55},
63 {18.12, 2.35, -14.63}}
64 },
65event = {GUID = "2bc354",snaps = {}},
66item = {GUID = "9d26ba",snaps = {}},
67rival = {GUID = "614dd7",snaps = {}},
68starter = {GUID = "0bcd7a",snaps = {}}
69}
70
71-- Define array of token GUIDs
72tokens = {
73 Red = "779503",
74 Orange = "259d2d",
75 Yellow = "228b14",
76 Green = "f8060c",
77 Blue = "2e1b44",
78 Purple = "da7b9c",
79 Pink = "c3f1e1",
80 White = "3329af"}
81
82function onLoad(saveData)
83 local data = {}
84 if saveData ~= "" then
85 data = JSON.decode(saveData)
86 end
87 if data['loaded'] then
88 megas = data['megas']
89 gyms = data['gyms']
90 autodeal = data['autodeal']
91 autofill = data['autofill']
92 setPlayButtons()
93 else
94 -- Make buttons
95 self.createButton({position={-5.8,0.6,7.6},
96 click_function="setupBoard",
97 function_owner=self,
98 label="Start Game!",
99 width=1200,
100 height=400,
101 font_size=200
102 })
103 self.createButton({position={-5.8,0.6,6.7},
104 click_function="toggleMega",
105 function_owner=self,
106 label="Not using Megas",
107 width=900,
108 height=300,
109 font_size=110,
110 color = {r=1, g=0.75, b=0.75}
111 })
112 self.createButton({position={-5.8,0.6,6.1},
113 click_function="toggleGyms",
114 function_owner=self,
115 label="Not using Gyms",
116 width=900,
117 height=300,
118 font_size=110,
119 color = {r=1, g=0.75, b=0.75}
120 })
121 self.createButton({position={-5.8,0.6,5.5},
122 click_function="toggleAutodeal",
123 function_owner=self,
124 label="Do not autodeal",
125 width=900,
126 height=300,
127 font_size=110,
128 color = {r=1, g=0.75, b=0.75}
129 })
130 end
131end
132
133function onSave()
134 return JSON.encode({loaded=loaded,
135 megas = megas,
136 gyms = gyms,
137 autodeal = autodeal,
138 autofill = autofill})
139end
140
141function setupBoard(obj,color,altClick)
142 if megas then setupMegas() end
143 if gyms then setupGyms() end
144
145 -- Shuffle pokemon and deal tokens to the appropriate snap points
146 for name,info in pairs(decks) do
147 deck = getObjectFromGUID(info['GUID'])
148 deck.randomize()
149 for _,pos in pairs(info['snaps']) do
150 deck.takeObject({position = pos, rotation = {0, 180, 0}})
151 end
152 end
153
154 --[[ THEORETICALLY this should work, but for some reason
155 when it deals the tokens to the players, they get
156 stuck in the player's hands and the only way to get
157 them out is to right-click -> toggle -> hands. It's
158 only when it's dealt this way...
159 This behavior exists on the expansion rule cards too,
160 but VERY inconsistently...
161 whyyyy.... Oh well, we have a hacky workaround now]]
162 -- Deal starters to players
163 if autodeal then
164 getObjectFromGUID(decks['starter']['GUID']).deal(1)
165 end
166
167 -- Commence hacky workaround
168 Wait.time(finalize, 3.5)
169
170 -- Remove setup buttons so they don't accidentally get pressed later
171 for i,t in pairs(self.getButtons()) do
172 self.removeButton(i-1)
173 end
174
175 setPlayButtons()
176 autofill = true
177end
178
179function setPlayButtons()
180 -- Create Unknown Dungeon shuffle button
181 self.createButton({
182 position = {6.57,0.6,6.15},
183 click_function = "reshuffle",
184 function_owner = getObjectFromGUID(decks['yellow']['GUID']),
185 label="Reshuffle",
186 width=600,
187 height=300,
188 font_size=110
189 })
190
191 -- Create button for turning autofill on/off
192 self.createButton({position={2.2,0.6,7.6},
193 click_function="toggleAutofill",
194 function_owner=self,
195 label="Auto-replace Pokemon\nOn",
196 width=950,
197 height=400,
198 font_size=95
199 })
200 -- Initialize the button to the correct state (if loading from save)
201 autofill = not autofill
202 toggleAutofill()
203end
204
205function finalize()
206 -- Move seated player tokens to Pallet Town
207 for i,player in ipairs(Player.getPlayers()) do
208 hasToken = function() for color,_ in pairs(tokens) do if player.color == color then return true end end return false end
209 if player.seated and hasToken() then
210 --Math time!
211 x = -16.4 + (i-1)%3*1.4
212 z = -8-math.floor((i-1)/3)*1.25
213 tkn = getObjectFromGUID(tokens[player.color])
214 tkn.setPosition({x, 2.27, z})
215 tkn.setRotation({0, 0, 0})
216 -- This is where the hacky workaround begins
217 if autodeal or gyms or megas then
218 for _,item in pairs(player.getHandObjects()) do
219 item.use_hands = false
220 if item.name == "Custom_Model" then
221 r = item.getRotation()
222 item.setRotation({r['x'],r['y'],180})
223 end
224 end
225 end
226 end
227 end
228 loaded = true
229end
230
231function setupMegas()
232 -- Get the Mega box
233 box = getObjectFromGUID(megaGUID)
234 -- Pull out the info deck and deal a card to all players, delete extras
235 box.takeObject({position={0, 2, 0},
236 callback_function = function(obj) dealToAll(obj) end})
237 -- Make a stand for the Mega ball, spawn the ball on it
238 stand = spawnObject({
239 type="BlockSquare",
240 scale={2, 1, 2},
241 position={-15.48, 2.79, -15.66}})
242 stand.setColorTint({r=0.15,g=0.15,b=0.15})
243 stand.setLock(true)
244 stand.setSnapPoints( {{position={0, 0.5, 0}}} )
245 box.takeObject({position={-15.48, 2.79, -15.66},rotation={0, 150, 0}})
246 -- Add Mega Evolution event cards to Event Deck
247 getObjectFromGUID(decks['event']['GUID']).putObject(box.takeObject())
248 -- Add Mega item cards to Item Deck
249 getObjectFromGUID(decks['item']['GUID']).putObject(box.takeObject())
250end
251
252function setupGyms()
253 -- Get the Gym box
254 box = getObjectFromGUID(gymGUID)
255 -- Pull out the info deck and deal a card to all players, delete extras
256 box.takeObject({position={0, 2, 0},
257 callback_function = function(obj) dealToAll(obj) end})
258 -- Get Gym Leader deck and disperse gym leaders around Kanto
259 GLD = box.takeObject({position={0, 2, 0}})
260 GLD.takeObject({position={-13.80, 2.36, 9.06},rotation={0, 90, 0}}) -- Brock
261 GLD.takeObject({position={3.66, 2.36, 15.28},rotation={0, 180, 0}}) -- Misty 1
262 GLD.takeObject({position={3.66, 3, 15.28},rotation={0, 180, 0}}) -- Misty 2
263 GLD.takeObject({position={1.27, 2.36, 0.75},rotation={0, 180, 0}}) -- Lt. Surge
264 GLD.takeObject({position={0.96, 2.36, -8.25},rotation={0, 180, 0}}) -- Koga
265 GLD.takeObject({position={7.07, 2.36, 8.60},rotation={0, 180, 0}}) -- Sabrina
266 GLD.takeObject({position={-15.20, 2.36, -3.47},rotation={0, 180, 0}}) -- Giovani
267 GLD.takeObject({position={0.96, 3, -8.25},rotation={0, 180, 0}}) -- Janine
268 GLD.takeObject({position={-7.36, 2.36, 3.58},rotation={0, 180, 0}}) -- Erika
269 GLD.takeObject({position={-15.20, 3, -3.47},rotation={0, 180, 0}}) -- Blue
270 GLD.takeObject({position={-13.98, 2.36, -12.20},rotation={0, 180, 0}}) -- Blaine
271 broadcastToAll("Choose one of the two gym leaders in each of Cerulean City, Fuchsia City, and Viridian City.")
272 -- Pull out bag of badges and disperse them accordingly
273 bob = box.takeObject({position={0, 2, 0}}) --Bag of badges, bob!
274 badgePos = {{-8.79, 2.28, -11.19},
275 {1.80, 2.28, -10.31},
276 {5.99, 2.28, 6.61},
277 {-3.64, 2.28, 6.84},
278 {6.12, 2.28, -0.07},
279 {5.91, 2.28, 13.58},
280 {-13.02, 2.28, 11.59},
281 {-14.85, 2.28, 0.10}}
282 for _,position in ipairs(badgePos) do
283 badge = bob.takeObject({position=position})
284 badge.setScale({0.35,0.35,0.35})
285 badge.setLock(true)
286 end
287 destroyObject(bob)
288end
289
290function dealToAll(obj)
291 obj.deal(1)
292 destroyObject(obj)
293end
294
295function toggleMega()
296 if megas then
297 self.editButton({index=1,
298 color = {r=1, g=0.75, b=0.75},
299 label = "Not using Megas"})
300 else
301 self.editButton({index=1,
302 color = {r=0.75, g=1, b=0.75},
303 label = "Using Megas"})
304 end
305 megas = not megas
306end
307
308function toggleGyms()
309 if gyms then
310 self.editButton({index=2,
311 color = {r=1, g=0.75, b=0.75},
312 label = "Not using Gyms"})
313 else
314 self.editButton({index=2,
315 color = {r=0.75, g=1, b=0.75},
316 label = "Using Gyms"})
317 end
318 gyms = not gyms
319end
320
321function toggleAutodeal()
322 if autodeal then
323 self.editButton({index=3,
324 color = {r=1, g=0.75, b=0.75},
325 label = "Do not autodeal"})
326 else
327 self.editButton({index=3,
328 color = {r=0.75, g=1, b=0.75},
329 label = "Autodeal starters"})
330 end
331 autodeal = not autodeal
332end
333
334function toggleAutofill()
335 if autofill then
336 self.editButton({index=1,
337 color = {r=0.55, g=0.55, b=0.55},
338 label = "Auto-replace Pokemon\nOff"})
339 else
340 self.editButton({index=1,
341 color = {r=1, g=1, b=1},
342 label = "Auto-replace Pokemon\nOn"})
343 end
344 autofill = not autofill
345end
346
347function onObjectPickUp(color,obj)
348 if autofill then
349 -- Only care if it's a generic custom model (tokens)
350 if obj.name == "Custom_Model" and obj.tag == "Generic" then
351 pos = obj.getPosition()
352 -- Check that its X and Z coord's are within the bounds of the table
353 if math.abs(pos[1]) < 29 and math.abs(pos[3]) < 18 then
354 -- Check its position once it's put down to determine if another token should be put out
355 Wait.condition(function() checkMove(obj,pos) end, function() if obj.held_by_color ~= color then return true else return false end end)
356 end
357 end
358 end
359end
360
361function checkMove(obj,pos)
362 newPos = obj.getPosition()
363 -- Check if it travelled more than 3 in either X or Z directions
364 if (newPos[1] < pos[1]-3 or newPos[1] > pos[1]+3) or
365 (newPos[3] < pos[3]-3 or newPos[3] > pos[3]+3) then
366 -- Try to find a snap point near original position
367 found = false
368 for name,deck in pairs(decks) do
369 if not found then
370 for _,snap in ipairs(deck['snaps']) do
371 --[[If the snap is within 1 of the original position of the piece
372 picked up, we'll consider it good]]
373 if snap[1]-1 < pos[1] and pos[1] < snap[1]+1 and
374 snap[3]-1 < pos[3] and pos[3] < snap[3]+1 then
375 found = true
376 -- Cast a ray to see if anything is at that snap point
377 hitTable = Physics.cast({
378 origin = {snap[1],2,snap[3]},
379 direction = {0,1,0},
380 max_distance = 6
381 })
382 piece = false
383 for _,hit in pairs(hitTable) do
384 -- Ignore the game board
385 if hit['hit_object'].guid != self.guid then
386 piece = true
387 end
388 end
389 if not piece then
390 -- Deal a new one!
391 d = getObjectFromGUID(deck['GUID'])
392 -- Make sure there's things to deal before trying to deal
393 local c = 0
394 for _,__ in pairs(d.getObjects()) do c = c+1 end
395 if c > 0 then
396 d.takeObject({position = snap, rotation = {0, 180, 0}})
397 end
398 end
399 end
400 end
401 end
402 end
403 end
404end