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

📄 class_character.php

📁 三國好好 子
💻 PHP
📖 第 1 页 / 共 4 页
字号:
			if ($ret==1){ $_SESSION['disp_msg'][] = $lang_fun["gai_wep"]; }
			} else {
    			$ret=$this->skillup("fight");
        		if ($ret==1){ $_SESSION['disp_msg'][]= $lang_fun["gai_att"]; }
    			$ret=$this->skillup("defence");
    			if ($ret==1){ $_SESSION['disp_msg'][]= $lang_fun["gai_def"]; }
			}
    	}
    }

    //FIXME: there should be a separate field in the database for characterrole, since right now it abuses the username
    function setRole($role){
        $this->username= $role;
    }

    function is_npc(){
        return strpos($this->user,'phaos_') === 0 ;
    }

    //TODO: ghosts, and flying creatures might be able to pass where others cannot
    function sql_may_pass() {

        //HACK
        //determine whether the npc is somewhere we he is not supposed to be and maybe walled in. Allow him to move to fix that.
        $condition_pass = "pass='y'";
		$result = mysql_query ("SELECT id, buildings, pass FROM phaos_locations WHERE id=$this->location AND $condition_pass");
        $position_ok = mysql_num_rows($result)>0;
        if(!$position_ok){
            return '1=1';
        }

        return $this->real_sql_may_pass();
    }

    //TODO: ghosts, and flying creatures might be able to pass where others cannot
    function real_sql_may_pass() {

        if( $this->is_npc() ) {
            return "( buildings='n' AND pass='y' )";
        }else{
            return "pass='y'";
        }
    }


    function relocate($direction){
        global $npc_dir_map;

        $condition_pass = $this->sql_may_pass();

		// move to new map location
        $npc_dir= $npc_dir_map[intval($direction)];

        $query= "SELECT `$npc_dir` FROM phaos_locations WHERE id=".$this->location;
        //defined('DEBUG') and DEBUG and $GLOBALS['debugmsgs'][]= " relocate querying: ".$query;
		$result = mysql_query ($query);
		if ( (@list($newloc)=mysql_fetch_array($result)) && $newloc ) {
			// get and set the new location for the NPC

			$result = mysql_query ("SELECT id, buildings, pass FROM phaos_locations WHERE id=$newloc AND $condition_pass");
                if ($row = mysql_fetch_array($result)) {
                    /*OLD v0.89
    				if ($row["buildings"]=="n" && $row["pass"]=='y') {
                    */
    					$this->location = $newloc;
                		// now we can update the DB
                        $query= "UPDATE phaos_characters SET
                			location	=".$this->location."
                			WHERE id	='".$this->id."'";
                		$result = mysql_query($query);
                		if (!$result) {showError(__FILE__,__LINE__,__FUNCTION__); exit;}
                        defined('DEBUG') and DEBUG and $GLOBALS['debugmsgs'][]= " moving $this->name($this->id) to $newloc";
                        return true;
                    /*OLD v0.89
    				}else{
                        defined('DEBUG') and DEBUG and $GLOBALS['debugmsgs'][]= " moving $this->name($this->id) to $newloc was <b>blocked</b>";
                    }
                    */
    			} else {
                    defined('DEBUG') and DEBUG and $GLOBALS['debugmsgs'][]= " moving $this->name($this->id) to $newloc was <b>blocked</b>";
    				//die("why could we not select data for the NPC's NEW location ($newloc/ID:".$row["id"].")?");
    			}
		}else{
            defined('DEBUG') and DEBUG and $GLOBALS['debugmsgs'][]= " no adjacent square in direction $npc_dir at $this->location";
        }
        return false;
    }

}// end class character

