--- deliantra/server/socket/request.C 2006/12/19 04:58:05 1.45 +++ deliantra/server/socket/request.C 2007/01/19 17:50:11 1.63 @@ -1,6 +1,7 @@ /* CrossFire, A Multiplayer game for X-windows + Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team Copyright (C) 2001 Mark Wedel Copyright (C) 1992 Frank Tore Johansen @@ -140,22 +141,19 @@ static void clear_map (player *pl) { - client &socket = *pl->socket; + memset (&pl->ns->lastmap, 0, sizeof (pl->ns->lastmap)); - memset (&socket.lastmap, 0, sizeof (socket.lastmap)); + if (pl->ns->newmapcmd == 1) + pl->ns->send_packet ("newmap"); - if (socket.newmapcmd == 1) - socket.send_packet ("newmap"); - - socket.update_look = 1; - socket.look_position = 0; + pl->ns->floorbox_reset (); } /** check for map change and send new map data */ static void check_map_change (player *pl) { - client &socket = *pl->socket; + client &socket = *pl->ns; object *ob = pl->ob; char buf[MAX_BUF]; /* eauugggh */ @@ -181,7 +179,7 @@ flags |= 8; snprintf (buf, MAX_BUF, "mapinfo - spatial %d %d %d %d %d %s", - flags, socket.mapx / 2 - ob->x, socket.mapy / 2 - ob->y, ob->map->width, ob->map->height, ob->map->path); + flags, socket.mapx / 2 - ob->x, socket.mapy / 2 - ob->y, ob->map->width, ob->map->height, &ob->map->path); } else snprintf (buf, MAX_BUF, "mapinfo current"); @@ -199,8 +197,7 @@ else { socket_map_scroll (&socket, ob->x - socket.current_x, ob->y - socket.current_y); - socket.update_look = 1; - socket.look_position = 0; + socket.floorbox_reset (); } } @@ -286,8 +283,8 @@ // initial map and its origin maptile *map = pl->ob->map; sint16 dx, dy; - int mapx = pl->socket->mapx / 2 - pl->ob->x; - int mapy = pl->socket->mapy / 2 - pl->ob->y; + int mapx = pl->ns->mapx / 2 - pl->ob->x; + int mapy = pl->ns->mapy / 2 - pl->ob->y; int max_distance = 8; // limit maximum path length to something generous while (*buf && map && max_distance) @@ -296,30 +293,30 @@ switch (dir) { - case '1': - dx = 0; - dy = -1; - map = get_map_from_coord (map, &dx, &dy); - map && (mapy -= map->height); - break; - case '2': - mapx += map->width; - dx = map->width; - dy = 0; - map = get_map_from_coord (map, &dx, &dy); - break; - case '3': - mapy += map->height; - dx = 0; - dy = map->height; - map = get_map_from_coord (map, &dx, &dy); - break; - case '4': - dx = -1; - dy = 0; - map = get_map_from_coord (map, &dx, &dy); - map && (mapx -= map->width); - break; + case '1': + dx = 0; + dy = -1; + map = map->xy_find (dx, dy); + map && (mapy -= map->height); + break; + case '2': + mapx += map->width; + dx = map->width; + dy = 0; + map = map->xy_find (dx, dy); + break; + case '3': + mapy += map->height; + dx = 0; + dy = map->height; + map = map->xy_find (dx, dy); + break; + case '4': + dx = -1; + dy = 0; + map = map->xy_find (dx, dy); + map && (mapx -= map->width); + break; } --max_distance; @@ -340,7 +337,7 @@ if (map->tile_path[3]) flags |= 8; - snprintf (bigbuf, MAX_BUF, "mapinfo %s spatial %d %d %d %d %d %s", token, flags, mapx, mapy, map->width, map->height, map->path); + snprintf (bigbuf, MAX_BUF, "mapinfo %s spatial %d %d %d %d %d %s", token, flags, mapx, mapy, map->width, map->height, &map->path); } else snprintf (bigbuf, MAX_BUF, "mapinfo %s nomap", token); @@ -348,7 +345,7 @@ else snprintf (bigbuf, MAX_BUF, "mapinfo %s unsupported", token); - pl->socket->send_packet (bigbuf); + pl->ns->send_packet (bigbuf); } /** This is the Setup cmd - easy first implementation */ @@ -405,10 +402,7 @@ safe_strcat (cmdback, param, &slen, HUGE_BUF); } else if (!strcmp (cmd, "exp64")) - { - ns->exp64 = atoi (param); - safe_strcat (cmdback, param, &slen, HUGE_BUF); - } + safe_strcat (cmdback, param, &slen, HUGE_BUF); else if (!strcmp (cmd, "spellmon")) { ns->monitor_spells = atoi (param); @@ -575,10 +569,7 @@ void AddMeCmd (char *buf, int len, client * ns) { - if (ns->status != Ns_Add || add_player (ns)) - ns->send_packet ("addme_failed"); - else - ns->send_packet ("addme_success"); + INVOKE_CLIENT (ADDME, ns, ARG_DATA (buf, len)); } /** Reply to ExtendedInfos command */ @@ -725,10 +716,9 @@ ns->faces_sent[face] |= NS_FACESENT_SMOOTH; - packet sl; + packet sl ("smooth"); - sl << "smooth " - << uint16 (face) + sl << uint16 (face) << uint16 (smoothface); ns->send_packet (sl); @@ -815,16 +805,27 @@ : time = (int) (MAX_TIME / FABS (pl->ob->speed)); /* Send confirmation of command execution now */ - packet sl; - sl << "comc " << uint16 (cmdid) << uint32 (time); - pl->socket->send_packet (sl); + packet sl ("comc"); + sl << uint16 (cmdid) << uint32 (time); + pl->ns->send_packet (sl); } - /** This is a reply to a previous query. */ void -ReplyCmd (char *buf, int len, player *pl) +ReplyCmd (char *buf, int len, client *ns) { + if (ns->state == ST_CUSTOM) + { + INVOKE_CLIENT (REPLY, ns, ARG_DATA (buf, len)); + return; + } + + if (!ns->pl) + return; //TODO: depends on the exact reply we are after + //TODO: but right now, we always have a ns->pl + + player *pl = ns->pl; + /* This is to synthesize how the data would be stored if it * was normally entered. A bit of a hack, and should be cleaned up * once all the X11 code is removed from the server. @@ -837,52 +838,22 @@ /* this avoids any hacking here */ - switch (pl->state) + switch (ns->state) { case ST_PLAYING: LOG (llevError, "Got reply message with ST_PLAYING input state\n"); break; - case ST_PLAY_AGAIN: - /* We can check this for return value (2==quit). Maybe we - * should, and do something appropriate? - */ - receive_play_again (pl->ob, buf[0]); - break; - - case ST_ROLL_STAT: - key_roll_stat (pl->ob, buf[0]); - break; - case ST_CHANGE_CLASS: - key_change_class (pl->ob, buf[0]); break; - case ST_CONFIRM_QUIT: - key_confirm_quit (pl->ob, buf[0]); - break; - - case ST_CONFIGURE: - LOG (llevError, "In client input handling, but into configure state\n"); - pl->state = ST_PLAYING; - break; - - case ST_GET_NAME: - receive_player_name (pl->ob, 13); - break; - - case ST_GET_PASSWORD: - case ST_CONFIRM_PASSWORD: - receive_player_password (pl->ob, 13); - break; - case ST_GET_PARTY_PASSWORD: /* Get password for party */ receive_party_password (pl->ob, 13); break; default: - LOG (llevError, "Unknown input state: %d\n", pl->state); + LOG (llevError, "Unknown input state: %d\n", ns->state); } } @@ -942,7 +913,6 @@ } /** client wants the map resent */ - void MapRedrawCmd (char *buf, int len, player *pl) { @@ -1039,53 +1009,54 @@ char buf[MAX_BUF]; uint16 flags; - packet sl; - sl << "stats "; + client *ns = pl->ns; + if (!ns) + return; - if (pl->ob) - { - AddIfShort (pl->last_stats.hp, pl->ob->stats.hp, CS_STAT_HP); - AddIfShort (pl->last_stats.maxhp, pl->ob->stats.maxhp, CS_STAT_MAXHP); - AddIfShort (pl->last_stats.sp, pl->ob->stats.sp, CS_STAT_SP); - AddIfShort (pl->last_stats.maxsp, pl->ob->stats.maxsp, CS_STAT_MAXSP); - AddIfShort (pl->last_stats.grace, pl->ob->stats.grace, CS_STAT_GRACE); - AddIfShort (pl->last_stats.maxgrace, pl->ob->stats.maxgrace, CS_STAT_MAXGRACE); - AddIfShort (pl->last_stats.Str, pl->ob->stats.Str, CS_STAT_STR); - AddIfShort (pl->last_stats.Int, pl->ob->stats.Int, CS_STAT_INT); - AddIfShort (pl->last_stats.Pow, pl->ob->stats.Pow, CS_STAT_POW); - AddIfShort (pl->last_stats.Wis, pl->ob->stats.Wis, CS_STAT_WIS); - AddIfShort (pl->last_stats.Dex, pl->ob->stats.Dex, CS_STAT_DEX); - AddIfShort (pl->last_stats.Con, pl->ob->stats.Con, CS_STAT_CON); - AddIfShort (pl->last_stats.Cha, pl->ob->stats.Cha, CS_STAT_CHA); - } - - if (pl->socket->exp64) - for (int s = 0; s < NUM_SKILLS; s++) - if (pl->last_skill_ob[s] && pl->last_skill_exp[s] != pl->last_skill_ob[s]->stats.exp) + object *ob = pl->ob; + if (!ob) + return; + + packet sl ("stats"); + + AddIfShort (ns->last_stats.hp, ob->stats.hp, CS_STAT_HP); + AddIfShort (ns->last_stats.maxhp, ob->stats.maxhp, CS_STAT_MAXHP); + AddIfShort (ns->last_stats.sp, ob->stats.sp, CS_STAT_SP); + AddIfShort (ns->last_stats.maxsp, ob->stats.maxsp, CS_STAT_MAXSP); + AddIfShort (ns->last_stats.grace, ob->stats.grace, CS_STAT_GRACE); + AddIfShort (ns->last_stats.maxgrace, ob->stats.maxgrace, CS_STAT_MAXGRACE); + AddIfShort (ns->last_stats.Str, ob->stats.Str, CS_STAT_STR); + AddIfShort (ns->last_stats.Dex, ob->stats.Dex, CS_STAT_DEX); + AddIfShort (ns->last_stats.Con, ob->stats.Con, CS_STAT_CON); + AddIfShort (ns->last_stats.Int, ob->stats.Int, CS_STAT_INT); + AddIfShort (ns->last_stats.Wis, ob->stats.Wis, CS_STAT_WIS); + AddIfShort (ns->last_stats.Pow, ob->stats.Pow, CS_STAT_POW); + AddIfShort (ns->last_stats.Cha, ob->stats.Cha, CS_STAT_CHA); + + for (int s = 0; s < NUM_SKILLS; s++) + if (object *skill = pl->last_skill_ob[s]) + if (skill->stats.exp != ns->last_skill_exp [s]) { + ns->last_skill_exp [s] = skill->stats.exp; + /* Always send along the level if exp changes. This is only * 1 extra byte, but keeps processing simpler. */ sl << uint8 (s + CS_STAT_SKILLINFO) - << uint8 (pl->last_skill_ob[s]->level) - << uint64 (pl->last_skill_ob[s]->stats.exp); - - pl->last_skill_exp[s] = pl->last_skill_ob[s]->stats.exp; + << uint8 (skill->level) + << uint64 (skill->stats.exp); } - if (pl->socket->exp64) - { AddIfInt64 (pl->last_stats.exp, pl->ob->stats.exp, CS_STAT_EXP64) } - else - { AddIfInt (pl->last_stats.exp, (int) pl->ob->stats.exp, CS_STAT_EXP) } + AddIfInt64 (ns->last_stats.exp, ob->stats.exp, CS_STAT_EXP64); + AddIfShort (ns->last_level, ob->level, CS_STAT_LEVEL); + AddIfShort (ns->last_stats.wc, ob->stats.wc, CS_STAT_WC); + AddIfShort (ns->last_stats.ac, ob->stats.ac, CS_STAT_AC); + AddIfShort (ns->last_stats.dam, ob->stats.dam, CS_STAT_DAM); + AddIfFloat (ns->last_speed, ob->speed, CS_STAT_SPEED); + AddIfShort (ns->last_stats.food, ob->stats.food, CS_STAT_FOOD); + AddIfFloat (ns->last_weapon_sp, pl->weapon_sp, CS_STAT_WEAP_SP); + AddIfInt (ns->last_weight_limit, weight_limit[ob->stats.Str], CS_STAT_WEIGHT_LIM); - AddIfShort (pl->last_level, (char) pl->ob->level, CS_STAT_LEVEL); - AddIfShort (pl->last_stats.wc, pl->ob->stats.wc, CS_STAT_WC); - AddIfShort (pl->last_stats.ac, pl->ob->stats.ac, CS_STAT_AC); - AddIfShort (pl->last_stats.dam, pl->ob->stats.dam, CS_STAT_DAM); - AddIfFloat (pl->last_speed, pl->ob->speed, CS_STAT_SPEED); - AddIfShort (pl->last_stats.food, pl->ob->stats.food, CS_STAT_FOOD); - AddIfFloat (pl->last_weapon_sp, pl->weapon_sp, CS_STAT_WEAP_SP); - AddIfInt (pl->last_weight_limit, (sint32) weight_limit[pl->ob->stats.Str], CS_STAT_WEIGHT_LIM); flags = 0; if (pl->fire_on) @@ -1094,10 +1065,10 @@ if (pl->run_on) flags |= SF_RUNON; - AddIfShort (pl->last_flags, flags, CS_STAT_FLAGS); + AddIfShort (ns->last_flags, flags, CS_STAT_FLAGS); - if (pl->socket->sc_version < 1025) - { AddIfShort (pl->last_resist[ATNR_PHYSICAL], pl->ob->resist[ATNR_PHYSICAL], CS_STAT_ARMOUR) } + if (ns->sc_version < 1025) + { AddIfShort (ns->last_resist[ATNR_PHYSICAL], ob->resist[ATNR_PHYSICAL], CS_STAT_ARMOUR) } else for (int i = 0; i < NROFATTACKS; i++) { @@ -1105,24 +1076,24 @@ if (atnr_cs_stat[i] == -1) continue; - AddIfShort (pl->last_resist[i], pl->ob->resist[i], (char) atnr_cs_stat[i]); + AddIfShort (ns->last_resist[i], ob->resist[i], atnr_cs_stat[i]); } - if (pl->socket->monitor_spells) + if (pl->ns->monitor_spells) { - AddIfInt (pl->last_path_attuned, pl->ob->path_attuned, CS_STAT_SPELL_ATTUNE); - AddIfInt (pl->last_path_repelled, pl->ob->path_repelled, CS_STAT_SPELL_REPEL); - AddIfInt (pl->last_path_denied, pl->ob->path_denied, CS_STAT_SPELL_DENY); + AddIfInt (ns->last_path_attuned, ob->path_attuned, CS_STAT_SPELL_ATTUNE); + AddIfInt (ns->last_path_repelled, ob->path_repelled, CS_STAT_SPELL_REPEL); + AddIfInt (ns->last_path_denied, ob->path_denied, CS_STAT_SPELL_DENY); } - rangetostring (pl->ob, buf); /* we want use the new fire & run system in new client */ - AddIfString (pl->socket->stats.range, buf, CS_STAT_RANGE); - set_title (pl->ob, buf); - AddIfString (pl->socket->stats.title, buf, CS_STAT_TITLE); + rangetostring (ob, buf); /* we want use the new fire & run system in new client */ + AddIfString (ns->stats.range, buf, CS_STAT_RANGE); + set_title (ob, buf); + AddIfString (ns->stats.title, buf, CS_STAT_TITLE); /* Only send it away if we have some actual data */ if (sl.length () > 6) - pl->socket->send_packet (sl); + ns->send_packet (sl); } /** @@ -1131,17 +1102,15 @@ void esrv_new_player (player *pl, uint32 weight) { - pl->last_weight = weight; - - packet sl; - sl << "player "; + packet sl ("player"); sl << uint32 (pl->ob->count) << uint32 (weight) << uint32 (pl->ob->face->number) << data8 (pl->ob->name); - pl->socket->send_packet (sl); + pl->ns->last_weight = weight; + pl->ns->send_packet (sl); SET_FLAG (pl->ob, FLAG_CLIENT_SENT); } @@ -1165,10 +1134,9 @@ return; } - packet sl; + packet sl ("anim"); - sl << "anim " - << uint16 (anim_num) + sl << uint16 (anim_num) << uint16 (0); /* flags - not used right now */ /* Build up the list of faces. Also, send any information (ie, the @@ -1539,7 +1507,7 @@ /* If there is no object for this space, or if the face for the object * is the blank face, set the smoothlevel to zero. */ - if (!ob || ob->face == blank_face || MAP_NOSMOOTH (mp)) + if (!ob || ob->face == blank_face) smoothlevel = 0; else { @@ -1586,6 +1554,28 @@ return result; } +// prefetch (and touch) all maps within a specific distancd +static void +prefetch_surrounding_maps (maptile *map, int distance) +{ + map->last_access = runtime; + + if (--distance) + for (int dir = 4; --dir; ) + if (const shstr &path = map->tile_path [dir]) + if (maptile *&neigh = map->tile_map [dir]) + prefetch_surrounding_maps (neigh, distance); + else + neigh = maptile::find_async (path, map); +} + +// prefetch a generous area around the player +static void +prefetch_surrounding_maps (object *op) +{ + prefetch_surrounding_maps (op->map, 3); +} + /** * This function uses the new map1 protocol command to send the map * to the client. It is necessary because the old map command supports @@ -1599,7 +1589,7 @@ * and use the other 4 bits for other informatiion. For full documentation * of what we send, see the doc/Protocol file. * I will describe internally what we do: - * the socket->lastmap shows how the map last looked when sent to the client. + * the ns->lastmap shows how the map last looked when sent to the client. * in the lastmap structure, there is a cells array, which is set to the * maximum viewable size (As set in config.h). * in the cells, there are faces and a count value. @@ -1620,14 +1610,17 @@ uint8 extendedinfos; maptile *m; - client &socket = *pl->contr->socket; + client &socket = *pl->contr->ns; + + if (!pl->active) + return; check_map_change (pl->contr); + prefetch_surrounding_maps (pl); - packet sl; + packet sl (socket.mapmode == Map1Cmd ? "map1" : "map1a"); packet esl; - sl << (socket.mapmode == Map1Cmd ? "map1 " : "map1a "); startlen = sl.length (); /*Extendedmapinfo structure initialisation */ @@ -1681,7 +1674,6 @@ ax = 0; for (x = pl->x - socket.mapx / 2; x < max_x; x++, ax++) { - emask = mask = (ax & 0x3f) << 10 | (ay & 0x3f) << 4; /* If this space is out of the normal viewable area, we only check @@ -1753,6 +1745,8 @@ } else if (d > 3) { + m->touch (); + int need_send = 0, count; /* This block deals with spaces that are not visible for whatever @@ -1795,6 +1789,8 @@ } else { + m->touch (); + /* In this block, the space is visible or there are head objects * we need to send. */ @@ -1998,8 +1994,8 @@ { int i, j; sint16 ax, ay, nx, ny; /* ax and ay goes from 0 to max-size of arrays */ - New_Face *face, *floor; - New_Face *floor2; + facetile *face, *floor; + facetile *floor2; int d, mflags; struct Map newmap; maptile *m, *pm; @@ -2021,29 +2017,25 @@ memset (&newmap, 0, sizeof (struct Map)); - for (j = (pl->y - pl->contr->socket->mapy / 2); j < (pl->y + (pl->contr->socket->mapy + 1) / 2); j++) - { - for (i = (pl->x - pl->contr->socket->mapx / 2); i < (pl->x + (pl->contr->socket->mapx + 1) / 2); i++) - { - ax = i; - ay = j; - m = pm; - mflags = get_map_flags (m, &m, ax, ay, &ax, &ay); + for (j = (pl->y - pl->contr->ns->mapy / 2); j < (pl->y + (pl->contr->ns->mapy + 1) / 2); j++) + for (i = (pl->x - pl->contr->ns->mapx / 2); i < (pl->x + (pl->contr->ns->mapx + 1) / 2); i++) + { + ax = i; + ay = j; + m = pm; + mflags = get_map_flags (m, &m, ax, ay, &ax, &ay); - if (mflags & P_OUT_OF_MAP) - continue; + if (mflags & P_OUT_OF_MAP) + continue; - if (mflags & P_NEED_UPDATE) - update_position (m, ax, ay); + /* If a map is visible to the player, we don't want to swap it out + * just to reload it. This should really call something like + * swap_map, but this is much more efficient and 'good enough' + */ + if (mflags & P_NEW_MAP) + m->timeout = 50; + } - /* If a map is visible to the player, we don't want to swap it out - * just to reload it. This should really call something like - * swap_map, but this is much more efficient and 'good enough' - */ - if (mflags & P_NEW_MAP) - m->timeout = 50; - } - } /* do LOS after calls to update_position */ if (pl->contr->do_los) { @@ -2068,7 +2060,7 @@ void send_plugin_custom_message (object *pl, char *buf) { - pl->contr->socket->send_packet (buf); + pl->contr->ns->send_packet (buf); } /** @@ -2123,7 +2115,10 @@ void esrv_update_spells (player *pl) { - if (!pl->socket->monitor_spells) + if (!pl->ns) + return; + + if (!pl->ns->monitor_spells) return; for (object *spell = pl->ob->inv; spell; spell = spell->below) @@ -2163,7 +2158,7 @@ if (flags & UPD_SP_GRACE ) sl << uint16 (spell->last_grace); if (flags & UPD_SP_DAMAGE) sl << uint16 (spell->last_eat); - pl->socket->send_packet (sl); + pl->ns->send_packet (sl); } } } @@ -2172,7 +2167,7 @@ void esrv_remove_spell (player *pl, object *spell) { - if (!pl->socket->monitor_spells) + if (!pl->ns->monitor_spells) return; if (!pl || !spell || spell->env != pl->ob) @@ -2181,12 +2176,11 @@ return; } - packet sl; + packet sl ("delspell"); - sl << "delspell " - << uint32 (spell->count); + sl << uint32 (spell->count); - pl->socket->send_packet (sl); + pl->ns->send_packet (sl); } /* appends the spell *spell to the Socklist we will send the data to. */ @@ -2244,11 +2238,10 @@ return; } - if (!pl->socket->monitor_spells) + if (!pl->ns->monitor_spells) return; - packet sl; - sl << "addspell "; + packet sl ("addspell"); if (!spell) { @@ -2272,7 +2265,7 @@ if (sl.length () >= (MAXSOCKBUF - (26 + strlen (spell->name) + (spell->msg ? strlen (spell->msg) : 0)))) { - pl->socket->send_packet (sl); + pl->ns->send_packet (sl); sl.reset (); sl << "addspell "; @@ -2296,6 +2289,6 @@ } /* finally, we can send the packet */ - pl->socket->send_packet (sl); + pl->ns->send_packet (sl); }