User:Free Portal/Sandbox/LSL Library/Simple Random Vendor

From Second Life Wiki
Jump to navigation Jump to search

Simple Random Vendor

뽑기 기계 스크립트 입니다. 정확한 용어를 정리 하기 힘들어 임의로 Simple Random Vendor라는 이름을 붙혔습니다. 터치하거나 정해진 금액을 지불하면 컨텐츠에 들어 있는 오브젝트중 하나를 임의로 선택하여 구매자에게 주게 됩니다.

스크립트 사용 방법

  1. 기본 설정
    • 오브젝트 생성후 스크립트와 2개의 노트카드를 컨텐트탭에 넣습니다.
  2. config.cfg 파일 설정
    • PRICE
      • 가격을 설정할수 있으며 0미만의 값 입력시 자동으로 가격을 0으로 책정합니다. 최대 2,147,483,647 까지 가능합니다. 가격이 0으로 설정된 경우 자동으로 Free Pay 모드로 전환되며, 오브젝트를 터치시에는 무료로 아이템을 지급하지만 파이 메뉴에서 지불을 선택 할시 최소 1린든 부터 자율 지불을 할수 있게 됩니다.
    • GROUP
      • 그룹은 오브젝트의 설정된 그룹을 기반으로하며, 그룹 설정을 통해서 그룹 전용 또는 모두 사용 가능하게 제한을 줄 수 있습니다.
      • 0: 그룹 전용
      • 1: 비그룹 허용
    • MSG_DISPLAY
      • 오브젝트 상단에 표시될 메세지를 보이거나 감출수 있습니다. 설정 할수 없는 옵션으로는 GROUP으로 설정될 경우 자동으로 메시지의 최상단에 [GROUP ONLY]라는 메시지가 표시 됩니다.
      • 0 : 모두 보이기
      • 1 : 감추기
      • 2 : 메세지만 보이기
      • 3 : 부가 정보만 보이기
    • MSG
      • MSG 옵션을 통해서 오브젝트 상단에 표시될 메세지를 설정 할수 있습니다. 한글로 총 90문자 까지 표시 가능하며 숫자와 영문 알패뱃은 180문자까지 허용합니다.
    • MSG_COLOR
      • 오 브젝트 상단에 표실될 메세지의 색상을 설정합니다. 생상은 vector 값으로 설정할수 있습니다. 기본 설정은 whitle 입니다 <1.0, 1.0, 1.0>

config.cfg

<lsl>

  1. 가격 설정

PRICE=0

  1. 그룹 설정 (0 그룹 / 1 비그룹 허용)

GROUP=0

  1. 메세지 보이기 (0 보이기 / 1 감추기 / 2 메세지만 보이기 / 3 부가 정보만 보이기 )

MSG_DISPLAY=0

  1. 메세지 설정

MSG=Random Vendor (Demo)

  1. 메세지 색상

MSG_COLOR=<1.0, 1.0, 1.0> </lsl>

random_vendor.lsl

반드시 포함 해야지 동작하는 것들 (이름도 동일 해야함)

  • random_vendoer.lsl
    스크스크립트 형태로 이름은 반듯이 random_vendoer.lsl 이며 풀퍼미션 이여야함
  • config.cfg
    노트 카드 형태로 일므은 반듯이 config.cfg 이여야함
  • gpl-2.0.txt
    노트카드 형태로 누락시 스크립트 동장 않함 gpl-2.0.txt 라이센스 다운 받기

<lsl> // Simple Random Vendor // Copyright (C) 2010 Free Portal // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or 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 GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


// Simple Random Vendor // @author Free Portal (thedig@hyouk.net) // ---------------------------------------------------------------------------- // Sep 27, 2009 v1.0 - Simple Random Vendor, orig code // ----------------------------------------------------------------------------

// ---------------------------------------------------------------------------- // Global Variables: // ---------------------------------------------------------------------------- key kQuery; key agentId; integer iLine; integer itemAmount; integer itemCount; integer priceValue; integer groupValue; integer msgDispValue; list paramList; list paramValueList; string msg; vector msgColorValue;

// ---------------------------------------------------------------------------- // User Defined Functions // ----------------------------------------------------------------------------

