starting with TGB and starting on FreeCell
by J Sears · 11/16/2006 (7:25 pm) · 3 comments
Well everyone else had a blog so I decided to make one too. After doing 4 of the 5 tutorials it mentions right off in the doucment that comes with it and finding them pretty simple I decided to make a FreeCell game, it's a little more work then the others and requires mouse actions not covered by those tutorials.
To my disappointment the tutorials I all found for the mouse on dragging objects were slightly out of date some of those things looking back were very minor but not knowing the functions well can be very frustrating for a new person. So I hunted the forums and resources and combined several sources to make it work. Started off with just making a single mouse click make an object dismount it's spot and mount across the screen, then added mouse drag and then worked on the rules. It looks like I have all the game rules figured out and working. Not that any of this code is fantastic but sharing for 2 reasons. 1 other new people may find it usefull. 2 it seems kind of clunky to me when I look at it so maybe someone will notice some things that optimize it that they want to share.
Now I'm on to setting up an easy way to write in the millions of different freecell levels that exist. I was trying to work with arrays for a while but I had read they couldn't be based into a function in script and I don't want to break into the code yet. So after racking my brain for hours on a solution and finding no easy one I started to think if it wouldn't be just as fast to do it in level editor. If I saved one level with the cards all off the screen all their link points set up and all their values entered then I could quickly mount down the columns and the only values that would need to change is all of the bottom row be mountable and selectable, the rest of the values will get changed from the mouse script. Now the problem I see there is I don't see any way to set a precise link point location so every card will mount the same. Could take a while to get it exact enough you couldn't notice if I just did trial and error by eye.
Then the fun stuff a move counter (should take like 2 minutes) a timer from when the level loads and a gui to display those and a choose level/new game gui
That should keep me busy for a couple days (will enter game levels over time)
Future steps depending on how it goes is to see if I can figure out how to make an AI to play the levels that should be very very tough for my knowledge.
thanks for reading
To my disappointment the tutorials I all found for the mouse on dragging objects were slightly out of date some of those things looking back were very minor but not knowing the functions well can be very frustrating for a new person. So I hunted the forums and resources and combined several sources to make it work. Started off with just making a single mouse click make an object dismount it's spot and mount across the screen, then added mouse drag and then worked on the rules. It looks like I have all the game rules figured out and working. Not that any of this code is fantastic but sharing for 2 reasons. 1 other new people may find it usefull. 2 it seems kind of clunky to me when I look at it so maybe someone will notice some things that optimize it that they want to share.
function sceneWindow2D::onMouseDown( %this, %mod, %worldPos, %mouseClicks )
{
//check and see if an object is already selected
if($haveCard == true)
{
//run our check for mountability
cardMountCheck(%worldPos);
}
else
{
//run our check for selectability
cardSelectCheck(%worldPos);
}
}
function sceneWindow2D::onMouseUp(%this, %mod, %worldPos, %mouseClicks)
{
if($cardDragged)
{
//run check for mountability
cardMountCheck(%worldPos);
}
$cardDragged = 0;
}
function t2dSceneWindow::objectFollowCheck(%this)
{
//make the card dismount, set it to level 0 to be above everything else then make it follow mouse
$selectedObj.dismount();
$selectedObj.Level = 0;
%mousePos = %this.mousePos;
$selectedObj.setPosition(%mousePos);
}
function sceneWindow2D::onMouseDragged( %this, %mod, %worldPos, %mouseClicks )
{
//lets store the mouses position and set $cardDragged to true for mouse up to work the way we want
%this.mousePos = %worldPos;
%this.objectFollowCheck();
$cardDragged = true;
}
function cardMountCheck(%worldPos)
{
//lets get a list of all the objects at the clicked point in the t2dScene
%objList = t2dScene.pickPoint(%worldPos);
//lets get a count of how many objects in the list
%objCount = getWordCount(%objList);
//we will start looping through the list
for(%i=0;%i<%objCount;%i++)
{
//grabing the entry corresponding to the loop
%obj = getWord(%objList, %i);
//if we find an object in the list that "isMountable = true"
if(%obj.isMountable)
{
//we toggle a value so we know we found an object that "isMountable"
%mountable = true;
//we kick out of the loop
%i = %objCount;
}
}
//if we found an "isMountable" object
if(%mountable == 1 && $selectedObj.cardColor != %obj.cardColor && %obj.cardValue == $selectedObj.cardValue +1)
{
//we then mount the selectedObj to the mountable one set selectedObj to mountable since it's the top card
//and make the card underneath it no longer mountable or selectable since it's not the top card, adjust new card's layer
$selectedObj.mount(%obj,0,0.5,0,false,false,false,false);
$selectedObj.isMountable = 1;
%obj.isMountable=0;
%obj.isSelectable = 0;
$selectedObj.Layer = %obj.Layer -1;
}
else if(%mountable == 1 && %obj.isStackable == 1 && %obj.cardValue == $selectedObj.cardValue -1 && %obj.cardSuit == $selectedObj.cardSuit)
{
//very similar as above except since this is the stack pile we have to account for stackable as well plan on combining the common
//statements between this and above in one function to make things neater
$selectedObj.Layer = %obj.Layer -1;
$selectedObj.isStackable = 1;
$selectedObj.isMountable = 1;
$selectedObj.wasStackable = 1;
%obj.isStackable=0;
%obj.isMountable=0;
%obj.isSelectable=0;
$selectedObj.mount(%obj,0,0,0,false,false,false,false);
}
else if(%mountable == 1 && %obj.isFreeCell == 1)
{
//this is for the FreeCell location this time the card we drop is made not mountable since only one card may occupy FreeCell at a time
%obj.isMountable=0;
$selectedObj.mount(%obj,0,0,0,false,false,false,false);
$selectedObj.Layer = 1;
$selectedObj.isMountable = 0;
}
else if(%mountable == 1 && %obj.isSpaceOpen == 1)
{
//this is for if a column is open, hoped to use freecell statement above for this but couldn't get a good way for both, this card is mountable
%obj.isMountable=0;
$selectedObj.mount(%obj,0,0,0,false,false,false,false);
$selectedObj.Layer = %obj.Layer -1;
$selectedObj.isMountable = 1;
}
else
{
//these statements put the card back to it's old spot if new spot was not a valid move, statents for the mount offset difference
if($oldSpot.isMountable == 1 && $oldSpot.isStackable != 1)
{
$selectedObj.mount($oldSpot,0,0.5,0,false,false,false,false);
}
else
{
$selectedObj.mount($oldSpot,0,0,0,false,false,false,false);
}
$selectedObj.isMountable = 1;
$selectedObj.Level = $oldSpot.Level - 1;
}
//unselect the obj and since no longer have a card selected set to false
$haveCard = false;
$selectedObj = 0;
}
function cardSelectCheck(%worldPos)
{
//lets get a list of all the objects at the clicked point in the t2dScene
%objList = t2dScene.pickPoint(%worldPos);
//lets get a count of how many objects in the list
%objCount = getWordCount(%objList);
//we will start looping through the list
for(%i=0;%i<%objCount;%i++)
{
//grabing the entry corresponding to the loop
%obj = getWord(%objList, %i);
//if we find an object in the list that "isSelectable = true"
if(%obj.isSelectable)
{
//we toggle a value so we know we found an object that "isSelectable"
%selected = true;
//we kick out of the loop
%i = %objCount;
}
}
//if we found an "isSelectable" object
if(%selected)
{
//we then store that object as the selectedObj also set haveCard to true since have a selection record
//the previous mounted spot in case next move isn't valid so we can send the card back make old object
//remountable/restackable if needed
$selectedObj = %obj;
$haveCard = true;
$oldSpot = $selectedObj.getMountedParent();
//set up the card that was the mount to the old card's settings
$oldSpot.isMountable = 1;
if($oldSpot.isFreeCell != 1 || $oldSpot.isSpaceOpen != 1)
{
$oldSpot.isSelectable = $selectedObj.isSelectable;
}
$oldSpot.isStackable = $selectedObj.isStackable;
$selectedObj.isMountable = 0;
}
}Now I'm on to setting up an easy way to write in the millions of different freecell levels that exist. I was trying to work with arrays for a while but I had read they couldn't be based into a function in script and I don't want to break into the code yet. So after racking my brain for hours on a solution and finding no easy one I started to think if it wouldn't be just as fast to do it in level editor. If I saved one level with the cards all off the screen all their link points set up and all their values entered then I could quickly mount down the columns and the only values that would need to change is all of the bottom row be mountable and selectable, the rest of the values will get changed from the mouse script. Now the problem I see there is I don't see any way to set a precise link point location so every card will mount the same. Could take a while to get it exact enough you couldn't notice if I just did trial and error by eye.
Then the fun stuff a move counter (should take like 2 minutes) a timer from when the level loads and a gui to display those and a choose level/new game gui
That should keep me busy for a couple days (will enter game levels over time)
Future steps depending on how it goes is to see if I can figure out how to make an AI to play the levels that should be very very tough for my knowledge.
thanks for reading
About the author
Recent Blogs
• <edit>• My thoughts, and my farewell
• some progress on cards
• teaching my computer to play cards
• I'm not dead
#2
11/17/2006 (6:38 am)
Well in this one I was talking about TGB, I do have TGE 1.5 also but I originally got TGE back when it was 1.3 and the getting started worked then. It's tough to increase your knowledge of TGE the way everything is that's why I switched to TGB for now to get the scripting down and make a couple things then start working on TGE some more
#3
Sorry about interrupting your blog :) Ill be quiet from now on :P
11/17/2006 (6:42 am)
Ok, sorry my bad! I was really tired, and desperate to find an answer...i still havent found it...Sorry about interrupting your blog :) Ill be quiet from now on :P

Torque Owner Mads Laumann
WHen your talking about the tutorials that it mentions in teh documentation, what exacly are you reffering too? I brought my TGE yestoday, so havent spend very much time with it, but i find it pretty hard to find any tutorials for beginners... and if i find some they are fro TGE 1.3 or TGE 1.4, which it out dated.
Actually im not able to complete the first tutorial "GEtting Started" that is in the TGE SDK. And it is not because im a code noob, i am a pretty good coder, so thats not the problem. I followed the tutorial 100 % and it just dotn work. Well all it says in the tutorial works great, but i am not able to move around in the final game :(
I posted this thread yestoday and havent got any response...maybe you can help me? http://www.garagegames.com/mg/forums/result.thread.php?qt=53908
Sorry for using your blog to ask questions, hope its ok :)