Free learning code

C++, C#, Java, PHP, ect...
Post Reply
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Free learning code

Post by hallsofvallhalla »

Since I have basically scrapped my mini game idea I thought i would share some knowledge on how I was doing it. Maybe someone can learn from it, here is a post I posted in the FS admin section, this code is no longer being used so i share with my indie-resource buds :)...

K grab a comfortable chair, a drink, and a sandwich, and snuggle up to the PC for a moment. I want to take you through a little on how I crafting will work

First

Code: Select all

$tag = $_GET['tag'];
	$weapon = $_GET['type'];
	$hammer = $_GET['hammer'];
	$surface = $_GET['surface'];
	$pid = $_GET['pid'];
We get the variables passed through the browser that the player selected for their hammer and surface. Example, hammer they might have choosen speed, so $hammer will equal "speed".

we then do some checks to see who owns the forge and if the player can afford to craft but I put it here so you could see anyways.

Code: Select all

$inn="SELECT * from weaponforges WHERE pid='$pid' AND sid = '$tag'";
    $inn2=mysql_query($inn) or die("Could not get weapon forge");
	  $inn3=mysql_fetch_array($inn2);	
if ($inn3[pname] == $playerinfo3[name]){$inn3[price] = 0;}
if ($playerinfo3[credits] < $inn3[price])
		{
	echo "You cannot afford to use this Weapon Forge.<br>";
	echo "<center><br><A href='/locations/map.php'>Go Back";
	$updatecheat="Update anticheat set weaponforge=0 where id='$id'";
	    mysql_query($updatecheat) or die("Could not update cheat");
	exit;
	
	}
	else
	{
		 
	 $updateplayers="Update players set credits=credits-'$inn3[price]' where id='$id'";
	    mysql_query($updateplayers) or die("Could not update player stats");
	 
	 $updateowner="Update players set credits=credits+'$inn3[price]' where name='$inn3[pname]'";
	    mysql_query($updateowner) or die("Could not update owner stats");	
		
		 $updateinn="Update weaponforges set total=total+'$inn3[price]' where pid='$pid' AND sid = '$tag'";
	    mysql_query($updateinn) or die("Could not update weapon forge stats");	

Okay back on track

We next query for the players skill in weapon crafting and the recipes difficulty...

Code: Select all

  $skills="SELECT weapon_craft from skills where id='$id'";
    $skills2=mysql_query($skills) or die("Could not get user skills");
    $skills3=mysql_fetch_array($skills2);
	 
	   $recipeinfo="SELECT * from recipes where name='$weapon'";
    $recipeinfo2=mysql_query($recipeinfo) or die("Could not get recipe stats");
    $recipeinfo3=mysql_fetch_array($recipeinfo2);



Next we modify the players skill roll based on the difficulty of the recipe

Code: Select all

/////skills roll/////////////////////////
	///////////////////////////////////////////////////////////////
	if ($skills3[0] >= $recipeinfo3[difficulty])
	{
	$modify = ($skills3[0] - $recipeinfo3[difficulty]) * 5;
	$modify2= 50 + $modify;
	}
	else 
	{
	$modify = ($recipeinfo3[difficulty] - $skills3[0]) * 5;
	$modify2= 50 - $modify;
	}
    if ($modify2 <= 10){$modify2 = 10;}
	if ($modify2 >= 90){$modify2 = 90;}

Next I assign the database name to the modifier name so that when we update backpack it matches database and then i create base stats per weapon..

Code: Select all

if ($weapon == 'Blade'){$name = 'One Handed Blade'; $speed = 8; $damage = 3; $rating = 2;}
	if ($weapon == 'TwoH_Blade'){$name = 'Two Handed Blade';$speed = 4;$damage = 7; $rating = 4;}
	if ($weapon == 'Axe'){$name = 'Axe';$speed = 7;$damage = 4;  $rating = 2;}
	if ($weapon == 'Pole_Arm'){$name = 'Pole Arm';$speed = 5;$damage = 5; $rating = 4;}
	if ($weapon == 'Bow'){$name = 'Bow';$speed = 5;$damage = 3; $rating = 3;}
	if ($weapon == 'Blunt'){$name = 'Blunt Weapon';$speed = 9;$damage = 4; $rating = 2;}
	if ($weapon == 'Knife'){$name = 'Knife';$speed = 10;$damage = 2;  $rating = 1;}
	if ($weapon == 'Crossbow'){$name = 'Crossbow';$speed = 3;$damage = 4;  $rating = 3;}
	if ($weapon == 'Light_Pistol'){$name = 'Light Pistol';$speed = 9;$damage = 6;  $rating = 5;}
	if ($weapon == 'Heavy_Pistol'){$name = 'Heavy Pistol';$speed = 5;$damage = 9;  $rating = 5;}
	if ($weapon == 'Energy_Pistol'){$name = 'Energy Pistol';$speed = 9;$damage = 10;  $rating = 7;}
	if ($weapon == 'Energy_Rifle'){$name = 'Energy Rifle';$speed = 6;$damage = 13;  $rating = 7;}
	if ($weapon == 'Light_Rifle'){$name = 'Light Rifle';$speed = 7;$damage = 9;  $rating = 6;}
	if ($weapon == 'Heavy_Rifle'){$name = 'Heavy Rifle';$speed = 2;$damage = 13;  $rating = 6;}
	if ($weapon == 'Shotgun'){$name = 'Shotgun';$speed = 4;$damage = 16;  $rating = 4;}
	

Okay new here is where the players skill gets checked per every forge level
each level affects a stat
Here is how the levels work
1. Melting = durability
2. Forming = Rating
3. Hammer = Speed
4. Temper = Damage
5. Quenching = Craft Points

checks forsucceed or fail

Code: Select all

$meltroll = rand(1,100);
	if ($meltroll < $modify2){$melt = 1;}
	$form = rand(1,100);
	if ($formroll < $modify2){$form = 1;}
	$hammerroll = rand(1,100);
	if ($hammerroll < $modify2){$hammered = 1;}
	$temperroll = rand(1,100);
	if ($temperroll < $modify2){$temper = 1;}
	$quenchroll = rand(1,100);
	if ($quenchroll < $modify2){$quench = 1;}
New each level and how it affects the stat

Code: Select all

///////////////////////////Melting///////////////////////
	$duramod = rand(1,$skills3[0]);
	$duramod2 = rand(1,$skills3[0]);
	if ($duramod < 3){$duramod = 3;} 
	if ($duramod2 < 3){$duramod2 = 3;} 
	if ($melt == 1)
	{ 
	$durability = $modify2 - $meltroll;
	if ($durability < 6){$durability = 6;}
	if ($durability > 20){$durability = 20;}
	if ($hammer == "durability"){$durability = $durability + $duramod;}
	if ($surface == "durability"){$durability = $durability + $duramod2;}
	}
If melting succeeds it minuses craft roll from the players roll and thats the base durability, this way the better the player skill and the better the roll the better starting durability. Also if the player choose durability as a hammer or surface then it adds those

Code: Select all

if ($melt != 1)
	{
	$basedura = rand(1,$skills3[0]);
	if ($basedura < 2){$basedura = 2;} 
	if ($basedura > 6){$basedura = 6;} 
	$durability = $durability + $basedura;
	if ($hammer == "durability"){$durability = $durability + $duramod;}
	if ($surface == "durability"){$durability = $durability + $duramod2;}
	}
	////////////////////////////////////////////////////////
If the roll fails then the weapon still is made but has low durability. No more wasting of materials because of fails, only if they fail in 4 or more of the attempts does the whole thing fail. :)

