/* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team * Copyright (©) 2002 Mark Wedel & Crossfire Development Team * Copyright (©) 1992 Frank Tore Johansen * * Deliantra is free software: you can redistribute it and/or modify it under * the terms of the Affero GNU General Public License as published by the * Free Software Foundation, either version 3 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 Affero GNU General Public License * and the GNU General Public License along with this program. If not, see * . * * The authors can be reached via e-mail to */ /** * \file * Basic client output functions. * * \date 2003-12-02 * * This file implements some of the simpler output functions to the * client. Basically, things like sending text strings along */ #include #include #include #include #include #include /** * Draws a normal message on the client. It is pretty * much the same thing as the draw_info above, but takes a color * parameter. the esrv_drawinfo functions should probably be * replaced with this, just using black as the color. */ static void esrv_print_msg (client *ns, int color, const char *str) { ns->send_msg (color, "info", str); } /** * Frontend for esrv_print_msg * \param colr message color * \param pl player to send to. Can be NULL * \param tmp message to send. Can be NULL * * If pl is NULL or without contr set, writes message to log. * * Else sends message to player via esrv_print_msg */ static void print_message (int colr, const object *pl, const char *tmp) { if (!tmp) tmp = "[NULL]"; if (!pl || (pl->type == PLAYER && pl->contr == NULL)) return; if (pl->type == PLAYER) esrv_print_msg (pl->contr->ns, colr, (char *)tmp); } bool client::msg_suppressed (const char *msg) { if (!pl) return false; if (pl->outputs_count <= 1 || !pl->outputs_sync) return false; int len = strlen (msg); if (len > MSG_BUF_SIZE) return false; msg_buf *lru = msgbuf; for (msg_buf *buf = msgbuf; buf < msgbuf + MSG_BUF_COUNT; ++buf) { if (len == buf->len && !memcmp (msg, buf->msg, len)) { // found matching buf, see if expired if (buf->expire <= server_tick || !buf->count) { // yes, take over matching buffer, print buf->expire = server_tick + pl->outputs_sync; buf->count = pl->outputs_count; return false; } // no, suppress --buf->count; return true; } if (lru->expire > buf->expire) lru = buf; } // new message, evoke oldest buffer lru->expire = server_tick + pl->outputs_sync; lru->count = pl->outputs_count; lru->len = len; memcpy (lru->msg, msg, len); return false; } /** * Sends message to player(s). * * flags is various flags - mostly color, plus a few specials. * * pri is unused. * * pl can be passed as NULL - in fact, this will be done if NDI_ALL is set * in the flags. * * If message is black, and not NDI_UNIQUE, gets sent through output buffers. * */ void new_draw_info (int flags, int pri, const object *op, const char *buf) { if (flags & NDI_ALL) { for_all_players (pl) new_draw_info (flags & ~NDI_ALL, 0, pl->ob, buf); } else { if (!op || !op->contr || !op->contr->ns) return; if ((flags & (NDI_COLOR_MASK | NDI_UNIQUE)) != NDI_BLACK || !op->contr->ns->msg_suppressed (buf)) print_message (flags & NDI_COLOR_MASK, op, buf); } } /** * Wrapper for new_draw_info printf-like. * * This is a pretty trivial function, but it allows us to use printf style * formatting, so instead of the calling function having to do it, we do * it here. It may also have advantages in the future for reduction of * client/server bandwidth (client could keep track of various strings */ void new_draw_info_format (int flags, int pri, const object *pl, const char *format, ...) { va_list ap; va_start (ap, format); new_draw_info (flags, pri, pl, vformat (format, ap)); va_end (ap); } /** * Writes to everyone on the map *except* op. This is useful for emotions. */ void new_info_map_except (int color, maptile * map, object *op, const char *str) { for_all_players (pl) if (pl->ob->map == map && pl->ob != op) new_draw_info (color, 0, pl->ob, str); } /** * Writes to everyone on the specified map */ void new_info_map (int color, maptile * map, const char *str) { for_all_players (pl) if (pl->ob->map == map) new_draw_info (color, 0, pl->ob, str); } /** * Sets player title. */ void set_title (object *pl, char *buf) { /* Eneq(@csd.uu.se): Let players define their own titles. */ if (pl->contr->own_title[0] == '\0') sprintf (buf, "Player: %s the %s", (const char *) pl->name, (const char *) pl->contr->title); else sprintf (buf, "Player: %s %s", (const char *) pl->name, (const char *) pl->contr->own_title); } // formerly a macro, used only by magic map, so optimised it out static inline faceidx GET_MAP_FACE (mapspace &ms, int layer) { if (object *op = ms.faces_obj [layer]) return op->face; else return 0; } /** * Helper for magic map creation. * * Takes a player, the map_mark array and an x and y starting position. * pl is the player. * px, py are offsets from the player. * * This function examines all the adjacant spaces next to px, py. * It updates the map_mark arrow with the color and high bits set * for various code values. */ static void magic_mapping_mark_recursive (object *pl, char *map_mark, int px, int py) { for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { int x = px + dx; int y = py + dy; if (abs (x) >= MAGIC_MAP_HALF || abs (y) >= MAGIC_MAP_HALF) continue; mapxy pos (pl); pos.move (x, y); if (!pos.normalise ()) continue; mapspace &ms = pos.ms (); if (map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] == 0) { int mflags = ms.flags (); int f = GET_MAP_FACE (ms, 0); if (f == blank_face) { f = GET_MAP_FACE (ms, 1); if (f == blank_face) f = GET_MAP_FACE (ms, 2); } int magicmap = faces [f].magicmap; /* Should probably have P_NO_MAGIC here also, but then shops don't * work. */ if (mflags & P_BLOCKSVIEW) map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_WALL | magicmap; else { map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_FLOOR | magicmap; magic_mapping_mark_recursive (pl, map_mark, x, y); } } } } } /** * Creates magic map for player. * * Note: For improved magic mapping display, the space that blocks * the view is now marked with value 2. Any dependencies of map_mark * being nonzero have been changed to check for 1. Also, since * map_mark is a char value, putting 2 in should cause no problems. * * This function examines the map the player is on, and determines what * is visible. 2 is set for walls or objects that blocks view. 1 * is for open spaces. map_mark should already have been initialised * to zero before this is called. * strength is an initial strength*2 rectangular area that we automatically * see in/penetrate through. */ static void magic_mapping_mark (object *pl, char *map_mark, int strength) { for (int x = -strength; x < strength; x++) { for (int y = -strength; y < strength; y++) { mapxy pos (pl); pos.move (x, y); if (!pos.normalise ()) continue; mapspace &ms = pos.ms (); int mflags = ms.flags (); int f = GET_MAP_FACE (ms, 0); if (f == blank_face) { f = GET_MAP_FACE (ms, 1); if (f == blank_face) f = GET_MAP_FACE (ms, 2); } int magicmap = faces [f].magicmap; if (mflags & P_BLOCKSVIEW) map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_WALL | magicmap; else { map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_FLOOR | magicmap; magic_mapping_mark_recursive (pl, map_mark, x, y); } } } } /** * Creates and sends magic map to player. * * The following function is a lot messier than it really should be, * but there is no real easy solution. * * Mark Wedel */ void draw_magic_map (object *pl) { int xmin, xmax, ymin, ymax; if (pl->type != PLAYER) { LOG (llevError, "Non player object called draw_map.\n"); return; } char *map_mark = (char *)calloc (MAGIC_MAP_SIZE * MAGIC_MAP_SIZE, 1); assert(("Out of memory!", map_mark != NULL)); /* First, we figure out what spaces are 'reachable' by the player */ magic_mapping_mark (pl, map_mark, 3); /* We now go through and figure out what spaces have been * marked, and thus figure out rectangular region we send * to the client (eg, if only a 10x10 area is visible, we only * want to send those 100 spaces.) */ xmin = MAGIC_MAP_SIZE; ymin = MAGIC_MAP_SIZE; xmax = 0; ymax = 0; for (int x = 0; x < MAGIC_MAP_SIZE; x++) for (int y = 0; y < MAGIC_MAP_SIZE; y++) if (map_mark[x + pl->map->width * y] | FACE_FLOOR) { xmin = x < xmin ? x : xmin; xmax = x > xmax ? x : xmax; ymin = y < ymin ? y : ymin; ymax = y > ymax ? y : ymax; } packet sl; sl.printf ("magicmap %d %d %d %d ", (xmax - xmin + 1), (ymax - ymin + 1), MAGIC_MAP_HALF - xmin, MAGIC_MAP_HALF - ymin); for (int y = ymin; y <= ymax; y++) for (int x = xmin; x <= xmax; x++) sl << uint8 (map_mark[x + MAGIC_MAP_SIZE * y] & ~FACE_FLOOR); pl->contr->ns->send_packet (sl); free (map_mark); }