entity.php

来自「一个基于web的rpg游戏源代码」· PHP 代码 · 共 183 行

PHP
183
字号
<?php/*** Netlands World Server is the coordinator of the VR in the Netlands Project* Copyright (C) 2002 Ricard Pillosu* * This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License* as published by the Free Software Foundation; either version 2* of the License, or (at your option) any later version.* * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*//* vim: set expandtab tabstop=4 shiftwidth=4 *//*** Get info on some entity** Input: <get_entity me="x" id="x" [filter="x,x,x,..."]/>* Ouput: <get_entity to="x" id="x" .../>** @param connection object  Reference to connection* @param user       object  Reference to user* @param character  object  Reference to character* @param command    array   Contains command received*/function command_get_entity(&$connection, &$user, &$character, $command){    global $entities;    // TODO    // From now all players can ask about any entity. This logically is bad.    // We have to put a lot of filters here, since you cannot get info    // about an object far from you ...    // In short, we would have to check if $character can perceive the data    // from one of his senses    // Search id    try {        $entity = &$entities->get_entity($command["id"]);    } catch(exception $exception) {        $answer = array(            "ok"    => "0",            "id"    => $command["id"],            "msg"   => $exception->get_msg());        return($answer);    }    // Fill $answer with all relevant entity variables    $answer = $entity->get_game_vars();    $answer["parent_id"] = $entity->parent->get_key();    $answer["type"] = $entities->get_type_name($entity->type);    // filter if we need to    if(!empty($command["filter"])) {        $keys_to_filter = preg_split("/,/", $command["filter"]);           $keys = array_keys($answer);        $valid_keys = array_intersect($keys, $keys_to_filter);        foreach($answer as $k => $v) {            if(in_array($k, $valid_keys) == FALSE) {                unset($answer[$k]);            }        }    }    // We always return these    $answer["to"] = $character->get_key();    $answer["id"] = $entity->get_key();    return($answer);}/*** Get all entities included in id's entity ** Input: <get_childs me="x" id="x" [type="x"]/>* Ouput: <get_childs to="x" id="x" child_id="x"/>[...]** @param connection object  Reference to connection* @param user       object  Reference to user* @param character  object  Reference to character* @param command    array   Contains command received*/function command_get_childs(&$connection, &$user, &$character, $command){    global $entities;    // TODO    // As get_entity ... can the character perceive this data?    // Search id    try {        $entity = &$entities->get_entity($command["id"]);        if(!empty($command["type"])) {            $filter_type = TRUE;            $f_type = $entities->get_type_from_name($command["type"]);        } else {            $filter_type = FALSE;        }    } catch(exception $exception) {        $answer = array(            "ok"    => "0",            "id"    => $command["id"],            "msg"   => $exception->get_msg());        return($answer);    }    // Fill $answer with all relevant entity variables    foreach (array_keys($entity->childs) as $key) {        // Fill $answer with all relevant entity variables        $t_entity = & $entity->childs["$key"];        $t_answer = $t_entity->get_game_vars();        $t_answer["xml_name"] = "get_entity";        $t_answer["to"] = $character->get_key();        $t_answer["id"] = $t_entity->get_key();        $t_answer["parent_id"] = $t_entity->parent->get_key();        $t_answer["type"] = $entities->get_type_name($t_entity->type);        // filter by type        if($filter_type == TRUE) {            if($t_entity->type != $f_type) {                continue;            }        }        $answer[] = $t_answer;    }    // Fix top name of the XML    $answer[0]["_xml_name"] = "get_childs";    // No answer if no childs exist :)    if(isset($answer)) {        return($answer);    }    return(array());}/*** Return in wich entity [id] is included** Input: <get_parent me="x" id="x" />* Ouput: <get_parent to="x" id="x" parent_id="x"/>* Maybe this command will be erased in the future, since get_entity can * replace it** @param connection object  Reference to connection* @param user       object  Reference to user* @param character  object  Reference to character* @param command    array   Contains command received*/function command_get_parent(&$connection, &$user, &$character, $command){    global $entities;    // TODO    // As get_entity ... can the character perceive this data?    // Search id    try {        $entity = &$entities->get_entity($command["id"]);    } catch(exception $exception) {        $answer = array(            "ok"    => "0",            "id"    => $command["id"],            "msg"   => $exception->get_msg());        return($answer);    }    //    // Fill $answer with all parent_id    $answer = array (        "to" => $character->get_key(),        "id" => $entity->get_key(),        "parent_id" => $entity->parent->get_key());    return($answer);}?>

⌨️ 快捷键说明

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