--- deliantra/server/common/compat.C 2010/04/04 04:59:20 1.8
+++ deliantra/server/common/compat.C 2018/11/17 23:40:00 1.19
@@ -1,24 +1,25 @@
/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
- *
- * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
+ *
+ * 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
*/
@@ -36,8 +37,7 @@
#include "define.h"
#include "path.h"
-
-/* buf_overflow() - we don't want to exceed the buffer size of
+/* buf_overflow() - we don't want to exceed the buffer size of
* buf1 by adding on buf2! Returns true if overflow will occur.
*/
int
@@ -60,20 +60,6 @@
/////////////////////////////////////////////////////////////////////////////
/*
- * fatal() is meant to be called whenever a fatal signal is intercepted.
- * It will call the emergency_save and the clean_tmp_files functions.
- */
-//TODO: only one caller left
-void
-fatal (const char *msg)
-{
- LOG (llevError, "FATAL: %s\n", msg);
- cleanup (msg, 1);
-}
-
-/////////////////////////////////////////////////////////////////////////////
-
-/*
* The random functions here take luck into account when rolling random
* dice or numbers. This function has less of an impact the larger the
* difference becomes in the random numbers. IE, the effect is lessened
@@ -89,23 +75,31 @@
* not the recipient (ie, the poor slob getting hit). [garbled 20010916]
*/
int
-random_roll (int r_min, int r_max, const object *op, int goodbad)
+random_roll (int r_min, int r_max, const object *op, bool prefer_high)
{
- r_max = max (r_min, r_max);
+ if (r_min > r_max)
+ swap (r_min, r_max);
- int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */
+ int range = r_max - r_min + 1;
+ int num = rndm (r_min, r_max);
- if (op->type == PLAYER)
+ if (op->stats.luck)
{
- int luck = op->stats.luck;
+ int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */
- if (rndm (base) < min (10, abs (luck)))
+ if (rndm (base) < min (10, abs (op->stats.luck)))
{
- //TODO: take luck into account
+ // we have a winner, increase/decrease number by one accordingly
+ int adjust = sign (op->stats.luck);
+
+ if (!prefer_high)
+ adjust = -adjust;
+
+ num = clamp (num + adjust, r_min, r_max);
}
}
- return rndm (r_min, r_max);
+ return num;
}
/*
@@ -113,37 +107,37 @@
* for exp loss calculations for players changing religions.
*/
sint64
-random_roll64 (sint64 r_min, sint64 r_max, const object *op, int goodbad)
+random_roll64 (sint64 r_min, sint64 r_max, const object *op, bool prefer_high)
{
- sint64 omin = r_min;
- sint64 range = max (0, r_max - r_min + 1);
- int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */
+ if (r_min > r_max)
+ swap (r_min, r_max);
- /*
- * Make a call to get two 32 bit unsigned random numbers, and just to
+ sint64 range = r_max - r_min + 1;
+ /*
+ * Make a call to get two 32 bit unsigned random numbers, and just do
* a little bitshifting.
*/
- sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31);
+ sint64 num = rndm.next () ^ (sint64 (rndm.next ()) << 31);
- if (op->type != PLAYER)
- return ((ran % range) + r_min);
+ num = num % range + r_min;
- int luck = op->stats.luck;
-
- if (rndm (base) < min (10, abs (luck)))
+ if (op->stats.luck)
{
- /* we have a winner */
- ((luck > 0) ? (luck = 1) : (luck = -1));
- range -= luck;
- if (range < 1)
- return (omin); /*check again */
+ int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */
- ((goodbad) ? (r_min += luck) : (range));
+ if (rndm (base) < min (10, abs (op->stats.luck)))
+ {
+ // we have a winner, increase/decrease number by one accordingly
+ int adjust = sign (op->stats.luck);
- return (max (omin, min (r_max, (ran % range) + r_min)));
+ if (!prefer_high)
+ adjust = -adjust;
+
+ num = clamp (num + adjust, r_min, r_max);
+ }
}
- return ran % range + r_min;
+ return num;
}
/*
@@ -154,7 +148,7 @@
* The args are num D size (ie 4d6) [garbled 20010916]
*/
int
-die_roll (int num, int size, const object *op, int goodbad)
+die_roll (int num, int size, const object *op, bool prefer_high)
{
int min_roll, luck, total, i, gotlucky;
@@ -165,7 +159,7 @@
if (size < 2 || diff < 1)
{
- LOG (llevError, "Calling die_roll with num=%d size=%d\n", num, size);
+ LOG (llevError | logBacktrace, "Calling die_roll with num=%d size=%d\n", num, size);
return num; /* avoids a float exception */
}
@@ -182,7 +176,7 @@
diff -= luck;
if (diff < 1)
return (num); /*check again */
- ((goodbad) ? (min_roll += luck) : (diff));
+ ((prefer_high) ? (min_roll += luck) : (diff));
total += max (1, min (size, rndm (diff) + min_roll));
}
else
@@ -310,67 +304,12 @@
return (path);
}
-/**
- * open_and_uncompress() first searches for the original filename. If it exist,
- * then it opens it and returns the file-pointer.
- */
-FILE *
-open_and_uncompress (const char *name, int flag, int *compressed)
-{
- *compressed = 0;
- return fopen (name, "r");
-}
-
-/*
- * See open_and_uncompress().
- */
-
-void
-close_and_delete (FILE * fp, int compressed)
-{
- fclose (fp);
-}
-
-/*
- * Strip out the media tags from a String.
- * Warning the input string will contain the result string
- */
-void
-strip_media_tag (char *message)
-{
- int in_tag = 0;
- char *dest;
- char *src;
-
- src = dest = message;
- while (*src != '\0')
- {
- if (*src == '[')
- {
- in_tag = 1;
- }
- else if (in_tag && (*src == ']'))
- in_tag = 0;
- else if (!in_tag)
- {
- *dest = *src;
- dest++;
- }
- src++;
- }
- *dest = '\0';
-}
-
#define EOL_SIZE (sizeof("\n")-1)
void
strip_endline (char *buf)
{
- if (strlen (buf) < sizeof ("\n"))
- {
- return;
- }
- if (!strcmp (buf + strlen (buf) - EOL_SIZE, "\n"))
- buf[strlen (buf) - EOL_SIZE] = '\0';
+ if (*buf && buf [strlen (buf) - 1] == '\n')
+ buf [strlen (buf) - 1] = '\0';
}
/**