// ---------------------------------------------------------------------------- // init // ---------------------------------------------------------------------------- // @comment: // init variables // ---------------------------------------------------------------------------- init() {

   paramValueList  = [];
   msgColorValue   = <1.0, 1.0, 1.0>;
   paramList       = [ "PRICE", "GROUP", "MSG_DISPLAY", "MSG", "MSG_COLOR"];

}

// ---------------------------------------------------------------------------- // getParam // ---------------------------------------------------------------------------- // @param: // string param // // @return: // string // // @comment: // get parameter value // ---------------------------------------------------------------------------- string getParam(string param) {

   string  tmp;
   integer index = llListFindList(paramList, (list)param);
   if (index != -1)
       tmp = llList2String(paramValueList, index);
   
   return tmp;

}

// ---------------------------------------------------------------------------- // giveItem // ---------------------------------------------------------------------------- // @param: // key id // // @param: // integer amount // // @comment: // gvie item to agent // ---------------------------------------------------------------------------- giveItem(key id, integer amount) {

   list        inventory;
   integer     num = llGetInventoryNumber(INVENTORY_ALL);
   string      script = llGetScriptName();
   integer     i = 0;
   
   if (groupValue == 0) {
       
       if (!llSameGroup(id)) {
           llInstantMessage(id, "Active group or Join group please ->" +
               " secondlife:///app/group/" +
               llList2String(llGetObjectDetails(llGetKey(),
               [OBJECT_GROUP]), 0) + "/about ");
               
       if (priceValue == 0 && amount > 0) {
           llGiveMoney(id, amount);
           return ;
       }
       if (priceValue != 0)
           llGiveMoney(id, amount);
           return ;
       }        
   }
   for (i = 0; i < num; ++i) {
       string name = llGetInventoryName(INVENTORY_ALL, i);
       if (llGetInventoryType(name) == INVENTORY_OBJECT) {
           if(llGetInventoryPermMask(name, MASK_OWNER) & PERM_COPY)
               inventory += name;
       }
   }
   if (llGetListLength(inventory) < 1) {
       llInstantMessage(id, "Sorry. no item.");
       if (priceValue == 0 && amount > 0) {
           llGiveMoney(id, amount);
           return ;
       }
       if (priceValue != 0) {
           llGiveMoney(id, amount);
           return ;
       }
   }
   else
       llGiveInventory(id, llList2String(inventory,
           (integer)llFrand(llGetListLength(inventory))));

}

// ---------------------------------------------------------------------------- // checkValid() // ---------------------------------------------------------------------------- // @param: // integer // // @comment: // checkValid // ---------------------------------------------------------------------------- integer checkValid() {

   integer perms = (PERM_MODIFY | PERM_COPY | PERM_TRANSFER);
   integer nextPerms = llGetInventoryPermMask(llGetScriptName(), MASK_NEXT);
   integer num = llGetInventoryNumber(INVENTORY_NOTECARD);
   integer isExist = 0;
   
   llSetText("Vendor offline.", msgColorValue, 1);
   if (llGetScriptName() != "random_vendor.lsl") {
       llOwnerSay("[Error] :: script name must be random_vendor.lsl");
       return FALSE;
   }
   
   if((nextPerms & perms) != perms) {
       llOwnerSay("[Error] :: [random_vendor.lsl] must be full permissions");
       return FALSE;
   }
   
   while (num-- > 0) {
       string name = llGetInventoryName(INVENTORY_NOTECARD, num);
       if (llSubStringIndex(name, "gpl-2.0.txt") != -1) {
           isExist = 1;
       }
   }
   
   if (isExist != 1) {
       llOwnerSay("[Error] :: need to gpl-2.0.txt");
       return FALSE;
   }
   
   return TRUE;

}

// ---------------------------------------------------------------------------- // Main Script // ----------------------------------------------------------------------------

// ---------------------------------------------------------------------------- // state default // ---------------------------------------------------------------------------- // @ comment: // default state // ---------------------------------------------------------------------------- default {

   state_entry() {
       init();
       if (checkValid())
           state debit;
   }
   
   on_rez(integer param) {
       llResetScript();
   }
   
   changed(integer change) {
       if (change & CHANGED_OWNER)
           llResetScript();
       if (change & CHANGED_INVENTORY)
           llResetScript();
   }

}