Code: Select all

	///////////////////////////Forming///////////////////////
	$formmod = rand(1,$skills3[0]);
	$formmod2 = rand(1,$skills3[0]);
	$formmod = $formmod/2;
	$formmod2 = $formmod2/2;
	if ($formmod < 1){$formmod = 1;} 
	if ($formmod2 < 1){$formmod2 = 1;} 
	if ($form == 1)
	{ 
	if ($hammer == "rating"){$rating = $rating + $formmod;}
	if ($surface == "rating"){$rating = $rating + $formmod2;}
	}
	if ($form != 1)
	{
	$baserate = rand(1,$rating);
	$rating = $baserate;
	if ($hammer == "rating"){$rating = $rating + $formmod;}
	if ($surface == "rating"){$rating = $rating + $formmod2;}
	}
	////////////////////////////////////////////////////////

Forming and the rest are much the same except it work off the base stat.

Code: Select all

///////////////////////////hammering///////////////////////
	$hammermod = rand(1,$skills3[0]);
	$hammermod2 = rand(1,$skills3[0]);
	$hammermod = $hammermod/2;
	$hammermod2 = $hammermod2/2;
	if ($hammermod < 1){$hammermod = 1;} 
	if ($hammermod2 < 1){$hammermod2 = 1;} 
	if ($hammered == 1)
	{ 
	if ($hammer == "speed"){$speed = $speed + $formmod;}
	if ($surface == "speed"){$speed = $speed + $formmod2;}
	}
	if ($hammered != 1)
	{
	$baserate = rand(1,$speed);
	$speed = $baserate;
	if ($hammer == "speed"){$speed = $speed + $hammermod;}
	if ($surface == "speed"){$speed = $speed + $hammermod2;}
	}
	////////////////////////////////////////////////////////	