//the blueprints suck atm, but I try to make the better of it
class np_character_from_blueprint extends character {
	function np_character_from_blueprint($blueprint,$level=1,$username='phaos_npc'){
        $this->level= intval($level);
        if($level<0){
            $level= 1;
            echo "<p>bad level input for npc!</p>";
        }

		//define main vars
		$this->id= -1;
		$this->name=$blueprint["name"];
		$this->user= $username;
		$this->cclass=$blueprint["class"];
		$this->race=$blueprint["race"];
		$this->sex= rand(0,1)?'Female':'Male';
		$this->image=$blueprint["image_path"];
		$this->age=$this->level*$this->level;
		$this->location= 0;
		//define attribute vars
		$this->strength = (int)($blueprint["min_damage"]+3*($this->level-1));
    	$this->dexterity = (int)($blueprint["max_damage"]-$blueprint["min_damage"]+2*$this->level+2);
        $this->wisdom = (int)($blueprint["xp_given"]/2+$this->level);
        //define changeable vars( well except constitution )
        $this->hit_points = $blueprint["hit_points"]+rand(0,$this->level*3);
        $this->constitution = (int)(($this->hit_points+10)/6);
        $this->stamina_points= ($this->constitution+$this->strength)*5;
        $this->level = $this->level;

        //This are the most significant changes from 0.90
        $ac_left= fairInt( $blueprint['AC']*sqrt($this->level*0.25) );
        $this->xp   = fairInt( $blueprint["xp_given"]*(0.50+sqrt($this->level*0.25)) );
        $this->gold = fairInt( $blueprint["gold_given"]*(0.50+sqrt($this->level*0.25)) );

        $this->stat_points = 0;

        //skills
        $this->fight = 4+$this->level;
        $this->defence = (int)($blueprint['AC']/4+$this->level-1);
        $this->lockpick = 1+ (int)($this->wisdom/4);
        $this->traps = 1 + (int)($this->wisdom/2);

        //define equipment vars
        $this->weapon = 0;
        $this->armor = 0;
        $this->boots = 0;
        $this->shield = 0;
        $this->gloves = 0;
        $this->helm = 0;

        //FIXME: we need natural armor to clothe e.g. dragons
        //FIXME: armor class needs to be spent more evenly among armor types
        if($ac_left>0) {
            $armors= fetch_all("select id, armor_class from phaos_armor where armor_class<=$ac_left order by armor_class DESC LIMIT 1");
            if(count($armors)>0){
                $this->armor= $armors[0]['id'];
                $this->armor_ac= $armors[0]['armor_class'];
                $ac_left -= $this->armor_ac;
            }
        }

        if($ac_left>0) {
            $boots= fetch_all("select id, armor_class from phaos_boots where armor_class<=$ac_left order by armor_class DESC LIMIT 1");
            if(count($boots)>0){
                $this->boots= $boots[0]['id'];
                $this->boots_ac= $boots[0]['armor_class'];
                $ac_left -= $this->boots_ac;
            }
        }

        //fill weapon:
        $blueprint['avg_damage']= (int)(($blueprint["min_damage"]+$blueprint["max_damage"])*0.5);
        $weapons= fetch_all("select * from phaos_weapons where min_damage<=$blueprint[min_damage] and $blueprint[avg_damage]<= 2*max_damage order by RAND() LIMIT 1");
        if(count($weapons)>0){
            $this->weapon= $weapons[0]['id'];
            $this->weapon_min = $weapons[0]['min_damage'];
            $this->weapon_max = $weapons[0]['max_damage'];
            $this->weapon_name = $weapons[0]['name'];
        }else{
            $this->weapon_min = 0;
            $this->weapon_max = 0;
            $this->weapon_name = "natural weapon";
        }
        $this->weaponless = $blueprint['avg_damage']+ 2*(int)($this->level);

        //calculated stuff
        $this->available_points = $this->strength+$this->dexterity+$this->wisdom+$this->constitution;
        $this->max_hp = $this->constitution*6;
        $this->max_stamina = ($this->constitution+$this->strength)*10;
        $this->max_rep = 7;

        if($this->hit_points>$this->max_hp){
            $this->max_hp= $this->hit_points;
        }
        if($this->stamina_points>$this->max_stamina){
            $this->max_stamina= $this->stamina_points;
        }

        //other stuff
        $actTime=time();
        $this->regen_time = $actTime;
        $this->stamina_time = $actTime;
        $this->rep_time = $actTime;
        $this->no_regen_hp = $blueprint["hit_points"];
        //regeneration
        $this->time_since_regen = $actTime-$this->regen_time;
        $this->stamina_time_since_regen = $actTime-$this->stamina_time;
        $this->rep_time_since_regen = $actTime-$this->rep_time;
        //reputation
        $this->rep_points = rand(0,$this->level-1);
        $this->rep_helpfull = rand(0,$this->level-1);
        $this->rep_generious = rand(0,$this->level-1);
        $this->rep_combat = rand(0,$this->level-1);
        //weapon & fight Calculation

        $this->max_inventory=$this->strength*5;

    	if( !$this->image ) {
            $this->image = "images/monster/forest_troll.gif";
        }
    }

}