// ---------------------------------------------------------------------------- // state default // ---------------------------------------------------------------------------- // @ comment: // debit check // ---------------------------------------------------------------------------- state debit {

   state_entry()
   {
       if(checkValid()) {
           llOwnerSay(":: accept debit permission");
           llRequestPermissions(llGetOwner(), PERMISSION_DEBIT);
       }
   }
   
   run_time_permissions(integer perm) {
       if(perm & PERMISSION_DEBIT)
           state config;
   }
   
   touch_start(integer total_number) {
       integer i;
       
       for (i = 0; i < total_number; i++) {
           if (llDetectedKey(i) == llGetOwner()) {
               llOwnerSay("Accept debit permission");
               llRequestPermissions(llGetOwner(), PERMISSION_DEBIT);
           }
           else
               llInstantMessage(llDetectedKey(i), "Vender not ready...");
       }
   }
   
   on_rez(integer param) {
       llResetScript();
   }
   changed(integer change) {
       if (change & CHANGED_OWNER)
           llResetScript();
       if (change & CHANGED_INVENTORY)
           state configRetry;
   }

}

// ---------------------------------------------------------------------------- // state vendor // ---------------------------------------------------------------------------- // @ comment: // main vendor state // ---------------------------------------------------------------------------- state vendor {

   state_entry() {
       if(priceValue == 0) {
           llSetClickAction(CLICK_ACTION_TOUCH);
           llSetPayPrice(1, [PAY_DEFAULT ,PAY_DEFAULT, PAY_DEFAULT,
               PAY_DEFAULT]);
       }
       else {
           llSetClickAction(CLICK_ACTION_PAY);
           llSetPayPrice(PAY_HIDE, [priceValue ,PAY_HIDE, PAY_HIDE,
               PAY_HIDE]);
       }
       
       string _msg = "";
       if (groupValue == 0)
           _msg += "[GROUP ONLY]\n";
       if (msgDispValue == 0) {
           _msg += msg + "\n";
           _msg += "Item amount " + (string)itemCount + ".\n";
           if (priceValue == 0)
               _msg += "Price per L$ "+ "Free Pay.";
           else
               _msg += "Price per L$ "+ (string)priceValue + ".";
       }
       if (msgDispValue == 1)
           _msg = "";
       if (msgDispValue == 2)
           _msg += msg + "\n";
       if (msgDispValue == 3) {
           _msg += "Item amount " + (string)itemCount + ".\n";
           if (priceValue == 0)
               _msg += "Price per L$ "+ "Free Pay.";
           else
               _msg += "Price per L$ "+ (string)priceValue + ".";
       }
       llSetText(_msg, msgColorValue, 1.0);
   }
   
   touch_start (integer n) {
       if (priceValue != 0)
           return ;
       else {
           agentId = llDetectedKey(0);
           itemAmount = 0;
           state cash;
       }
   }
   
   money(key id, integer amount) {
       if (priceValue != 0) {
           if (amount != priceValue) {
               llGiveMoney(id, amount);
               llInstantMessage(id, "You paid " + (string)amount +
                   ", which is the wrong price, the price is: " +
                   (string)priceValue);
               return ;
           }
       }
       agentId     = id;
       itemAmount  = amount;
       state cash;
   }
   
   on_rez(integer param) {
       llResetScript();
   }
   
   changed(integer change) {
       if (change & CHANGED_OWNER)
           llResetScript();
       if (change & CHANGED_INVENTORY)
           state configRetry;
   }

}

