Many code snippets for FPS games by BMacZero30th Dec 2005 22:45
|
---|
Summary Several functions and other code snippets for use in FPS games. All are from this codebase. Please give credit to appropriate creators (listed below) Description A few of these codes have been moded slightly for instructional purposes and performance. Most of the rem statements are from the original authors. If some of these snippets don't work do not complain to me. Here I have meerly compiled them into one place. Also - this is BY NO MEANS the only code of this sort out there. Don't give up looking if it's not here. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com `Here are the many FPS parts. Happy Coding. `You don't need to credit me, just credit the original authors `=========================================================================================================================== `=========================================================================================================================== `**************************************************************** `*** Radar Function *** `*** By JessTicular *** `*** 27th Sept 2003 *** `*** www.JessTicular.tk *** `*** Give credit where it's due. Thanks *** `**************************************************************** FUNCTION radar_disp() `radar box you can change these values to suit your needs INK RGB(0,255,0),0 `top LINE (SCREEN WIDTH()-100),0,SCREEN WIDTH(),0 `bottom LINE (SCREEN WIDTH()-100),100,SCREEN WIDTH(),100 `left LINE (SCREEN WIDTH()-100),0,(SCREEN WIDTH()-100),100 `right LINE (SCREEN WIDTH()-1),0,(SCREEN WIDTH()-1),100 INK RGB(255,0,0),0 `Change the 100 to the heighest number of object you have. FOR a = 1 TO 100 `This if assumes your character is object number 1. `It gives your object's circle a larger radius and a different colour. IF a = 1 radius# = 2 INK RGB(255,0,0),0 ELSE radius# = 1 INK RGB(100,100,255),0 ENDIF REMSTART Ok... firstly it checks to see if this object number exists. The circle is positioned CIRCLE x,y,radius. The x value here is the x value in 3D space, hence OBJECT POSITION X(a), the y value is the z value in 3D space, hence OBJCET POSITION Z(a). (OBJECT POSITION X(a)/100) is done because: In my game, i used a matrix of 10000x10000 Because the box for the radar is only 100x100, the values to position them, had to be < 100 Since OBJECT POSITION X(a) MUST BE < 10000 (otherwise it'd be off the matrix) then (OBJECT POSITION X(a)/100) MUST BE < 100 If you are using a matrix of size, 500x500, simply make the code (OBJECT POSITION X(a)/5) or for matrix 65000x65000 make it (OBJECT POSITION X(a)/650) etc. etc... This will give you your value < 100 REMEND IF OBJECT EXIST(a)=1 CIRCLE ((SCREEN WIDTH()-101)+(OBJECT POSITION X(a)/100)),(100 - (OBJECT POSITION Z(a)/100)),radius# ENDIF NEXT a ENDFUNCTION `=========================================================================================================================== `=========================================================================================================================== remstart This is a simple example of how to use model-dependent cameras and a turret/weapon aiming system using the mouse. Most of this code is unoptimized and still under development for a game project, so there are functions which do nothing that is associated with stated the example. USE: You can use the mouse to rotate the turret and aim the weapons on the turret. Everything is bone-based using models made and rigged in Milkshape 3D. The further you move from the center of the screen the faster the turret and weapon will track. LEGAL STUFF: This code is provided for use by anyone for whatever they wish. remend init: perform checklist for display modes display_mode_index = checklist quantity() display_width = checklist value a(display_mode_index) display_height = checklist value b(display_mode_index) display_depth = checklist value c(display_mode_index) set display mode display_width, display_height, display_depth rem mouse control code here>> rem calculate mouse hotspot bounds - when the mouse is inside the hotspot, no movement takes place mouse_hotspot_x_min = display_width * 0.5 - ((display_width * 0.01) * 0.5) mouse_hotspot_x_max = display_width * 0.5 + ((display_width * 0.01) * 0.5) mouse_hotspot_y_min = display_height * 0.5 - ((display_height * 0.01) * 0.5) mouse_hotspot_y_max = display_height * 0.5 + ((display_height * 0.01) * 0.5) rem calculate the distance between the screen edges and the hotspot bounds mouse_look_width = display_width - mouse_hotspot_x_min mouse_look_height = display_height - mouse_hotspot_y_min rem <<mouse control code here sync on distance_conversion# = 1.0169864960107410848896146814664 loadobjects: load object "d:nxs.x",1 load object "d:bmn.x",2 load object "d:hfr.x",3 load object "d:osp.x",4 position object 2,25,0,25 position object 3,25,0,-25 position object 4,-25,0,25 perform checklist for object limbs 1 temp = checklist quantity() turn_rate# = 0.2 acceleration# = 0.001 max_ground_speed# = .125 maximum_turret_track_speed# = 0.125 maximum_weapon_track_speed# = 0.125 rem object loading logic - steps through the nodes of an object and finds the ones it needs for camera control for c = 1 to temp - 1 if limb name$ (1,c) = "CodeTurret" then turret_limb = c if limb name$ (1,c) = "CodeWeapon" then weapon_limb = c if limb name$ (1,c) = "Camera_Outside_0" then camera_outside_limb = c if limb name$ (1,c) = "Cam_Out_0_Target" then cam_out_0_target = c if limb name$ (1,c) = "Camera_Inside_0" then camera_inside_limb = c if limb name$ (1,c) = "Cam_In_0_Target" then cam_in_0_target = c rem if limb name$ (1,c) = "max_speed_.15" then max_speed = c next c rem camera control code here>> rem set up the cameras for the first pass current_camera = camera_outside_limb current_camera_target = cam_out_0_target cam_x_pos# = limb position x (1, current_camera) cam_y_pos# = limb position y (1, current_camera) cam_z_pos# = limb position z (1, current_camera) cam_x_tgt# = limb position x (1, current_camera_target) cam_y_tgt# = limb position y (1, current_camera_target) cam_z_tgt# = limb position z (1, current_camera_target) rem <<camera control code here rem position mouse 959, 599 DO x_camera_flag = 0 rem mouse control code here>> rem save old angle values old_weapon_angle_x# = weapon_angle_x# old_turret_angle_y# = turret_angle_y# rem get current mouse position mouse_x# = mousex() mouse_y# = mousey() rem check to see if mouse is inside hotspot if mouse_x# < mouse_hotspot_x_min turret_turn_speed# = maximum_turret_track_speed# * ((mouse_hotspot_x_min - mouse_x#) / mouse_look_width) turret_angle_y# = turret_angle_y# - turret_turn_speed# endif if mouse_x# > mouse_hotspot_x_max turret_turn_speed# = maximum_turret_track_speed# * (1 - ((display_width - mouse_x#) / mouse_look_width)) turret_angle_y# = turret_angle_y# + turret_turn_speed# endif if mouse_y# > mouse_hotspot_y_max weapon_turn_speed# = maximum_weapon_track_speed# * (1 - ((display_height - mouse_y#) / mouse_look_height)) weapon_angle_x# = weapon_angle_x# + weapon_turn_speed# endif if mouse_y# < mouse_hotspot_y_min weapon_turn_speed# = maximum_weapon_track_speed# * ((mouse_hotspot_y_min - mouse_y#) / mouse_look_height) weapon_angle_x# = weapon_angle_x# - weapon_turn_speed# endif rem <<mouse control code here mouse_command = mouseclick() cmd$ = inkey$() select cmd$ case "1" ground_speed# = 0 endcase case "2" ground_speed# = max_ground_speed# * 0.1125 endcase case "3" ground_speed# = max_ground_speed# * 0.225 endcase case "4" ground_speed# = max_ground_speed# * 0.3375 endcase case "5" ground_speed# = max_ground_speed# * 0.45 endcase case "6" ground_speed# = max_ground_speed# * 0.5625 endcase case "7" ground_speed# = max_ground_speed# * 0.675 endcase case "8" ground_speed# = max_ground_speed# * 0.7875 endcase case "9" ground_speed# = max_ground_speed# * 0.9 endcase case "0" ground_speed# = max_ground_speed# endcase case "," turret_angle_y# = turret_angle_y# - turret_turn_speed# endcase case "." turret_angle_y# = turret_angle_y# + turret_turn_speed# endcase rem camera control code here>> rem Toggles between the player's primary camera and the external camera case "x" if current_camera = camera_inside_limb and x_camera_flag = 0 current_camera = camera_outside_limb current_camera_target = cam_out_0_target x_camera_flag = 1 endif if current_camera = camera_outside_limb and x_camera_flag = 0 current_camera = camera_inside_limb current_camera_target = cam_in_0_target x_camera_flag = 1 endif endcase rem <<camera control code here endselect rem mouse control code here>> rem check roation angle against constraints if weapon_angle_x# > 45 weapon_angle_x# = 45 old_weapon_angle_x# = 45 endif if weapon_angle_x# < 0 weapon_angle_x# = 0 old_weapon_angle_x# = 0 endif rem calculate the turn angle for both turret and weapon turret_turn# = CurveAngle(turret_angle_y#,old_turret_angle_y#,24) weapon_turn# = CurveAngle(weapon_angle_x#,old_weapon_angle_x#,24) rem rotate the turret along its Y-axis rotate limb 1,turret_limb ,0,turret_turn#,0 rem rotate the weapon along its X-axis rotate limb 1,weapon_limb ,weapon_turn#,0,0 rem rotate the current camera's node along its X-axis, the same amount as the weapon rotate limb 1,current_camera, weapon_turn#,0,0 rem <<mouse control code here if leftkey() = 1 then heading# = heading# - turn_rate# if rightkey() = 1 then heading# = heading# + turn_rate# if ground_speed# > 0.5 then ground_speed# = 0.5 yrotate object 1, heading# move object 1, ground_speed# rem camera control code here>> rem this block controls our camera system, always keeping it aimed where the turret and weapon are pointed rem save old camera data old_cam_x_pos# = cam_x_pos# old_cam_y_pos# = cam_y_pos# old_cam_z_pos# = cam_z_pos# old_cam_x_tgt# = cam_x_tgt# old_cam_y_tgt# = cam_y_tgt# old_cam_z_tgt# = cam_z_tgt# rem get the current camera's position cam_x_pos# = limb position x (1, current_camera) cam_y_pos# = limb position y (1, current_camera) cam_z_pos# = limb position z (1, current_camera) rem get the current camera target's position cam_x_tgt# = limb position x (1, current_camera_target) cam_y_tgt# = limb position y (1, current_camera_target) cam_z_tgt# = limb position z (1, current_camera_target) rem position camera and aim it at the target position camera cam_x_pos#, cam_y_pos#, cam_z_pos# point camera cam_x_tgt#, cam_y_tgt#, cam_z_tgt# rem <<camera control code here text 10,10,"Number of Nodes in Model: " + str$(temp) text 10,30,"Turret Node: " + str$(turret_limb) text 10,50,"Weapon Node: " + str$(weapon_limb) text 10,70,"External Camera Node: " + str$(camera_outside_limb) text 10,90,"Internal Camera Node: " + str$(camera_inside_limb) text 10,110,"Camera Position: " + str$(cam_x_pos#) + " " + str$(cam_y_pos#) + " " + str$(cam_z_pos#) text 10,130,"Camera Target: " + str$(cam_x_tgt#) + " " + str$(cam_y_tgt#) + " " + str$(cam_z_tgt#) text 10,150,"Speed: " + str$(ground_speed# * 100) + " meters/second" text 10,170,"Current Camera: " + str$(current_camera) text 10,190,"Left/Right Arrow keys to turn, mouse to control turret" text 10,210,"Display Mode: " + str$(display_width) + " x " + str$(display_height) + " x " + str$(display_depth) text 10,230,"Mouse Hotspot: " + str$(mouse_hotspot_x_min) + " " + str$(mouse_hotspot_x_max) + " " + str$(mouse_hotspot_y_min) + " " + str$(mouse_hotspot_y_max) text 10,250,"Maximum Speed Offset: " + str$(max_speed) text 10,270,"Object Length: " + str$(object size z(1) * distance_conversion#) + " meters" text 10,310,"Press ESC to quit" sync LOOP `=========================================================================================================================== `=========================================================================================================================== `Code for changing guns in FPS games remstart Controls: UP = show gun 1 DOWN = show gun 2 LEFT = show no guns remend rem make the guns - you should replace these with gun models make object cylinder 1,2 make object cube 2,4 rem rotate objects to form a gun rotate object 1,90,1,1 rotate object 2,90,1,1 rem position objects in the right place - where you hold a gun position object 1,1,-7,1.5 position object 2,1,-7,1.5 rem hide both guns - when a=3 nboth guns hidden (see a=3 below) a=3 rem begin loop do rem a=1 - show gun 1 if a=1 show object 1 hide object 2 endif rem a=2 - show gun 2 if a=2 show object 2 hide object 1 endif rem a=3 - hide both guns if a=3 hide object 1 hide object 2 endif rem when up key pressed perform a=1(show gun 1) if upkey()=1 a=1 endif rem when down key pressed perform a=2(show gun 2) if downkey()=1 a=2 endif rem when left key pressed perform a=3(hide both guns) if leftkey()=1 a=3 endif rem end loop loop `=========================================================================================================================== `=========================================================================================================================== `Gun Movement to Simulate Breating/Moving do REM First you need two variables a1#=wrapvalue(a1#+4) a2#=cos(a1#) REM don't ask me how it works it just does.(These variables are inside the main loop.) REM You then need to add this piece of code to your moving forward code. position object 1,5+a2#,-7+a2#,10 REM (Again in the main loop) REM it should work like this if upkey()=1 (Can be any key you like) XTest# = Newxvalue(X#,cAY#,5) ZTest# = Newzvalue(Z#,cAY#,5) If XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000 X#=XTest# Z#=ZTest# position object 1,5+a2#,-7+a2#,10 Endif Endif REM to make the camera move from side to side like you are walking do this. if mouseclick()=2(or whatever your key is)then Position camera X#+a2#,Y#+a2#*4+35,Z# REM Bare in mind that the X#,Y#,Z# are just what I have called the coordinates and you REM should just add them to what you `have put. oh and heres a cool effect position camera X#,Y#+35+a2#/4,Z# `this will make it look like you are breathing. (once again add to the main loop) `Again play around with this code till you get the result you like. loop `=========================================================================================================================== `=========================================================================================================================== `This tutorial will teach you how to make a basic flashlight for your games. `It uses the ghost object command to perform a neat trick against objects it's viewed `through. `Tutorial made by MikeS of CurvedBasic 2-5-05 sync on : sync rate 60 `Set our refresh rate set display mode 1024,768,32 `Set the display mode color backdrop rgb(0,0,0) `Color the background of our world black make object cone 2,1 `This will be our flashlight set object smoothing 2,100 `The cone object by itself is very blocky, so we smooth it. ghost object on 2,-1 `This is the most important part of this flashlight snippet. `Basically, with the ghost object command set to a negative `value, it inverts all the colors around the object. `Thus, this can be used as a cheap effect for a flashlight. xrotate object 2,-90 `We rotate our cone object now. position object 2,0,-.1,2 `Position it a little away from us lock object on 2 `Now we lock the object onto our screen for x=3 to 20 `Here is where we create some demo objects. make object cube x,10 `This will give us some random objects to view with our `flashlight position object x,x*10,0,10 color object x,rgb(rnd(55),rnd(55),rnd(55)) set object light x,1 next x `We want things dark, so we use these commands to darken the area, and set the lighting `to a perfect black set ambient light 20 color ambient light rgb(0,0,0) do `Initialize our loop text 0,0,"Use 1-5 to change the light color" `A little bit of instructions for our user. :) `Here we have some fun by coloring our light however we like with if-endif statements. if keystate(2)=1 `Detect which key was pressed. color object 2,rgb(255,255,255) `Color the object endif `End our statement if keystate(3)=1 color object 2,rgb(0,0,0) endif if keystate(4)=1 color object 2,rgb(255,0,0) endif if keystate(5)=1 color object 2,rgb(0,255,0) endif if keystate(6)=1 color object 2,rgb(0,0,255) endif control camera using arrowkeys 0,1,1 `This handy command moves our camera. sync `Refresh our screen loop `Go back to our do statement. `=========================================================================================================================== `=========================================================================================================================== sync on : sync rate 60 hide mouse autocam off load image "Flasha.bmp",101 load image "Flashb.bmp",102 load sound "Metal Shot.wav",1 load object "Soldier.3DS",1 position object 1,0,-1,3 yrotate object 1,270 load object "Muzzel Flash.x",2 position object 2,.93,0.35,2.8 scale object 2,.2,.2,.2 yrotate object 2,270 texture object 2,101 set object 2,1,0,0 hide object 2 time=1000 color backdrop rgb(0,0,0) camy#=2 for n=1 to 110 camy#=camy#-.02 position camera 2,camy#,1 point camera 0,0,3 sync next n top: play object 1 sync wait 1250 flash=100 show object 2 play sound 1 for n=1 to 3 flash=flash+1 : if flash=103 then flash=101 texture object 2,flash wait 50 sync next n hide object 2 set text font "Goudy Old Style" set text size 25 yellow=rgb(200,150,0) ink yellow,yellow wait time time=500 do text 180,10,"Press any key to play animation again" if inkey$()<>"" then goto top sync loop `=========================================================================================================================== `=========================================================================================================================== rem Amoeba PAthfinding by Elthan (Use for AI players) rem (www.elthanart.tk // www.darkbasico.tk) rem From Spanish Community set display mode 800,600,32 sync on : sync rate 30 REM type user define for array mapa(=Map) type datos obstaculo as integer generacion as integer xPadre as integer yPadre as integer endtype rem Map dim mapa(6,6) as datos rem Walls mapa(3,1).obstaculo=1 mapa(3,2).obstaculo=1 : mapa(5,2).obstaculo=1 mapa(2,3).obstaculo=1 : mapa(3,3).obstaculo=1 : mapa(5,3).obstaculo=1 mapa(5,4).obstaculo=1 mapa(1,5).obstaculo=1 : mapa(2,5).obstaculo=1 : mapa(4,5).obstaculo=1 : mapa(5,5).obstaculo=1 mapa(4,6).obstaculo=1 rem Start and End squares. rem Change to make test. mapa(2,2).obstaculo=2 : mapa(5,6).obstaculo=3 for x=1 to 6 for y=1 to 6 if mapa(x,y).obstaculo=2 mapa(x,y).generacion=1 : paso=1 xP=x : yP=y endif if mapa(x,y).obstaculo=3 then xF=x : yF=y next y next x rem Main Loop do cls gosub texto_informativo gosub dibujar_mapa if encontrado=0 and spacekey()=1 then gosub ameba sync loop rem Pathfinding sub ameba: while encontrado=0 rem Search in array for x=1 to 6 for y=1 to 6 if mapa(x,y).generacion=paso xA=x : yA=y rem First Square (like degrees) rem 4 3 2 rem 5 * 1 rem 6 7 8 rem Check if fisrt square is into the array if xA+1<7 rem Check if first square is a wall and not processed if mapa(xA+1,yA).obstaculo<>1 and mapa(xA+1,yA).generacion=0 rem Assign next generation mapa(xA+1,yA).generacion=paso+1 rem Assign a father to this square mapa(xA+1,yA).xPadre=xA : mapa(xA+1,yA).yPadre=yA rem Check if we arrive to the end if xA+1=xF and yA=yF then encontrado=1 endif endif rem 2nd if xA+1<7 and yA-1>0 if mapa(xA+1,yA-1).obstaculo<>1 and mapa(xA+1,yA-1).generacion=0 mapa(xA+1,yA-1).generacion=paso+1 mapa(xA+1,yA-1).xPadre=xA : mapa(xA+1,yA-1).yPadre=yA if xA+1=xF and yA-1=yF then encontrado=1 endif endif rem 3rd if yA-1>0 if mapa(xA,yA-1).obstaculo<>1 and mapa(xA,yA-1).generacion=0 mapa(xA,yA-1).generacion=paso+1 mapa(xA,yA-1).xPadre=xA : mapa(xA,yA-1).yPadre=yA if xA=xF and yA-1=yF then encontrado=1 endif endif rem 4th if xA-1>0 and yA-1>0 if mapa(xA-1,yA-1).obstaculo<>1 and mapa(xA-1,yA-1).generacion=0 mapa(xA-1,yA-1).generacion=paso+1 mapa(xA-1,yA-1).xPadre=xA : mapa(xA-1,yA-1).yPadre=yA if xA-1=xF and yA-1=yF then encontrado=1 endif endif rem 5th if xA-1>0 if mapa(xA-1,yA).obstaculo<>1 and mapa(xA-1,yA).generacion=0 mapa(xA-1,yA).generacion=paso+1 mapa(xA-1,yA).xPadre=xA : mapa(xA-1,yA).yPadre=yA if xA-1=xF and yA=yF then encontrado=1 endif endif rem 6th if xA-1>0 and yA+1<7 if mapa(xA-1,yA+1).obstaculo<>1 and mapa(xA-1,yA+1).generacion=0 mapa(xA-1,yA+1).generacion=paso+1 mapa(xA-1,yA+1).xPadre=xA : mapa(xA-1,yA+1).yPadre=yA if xA-1=xF and yA+1=yF then encontrado=1 endif endif rem 7th if yA+1<7 if mapa(xA,yA+1).obstaculo<>1 and mapa(xA,yA+1).generacion=0 mapa(xA,yA+1).generacion=paso+1 mapa(xA,yA+1).xPadre=xA : mapa(xA,yA+1).yPadre=yA if xA=xF and yA+1=yF then encontrado=1 endif endif rem 8th if xA+1<7 and yA+1<7 if mapa(xA+1,yA+1).obstaculo<>1 and mapa(xA+1,yA+1).generacion=0 mapa(xA+1,yA+1).generacion=paso+1 mapa(xA+1,yA+1).xPadre=xA : mapa(xA+1,yA+1).yPadre=yA if xA+1=xF and yA+1=yF then encontrado=1 endif endif endif next y next x inc paso endwhile xA=xF : yA=yF rem Follow the path in inverse order while xA<>xP or yA<>yP x1=xA : y1=yA xA=mapa(x1,y1).xPadre yA=mapa(x1,y1).yPadre rem To draw a purple box on this square mapa(x1,y1).obstaculo=4 endwhile return rem Draw map dibujar_mapa: ink RGB(255,255,255),0 box 48,48,351,351 ink 0,0 box 49,49,350,350 set text size 20 for x=1 to 6 for y=1 to 6 ink RGB(0,120,0),0 if mapa(x,y).obstaculo=1 then ink RGB(255,0,0),0 box x*50,y*50,x*50+49,y*50+49 ink RGB(255,128,0),0 center text (x*50)+25,(y*50)+10,str$(mapa(x,y).xPadre)+" : "+str$(mapa(x,y).yPadre) if mapa(x,y).obstaculo=2 ink RGB(0,0,255),0 box x*50,y*50,x*50+49,y*50+49 ink RGB(255,128,0),0 text (x*50)+18,(y*50)+10,"P" endif if mapa(x,y).obstaculo=3 ink RGB(255,255,0),0 box x*50,y*50,x*50+49,y*50+49 ink RGB(255,128,0),0 text (x*50)+18,(y*50)+10,"F" endif if mapa(x,y).obstaculo=4 ink RGB(100,0,100),0 box x*50,y*50,x*50+49,y*50+49 ink RGB(255,128,0),0 center text (x*50)+25,(y*50)+10,str$(mapa(x,y).xPadre)+" : "+str$(mapa(x,y).yPadre) endif next y next x return rem Show text texto_informativo: set text size 20 text 5,5,"AMOEBA PATHFINDING" text 400,50,"Press Espace to continue." text 400,70,"Each square shows coords of its father." return |