I'm still learning a lot about Second Life scripting even though I've been through a couple of rounds of what I would call significant digging. The latest learning as you might know from this blog was a Tic-Tac-Toe game I built. I've been working on another game, still in the rough draft phase trying to discern what might be possible and found a page of examples on the lsl wiki.
http://www.lslwiki.net/lslwiki/wakka.php?wakka=examples
I was digging through these and found a full example of building a Tic-Tac-Toe game. Had I known this existed, I probably wouldn't have read it before hand anyways because I sometimes like to figure stuff out then compare to how others solve the same problems.
http://www.lslwiki.net/lslwiki/wakka.php?wakka=ExampleTicTacToe
As I expected, we went about it in different ways, but I do think that both solutions are fairly equal. I did force people to sit at the table and after I was done thought it would be better to have people simply click to choose sides. He did the later and it is a lot easier to setup than the code I did to watch for people that sit down. Good learning experience though.
One of the best parts of this example is the discussion on Version Control. (http://www.lslwiki.net/lslwiki/wakka.php?wakka=ExampleTicTacToeVersionControl) This is something I struggled with early on and he has a very elegant solution to keep all the scripts in the root prim and copy them out to the child prims when you type "/1 listen" into chat. I spent a lot of time reading this section of the example and will probably be implementing something similar for a future game. A full example isn't given and it glosses over some of the aspects that make this a general use solution so this is definitely going to be home grown.
I haven't really gotten past that page yet and I'll have to spend some time to finish it. Very worthwhile reading.
Showing posts with label tic tac toe. Show all posts
Showing posts with label tic tac toe. Show all posts
Thursday, March 20, 2008
Thursday, January 10, 2008
Building A Second Life Checkers Game #9
I've finished the tic-tac-toe game. It works and I've played it a couple of times with people. Probably a few more bugs and I could make it look a little nicer as well, but overall the goal is complete, I built a two player table top game. I'm trying to figure out if I'm going to go back and finish the checkers game. I don't think I will because there doesn't seem to be a lot of value in it. I would like to be more innovative than that and make a new game that is only available virtually. Something completely new. I started on and I'll have a screen shot of that tomorrow, but it doesn't have any mechanics besides something in my head. When making video games I typically just get a mechanic working then tweak it until some sort of game forms. So, after 9 posts I feel like I may have mislead you into thinking we would have a checkers game and here we end up with a lot less, except for the knowledge that it is possible and I have most of the functional code required to do it. Anyway, I'll keep digging until I have something more. If you want a copy of the tic-tac-toe game, send me and e-mail or find me in SL: Wood Wheels.

One thing I don't think that shows here is that the green triangles point in the direction of the player who's move it is. When the player makes a move I change the rotation of all the green triangles like this.
This may look pretty simple, but it has a trick. I originally tried to just rotate 180 around the z axis, but as I should have known before I started this would not work as rotations are cumulative and coming up with a rotation axis is not trivial piece of math. Quaternians are simply a vector associated with a rotation angle around that vector. They can be hard to calculate programatically.
The solution? Find the two rotation angles at build time and use them as static values.
Cheating I know, but most programming is about getting something to work, even though it might not be the most elegant solution. At least that is how I work. Why not the elegant solution you ask? One takes hours and hours and one just a few minutes and schedule is usually the most important element of all. Also, if it really needs to be elegant, it is better to get it working and come back in later if it really needs it.

One thing I don't think that shows here is that the green triangles point in the direction of the player who's move it is. When the player makes a move I change the rotation of all the green triangles like this.
RotateBtns(integer xoro)
{
integer itmp;
integer btnnum=0;
integer btnlinknum;
integer pos = 0;
rotation rot;
if (xoro==1)
{
rot = rot0;
}
else
{
rot = rot180;
}
for (;pos<9;pos++)
{
itmp = llList2Integer(btnpositions,pos);
if (itmp>=0)
{
llSetLinkPrimitiveParams(itmp,[PRIM_ROTATION,rot]);
}
}
}
This may look pretty simple, but it has a trick. I originally tried to just rotate 180 around the z axis, but as I should have known before I started this would not work as rotations are cumulative and coming up with a rotation axis is not trivial piece of math. Quaternians are simply a vector associated with a rotation angle around that vector. They can be hard to calculate programatically.
The solution? Find the two rotation angles at build time and use them as static values.
vector eul = <0,77,0>; //0 degrees around the z-axis, in Euler form
eul *= DEG_TO_RAD; //convert to radians
rot0 = llEuler2Rot(eul); //convert to quaternion
eul = <3.9,283.0,184.45>; //180 degrees around the z-axis, in Euler form
eul *= DEG_TO_RAD; //convert to radians
rot180 = llEuler2Rot(eul); //convert to quaternion
Cheating I know, but most programming is about getting something to work, even though it might not be the most elegant solution. At least that is how I work. Why not the elegant solution you ask? One takes hours and hours and one just a few minutes and schedule is usually the most important element of all. Also, if it really needs to be elegant, it is better to get it working and come back in later if it really needs it.
Wednesday, January 9, 2008
Building A Second Life Checkers Game #8

Success. I finally got to the point where I could do some testing. Finding someone to help you test is incredibly hard. I have about 30 people listed as friends and of course no one is online. I start asking around at various sand boxes and no one will even lift a finger. Finally one of my friends logs on and it is the person who was harassing me just a while back. I ask him and he is more than eager to help.
So it really pays to try and make friends even when someone is being rude.
There were a couple of small problems, but overall it is working. The next step is to redo the "programmer art" and make it look a little more professional. I've been watching HGTV to try and decide on a color scheme for the game that will fit in most residences. Yeah right.
Labels:
checkers game,
second life,
second life script,
tic tac toe
Tuesday, January 8, 2008
Building A Second Life Checkers Game #7
The checkers game, currently the tic-tac-toe game is coming along nicely. I made really great progress last night.

The pieces are placed in order as the green traingles are touched. The peices are stored in a box below the table and the triangles are swapped to that location when the triangle is touched. Next I'm starting to work on the seating and makeing the game play depended on two avatars seated on the "chairs". The little green button on the left resets the entire board.
Here is some code to move a piece to a specific location on the board.
Most of this code was outlined in post #4. I just consolidated it into a function.
And some code to move a piece into the storage box.
When the game starts up I store the location of all the buttons.
Here is the ShowAllBtns function.
I'm going through all the board positions looking for a -1 meaning that the location doesn't already have a piece. If it doesn't, I move the first (then next) button to that location and save which button is at that point in the btnpositions list. At the end I move the rest of the buttons that were not used to storage so they are not visible. Using this list, when a button is touched we can convert the link number given by the touch event into a position on the board by traversing the btnpositions list.

The pieces are placed in order as the green traingles are touched. The peices are stored in a box below the table and the triangles are swapped to that location when the triangle is touched. Next I'm starting to work on the seating and makeing the game play depended on two avatars seated on the "chairs". The little green button on the left resets the entire board.
Here is some code to move a piece to a specific location on the board.
MoveLinkToPos(integer iLinkNum, integer posWhere)
{
key rootKey = llGetLinkKey(1);
list params = llGetPrimitiveParams([PRIM_SIZE]);
vector sz = llList2Vector(params,0);
//llOwnerSay("area size = "+(string)sz);
integer px = posWhere / 3;
integer py = posWhere % 3;
//llOwnerSay(" px="+(string)px+" py="+(string)py);
// set the position of the piece to 0,0
float sqr_x_size = sz.x / 3;
float sqr_y_size = sz.y / 3;
//llOwnerSay(" sqr_x_size="+(string)sqr_x_size+" sqr_y_size="+(string)sqr_y_size);
vector p = < (sqr_x_size * px) - (sz.x/2) + (sqr_x_size/2),
(sqr_y_size * py) - (sz.y/2) + (sqr_y_size/2),
0.051 >;
//llOwnerSay(" new pos = "+(string)p);
llSetLinkPrimitiveParams(iLinkNum,[PRIM_POSITION,p]);
}
Most of this code was outlined in post #4. I just consolidated it into a function.
And some code to move a piece into the storage box.
MoveLinkToStorage(integer iLinkNum)
{
llSetLinkPrimitiveParams(iLinkNum,[PRIM_POSITION,vStorage]);
}
When the game starts up I store the location of all the buttons.
for (i=2;i<=iNumPrims+1;i++)
{
string stName = llGetLinkName(i);
key keyObj = llGetLinkKey(i);
list a = llGetObjectDetails(keyObj,([OBJECT_POS]));
vector p = llList2Vector(a,0) - regionPos;
if (stName=="movebtn")
{
btnlinks = llListInsertList(btnlinks,[i],0);
}
else if (stName=="xtile") ....
}
ShowAllBtns();
Here is the ShowAllBtns function.
ShowAllBtns()
{
integer itmp;
integer btnnum=0;
integer btnlinknum;
integer pos = 0;
vector v;
for (;pos<9;pos++)
{
itmp = llList2Integer(board,pos);
if (itmp==-1)
{
btnlinknum = llList2Integer(btnlinks,btnnum);
//llOwnerSay("move linkbtn="+(string)btnlinknum+" to pos="+(string)pos);
MoveLinkToPos(btnlinknum,pos);
btnnum++;
}
else
{
btnlinknum = -1;
}
//llOwnerSay(" adding btnlinknum="+(string)btnlinknum+" at pos="+(string)pos);
btnpositions = llListReplaceList(btnpositions,[btnlinknum],pos,pos);
}
//llOwnerSay("btnpositions="+(string)btnpositions);
// move the rest of the buttons to the storage
for (;btnnum<9;btnnum++)
{
btnlinknum = llList2Integer(btnlinks,btnnum);
MoveLineToStorage(btnlinknum);
}
}
I'm going through all the board positions looking for a -1 meaning that the location doesn't already have a piece. If it doesn't, I move the first (then next) button to that location and save which button is at that point in the btnpositions list. At the end I move the rest of the buttons that were not used to storage so they are not visible. Using this list, when a button is touched we can convert the link number given by the touch event into a position on the board by traversing the btnpositions list.
integer FindBtnPos(integer iLinkNum)
{
integer count = llGetListLength(btnpositions);
integer itmp;
integer i=0;
for (;i<9;i++)
{
itmp = llList2Integer(btnpositions,i);
//llOwnerSay("btn at pos "+(string)i+" = "+(string)itmp);
if (itmp==iLinkNum)
{
return i;
}
}
return -1;
}
Labels:
checkers game,
physics,
second life,
second life script,
tic tac toe
Subscribe to:
Posts (Atom)