/**
* @param: none
* return: none
* purpose: generate new NPC/monster and add to database
*/
function npcgen() {
	$res = mysql_query ("SELECT * FROM phaos_opponents WHERE location='0' ORDER BY RAND() LIMIT 1") or die(mysql_error());
	if($blueprint = mysql_fetch_array($res)) {
        //create 50% level 1 characters, and not more than 37,5% characters with level>3
        $level= 1+(int)(rand(0,1)*(pow(1+rand(0,10)*rand(0,10)*0.01,4)+rand(0,99)*0.01));
        $npc= new np_character_from_blueprint($blueprint, $level);

        $condition_passable= $npc->real_sql_may_pass();

        //TODO: add generator regions/locations feature to phaos

        $tries= 10;
        while($tries-->0){
            $res= null;
            //FIXME: this actually should depend on the area covered by dungeons
            //20050717
            //Wilderness    14277
            //Woodlands 	1891
            //Dungeon       675
    		if( !@$res && rand(0,99)<4 ) {
                $location= 'Rune Gate%';
                $sql = "SELECT id FROM phaos_locations WHERE (name LIKE 'Rune Gate%' OR name LIKE 'Dungeon') AND $condition_passable ORDER BY RAND() LIMIT 1";                  
                //defined('DEBUG') and DEBUG and $GLOBALS['debugmsgs'][]= __FUNCTION__.": sql: $sql";
                $res = mysql_query ($sql) or die(mysql_error());
            }
            if( !@$res) {
                $location= 'Wilderness';
                $sql = "SELECT id FROM phaos_locations WHERE (name LIKE 'Wilderness' OR name LIKE 'Woodlands' OR name LIKE 'Rune Gate%' OR name LIKE 'Dungeon') AND $condition_passable ORDER BY RAND() LIMIT 1";
                //defined('DEBUG') and DEBUG and $GLOBALS['debugmsgs'][]= __FUNCTION__.": sql: $sql";
                $res = mysql_query ($sql) or die(mysql_error());
            }
    		list($locationid) = mysql_fetch_array($res);

            //check whether location is crowded
            $res = mysql_query ("SELECT count(*) FROM phaos_characters WHERE location='$locationid' AND username='phaos_npc'") or die(mysql_error());
            list($count)= mysql_fetch_array($res);
            if($count>$level+1) {
                defined('DEBUG') and DEBUG and $GLOBALS['debugmsgs'][]= " location $locationid is <b>crowded</b>, not placing here ($count npcs)";
                //trying to fix
                $res = mysql_query ("SELECT id FROM phaos_characters WHERE location='$locationid' AND username='phaos_npc'") or die(mysql_error());
                while(list($id)= mysql_fetch_array($res)){
                    $crowd= new character($id);
                    $crowd->relocate( (int)rand(1,8) );
                }
            }else{
                break;//stop while loop
            }
        }

	} else {
		die("cant find valid mob in DB: ".mysql_error());
	}
    $npc->place($locationid);
	DEBUG and  $_SESSION['disp_msg'][] = "**DEBUG: $npc->name($npc->level) generated at location $location $locationid";
	return 1;
}

function refsidebar() {
	//REFRESH SIDEBAR INFO
	?>
	<script language="JavaScript">
	<!--
	parent.side_bar.location.reload();
	//-->
	</script>
	<?
}

function diceroll(){
	return rand(1,15)+rand(1,15);
	// odds, rolling 2 15-side dice
	//   2=0.4%      3=0.9%      4=1.3%      5=1.8%      6=2.2%      7=2.7%      8=3.1%      9=3.5%     10=4.0%     11=4.5%
	//  12=4.9%     13=5.4%     14=5.7%     15=6.2%     16=6.6%     17=6.1%     18=5.8%     19=5.4%     20=4.9%     21=4.4%
	//  22=4.0%     23=3.6%     24=3.1%     25=2.7%     26=2.2%     27=1.8%     28=1.3%     29=0.9%     30=0.4%
}

function fairInt($f){
    return (int)($f+rand(0,99)*0.01);
}

function getclan_sig($plname){
    $result = mysql_query ("SELECT * FROM phaos_clan_in WHERE clanmember = '$plname'");
    if ($row = mysql_fetch_array($result)) {
    	$mem_clan = $row["clanname"];
    } else {return false;}

    $result = mysql_query ("SELECT * FROM phaos_clan_admin WHERE clanname = '$mem_clan'");
    if ($row = mysql_fetch_array($result)) {
    	$clan_name = $row["clanname"];
    	$clan_sig = $row["clansig"];
    }
    if ($clan_sig !== "no" or $clan_sig !== ""){
    ?>
    <img src="<? echo $clan_sig; ?>" width="32" height="32" alt="<? echo $clan_name; ?>">
    <?
    }
}// end function getclan_sig

?>

⌨️ 快捷键说明

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