Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 01-18-2014, 07:17 PM
100Zero100 100Zero100 is offline
Registered User
Join Date: Jul 2006
Posts: 31
100Zero100 is on a distinguished road
Dump Useless Local NPCs

I made this script in 2011-2012 and it worked a bit differently (some stuff was changed it seems). Basically the idea is to delete any putnpc2's that exist on the server, in levels that no longer exist.

The first time running the script on UN (2011-2012) reduced their local NPCs from ~4950 to ~690. Running it on UN earlier today reduced their local NPCs from ~6k to ~4k. Running it on Era about an hour ago reduced their local NPCs from > 10k (loadFolder only goes up to 10k entries) to ~3k after 2 presses (first press: ~6k. second press: ~3k). I've had good experiences with it reducing NPC server startup times as well as NC load times (for servers with slow NCs).

It's part of a bigger NPC. I just snipped this part since it seems the most universal.

Graal Script Code:
// NaS
const tempFolder "temp/";
npcFolder "npcs/";
prefix "npclocalnpc";
ext ".nw";

onCreated() {
purgeLocal() {
temp.folder.loadFolder(npcFolder prefix "*",0);
temp.oldSize folder.size();
 for (
temp.fileNamefolder) {
temp.file removeEscapesFromFileName(fileName);
temp.tokens file.tokenize("_");
temp.endlen tokens[tokens.size() - 1].length() + tokens[tokens.size() - 2].length() + 3;
temp.lvl file.substring(prefix.length() + 1file.length() - prefix.length() - endlen) @ ext;
  if (!
levelExists(lvl)) {
moveFile(npcFolder fileNametempFolder fileName);
deleteFile(tempFolder fileName);
temp.folder.loadFolder(npcFolder prefix "*",0);
temp.newSize folder.size();
format("Local NPC count went from %i to %i."oldSizenewSize));

Your NPC server needs rw to the npcs/npclocalnpc* and it also needs rw to some temporary folder (I put temp/, but it can be anything else; defined in const tempFolder). Likely needs 'r' to all of your levels in order for the levelExists() check to run. You can remove the rw when you're done.

Basically, since sometime in 2012 you can no longer deleteFile() anything in npcs/ (even with npc server rw and protectdbnpcs=false in server ops (I tested, jic)), but you can still moveFile() them to another folder and delete them there. It's an unfortunate nuisance, but so it is.

It outputs how many local NPCs were deleted in an echo(), so let me know if your results are interesting.
Hi. I'm NaS!
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT +2. The time now is 03:00 AM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (C) 1998-2008 Linux cyberjoueurs All Rights Reserved.