// ---------------------------------------------------------------------------- // state configInit // ---------------------------------------------------------------------------- // @ comment: // init parameters value // ---------------------------------------------------------------------------- state configInit {

   state_entry() {
       integer valid       = TRUE;
       itemCount           = llGetInventoryNumber(INVENTORY_OBJECT);
       priceValue          = (integer)getParam("PRICE");
       groupValue          = (integer)getParam("GROUP");
       msgDispValue        = (integer)getParam("MSG_DISPLAY");
       msg                 = getParam("MSG");
       msgColorValue       = (vector)getParam("MSG_COLOR");
       
       if (priceValue < 0) {
           valid = FALSE;
       }
       if (groupValue != 0 && groupValue != 1) {
           llOwnerSay("[Error] :: configuration [GROUP]");
           valid = FALSE;
       }
       if (msgDispValue != 0 && msgDispValue != 1 &&
           msgDispValue != 2 && msgDispValue != 3) {
           llOwnerSay("[Error] :: configuration [GROUP_DISPLAY]");
           valid = FALSE;
       }
       if (llStringLength(msg) > 190) {
           msg = llDeleteSubString(msg, 190, llStringLength(msg));
       }
       if (valid) {
           llOwnerSay(":: vendor ready!");
           state vendor;
       }
       else {
           llSetClickAction(CLICK_ACTION_NONE);
       }
   }
   
   on_rez(integer param) {
       llResetScript();
   }
   
   changed(integer change) {
       if (change & CHANGED_OWNER)
           llResetScript();
       if (change & CHANGED_INVENTORY)
           state configRetry;
   }

}

// ---------------------------------------------------------------------------- // state cash // ---------------------------------------------------------------------------- // @ comment: // cash check // ---------------------------------------------------------------------------- state cash {

   state_entry() {
       llSetClickAction(CLICK_ACTION_NONE);
       llSetPayPrice(PAY_HIDE, [PAY_HIDE ,PAY_HIDE, PAY_HIDE, PAY_HIDE]);
       llSetText("Now Processing...", msgColorValue, 1);
       giveItem(agentId, itemAmount);
       state vendor;
   }
   
   touch(integer i) {}
   
   on_rez(integer param) {
       llResetScript();
   }
   
   changed(integer change) {
       if (change & CHANGED_OWNER)
           llResetScript();
       if (change & CHANGED_INVENTORY)
           state configRetry;
   }

}

// ---------------------------------------------------------------------------- // state config // ---------------------------------------------------------------------------- // @ comment: // get config paramers from notecard // ---------------------------------------------------------------------------- state config {

   state_entry() {
       integer num = llGetInventoryNumber(INVENTORY_NOTECARD);
       integer index = -1;
       paramValueList  = [];
       iLine           = 0;
       
       llSetClickAction(CLICK_ACTION_NONE);
       llSetText("Now Loading...", msgColorValue, 1.0);
       
       if (!checkValid())
           return ;
       
       while (num-- > 0) {
           string name = llGetInventoryName(INVENTORY_NOTECARD, num);
           if (llSubStringIndex(name, "config.cfg") != -1) {
               index = num;
           }
       }
       
       if (index != -1) {
           kQuery = llGetNotecardLine(llGetInventoryName(
                       INVENTORY_NOTECARD, index), iLine);
       }
       else {
           llOwnerSay("[Error] :: config.cfg notecard missing.");
       }
   }
   
   dataserver(key query_id, string data) {
       if (query_id == kQuery) {
           if (data == EOF) {
               state configInit;
           } else {
               if (llSubStringIndex(data, "#") != 0 && data != "") {
                   string  param;
                   string  value;
                   integer index;
                   
                   param   = llGetSubString(data, 0,
                               llSubStringIndex(data, "=") - 1);
                   value   = llGetSubString(data,
                               llSubStringIndex(data, "=") + 1,
                               llStringLength(data));
                   index   = llListFindList(paramList, (list)param);
                   
                   if (index != -1)
                       paramValueList = llListReplaceList(paramValueList,
                                           (list)value, index, index);
               }
               iLine++;
               kQuery = llGetNotecardLine(llGetInventoryName(
                           INVENTORY_NOTECARD,0), iLine);
           }
       }
   }
   
   on_rez(integer param) {
       llResetScript();
   }
   
   changed(integer change) {
       if (change & CHANGED_OWNER)
           llResetScript();
       if (change & CHANGED_INVENTORY)
           state configRetry;
   }

}

// ---------------------------------------------------------------------------- // state configRetry // ---------------------------------------------------------------------------- // @ comment: // retry config when fail config // ---------------------------------------------------------------------------- state configRetry {

   state_entry() {
       state config;
   }

} </lsl>