vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 -- Gems, a silly gems-based minigame to kill time.
2 -- Use /gems to bring up the game.
3  
4 local Gems_GameIsOver = false;
5  
6 local Gems_Paused = false;
7 local Gems_TimerOn = false;
8 local Gems_TimerFunction;
9 local Gems_Tick;
10 local Gems_TimerEnd = 0;
11 local Gems_PulseTime = 0.5;
12 local Gems_Score = 0;
13  
14 local Gems_GemTypes = 10;
15  
16 local Gems_FallingX = -1;
17 local Gems_FallingY = 0;
18  
19 local Gems_GemTextures = {
20 "",
21 "Interface\\Icons\\INV_Misc_Gem_Topaz_01",
22 "Interface\\Icons\\INV_Misc_Gem_Diamond_01",
23 "Interface\\Icons\\INV_Misc_Gem_Opal_01",
24 "Interface\\Icons\\INV_Misc_Gem_Ruby_01",
25 "Interface\\Icons\\INV_Misc_Gem_Amethyst_01",
26 "Interface\\Icons\\INV_Misc_Gem_Bloodstone_01",
27 "Interface\\Icons\\INV_Misc_Gem_Crystal_01",
28 "Interface\\Icons\\INV_Misc_Gem_Emerald_01",
29 "Interface\\Icons\\INV_Misc_Gem_Sapphire_01",
30 "Interface\\Icons\\INV_Misc_Gem_Stone_01",
31 "Interface\\Icons\\INV_Misc_Gem_Variety_01",
32 "Interface\\Icons\\INV_Misc_Gem_01",
33 "Interface\\Icons\\INV_Misc_Gear_01",
34 "Interface\\Icons\\INV_Misc_Rune_01",
35 "Interface\\Icons\\INV_Ore_Arcanite_01",
36 "Interface\\Icons\\INV_Ore_Copper_01",
37 "Interface\\Icons\\INV_Ore_Iron_01",
38 "Interface\\Icons\\INV_Ore_Mithril_01",
39 "Interface\\Icons\\INV_Ore_Thorium_01",
40 "Interface\\Icons\\INV_Ore_Tin_01",
41 "Interface\\Icons\\INV_Ore_TrueSilver_01"
42 }
43  
44 -- some strings for the key bindings
45 BINDING_HEADER_GEMS = "Val's Gems";
46 BINDING_NAME_GEMS_LEFT = "Move Left";
47 BINDING_NAME_GEMS_RIGHT = "Move Right";
48 BINDING_NAME_GEMS_DOWN = "Move Down";
49  
50  
51 function Gems_OnLoad()
52 -- Register the slash command
53 SLASH_GEMS1 = "/gems";
54 SlashCmdList["GEMS"] = function(msg)
55 Gems_SlashCommand(msg);
56 end
57  
58 -- hide the frame when escape is pressed
59 tinsert(UISpecialFrames,"Gems_Frame");
60  
61 Gems_Write("Val's Gems loaded. Type |cffff0000/gems|r to play or |cffff0000/gems help|r for instructions.");
62 Gems_NewGame();
63 end
64  
65 function Gems_SlashCommand(msg)
66 if(msg == nil or msg == "") then
67 Gems_Toggle();
68 elseif(msg == "help") then
69 Gems_Help();
70 end
71 end
72  
73 function Gems_Help()
74 Gems_Write("Help for Val's Gems");
75 Gems_Write("=================");
76 Gems_Write("The premise of this game is easy - connect 4 of the same gem horizontally, vertically, or diagonally.");
77 Gems_Write("Use the < and > buttons to move the falling gems left or right, and the v button to drop the gem to the bottom.");
78 end
79  
80 function Gems_Toggle()
81 if(getglobal("Gems_Frame"):IsShown()) then
82 getglobal("Gems_Frame"):Hide();
83 else
84 getglobal("Gems_Frame"):Show();
85 end
86 end
87  
88 function Gems_NewGame()
89  
90 Gems_GameIsOver = false;
91  
92 Gems_TimerEnd = 0;
93 Gems_Paused = false;
94  
95 Gems_FallingX = -1;
96 Gems_FallingY = 0;
97 Gems_Score = 0;
98 Gems_UpdateScore(Gems_Score);
99  
100 getglobal("Gems_PauseButton"):UnlockHighlight();
101  
102 for i = 1,64 do
103 getglobal("Gems_Cell" .. i).gem = 0;
104 Gems_DrawCell(i);
105 end
106  
107 Gems_ScheduleTimer(Gems_PulseTime, Gems_Pulse);
108 end
109  
110 function Gems_Pulse()
111  
112 if(Gems_FallingX == -1) then
113 Gems_SpawnGem();
114 else
115 local gemcell = Gems_FallingX * 8 + Gems_FallingY;
116  
117 if(gemcell > 56) then
118 Gems_CheckMatch(Gems_FallingX, Gems_FallingY);
119 Gems_FallingX = -1;
120  
121 elseif(getglobal("Gems_Cell" .. gemcell+8).gem > 0) then
122 Gems_CheckMatch(Gems_FallingX, Gems_FallingY);
123 Gems_FallingX = -1
124  
125 else
126 --Gems_Write("gemcell is " .. gemcell);
127 local gemval = getglobal("Gems_Cell" .. gemcell).gem;
128 getglobal("Gems_Cell" .. gemcell).gem = 0;
129 getglobal("Gems_Cell" .. gemcell+8).gem = gemval;
130 Gems_DrawCell(gemcell);
131 Gems_DrawCell(gemcell+8);
132 Gems_FallingX = Gems_FallingX + 1;
133 end
134 end
135  
136 Gems_ScheduleTimer(Gems_PulseTime, Gems_Pulse);
137 end
138  
139 function Gems_DrawCell(cellnum)
140 local i = getglobal("Gems_Cell" .. cellnum).gem + 1;
141 --getglobal("Gems_Cell" .. cellnum):SetBackdropColor(Gems_GemColors[i]["r"], Gems_GemColors[i]["g"], Gems_GemColors[i]["b"]);
142 if(i == 1) then
143 getglobal("Gems_Cell" .. cellnum):Hide();
144 else
145 getglobal("Gems_Cell" .. cellnum):Show();
146 getglobal("Gems_Cell" .. cellnum):SetNormalTexture(Gems_GemTextures[i]);
147 end
148 end
149  
150 function Gems_SpawnGem()
151 local column = 4;
152 local gemval = math.random(1,Gems_GemTypes);
153  
154 Gems_FallingX = 0;
155 Gems_FallingY = column;
156  
157 --Gems_Write("spawning in " .. column);
158  
159 getglobal("Gems_Cell" .. Gems_FallingY).gem = gemval;
160 Gems_DrawCell(Gems_FallingY);
161 end
162  
163 function Gems_MoveLeft()
164 local gemcell = Gems_FallingX * 8 + Gems_FallingY;
165 if(Gems_FallingY > 1) then
166 if(Gems_FallingX == -1 or Gems_FallingX > 6 or getglobal("Gems_Cell" .. gemcell-1).gem > 0) then return end;
167 Gems_FallingY = Gems_FallingY - 1;
168 local gemval = getglobal("Gems_Cell" .. gemcell).gem;
169 getglobal("Gems_Cell" .. gemcell).gem = 0;
170 getglobal("Gems_Cell" .. gemcell-1).gem = gemval;
171 Gems_DrawCell(gemcell);
172 Gems_DrawCell(gemcell-1);
173 end
174 end
175  
176 function Gems_MoveRight()
177 local gemcell = Gems_FallingX * 8 + Gems_FallingY;
178 if(Gems_FallingY < 8) then
179 if(Gems_FallingX == -1 or Gems_FallingX > 6 or getglobal("Gems_Cell" .. gemcell+1).gem > 0) then return end;
180 Gems_FallingY = Gems_FallingY + 1;
181 local gemval = getglobal("Gems_Cell" .. gemcell).gem;
182 getglobal("Gems_Cell" .. gemcell).gem = 0;
183 getglobal("Gems_Cell" .. gemcell+1).gem = gemval;
184 Gems_DrawCell(gemcell);
185 Gems_DrawCell(gemcell+1);
186 end
187 end
188  
189 function Gems_MoveDown()
190 if(Gems_FallingX == -1) then
191 return;
192 end
193 local gemcell = Gems_FallingX * 8 + Gems_FallingY;
194 local gemval = getglobal("Gems_Cell" .. gemcell).gem;
195  
196 while(gemcell <= 56 and getglobal("Gems_Cell" .. gemcell+8).gem == 0) do
197 getglobal("Gems_Cell" .. gemcell).gem = 0;
198 getglobal("Gems_Cell" .. gemcell+8).gem = gemval;
199 Gems_DrawCell(gemcell);
200 Gems_DrawCell(gemcell+8);
201 gemcell = gemcell + 8;
202 Gems_FallingX = Gems_FallingX + 1;
203 end
204 end
205  
206 function Gems_Collapse()
207 local didcollapse = false;
208 for y = 1,8 do
209 for x = 7,1,-1 do
210 --Gems_Write("checking " .. x .. "," .. y .. " (" .. (x*8 + y) .. " - " .. getglobal("Gems_Cell" .. (x*8 + y)).gem);
211 if(getglobal("Gems_Cell" .. (x*8 + y)).gem == 0) then
212 -- Gems_Write("collapsing");
213 getglobal("Gems_Cell" .. (x*8 + y)).gem = getglobal("Gems_Cell" .. ((x-1)*8 + y)).gem;
214 getglobal("Gems_Cell" .. ((x-1)*8 + y)).gem = 0;
215 Gems_DrawCell(x*8 + y);
216 Gems_DrawCell((x-1)*8 + y);
217 didcollapse = true;
218 -- else
219 -- Gems_Write("breaking");
220 -- break;
221 end
222 end
223 end
224  
225 if(didcollapse) then
226 for y = 1,8 do
227 for x = 7,1,-1 do
228 if(getglobal("Gems_Cell" .. (x*8 + y)).gem > 0) then
229 Gems_CheckMatch(x,y);
230 end
231 end
232 end
233 end
234 end
235  
236 function Gems_CheckMatch(x, y)
237 local gemcell = x * 8 + y;
238 local gemval = getglobal("Gems_Cell" .. gemcell).gem;
239  
240 if(gemval == 0) then return end
241  
242 local htotal = Gems_ScanLeft(x, y, gemval) + Gems_ScanRight(x, y, gemval) - 1;
243 local dtotal1 = Gems_ScanUpLeft(x, y, gemval) + Gems_ScanDownRight(x, y, gemval) - 1;
244 local dtotal2 = Gems_ScanDownLeft(x, y, gemval) + Gems_ScanUpRight(x, y, gemval) - 1;
245 local vtotal = Gems_ScanUp(x, y, gemval) + Gems_ScanDown(x, y, gemval) -1;
246  
247 local scoreval = 0;
248  
249 if(htotal >= 4) then
250 if(y > 1) then
251 Gems_DelLeft(x, y-1, gemval);
252 end
253  
254 if(y < 8) then
255 Gems_DelRight(x, y+1, gemval);
256 end
257 scoreval = scoreval + (htotal - 3) * 5;
258 end
259  
260 if(vtotal >= 4) then
261 if(x > 0) then
262 Gems_DelUp(x-1, y, gemval);
263 end
264  
265 if(x < 7) then
266 Gems_DelDown(x+1, y, gemval);
267 end
268 scoreval = scoreval + (vtotal - 3) * 5;
269 end
270  
271 if(dtotal2 >= 4) then
272 if(x < 7 and y > 1) then
273 Gems_DelDownLeft(x+1, y-1, gemval);
274 end
275  
276 if(x > 0 and y < 8) then
277 Gems_DelUpRight(x-1, y+1, gemval);
278 end
279 scoreval = scoreval + (dtotal2 - 3) * 5;
280 end
281  
282 if(dtotal1 >= 4) then
283 if(x > 0 and y > 1) then
284 Gems_DelUpLeft(x-1, y-1, gemval);
285 end
286  
287 if(x < 7 and y < 8) then
288 Gems_DelDownRight(x+1, y+1, gemval);
289 end
290 scoreval = scoreval + (dtotal1 - 3) * 5;
291 end
292  
293 if(htotal >= 4 or vtotal >=4 or dtotal1 >= 4 or dtotal2 >= 4) then
294 getglobal("Gems_Cell" .. gemcell).gem = 0;
295 Gems_DrawCell(gemcell);
296 Gems_Collapse();
297 Gems_UpdateScore(Gems_Score + scoreval);
298 end
299 end
300  
301 function Gems_ScanLeft(x, y, gemval)
302 local total = 0;
303 local gemcell = x * 8 + y;
304 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
305 total = 1;
306  
307 if(y > 1) then
308 total = total + Gems_ScanLeft(x, y-1, gemval);
309 end
310 end
311 return total;
312 end
313  
314 function Gems_ScanRight(x, y, gemval)
315 local total = 0;
316 local gemcell = x * 8 + y;
317 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
318 total = 1;
319  
320 if(y < 8) then
321 total = total + Gems_ScanRight(x, y+1, gemval);
322 end
323 end
324  
325 return total;
326 end
327  
328 function Gems_ScanUp(x, y, gemval)
329 local total = 0;
330 local gemcell = x * 8 + y;
331 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
332 total = 1;
333  
334 if(x > 0) then
335 total = total + Gems_ScanUp(x-1, y, gemval);
336 end
337 end
338 return total;
339 end
340  
341 function Gems_ScanDown(x, y, gemval)
342 local total = 0;
343 local gemcell = x * 8 + y;
344 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
345 total = 1;
346  
347 if(x < 7) then
348 total = total + Gems_ScanDown(x+1, y, gemval);
349 end
350 end
351 return total;
352 end
353  
354 function Gems_ScanUpLeft(x, y, gemval)
355 local total = 0;
356 local gemcell = x * 8 + y;
357 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
358 total = 1;
359  
360 if(x > 0 and y > 1) then
361 total = total + Gems_ScanUpLeft(x-1, y-1, gemval);
362 end
363 end
364 return total;
365 end
366  
367 function Gems_ScanUpRight(x, y, gemval)
368 local total = 0;
369 local gemcell = x * 8 + y;
370 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
371 total = 1;
372  
373 if(x > 0 and y < 8) then
374 total = total + Gems_ScanUpRight(x-1, y+1, gemval);
375 end
376 end
377 return total;
378 end
379  
380 function Gems_ScanDownLeft(x, y, gemval)
381 local total = 0;
382 local gemcell = x * 8 + y;
383 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
384 total = 1;
385  
386 if(x < 7 and y > 1) then
387 total = total + Gems_ScanDownLeft(x+1, y-1, gemval);
388 end
389 end
390 return total;
391 end
392  
393 function Gems_ScanDownRight(x, y, gemval)
394 local total = 0;
395 local gemcell = x * 8 + y;
396 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
397 total = 1;
398  
399 if(x < 7 and y < 8) then
400 total = total + Gems_ScanDownRight(x+1, y+1, gemval);
401 end
402 end
403 return total;
404 end
405  
406 function Gems_DelLeft(x, y, gemval)
407 local gemcell = x * 8 + y;
408 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
409 getglobal("Gems_Cell" .. gemcell).gem = 0;
410 Gems_DrawCell(gemcell);
411  
412 if(y > 1) then
413 Gems_DelLeft(x, y-1, gemval);
414 end
415 end
416 end
417  
418 function Gems_DelRight(x, y, gemval)
419 local gemcell = x * 8 + y;
420 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
421 getglobal("Gems_Cell" .. gemcell).gem = 0;
422 Gems_DrawCell(gemcell);
423  
424 if(y < 8) then
425 Gems_DelRight(x, y+1, gemval);
426 end
427 end
428 end
429  
430 function Gems_DelUp(x, y, gemval)
431 local gemcell = x * 8 + y;
432 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
433 getglobal("Gems_Cell" .. gemcell).gem = 0;
434 Gems_DrawCell(gemcell);
435  
436 if(x > 0) then
437 Gems_DelUp(x-1, y, gemval);
438 end
439 end
440 end
441  
442 function Gems_DelDown(x, y, gemval)
443 local gemcell = x * 8 + y;
444 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
445 getglobal("Gems_Cell" .. gemcell).gem = 0;
446 Gems_DrawCell(gemcell);
447  
448 if(x < 7) then
449 Gems_DelDown(x+1, y, gemval);
450 end
451 end
452 end
453  
454 function Gems_DelUpLeft(x, y, gemval)
455 local gemcell = x * 8 + y;
456 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
457 getglobal("Gems_Cell" .. gemcell).gem = 0;
458 Gems_DrawCell(gemcell);
459  
460 if(x > 0 and y > 1) then
461 Gems_DelUpLeft(x-1, y-1, gemval);
462 end
463 end
464  
465 end
466  
467 function Gems_DelUpRight(x, y, gemval)
468 local gemcell = x * 8 + y;
469 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
470 getglobal("Gems_Cell" .. gemcell).gem = 0;
471 Gems_DrawCell(gemcell);
472  
473 if(x > 0 and y < 8) then
474 Gems_DelUpRight(x-1, y+1, gemval);
475 end
476 end
477 end
478  
479 function Gems_DelDownLeft(x, y, gemval)
480 local gemcell = x * 8 + y;
481 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
482 getglobal("Gems_Cell" .. gemcell).gem = 0;
483 Gems_DrawCell(gemcell);
484  
485 if(x < 7 and y > 1) then
486 Gems_DelDownLeft(x+1, y-1, gemval);
487 end
488 end
489 end
490  
491 function Gems_DelDownRight(x, y, gemval)
492 local gemcell = x * 8 + y;
493 if(getglobal("Gems_Cell" .. gemcell).gem == gemval) then
494 getglobal("Gems_Cell" .. gemcell).gem = 0;
495 Gems_DrawCell(gemcell);
496  
497 if(x < 7 and y < 8) then
498 Gems_DelDownRight(x+1, y+1, gemval);
499 end
500 end
501 end
502  
503 function Gems_UpdateScore(newscore)
504 Gems_Score = newscore;
505 --Gems_Write("Score is now " .. Gems_Score);
506 getglobal("Gems_ScoreString"):SetText(Gems_Score);
507 end
508  
509 function Gems_PauseToggle()
510 if(Gems_Paused == false) then
511 Gems_Paused = true;
512 Gems_TimerOn = false;
513 this:LockHighlight();
514 else
515 Gems_Paused = false;
516 Gems_TimerOn = true;
517 this:UnlockHighlight();
518 end
519 end
520  
521 function Gems_ScheduleTimer(time, func)
522 Gems_TimerFunction = func;
523 Gems_TimerEnd = GetTime() + time;
524 Gems_TimerOn = true;
525 end
526  
527 function Gems_OnUpdate()
528 if (not Gems_TimerOn) then return end
529 local Gems_Tick = GetTime();
530 if (Gems_TimerEnd - Gems_Tick < 0) then
531 Gems_TimerOn = false;
532 Gems_TimerFunction();
533 end
534 end
535  
536 function Gems_Difficulty_Initialize()
537 local info;
538 info = {
539 text = "Easy";
540 func = Gems_Difficulty_OnClick;
541 };
542 UIDropDownMenu_AddButton(info);
543  
544 info = {
545 text = "Medium";
546 func = Gems_Difficulty_OnClick;
547 };
548 UIDropDownMenu_AddButton(info);
549  
550 info = {
551 text = "Hard";
552 func = Gems_Difficulty_OnClick;
553 };
554 UIDropDownMenu_AddButton(info);
555  
556 info = {
557 text = "Crazy";
558 func = Gems_Difficulty_OnClick;
559 };
560 UIDropDownMenu_AddButton(info);
561  
562 end
563  
564 function Gems_Difficulty_OnLoad()
565 UIDropDownMenu_Initialize(Gems_Difficulty, Gems_Difficulty_Initialize);
566 UIDropDownMenu_SetSelectedID(Gems_Difficulty, 1);
567 UIDropDownMenu_SetWidth(60);
568 end
569  
570 function Gems_Difficulty_OnClick()
571 local oldID = UIDropDownMenu_GetSelectedID(Gems_Difficulty);
572 UIDropDownMenu_SetSelectedID(Gems_Difficulty, this:GetID());
573 if(oldID ~= this:GetID()) then
574 Gems_ChangeDifficulty();
575 end
576 end
577  
578 function Gems_Write(msg)
579 if (DEFAULT_CHAT_FRAME) then
580 DEFAULT_CHAT_FRAME:AddMessage(msg);
581 end
582 end
583  
584 function Gems_ChangeDifficulty()
585 local id = UIDropDownMenu_GetSelectedID(Gems_Difficulty);
586 if(id == 1) then
587 Gems_GemTypes = 10
588 elseif(id == 2) then
589 Gems_GemTypes = 13
590 elseif(id == 3) then
591 Gems_GemTypes = 15
592 elseif(id == 4) then
593 Gems_GemTypes = 20
594 end
595  
596 Gems_NewGame();
597 end