Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11-23-2016, 10:40 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
GS2 3D Raycaster

There are a few things that need to be fixed/improved, I never fully finished sprite rendering (it sort of works lol).

Feel free to build on this, or play around with it etc.



I had some floor rendering, but it was extremely laggy as I couldn't access the image buffer directly.


PHP Code:
function onCreated(){
  
findplayer("Gund").addweapon(this);  //Change to your name
}
//#CLIENTSIDE
function onCreated(){
  
this.maxlooplimit 2000000;
  
sleep(1);
  
this.map = {
    {
4,4,4,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4},
    {
1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
3,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
2,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,2,3,2,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,3,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,3,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,3,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,4,0,0,0,0,0,0,4,1,0,0,0,0,0,0,0,4,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,2,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,2,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,1,1,1,1,1,2,3,4,5,6,7,8,1,1,1,1,1,1,1,1,1,1,1,1},
  };
  
this.sprites = {
    {
"bomb.png"13},
    {
"door.png"31},
  };
  
this.texturewidth 64;
  
this.700;
  
this.zbuffer = new[this.w];
  
this.500;
  
this.off = {screenwidth/this.w/2screenheight/this.h/2};
  
this.off = {7070};
  
this.mapwidth this.map[0].size();
  
this.mapheight this.map.size();
  
this.posx 4;
  
this.posy 3;
  
this.dirx = -1;
  
this.diry 0;
  
this.planex 0.221;
  
this.planey 0.5;
  
this.time 0;
  
this.steps 0;
  
this.smode = -4;
  
this.wallcheck 8;
  
temp.rotspeed 0.08;
  
this.ntrig = {sin(temp.rotspeed), cos(temp.rotspeed)};
  
this.trig = {sin(-temp.rotspeed), cos(-temp.rotspeed)};
  if(
isobject("3D_R_GUN")){
    (@
"3D_R_GUN").destroy();
  }
  if(
isobject("Gunderak_3D")){
    (@
"Gunderak_3D").destroy();
  }
  
//return;
  
new GuiDrawingPanel("Gunderak_3D"){
    
useownprofile true;
    
profile.fontcolor "#FFFFFF";
    
width thiso.w;
    
height thiso.h;
    
thiso.off[0];
    
thiso.off[1];
    
clearall();
  }
  
onTimeout();
}
function 
sqrt(n) { 
  return 
n^0.5
}
function 
onTimeout(){
  
Gunderak_3D.drawimagestretched(00this.wthis.h/2"gunderak_sky.png"0011);
  
Gunderak_3D.drawimagestretched(0this.h/2this.wthis.h/2"gunderak_floor.png"0011);
  for(
temp.0temp.this.wtemp.++){
    
temp.camerax temp.x/this.-1;
    
temp.rayposx this.posx;
    
temp.rayposy this.posy;
    
temp.raydirx this.dirx this.planex temp.camerax;
    
temp.raydiry this.diry this.planey temp.camerax;
    
temp.mapx int(temp.rayposx);
    
temp.mapy int(temp.rayposy);
    
temp.sidedistx 0;
    
temp.sidedisty 0;
    
temp.deltadistx sqrt(+ (temp.raydiry temp.raydiry)/(temp.raydirx temp.raydirx));
    
temp.deltadisty sqrt(+ (temp.raydirx temp.raydirx)/(temp.raydiry temp.raydiry));
    
temp.perpwalldist 0;
    
temp.stepx 0;
    
temp.stepy 0;
    
temp.hit 0;
    
temp.side 0;
    if(
temp.raydirx 0){
      
temp.stepx = -1;
      
temp.sidedistx = (temp.rayposx temp.mapx)*temp.deltadistx;
    }else{
      
temp.stepx 1;
      
temp.sidedistx = (temp.mapx temp.rayposx) * temp.deltadistx;
    }
    if(
temp.raydiry 0){
      
temp.stepy = -1;
      
temp.sidedisty = (temp.rayposy temp.mapy) * temp.deltadisty;
    }else{
      
temp.stepy 1;
      
temp.sidedisty = (temp.mapy temp.rayposy) * temp.deltadisty;
    }
    
temp.count 0;
    while(
temp.hit == 0){
      
temp.count ++;
      if(
temp.sidedistx temp.sidedisty){
        
temp.sidedistx += temp.deltadistx;
        
temp.mapx += temp.stepx;
        
temp.side 0;
      }else{
        
temp.sidedisty += temp.deltadisty;
        
temp.mapy += temp.stepy;
        
temp.side 1;
      }
      if(
this.map[temp.mapx][temp.mapy] > 0){
        
temp.hit 1;
      }
    }
    if(
temp.side == 0){
      
temp.perpwalldist abs((temp.mapx temp.rayposx + (temp.stepx)/2)/temp.raydirx);
    }else{
      
temp.perpwalldist abs((temp.mapy temp.rayposy + (temp.stepy)/2)/temp.raydiry);
    }
    
temp.lineheight int(this.h/temp.perpwalldist);
    
temp.drawstart = (-temp.lineheight/2) + (this.h/2);
    
temp.drawend = (temp.lineheight/2)  + (this.h/2);
    
temp.this.map[temp.mapx][temp.mapy]-1;
    
temp.wallx 0;
    if(
temp.side == 0){
      
temp.wallx temp.rayposy temp.perpwalldist temp.raydiry;
    }else{
      
temp.wallx temp.rayposx temp.perpwalldist temp.raydirx;
    }
    
temp.wallx -= int(temp.wallx);
    
temp.texx int(temp.wallx *  this.texturewidth);
    if(
temp.side == && temp.raydirx 0){
      
temp.texx this.texturewidth temp.texx 1;
    }
    if(
temp.side == && temp.raydiry 0){
      
temp.texx this.texturewidth temp.texx 1;
    }
    
temp.texx this.texturewidth temp.texx 1;
    
temp.= {111};
    if(
temp.side == 1){
      
temp.= {0.650.650.65};
    }
    
temp.temp.drawend temp.drawstart;
    
Gunderak_3D.drawimagestretched(temp.xtemp.drawstart1temp.h"wolftextures.png"temp.texx+(thiso.texturewidth*temp.t), 01this.texturewidth);
    
this.zbuffer[temp.x] = temp.perpwalldist;
  }
    for(
temp.sprite this.sprites){
      
temp.spi temp.sprite[0];
      
temp.spx temp.sprite[1] - this.posx;
      
temp.spy temp.sprite[2] - this.posy;
      
temp.invdet 1/(this.planex this.diry this.dirx this.planey);
      
temp.transformx temp.invdet * (this.diry temp.spx this.dirx temp.spy);
      
temp.transformy temp.invdet * (-this.planey temp.spx this.planex temp.spy);
      
temp.spritescreenx int((this.w/2) * (temp.transformx temp.transformy));
      
temp.spriteheight abs(int(this.temp.transformy));
      
temp.drawstarty = -temp.spriteheight/this.h/2;
      if(
temp.drawstarty 0){
        
temp.drawstarty 0;
      }
      
temp.drawendy temp.spriteheight/this.h/2;
      if(
temp.drawendy >= this.h){
        
temp.drawendy this.-1;
      }
      
temp.spritewidth abs(int(this.h/temp.transformy));
      
temp.drawstartx = -temp.spritewidth/temp.spritescreenx;
      if(
temp.drawstartx 0){
        
temp.drawstartx 0;
      }
      
temp.drawendx temp.spritewidth/temp.spritescreenx;
      if(
temp.drawendx >= this.w){
        
temp.drawendx this.-1;
      }
      for(
temp.tx temp.drawstartxtemp.tx temp.drawendxtemp.tx ++){
        
temp.texx int(temp.tx - (-temp.spritewidth/temp.spritescreenx) * 32 temp.spritewidth);
        
temp.texx 0;
        
//Buggggs ugh
        
if(temp.transformy && temp.tx && temp.tx this.&& temp.transformy this.zbuffer[temp.tx]){
          
temp.sh temp.drawendy temp.drawstarty;
          
Gunderak_3D.drawimagestretched(temp.txtemp.drawstarty1temp.shtemp.spitemp.texx0132);
        }
      }
    }
  
temp.ms 0.1;
  if(
keydown(1)){
    
temp.olddirx this.dirx;
    
this.dirx this.dirx this.ntrig[1] - this.diry this.ntrig[0];
    
this.diry temp.olddirx this.ntrig[0] + this.diry this.ntrig[1];
    
temp.oldplanex this.planex;
    
this.planex this.planex this.ntrig[1] - this.planey this.ntrig[0];
    
this.planey temp.oldplanex this.ntrig[0] + this.planey this.ntrig[1];
  }
  if(
keydown(3)){
    
temp.olddirx this.dirx;
    
this.dirx this.dirx this.trig[1] - this.diry this.trig[0];
    
this.diry temp.olddirx this.trig[0] + this.diry this.trig[1];
    
temp.oldplanex this.planex;
    
this.planex this.planex this.trig[1] - this.planey this.trig[0];
    
this.planey temp.oldplanex this.trig[0] + this.planey this.trig[1];
  }
  if(
keydown(2)){
    
this.steps += this.smode;
    if(
this.map[int(this.posx this.dirx temp.ms*this.wallcheck)][int(this.posy)] == 0){
       
this.posx -= this.dirx temp.ms;
    }
    if(
this.map[int(this.posx)][int(this.posy this.diry temp.ms*this.wallcheck)] == 0){
      
this.posy -= this.diry temp.ms;
    }
  }
  if(
keydown(0)){
    
this.steps += this.smode;
    if(
this.map[int(this.posx this.dirx temp.ms*this.wallcheck)][int(this.posy)] == 0){
       
this.posx += this.dirx temp.ms;
    }
    if(
this.map[int(this.posx)][int(this.posy this.diry temp.ms*this.wallcheck)] == 0){
      
this.posy += this.diry temp.ms;
    }
  }
  if(
this.steps 10){
    
this.smode = -4;
  }
  if(
this.steps 1){
    
this.smode 4;
  }
  
temp.so int(this.steps);
  new 
GuiBitmapCtrl("3D_R_GUN") {
    
thiso.off[0] + thiso.w/35;
    
thiso.off[1] + thiso.64;
    
bitmaprectangle = {0,0,81,92 thiso.steps};
    
bitmap "gunderak_3d_gun.png";
    
thiso.catchevent(this"onMouseDown""onFireGun");
  }
  
settimer(0.05);
}
function 
onFireGun(){
  
//player.chat = "bang!";

Attached Thumbnails
Click image for larger version

Name:	wolftextures.png
Views:	259
Size:	25.2 KB
ID:	55967  
Attached Images
   
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #2  
Old 11-23-2016, 10:42 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Hit the character limit on that post, I used this tutorial: http://lodev.org/cgtutor/raycasting.html
It was written in C++, and I ported it to GS2.

There are two attachments named gunderak_sky.png and gunderak_floor.png, they are just 1x1 images, blue and gray respectively.
Best of luck clicking them.

My desire is to have people learn from this, and possibly build some minigames in GS2! As I don't have the time anymore to do this.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #3  
Old 11-23-2016, 01:06 PM
xXziroXx xXziroXx is offline
Master of Puppets
xXziroXx's Avatar
Join Date: May 2004
Location: Sweden
Posts: 5,288
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Send a message via AIM to xXziroXx Send a message via MSN to xXziroXx
Nice work, but a few things that would optimize it and probably make it run a lot smoother:

- You don't seem to be clearing the drawing panel each cycle? Means you're drawing an insane amount of stuff in the drawing panel that should drop fps more and more the longer you run it.

- Instead of redrawing the world every 0.05 cycle, check if the player has moved or tilted his viewpoint and only redraw the world if required. Would increase fps by loads!


I bet if you did both of the above, drawing floors wouldn't be that laggy either.
__________________

"A delayed game is eventually good, but a rushed game is forever bad." - Shigeru Miyamoto
Reply With Quote
  #4  
Old 11-23-2016, 07:19 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Quote:
Originally Posted by xXziroXx View Post
- You don't seem to be clearing the drawing panel each cycle? Means you're drawing an insane amount of stuff in the drawing panel that should drop fps more and more the longer you run it.
Isn't the drawing panel simply a buffer? If it is, not clearing it is the preferred method, as clearing a buffer is costly. Since you're filling it entirely again anyway, you don't have to clear it.
__________________
Reply With Quote
  #5  
Old 11-23-2016, 07:38 PM
xXziroXx xXziroXx is offline
Master of Puppets
xXziroXx's Avatar
Join Date: May 2004
Location: Sweden
Posts: 5,288
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Send a message via AIM to xXziroXx Send a message via MSN to xXziroXx
Quote:
Originally Posted by Crow View Post
Isn't the drawing panel simply a buffer?
I have no idea to be honest.
__________________

"A delayed game is eventually good, but a rushed game is forever bad." - Shigeru Miyamoto
Reply With Quote
  #6  
Old 11-23-2016, 11:32 PM
Crono Crono is offline
:pluffy:
Join Date: Feb 2002
Location: Sweden
Posts: 20,000
Crono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond repute
doom graal?
__________________
Reply With Quote
  #7  
Old 11-23-2016, 11:49 PM
PlanetOscar PlanetOscar is offline
Registered User
Join Date: Aug 2015
Location: Sweden
Posts: 177
PlanetOscar is just really nicePlanetOscar is just really nice
next rush b cyka blyat game?
Reply With Quote
  #8  
Old 11-24-2016, 01:29 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Quote:
Originally Posted by Crono View Post
doom graal?
If I had more spare time maybe!
This is the second time you've mentioned that idea haha
Reply With Quote
  #9  
Old 11-24-2016, 02:32 PM
Crono Crono is offline
:pluffy:
Join Date: Feb 2002
Location: Sweden
Posts: 20,000
Crono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond repute
ok graalenstein 3d?
__________________
Reply With Quote
  #10  
Old 11-24-2016, 06:05 PM
Kamaeru Kamaeru is offline
G2k1
Kamaeru's Avatar
Join Date: Dec 2001
Posts: 1,040
Kamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud of
It's really a shame Graal's frame rate is capped at 20fps.

For comparison many Atari, NES, SNES games, etc run at 60fps.
__________________
3DS friendcode: 1118-0226-7975
Reply With Quote
  #11  
Old 11-24-2016, 09:28 PM
Crono Crono is offline
:pluffy:
Join Date: Feb 2002
Location: Sweden
Posts: 20,000
Crono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond repute
Quote:
Originally Posted by Kamaeru View Post
It's really a shame Graal's frame rate is capped at 20fps.

For comparison many Atari, NES, SNES games, etc run at 60fps.
so do new consol- oh wait they dont.
__________________
Reply With Quote
  #12  
Old 11-25-2016, 07:38 AM
LimetasticSlushy LimetasticSlushy is offline
Registered User
Join Date: Jun 2016
Posts: 98
LimetasticSlushy has a spectacular aura aboutLimetasticSlushy has a spectacular aura about
Quote:
Originally Posted by PlanetOscar View Post
next rush b cyka blyat game?
If someone could figure out how to make Counter-Strike in Graal, I'd certainly be impressed.
Reply With Quote
  #13  
Old 11-25-2016, 10:43 AM
PlanetOscar PlanetOscar is offline
Registered User
Join Date: Aug 2015
Location: Sweden
Posts: 177
PlanetOscar is just really nicePlanetOscar is just really nice
Quote:
Originally Posted by Kamaeru View Post
It's really a shame Graal's frame rate is capped at 20fps.

For comparison many Atari, NES, SNES games, etc run at 60fps.
Alpho was (and maybe still is) working on something that would enable him to do something in 60 fps
Reply With Quote
  #14  
Old 11-25-2016, 05:17 PM
Kamaeru Kamaeru is offline
G2k1
Kamaeru's Avatar
Join Date: Dec 2001
Posts: 1,040
Kamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud of
Quote:
Originally Posted by PlanetOscar View Post
Alpho was (and maybe still is) working on something that would enable him to do something in 60 fps
What's even worse is that npc-server seems to only operate at 10fps. lol
__________________
3DS friendcode: 1118-0226-7975
Reply With Quote
  #15  
Old 11-25-2016, 06:15 PM
Crono Crono is offline
:pluffy:
Join Date: Feb 2002
Location: Sweden
Posts: 20,000
Crono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond repute
Quote:
Originally Posted by Kamaeru View Post
What's even worse is that npc-server seems to only operate at 10fps. lol
the other 10 is reserved for flying technology
__________________
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 09:36 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.