so as you can see this gives the armor and weapons much more of a dynamic craft. Players could create a wonderful item but fail in one place. They would have to craft a material into the weapon or armor to bring that stat up.
User avatar
Viper
Posts: 414
Joined: Thu Apr 30, 2009 3:01 am

Re: Free learning code

Post by Viper »

sorry for my stupidity but what is this in? php? i cant remember what you coded it in...
User avatar
Raven67854
Posts: 893
Joined: Thu Apr 23, 2009 12:51 am

Re: Free learning code

Post by Raven67854 »

#Viper
Php
C/C++/C#/Blitzmax/Visual Basic Programmer
Blender/XSI user
"It's not what you use. It's how you use it what you have" -Unknown author
"Sleeping the number one cause of death" -Raven67854
""No matter how much shit life throws at me. I will keep on going just to piss life off." -Raven67854

PC Specs:
AMD Athlon 64x2 5000+ 2.6ghz Black Edition
8 gigs Corsair ram 1066mhz
9800GTX+
2x750gb HD
Vista Home Premium X64
Ubuntu 9.04 X64
User avatar
Viper
Posts: 414
Joined: Thu Apr 30, 2009 3:01 am

Re: Free learning code

Post by Viper »

i win!

sweet. ok i will definitely be taking a look at this :)
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Free learning code

Post by hallsofvallhalla »

yes this is the php only, i can add in the javascript and ajax that is also part of it if you want. As a word of advice though, stick with PHP, less trouble and more cross browser compatibility
User avatar
Viper
Posts: 414
Joined: Thu Apr 30, 2009 3:01 am

Re: Free learning code

Post by Viper »

well im taking computer math/science courses in school next year and i hear thats all java script...but i want to learn php as well.
User avatar
Raven67854
Posts: 893
Joined: Thu Apr 23, 2009 12:51 am

Re: Free learning code

Post by Raven67854 »

Well if you wanna learn php go here.
http://www.w3schools.com/PHP/default.asp

It's a great place to learn php. If you need help setting up WAMP or whatever then just contact me on MSN I can guide you.
C/C++/C#/Blitzmax/Visual Basic Programmer
Blender/XSI user
"It's not what you use. It's how you use it what you have" -Unknown author
"Sleeping the number one cause of death" -Raven67854
""No matter how much shit life throws at me. I will keep on going just to piss life off." -Raven67854

PC Specs:
AMD Athlon 64x2 5000+ 2.6ghz Black Edition
8 gigs Corsair ram 1066mhz
9800GTX+
2x750gb HD
Vista Home Premium X64
Ubuntu 9.04 X64
User avatar
Viper
Posts: 414
Joined: Thu Apr 30, 2009 3:01 am

Re: Free learning code

Post by Viper »

yeah, i have heard of the site before.

but between life, zday and learning Java....PHP is a bit far off for me :)
dave3460
Posts: 117
Joined: Wed Jul 01, 2009 2:13 pm

Re: Free learning code

Post by dave3460 »

is there anyone that knows of ajax timer setup for a button to click when you use the button it countdown approx 45 secs then goes back to 45 .but i aslo would like to learn how to add a captu so after a set amount of time you need to re.enter the skills .the type of game i am working for is you can chat to friends or go afk when doing certain things .
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Free learning code

Post by hallsofvallhalla »

you can just use javascript for the timer

http://www.java-scripts.net/javascripts ... imer.phtml

as far the button also use js onlick event...onclick="gotimer()"


you could set a behind the scenes timer to test for the captha then make your own, take a table and fill it full of captchas, then

if (timer > 100)
{
select name from captcha where name = '$captchaentered';
.....


you will most likely have to do a ajax server hit to do the captcha in real time
Post Reply

Return to “Coding”