⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aiplayer.c

📁 这是一个相当棒的Linux下的台球游戏
💻 C
📖 第 1 页 / 共 2 页
字号:
/* aiplayer.c****    code for positioning artifitial intelligence player**    Copyright (C) 2001  Florian Berger**    Email: harpin_floh@yahoo.de, florian.berger@jk.uni-linz.ac.at****    This program is free software; you can redistribute it and/or modify**    it under the terms of the GNU General Public License Version 2 as**    published by the Free Software Foundation;****    This program is distributed in the hope that it will be useful,**    but WITHOUT ANY WARRANTY; without even the implied warranty of**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the**    GNU General Public License for more details.****    You should have received a copy of the GNU General Public License**    along with this program; if not, write to the Free Software**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.***/#define AIPLAYER_C#include "aiplayer.h"#include "vmath.h"#include <stdlib.h>#include <stdio.h>#include <math.h>VMvect (*ai_get_stroke_dir)( BallsType * balls, BordersType * walls, struct Player *pplayer ) = ai_get_stroke_dir_8ball;static double ai_skill=1.0; /* not used ...yet */static double ai_err=0.0;void ai_set_skill( double skill ) /* not used ...yet */{    ai_skill=skill;}void ai_set_err( double err ){    ai_err=err;}static int my_rand(int nr){    return rand()%nr;}static double my_rand01(){    return (double)rand()/(double)RAND_MAX;}VMfloat stroke_angle( BallType * bcue, BallType * bhit, HoleType * hole ){    VMvect r_hit;    r_hit = vec_scale(vec_unit(vec_diff(bhit->r,hole->aim)),(bcue->d+bhit->d)/2.0);    r_hit = vec_add(bhit->r,r_hit);    return(           vec_angle(                     vec_diff(r_hit,bcue->r),                     vec_diff(hole->aim,r_hit)                    )          );}int ball_in_way( int ballnr, VMvect aim, BallsType * balls ){    VMvect way, iball;    VMfloat par, norm, lway;    int inway=0;    int i;    for(i=0;i<balls->nr;i++) if( balls->ball[i].in_game && i!=ballnr ){        way   = vec_diff(aim,balls->ball[ballnr].r);        lway  = vec_abs(way);        iball = vec_diff(balls->ball[i].r,balls->ball[ballnr].r);        par   = vec_mul(vec_unit(way),iball);        norm  = vec_abs(vec_cross(vec_unit(way),iball));        if( par>0.0 && par<lway && norm<(balls->ball[i].d+balls->ball[ballnr].d)/2.0 ){            DPRINTF("ball_in_way:%d (ballnr=%d)\n",i,ballnr);            inway=1; break;        }    }    return( inway );}int ind_ball_nr( int nr, BallsType * balls ){    int i;    for( i=0 ; i<balls->nr ; i++ ){        if( balls->ball[i].nr == nr ) break;    }    return i;}int nth_in_game( int n, BallsType * balls, int full_half ){    int i;    for( i=0 ; i<balls->nr && n>=0 ; i++ ){        if( full_half == BALL_FULL && balls->ball[i].nr<8 && balls->ball[i].nr>0 ){            n--;        }        if( full_half == BALL_HALF && balls->ball[i].nr>8 ){            n--;        }        if( full_half == BALL_ANY && ( balls->ball[i].nr>8 || (balls->ball[i].nr<8 && balls->ball[i].nr>0) )){            n--;        }    }    return i;}VMvect ai_get_stroke_dir_8ball( BallsType * balls, BordersType * walls, struct Player * pplayer ){#define full_half (pplayer->half_full)    VMvect r_hit;    VMfloat angle, minangle;    BallType *bhit, *bcue;    HoleType *hole;    int minball;    int minhole;    int i,j;    minball=0;    minhole=-1;    minangle=M_PI;    bcue = &balls->ball[0];    for( i=1; i<balls->nr; i++ ) if ( balls->ball[i].in_game ){        if( ( full_half==BALL_HALF && balls->ball[i].nr>8 ) ||            ( full_half==BALL_FULL && balls->ball[i].nr<8 ) ||            ( full_half==BALL_ANY  && balls->ball[i].nr!=8 ) ||            ( balls->ball[i].nr==8 && balls_in_game(balls,full_half)==0 ) ){            bhit = &balls->ball[i];            for( j=0; j<walls->holenr; j++ ){                hole = &walls->hole[j];                r_hit = vec_scale(vec_unit(vec_diff(bhit->r,hole->aim)),(bcue->d+bhit->d)/2.0);                r_hit = vec_add(bhit->r,r_hit);                if( !ball_in_way(0,r_hit,balls) &&                    !ball_in_way(i,hole->aim,balls) ){                    angle = fabs( vec_angle( vec_diff(r_hit,bcue->r), vec_diff(hole->aim,r_hit) ) );                    if( angle<minangle ){                        minball = i; minhole = j;  minangle = angle;                        DPRINTF("aiplayer: ball:%d hole:%d\n",balls->ball[i].nr,minhole);                    }                }            }        }    }    DPRINTF("aiplayer: 1\n");    if( minball==0 ){  /* no proper ball found */        minball=0;        if        (full_half==BALL_FULL){            if( BM_get_balls_out_full()!=7 ){                minball=1+my_rand(7-BM_get_balls_out_full());            }        } else if (full_half==BALL_HALF){            if( BM_get_balls_out_half()!=7 ){                minball=1+my_rand(7-BM_get_balls_out_half());            }        } else if (full_half==BALL_ANY){            if( BM_get_balls_out_total()!=15 ){                minball=1+my_rand(15-BM_get_balls_out_total());            }        }        if( minball==0 ){            minball = ind_ball_nr(8,balls);        } else {            minball = nth_in_game(minball,balls,full_half);        }    }    DPRINTF("aiplayer: 2\n");    bhit = &balls->ball[minball];    if(minhole!=-1){        hole = &walls->hole[minhole];        r_hit = vec_scale(vec_unit(vec_diff(bhit->r,hole->aim)),(bcue->d+bhit->d)/2.0);        r_hit = vec_diff(vec_add(bhit->r,r_hit),bcue->r);    } else {  /* no proper ball found */        DPRINTF("aiplayer: no proper ball found\n");        r_hit = vec_diff(bhit->r,bcue->r);    }    r_hit=vec_add(r_hit,vec_scale(vec_xyz(my_rand01()-0.5,my_rand01()-0.5,my_rand01()-0.5),0.02*ai_err/vec_abs(r_hit)));    return vec_unit(r_hit);#undef full_half}VMvect ai_get_stroke_dir_9ball( BallsType * balls, BordersType * walls, struct Player * pplayer ){    VMvect r_hit;    VMfloat angle, minangle;    BallType *bhit, *bcue;    HoleType *hole;    int minind,minnr;    int minhole;    int i,j;    minnr=15;    minind=0;    minhole=-1;    minangle=M_PI;    bcue = &balls->ball[0];    for( i=1; i<balls->nr; i++ ){        if ( balls->ball[i].in_game && balls->ball[i].nr<minnr ){            minnr = balls->ball[i].nr;            minind = i;        }    }    bhit = &balls->ball[minind];    for( j=0; j<walls->holenr; j++ ){        hole = &walls->hole[j];        r_hit = vec_scale(vec_unit(vec_diff(bhit->r,hole->aim)),(bcue->d+bhit->d)/2.0);        r_hit = vec_add(bhit->r,r_hit);        if( !ball_in_way(0,r_hit,balls) &&

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -