OK, so this requires a bit of setup on your side first. Take your character to the spawn location you want new players to come into. Log out, then:
- Code: Select all
SELECT * FROM `character`;
Find the character name you just logged out of and copy the GeoID and GeoAlt values from its row.
Add a few numbers to your GeoAlt. like, if it's value is 5000, make it like 5025. This will cause the new player to drop into that location and prevent any "stuck in the terrain" issues.
The below code will set all new characters to spawn in that same exact location, every time.
- Code: Select all
delimiter $$
CREATE TRIGGER `t_set_first_spawn` before INSERT ON `character` FOR EACH ROW BEGIN
set New.GeoID = 117052637;
set New.GeoAlt = 5100;
END;
$$
But what if you want multiple locations around an area and want it to be random? Repeat the go to X spot, logout, get GeoID and GeoAlt for each possible spawn location you want, then, for each location, say we have 3 possible location, update the below code with the correct GeoID and GeoAlt values. Adding a bit to each GeoAlt.
- Code: Select all
delimiter $$
CREATE TRIGGER `t_set_first_spawn_rand` before INSERT ON `character` FOR EACH ROW BEGIN
declare rd int default 0;
set rd = (select truncate((RAND()*10 MOD 3),0));
CASE rd
when 0 then
set New.GeoID = 117052637;
set New.GeoAlt = 5100;
when 1 then
set New.GeoID = 1170123637;
set New.GeoAlt = 6100;
when 2 then
set New.GeoID = 126052637;
set New.GeoAlt = 4500;
else
set New.GeoID = 117052637;
set New.GeoAlt = 5100;
end CASE;
END;
$$
Explanation of RAND() selection:
Select RAND(); (which you can just run as a query) will return a 0.xxxxxxxx number.
To get an integer out of it, first we need it above 0, so in this case I multiplied by 10 since there are less than 10 possible spawn points in the case statement. You can just as easily multiple it by 1,000,000 if you really wanted.
The Modulus operator MOD will give the division remainder of the value to the right. e.g. 10 MOD 3 is 1 (3*3 = 9 + 1 = 10). 10 mod 5 = 0 (5 * 2 + 0 = 10 0 remainder). MOD is used to put the random number into a range of number from 0 to n-1. n being the amount of numbers you need.
So with that, if you needed say 6 spawn points you would use MOD 6 and the SELECT RAND would be changed to:
- Code: Select all
set rd = (select truncate((RAND()*10 MOD 6,0));
The CASE statements start at 0 and go to 2 (3 numbers including 0).
If you need more spawn points, copy the WHEN statement, add 1 to the value, and add 1 to the number on the MOD number for each spawn point you need.
The ELSE statement is there as a catch all, just in case MySQL/Maria's RAND() has an aneurysm and returns and elephant in a leisure suit. Just update the ELSE's geo data with the data from the 0 case and you'll be fine.